Exemple #1
0
        /// <summary>
        /// Sends a ControlMessage to NGRID server and gets it's response message.
        /// </summary>
        /// <param name="message">Message to send</param>
        /// <returns>Response message from server</returns>
        public ControlMessage SendMessageAndGetResponse(ControlMessage message)
        {
            //Create a WaitingMessage to wait and get response message and add it to waiting messages
            var outgoingMessage = new NGRIDControllerMessage
                                      {
                                          MessageData = NGRIDSerializationHelper.SerializeToByteArray(message),
                                          ControllerMessageTypeId = message.MessageTypeId
                                      };
            var waitingMessage = new WaitingMessage();
            lock (_waitingMessages)
            {
                _waitingMessages[outgoingMessage.MessageId] = waitingMessage;
            }

            try
            {
                //Send message to the server
                SendMessageInternal(outgoingMessage);

                //Wait until thread is signalled by another thread to get response (Signalled by CommunicationChannel_MessageReceived method)
                waitingMessage.WaitEvent.WaitOne(TimeSpan.FromSeconds(90));

                //Check if response received or timeout occured
                if(waitingMessage.ResponseMessage == null)
                {
                    throw new NGRIDException("Timeout occured. Response message did not received.");
                }
                
                return DeserializeControlMessage(waitingMessage.ResponseMessage);
            }
            finally
            {
                //Remove message from waiting messages
                lock (_waitingMessages)
                {
                    if (_waitingMessages.ContainsKey(outgoingMessage.MessageId))
                    {
                        _waitingMessages.Remove(outgoingMessage.MessageId);
                    }
                }
            }
        }
Exemple #2
0
 /// <summary>
 /// Deserializes a ControlMessage from a NGRIDControllerMessage.
 /// </summary>
 /// <param name="controllerMessage">NGRIDControllerMessage that includes ControlMessage</param>
 /// <returns>Deserialized ControlMessage object.</returns>
 private static ControlMessage DeserializeControlMessage(NGRIDControllerMessage controllerMessage)
 {
     return NGRIDSerializationHelper.DeserializeFromByteArray(
         () =>
         ControlMessageFactory.CreateMessageByTypeId(controllerMessage.ControllerMessageTypeId),
         controllerMessage.MessageData);
 }
Exemple #3
0
        /// <summary>
        /// Sends a ControlMessage to all connected NGRIDController instances.
        /// </summary>
        /// <param name="message">Message to send</param>
        private void SendMessageToAllReceivers(ControlMessage message)
        {
            var outgoingMessage = new NGRIDControllerMessage
            {
                ControllerMessageTypeId = message.MessageTypeId,
                MessageData = NGRIDSerializationHelper.SerializeToByteArray(message)
            };

            var receivers = GetAllReceiverCommunicators();
            foreach (var receiver in receivers)
            {
                try
                {
                    SendMessage(outgoingMessage, receiver);
                }
                catch (Exception ex)
                {
                    Logger.Warn(ex.Message, ex);
                }
            }
        }
Exemple #4
0
 /// <summary>
 /// Sends a message to a spesific communicator as a reply to an incoming message.
 /// </summary>
 /// <param name="communicator">Communicator to send message</param>
 /// <param name="message">Message to send</param>
 /// <param name="incomingMessage">Incoming message which is being replied</param>
 private void ReplyMessageToCommunicator(ICommunicator communicator, ControlMessage message, NGRIDControllerMessage incomingMessage)
 {
     //Create NGRIDControllerMessage that includes serialized GetApplicationListResponseMessage message
     var outgoingMessage = new NGRIDControllerMessage
     {
         ControllerMessageTypeId = message.MessageTypeId,
         MessageData = NGRIDSerializationHelper.SerializeToByteArray(message),
         RepliedMessageId = incomingMessage.MessageId
     };
     //Send message to communicator that sent to message
     SendMessage(outgoingMessage, communicator);
 }
Exemple #5
0
        private void ProcessGetApplicationWebServicesMessage(ICommunicator communicator, GetApplicationWebServicesMessage message, NGRIDControllerMessage controllerMessage)
        {
            try
            {
                //Find application
                ApplicationInfoItem application = null;
                foreach (var applicationInfoItem in _settings.Applications)
                {
                    if(applicationInfoItem.Name == message.ApplicationName)
                    {
                        application = applicationInfoItem;
                    }
                }

                if(application == null)
                {
                    //Send message
                    ReplyMessageToCommunicator(
                        communicator,
                        new GetApplicationWebServicesResponseMessage
                            {
                                WebServices = null,
                                Success = false,
                                ResultText = "No application found with name '" + message.ApplicationName + "'."
                            },
                        controllerMessage
                        );

                    return;
                }

                var webServiceList = new List<ApplicationWebServiceInfo>();
                foreach (var channel in application.CommunicationChannels)
                {
                    if ("WebService".Equals(channel.CommunicationType, StringComparison.OrdinalIgnoreCase))
                    {
                        webServiceList.Add(new ApplicationWebServiceInfo {Url = channel.CommunicationSettings["Url"]});
                    }
                }

                //Send web service list
                ReplyMessageToCommunicator(
                    communicator,
                    new GetApplicationWebServicesResponseMessage
                        {
                            WebServices = webServiceList.ToArray(),
                            Success = true,
                            ResultText = "Success."
                        },
                    controllerMessage
                    );
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message, ex);
            }
        }
Exemple #6
0
        private void ProcessUpdateApplicationWebServicesMessage(ICommunicator communicator, UpdateApplicationWebServicesMessage message, NGRIDControllerMessage controllerMessage)
        {
            try
            {
                //Find application
                ApplicationInfoItem application = null;
                foreach (var applicationInfoItem in _settings.Applications)
                {
                    if (applicationInfoItem.Name == message.ApplicationName)
                    {
                        application = applicationInfoItem;
                    }
                }

                if (application == null)
                {
                    //Send message
                    ReplyMessageToCommunicator(
                        communicator,
                        new OperationResultMessage()
                        {
                            Success = false,
                            ResultMessage = "No application found with name '" + message.ApplicationName + "'."
                        },
                        controllerMessage
                        );
                    return;
                }

                //Delete old service list
                application.CommunicationChannels.Clear();

                //Add new services
                if (message.WebServices != null && message.WebServices.Length > 0)
                {
                    foreach (var webServiceInfo in message.WebServices)
                    {
                        var channelInfo = new ApplicationInfoItem.CommunicationChannelInfoItem { CommunicationType = "WebService" };
                        channelInfo.CommunicationSettings["Url"] = webServiceInfo.Url;
                        application.CommunicationChannels.Add(channelInfo);
                    }
                }

                try
                {
                    //Save settings
                    _settings.SaveToXml();
                }
                catch (Exception ex)
                {
                    Logger.Error(ex.Message, ex);
                    ReplyMessageToCommunicator(
                        communicator,
                        new OperationResultMessage()
                        {
                            Success = false,
                            ResultMessage = "Can not save XML configuration file (NGRIDSettings.xml)."
                        },
                        controllerMessage
                        );
                    return;
                }

                //Send success message
                ReplyMessageToCommunicator(
                    communicator,
                    new OperationResultMessage()
                    {
                        Success = true,
                        ResultMessage = "Success."
                    },
                    controllerMessage
                    );
            }
            catch (Exception ex)
            {
                Logger.Error(ex.Message, ex);
                ReplyMessageToCommunicator(
                    communicator,
                    new OperationResultMessage()
                    {
                        Success = false,
                        ResultMessage = ex.Message
                    },
                    controllerMessage
                    );
                return;
            }
        }
Exemple #7
0
        /// <summary>
        /// Processes UpdateServerGraphMessage.
        /// </summary>
        /// <param name="communicator">Communicator that sent message</param>
        /// <param name="controlMessage">The message to be processed</param>
        /// <param name="controllerMessage">NGRIDControllerMessage object that includes controlMessage</param>
        private void ProcessUpdateServerGraphMessage(ICommunicator communicator, UpdateServerGraphMessage controlMessage, NGRIDControllerMessage controllerMessage)
        {
            try
            {
                var newSettings = new NGRIDSettings(Path.Combine(GeneralHelper.GetCurrentDirectory(), "NGRIDSettings.xml"));
                //var newDesignSettings = new NGRIDDesignSettings(Path.Combine(GeneralHelper.GetCurrentDirectory(), "NGRIDSettings.design.xml"));

                //Clear existing server lists
                newSettings.Servers.Clear();
                //newDesignSettings.Servers.Clear();

                //Add servers from UpdateServerGraphMessage
                newSettings.ThisServerName = controlMessage.ServerGraph.ThisServerName;
                foreach (var server in controlMessage.ServerGraph.Servers)
                {
                    //Settings
                    newSettings.Servers.Add(
                        new ServerInfoItem
                        {
                            Name = server.Name,
                            IpAddress = server.IpAddress,
                            Port = server.Port,
                            Adjacents = server.Adjacents
                        });
                    /*Design settings
                    newDesignSettings.Servers.Add(
                        new ServerDesignItem
                        {
                            Name = server.Name,
                            Location = server.Location
                        });*/
                }

                //Save settings
                newSettings.SaveToXml();
                //newDesignSettings.SaveToXml();
            }
            catch (Exception ex)
            {
                //Send fail message
                ReplyMessageToCommunicator(
                    communicator,
                    new OperationResultMessage {Success = false, ResultMessage = ex.Message},
                    controllerMessage
                    );
                return;
            }

            //Send success message
            ReplyMessageToCommunicator(
                communicator,
                new OperationResultMessage {Success = true, ResultMessage = "Success"},
                controllerMessage
                );
        }
Exemple #8
0
        /// <summary>
        /// Processes GetServerGraphMessage.
        /// </summary>
        /// <param name="communicator">Communicator that sent message</param>
        /// <param name="controllerMessage">NGRIDControllerMessage object that includes controlMessage</param>
        private void ProcessGetServerGraphMessage(ICommunicator communicator, NGRIDControllerMessage controllerMessage)
        {
            //Create response message
            var responseMessage =
                new GetServerGraphResponseMessage
                    {
                        ServerGraph =
                            new ServerGraphInfo
                                {
                                    ThisServerName = _settings.ThisServerName,
                                    Servers = new ServerGraphInfo.ServerOnGraph[_settings.Servers.Count]
                                }
                    };

            //Fill server settings
            for (var i = 0; i < _settings.Servers.Count; i++)
            {
                responseMessage.ServerGraph.Servers[i] = new ServerGraphInfo.ServerOnGraph
                                                             {
                                                                 Name = _settings.Servers[i].Name,
                                                                 IpAddress = _settings.Servers[i].IpAddress,
                                                                 Port = _settings.Servers[i].Port,
                                                                 Adjacents = _settings.Servers[i].Adjacents
                                                             };
            }

            //Fill server design settings
            for (var i = 0; i < responseMessage.ServerGraph.Servers.Length; i++)
            {
                foreach (var serverDesignItem in _designSettings.Servers)
                {
                    if (responseMessage.ServerGraph.Servers[i].Name == serverDesignItem.Name)
                    {
                        responseMessage.ServerGraph.Servers[i].Location = serverDesignItem.Location;
                        break;
                    }
                }
            }

            //Send response message
            ReplyMessageToCommunicator(
                communicator,
                responseMessage,
                controllerMessage
                );
        }
Exemple #9
0
        /// <summary>
        /// Processes RemoveApplicationMessage.
        /// </summary>
        /// <param name="communicator">Communicator that sent message</param>
        /// <param name="controlMessage">The message to be processed</param>
        /// <param name="controllerMessage">NGRIDControllerMessage object that includes controlMessage</param>
        private void ProcessRemoveApplicationMessage(ICommunicator communicator, RemoveApplicationMessage controlMessage, NGRIDControllerMessage controllerMessage)
        {
            try
            {
                var removedApplication = OrganizationLayer.RemoveApplication(controlMessage.ApplicationName);
                removedApplication.CommunicatorConnected -= ClientApplication_CommunicatorConnected;
                removedApplication.CommunicatorDisconnected -= ClientApplication_CommunicatorDisconnected;

                ReplyMessageToCommunicator(
                    communicator,
                    new RemoveApplicationResponseMessage
                        {
                            ApplicationName = controlMessage.ApplicationName,
                            Removed = true,
                            ResultMessage = "Success."
                        },
                    controllerMessage
                    );

                SendMessageToAllReceivers(
                    new ClientApplicationRemovedEventMessage
                        {
                            ApplicationName = removedApplication.Name
                        });
            }
            catch (Exception ex)
            {
                Logger.Warn(ex.Message, ex);
                ReplyMessageToCommunicator(
                    communicator,
                    new RemoveApplicationResponseMessage
                        {
                            ApplicationName = controlMessage.ApplicationName,
                            Removed = false,
                            ResultMessage = ex.Message
                        },
                    controllerMessage
                    );
            }
        }
Exemple #10
0
        /// <summary>
        /// Processes GetApplicationListMessage.
        /// </summary>
        /// <param name="communicator">Communicator that sent message</param>
        /// <param name="controllerMessage">NGRIDControllerMessage object that includes controlMessage</param>
        private void ProcessGetApplicationListMessage(ICommunicator communicator, NGRIDControllerMessage controllerMessage)
        {
            //Get all client applications
            var applicationList = OrganizationLayer.GetClientApplications();

            //Create ClientApplicationInfo array
            var clientApplications = new GetApplicationListResponseMessage.ClientApplicationInfo[applicationList.Length];
            for (var i = 0; i < applicationList.Length; i++)
            {
                clientApplications[i] = new GetApplicationListResponseMessage.ClientApplicationInfo
                                         {
                                             Name = applicationList[i].Name,
                                             CommunicatorCount = applicationList[i].ConnectedCommunicatorCount
                                         };
            }

            //Send response message
            ReplyMessageToCommunicator(
                communicator,
                new GetApplicationListResponseMessage
                    {
                        ClientApplications = clientApplications
                    },
                controllerMessage
                );
        }
Exemple #11
0
 /// <summary>
 /// This methods checks type of message (MessageTypeId) and calls appropriate method to process message.
 /// </summary>
 /// <param name="communicator">Communicator that sent message</param>
 /// <param name="controllerMessage">NGRIDControllerMessage object that includes controlMessage</param>
 /// <param name="controlMessage">The message to be processed</param>
 private void ProcessControllerMessage(ICommunicator communicator, NGRIDControllerMessage controllerMessage , ControlMessage controlMessage)
 {
     switch (controlMessage.MessageTypeId)
     {
         case ControlMessageFactory.MessageTypeIdGetApplicationListMessage:
             ProcessGetApplicationListMessage(communicator, controllerMessage);
             break;
         case ControlMessageFactory.MessageTypeIdAddNewApplicationMessage:
             ProcessAddNewApplicationMessage(controlMessage as AddNewApplicationMessage);
             break;
         case ControlMessageFactory.MessageTypeIdRemoveApplicationMessage:
             ProcessRemoveApplicationMessage(communicator, controlMessage as RemoveApplicationMessage, controllerMessage);
             break;
         case ControlMessageFactory.MessageTypeIdGetServerGraphMessage:
             ProcessGetServerGraphMessage(communicator, controllerMessage);
             break;
         case ControlMessageFactory.MessageTypeIdUpdateServerGraphMessage:
             ProcessUpdateServerGraphMessage(communicator, controlMessage as UpdateServerGraphMessage, controllerMessage);
             break;
         case ControlMessageFactory.MessageTypeIdGetApplicationWebServicesMessage:
             ProcessGetApplicationWebServicesMessage(communicator, controlMessage as GetApplicationWebServicesMessage, controllerMessage);
             break;
         case ControlMessageFactory.MessageTypeIdUpdateApplicationWebServicesMessage:
             ProcessUpdateApplicationWebServicesMessage(communicator, controlMessage as UpdateApplicationWebServicesMessage, controllerMessage);
             break;
         default:
             throw new NGRIDException("Undefined MessageTypeId for ControlMessage: " + controlMessage.MessageTypeId);
     }
 }