Example #1
0
        public string Post([FromBody] ServerConfigurationEntry _ServerConfigurationEntry)
        {
            //string value = null ;
            //ServerConfigurationEntry _ServerConfigurationEntry=JsonConvert.DeserializeObject<ServerConfigurationEntry>(value);
            string Server_ID = Guid.NewGuid().ToString();

            _ServerConfigurationEntry.Server_ID = Server_ID;
            string value = JsonConvert.SerializeObject(_ServerConfigurationEntry);

            Program.LocateServerEntry.Add(Server_ID, Program.CountForServerEntry);
            Program.ServerConfigurationList.Add(_ServerConfigurationEntry);
            Program.CountForServerEntry++;
            try
            {
                Program.ConfigFileSemaphore.WaitOne();
                using (StreamWriter SW = new StreamWriter(@"C:\Users\User\Documents\Assignment\Config\Config.txt", true))
                {
                    SW.WriteLine(value);
                }
                Program.ConfigFileSemaphore.Release(1);
                Console.WriteLine("Server Added Successfully");
            }
            catch (Exception E)
            {
                Program.ConfigFileSemaphore.Release(1);
                Console.WriteLine("Server NOT Added Successfully");
            }

            return(Server_ID);
        }
Example #2
0
        public bool Put([FromBody] ServerConfigurationEntry _ServerConfigurationEntry)
        {
            //ServerConfigurationEntry _ServerConfigurationEntry = JsonConvert.DeserializeObject<ServerConfigurationEntry>(value);
            int Index = Program.LocateServerEntry[_ServerConfigurationEntry.Server_ID];

            Program.ServerConfigurationList[Index] = _ServerConfigurationEntry;
            string value      = JsonConvert.SerializeObject(_ServerConfigurationEntry);
            string ConfigData = null;
            string temp       = null;

            try
            {
                Program.ConfigFileSemaphore.WaitOne();
                using (StreamReader SR = new StreamReader(@"C:\Users\User\Documents\Assignment\Config\Config.txt"))
                {
                    for (int i = 0; i < Program.CountForServerEntry; i++)
                    {
                        temp = SR.ReadLine();
                        if (i != Index)
                        {
                            ConfigData += temp;
                        }
                        else
                        {
                            ConfigData += value;
                        }
                        ConfigData += Environment.NewLine;
                    }
                }
                using (StreamWriter SW = new StreamWriter(@"C:\Users\User\Documents\Assignment\Config\Config.txt"))
                {
                    SW.Write(ConfigData);
                }
                Program.ConfigFileSemaphore.Release(1);
                Console.WriteLine("Server Modified Successfully at {0} index", Index);
                return(true);
            }
            catch (Exception E)
            {
                Program.ConfigFileSemaphore.Release(1);
                Console.WriteLine("Server Modified Successfully at {0} index", Index);
                return(false);
            }
        }