/// <summary> /// Processes MDSRegisterMessage objects. /// </summary> /// <param name="communicator">Sender communicator of message</param> /// <param name="message">Message</param> private void ProcessRegisterMessage(ICommunicator communicator, MDSRegisterMessage message) { //Set the communicator properties communicator.CommunicationWay = message.CommunicationWay; MDSRemoteApplication remoteApplication = null; //Find remote application lock (_remoteApplications) { foreach (var app in _remoteApplications.Values) { if (app.Name == message.Name && message.CommunicatorType == app.CommunicatorType) { remoteApplication = app; break; } } } //If application is found... if (remoteApplication != null) { try { //Add communicator to communicator list of remote application remoteApplication.AddCommunicator(communicator); //Remove communicator from tempoary communicators list. RemoveFromCommunicators(communicator.ComminicatorId); //Send success message to remote application SendOperationResultMessage(communicator, true, communicator.ComminicatorId.ToString(), message.MessageId); } catch (Exception ex) { Logger.Warn(ex.Message, ex); //An error occured, send failed message to remote application SendOperationResultMessage(communicator, false, ex.Message, message.MessageId); communicator.Stop(false); } } else //application == null { //Stop communicator, because a remote application can not connect this server that is not defined in settings file SendOperationResultMessage(communicator, false, "No remote application found with name: " + message.Name, message.MessageId); communicator.Stop(false); } }
/// <summary> /// Connects and registers to MDS server. /// </summary> private void ConnectAndRegister() { _communicationChannel.Connect(); try { var registerMessage = new MDSRegisterMessage { CommunicationWay = _communicationChannel.CommunicationWay, CommunicatorType = CommunicatorTypes.Application, Name = ApplicationName, Password = "" }; var reply = SendAndWaitForReply( registerMessage, MDSMessageFactory.MessageTypeIdMDSOperationResultMessage, 30000) as MDSOperationResultMessage; if (reply == null) { throw new MDSException("Can not send register message to the server."); } if (!reply.Success) { CloseCommunicationChannel(); throw new MDSException("Can not register to server. Detail: " + (reply.ResultText ?? "")); } //reply.ResultText must be CommunicatorId if successfully registered. _communicationChannel.ComminicatorId = Convert.ToInt64(reply.ResultText); } catch (MDSTimeoutException) { CloseCommunicationChannel(); throw new MDSTimeoutException("Timeout occured. Can not registered to MDS server."); } }