コード例 #1
0
        /// <summary>
        /// Processes NGRIDRegisterMessage objects.
        /// </summary>
        /// <param name="communicator">Sender communicator of message</param>
        /// <param name="message">Message</param>
        private void ProcessRegisterMessage(ICommunicator communicator, NGRIDRegisterMessage message)
        {
            //Set the communicator properties
            communicator.CommunicationWay = message.CommunicationWay;

            NGRIDRemoteApplication 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);
            }
        }
コード例 #2
0
        /// <summary>
        /// Connects and registers to NGRID server.
        /// </summary>
        private void ConnectAndRegister()
        {
            _communicationChannel.Connect();
            try
            {
                var registerMessage = new NGRIDRegisterMessage
                {
                    CommunicationWay = _communicationChannel.CommunicationWay,
                    CommunicatorType = CommunicatorTypes.Application,
                    Name             = ApplicationName,
                    Password         = ""
                };
                var reply = SendAndWaitForReply(
                    registerMessage,
                    NGRIDMessageFactory.MessageTypeIdNGRIDOperationResultMessage,
                    30000) as NGRIDOperationResultMessage;

                if (reply == null)
                {
                    throw new NGRIDException("Can not send register message to the server.");
                }

                if (!reply.Success)
                {
                    CloseCommunicationChannel();
                    throw new NGRIDException("Can not register to server. Detail: " + (reply.ResultText ?? ""));
                }

                //reply.ResultText must be CommunicatorId if successfully registered.
                _communicationChannel.ComminicatorId = Convert.ToInt64(reply.ResultText);
            }
            catch (NGRIDTimeoutException)
            {
                CloseCommunicationChannel();
                throw new NGRIDTimeoutException("Timeout occured. Can not registered to NGRID server.");
            }
        }