Exemple #1
0
        /// <summary>
        /// Update selected Server
        /// </summary>
        /// <param name="XMLFilePath"></param>
        /// <param name="SelectIp"></param>
        /// <returns></returns>
        public bool UpdateServer(string XMLFilePath, RenbarServer NewServer)
        {
            bool UpdateSucced = false;

            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(XMLFilePath);

                XmlNode AllServers = xmlDoc.SelectSingleNode("ServerAddress");

                #region Select old node
                XmlNode UpdateServer =
                    xmlDoc.SelectSingleNode("//Server[@IP='" + NewServer.ServerIP.ToString().Trim() + "']");
                #endregion

                #region Creat new server
                XmlElement   Servers = xmlDoc.CreateElement("Server");
                XmlAttribute SIp     = xmlDoc.CreateAttribute("IP");
                SIp.Value = NewServer.ServerIP.ToString().Trim();
                XmlAttribute SPort = xmlDoc.CreateAttribute("Port");
                SPort.Value = NewServer.ServerPort.ToString().Trim();
                XmlAttribute SIsMaster = xmlDoc.CreateAttribute("IsMaster");
                if (NewServer.IsMaster)
                {
                    SIsMaster.Value = "Y";
                }
                else
                {
                    SIsMaster.Value = "N";
                }
                Servers.Attributes.Append(SIp);
                Servers.Attributes.Append(SPort);
                Servers.Attributes.Append(SIsMaster);
                #endregion

                //replace
                AllServers.ReplaceChild(Servers, UpdateServer);

                xmlDoc.Save(XMLFilePath);
                UpdateSucced = true;
            }
            catch (Exception ex)
            {
                string ExceptionMsg = ex.Message + ex.StackTrace;
                // write to log file ..
                base.Writer(AssemblyInfoClass.ProductInfo, Log.Level.Error, ExceptionMsg, true);
            }
            return(UpdateSucced);
        }
Exemple #2
0
        /// <summary>
        /// Add Server Record to xml file
        /// </summary>
        /// <param name="XMLFilePath"></param>
        /// <param name="NewServer"></param>
        /// <returns>Whether add server succed</returns>
        public bool AddServer(string XMLFilePath, RenbarServer NewServer)
        {
            bool addSuccess = false;

            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.Load(XMLFilePath);
            XmlNode AllServers = xmlDoc.SelectSingleNode("ServerAddress");
            //創建新節點
            XmlElement   Servers = xmlDoc.CreateElement("Server");
            XmlAttribute SIp     = xmlDoc.CreateAttribute("IP");

            SIp.Value = NewServer.ServerIP.ToString().Trim();
            XmlAttribute SPort = xmlDoc.CreateAttribute("Port");

            SPort.Value = NewServer.ServerPort.ToString().Trim();
            XmlAttribute SIsMaster = xmlDoc.CreateAttribute("IsMaster");

            if (NewServer.IsMaster)
            {
                SIsMaster.Value = "Y";
            }
            else
            {
                SIsMaster.Value = "N";
            }

            try
            {
                //添加數據
                Servers.Attributes.Append(SIp);
                Servers.Attributes.Append(SPort);
                Servers.Attributes.Append(SIsMaster);

                AllServers.AppendChild(Servers);
                xmlDoc.Save(XMLFilePath);

                addSuccess = true;
            }
            catch (Exception ex)
            {
                string ExceptionMsg = ex.Message + ex.StackTrace;
                // write to log file ..
                base.Writer(AssemblyInfoClass.ProductInfo, Log.Level.Error, ExceptionMsg, true);
            }
            return(addSuccess);
        }
Exemple #3
0
        /// <summary>
        /// get server list from the xml
        /// </summary>
        /// <param name="XMLFilePath">xml file path</param>
        /// <returns>server list</returns>
        public List <RenbarServer> GetServers(string XMLFilePath)
        {
            List <RenbarServer> ServerList = new List <RenbarServer>();

            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(XMLFilePath);

                XmlNode     AllServers = xmlDoc.SelectSingleNode("ServerAddress");
                XmlNodeList Servers    = AllServers.ChildNodes;

                foreach (XmlNode Server in Servers)
                {
                    XmlElement CurrentServer = (XmlElement)Server;
                    if (CurrentServer.GetAttribute("IP") != null)
                    {
                        RenbarServer thisserver = new RenbarServer();

                        thisserver.ServerIP   = IPAddress.Parse(CurrentServer.GetAttribute("IP"));
                        thisserver.ServerPort = ushort.Parse(CurrentServer.GetAttribute("Port"));
                        if (CurrentServer.GetAttribute("IsMaster") == "Y")
                        {
                            thisserver.IsMaster = true;
                        }
                        else
                        {
                            thisserver.IsMaster = false;
                        }
                        ServerList.Add(thisserver);
                    }
                }
            }
            catch (Exception ex)
            {
                string ExceptionMsg = ex.Message + ex.StackTrace;
                // write to log file ..
                base.Writer(AssemblyInfoClass.ProductInfo, Log.Level.Error, ExceptionMsg, true);
            }
            return(ServerList);
        }
Exemple #4
0
        /// <summary>
        /// Add or Update Server Info
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Button_AU_Click(object sender, EventArgs e)
        {
            try
            {
                #region New Server Instance
                RenbarLib.Environment.RenbarServer newServer = new RenbarLib.Environment.RenbarServer();
                if (this.comboBox_Server.Text.Trim() != string.Empty && this.NumericUpDown_Port.Value.ToString().Trim() != string.Empty)
                {
                    newServer.ServerIP   = IPAddress.Parse(this.comboBox_Server.Text.Trim());
                    newServer.ServerPort = ushort.Parse(this.NumericUpDown_Port.Value.ToString().Trim());

                    if (this.checkBox_IsMaster.Checked)
                    {
                        newServer.IsMaster = true;
                    }
                    else
                    {
                        newServer.IsMaster = false;
                    }

                    #endregion

                    #region Update
                    if (IsExist(newServer.ServerIP.ToString().Trim()))
                    {
                        //走更新流程???????????完善????????????????

                        this.EnvSvr.UpdateServer(ServerXMLPath, newServer);

                        //MessageBox.Show("Already exist",MessageBoxButtons.OKCancel,);

                        MessageBox.Show("Update succed!");
                        Binding(ServerXMLPath);
                        return;
                    }
                    #endregion

                    #region ADD New
                    else
                    {
                        //如果不再,測試連接,不同問:仍舊添加?
                        if (this.EnvSvr.AddServer(ServerXMLPath, newServer))
                        {
                            MessageBox.Show("Add Succed!");
                            Binding(ServerXMLPath);
                            return;
                        }
                    }
                    #endregion
                }
                else
                {
                    MessageBox.Show("IP Address or Port is Empty");
                }
            }
            catch (Exception ex)
            {
                string ExceptionMsg = ex.Message + ex.StackTrace;
                MessageBox.Show("IP Address or Port Error");
            }
        }