Пример #1
0
        /// <summary>
        /// Gets all settings from XML file.
        /// </summary>
        private void LoadFromXml()
        {
            //Create XmlDocument object to read XML settings file
            var settingsXmlDoc = new XmlDocument();

            settingsXmlDoc.Load(FilePath);

            //Get Settings section
            var resultNodes = settingsXmlDoc.SelectNodes("/MDSConfiguration/Settings/Setting");

            if (resultNodes == null)
            {
                return;
            }

            foreach (XmlNode node in resultNodes)
            {
                _settings[node.Attributes["Key"].Value] = node.Attributes["Value"].Value;
            }

            if (string.IsNullOrEmpty(_settings["ThisServerName"]))
            {
                throw new MDSException("ThisServerName is not defined.");
            }

            _thisServerName = _settings["ThisServerName"];

            //Get Servers section
            resultNodes = settingsXmlDoc.SelectNodes("/MDSConfiguration/Servers/Server");
            if (resultNodes == null)
            {
                throw new MDSException("No server defined.");
            }

            foreach (XmlNode node in resultNodes)
            {
                Servers.Add(new ServerInfoItem
                {
                    Name      = node.Attributes["Name"].Value.Trim(),
                    IpAddress = node.Attributes["IpAddress"].Value,
                    Port      = Convert.ToInt32(node.Attributes["Port"].Value),
                    Adjacents = node.Attributes["Adjacents"].Value
                });
            }

            //Get Applications section
            resultNodes = settingsXmlDoc.SelectNodes("/MDSConfiguration/Applications/Application");
            if (resultNodes != null)
            {
                //Read all application entries from xml file
                foreach (XmlNode node in resultNodes)
                {
                    var application = new ApplicationInfoItem
                    {
                        Name = node.Attributes["Name"].Value
                    };

                    //Add predefined communication channels
                    foreach (XmlNode childNode in node.ChildNodes)
                    {
                        switch (childNode.Name)
                        {
                        case "Communication":
                            switch (childNode.Attributes["Type"].Value)
                            {
                            case "WebService":
                                var webServiceComm = new ApplicationInfoItem.CommunicationChannelInfoItem
                                {
                                    CommunicationType = "WebService"
                                };
                                webServiceComm.CommunicationSettings.Add("Url", childNode.Attributes["Url"].Value);
                                application.CommunicationChannels.Add(webServiceComm);
                                break;
                            }

                            break;
                        }
                    }

                    Applications.Add(application);
                }
            }

            //Get Routes section
            resultNodes = settingsXmlDoc.SelectNodes("/MDSConfiguration/Routes/Route");
            if (resultNodes != null)
            {
                //Read all route entries from xml file
                foreach (XmlNode node in resultNodes)
                {
                    var route = new RouteInfoItem
                    {
                        Name             = node.Attributes["Name"].Value,
                        DistributionType = GetAttribute(node, "DistributionType")
                    };

                    //Read all filter entries of route from xml file
                    var filterNodes = node.SelectNodes("/MDSConfiguration/Routes/Route/Filters/Filter");
                    if (filterNodes != null)
                    {
                        foreach (XmlNode filterNode in filterNodes)
                        {
                            var filter = new RouteInfoItem.FilterInfoItem
                            {
                                SourceServer           = GetAttribute(filterNode, "SourceServer"),
                                SourceApplication      = GetAttribute(filterNode, "SourceApplication"),
                                DestinationServer      = GetAttribute(filterNode, "DestinationServer"),
                                DestinationApplication =
                                    GetAttribute(filterNode, "DestinationApplication"),
                                TransmitRule = GetAttribute(filterNode, "TransmitRule")
                            };
                            route.Filters.Add(filter);
                        }
                    }

                    //Read all destination entries of route from xml file
                    var destinationNodes = node.SelectNodes("/MDSConfiguration/Routes/Route/Destinations/Destination");
                    if (destinationNodes != null)
                    {
                        foreach (XmlNode destinationNode in destinationNodes)
                        {
                            var destination = new RouteInfoItem.DestinationInfoItem
                            {
                                Application = GetAttribute(destinationNode, "Application"),
                                Server      = GetAttribute(destinationNode, "Server"),
                                RouteFactor = Convert.ToInt32(GetAttribute(destinationNode, "RouteFactor") ?? "1")
                            };
                            if (destination.RouteFactor <= 0)
                            {
                                destination.RouteFactor = 1;
                            }

                            route.Destinations.Add(destination);
                        }
                    }

                    Routes.Add(route);
                }
            }
        }
Пример #2
0
        private void ProcessUpdateApplicationWebServicesMessage(ICommunicator communicator, UpdateApplicationWebServicesMessage message, MDSControllerMessage 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 (MDSSettings.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;
            }
        }
Пример #3
0
        /// <summary>
        /// Gets all settings from XML file.
        /// </summary>
        private void LoadFromXml()
        {
            //Create XmlDocument object to read XML settings file
            var settingsXmlDoc = new XmlDocument();
            settingsXmlDoc.Load(FilePath);

            //Get Settings section
            var resultNodes = settingsXmlDoc.SelectNodes("/MDSConfiguration/Settings/Setting");
            if (resultNodes == null)
            {
                return;
            }

            foreach (XmlNode node in resultNodes)
            {
                _settings[node.Attributes["Key"].Value] = node.Attributes["Value"].Value;
            }

            if (string.IsNullOrEmpty(_settings["ThisServerName"]))
            {
                throw new MDSException("ThisServerName is not defined.");
            }

            _thisServerName = _settings["ThisServerName"];

            //Get Servers section
            resultNodes = settingsXmlDoc.SelectNodes("/MDSConfiguration/Servers/Server");
            if (resultNodes == null)
            {
                throw new MDSException("No server defined.");
            }

            foreach (XmlNode node in resultNodes)
            {
                Servers.Add(new ServerInfoItem
                                {
                                    Name = node.Attributes["Name"].Value.Trim(),
                                    IpAddress = node.Attributes["IpAddress"].Value,
                                    Port = Convert.ToInt32(node.Attributes["Port"].Value),
                                    Adjacents = node.Attributes["Adjacents"].Value
                                });
            }

            //Get Applications section
            resultNodes = settingsXmlDoc.SelectNodes("/MDSConfiguration/Applications/Application");
            if (resultNodes != null)
            {
                //Read all application entries from xml file
                foreach (XmlNode node in resultNodes)
                {
                    var application = new ApplicationInfoItem
                    {
                        Name = node.Attributes["Name"].Value
                    };

                    //Add predefined communication channels
                    foreach (XmlNode childNode in node.ChildNodes)
                    {
                        switch (childNode.Name)
                        {
                            case "Communication":
                                switch (childNode.Attributes["Type"].Value)
                                {
                                    case "WebService":
                                        var webServiceComm = new ApplicationInfoItem.CommunicationChannelInfoItem
                                        {
                                            CommunicationType = "WebService"
                                        };
                                        webServiceComm.CommunicationSettings.Add("Url", childNode.Attributes["Url"].Value);
                                        application.CommunicationChannels.Add(webServiceComm);
                                        break;
                                }

                                break;
                        }
                    }

                    Applications.Add(application);
                }
            }

            //Get Routes section
            resultNodes = settingsXmlDoc.SelectNodes("/MDSConfiguration/Routes/Route");
            if (resultNodes != null)
            {
                //Read all route entries from xml file
                foreach (XmlNode node in resultNodes)
                {
                    var route = new RouteInfoItem
                                {
                                    Name = node.Attributes["Name"].Value,
                                    DistributionType = GetAttribute(node, "DistributionType")
                                };

                    //Read all filter entries of route from xml file
                    var filterNodes = node.SelectNodes("/MDSConfiguration/Routes/Route/Filters/Filter");
                    if (filterNodes != null)
                    {
                        foreach (XmlNode filterNode in filterNodes)
                        {
                            var filter = new RouteInfoItem.FilterInfoItem
                                             {
                                                 SourceServer = GetAttribute(filterNode, "SourceServer"),
                                                 SourceApplication = GetAttribute(filterNode, "SourceApplication"),
                                                 DestinationServer = GetAttribute(filterNode, "DestinationServer"),
                                                 DestinationApplication =
                                                     GetAttribute(filterNode, "DestinationApplication"),
                                                 TransmitRule = GetAttribute(filterNode, "TransmitRule")
                                             };
                            route.Filters.Add(filter);
                        }
                    }

                    //Read all destination entries of route from xml file
                    var destinationNodes = node.SelectNodes("/MDSConfiguration/Routes/Route/Destinations/Destination");
                    if (destinationNodes != null)
                    {
                        foreach (XmlNode destinationNode in destinationNodes)
                        {
                            var destination = new RouteInfoItem.DestinationInfoItem
                                           {
                                               Application = GetAttribute(destinationNode, "Application"),
                                               Server = GetAttribute(destinationNode, "Server"),
                                               RouteFactor = Convert.ToInt32(GetAttribute(destinationNode, "RouteFactor") ?? "1")
                                           };
                            if (destination.RouteFactor <= 0)
                            {
                                destination.RouteFactor = 1;
                            }

                            route.Destinations.Add(destination);
                        }
                    }

                    Routes.Add(route);
                }
            }
        }