public ServerSettings CreateChatServer(string ServerName) { ServerSettings settings = new ServerSettings(); ServerList serverlist = new ServerList(); settings.server_name = ServerName; settings.server_password = GetServerPassword(); settings.backlog = GetServerBacklog(); settings.port_number = GetServerPortNumber(); settings.server_ip_address = GetServerIPAddress(); return settings; }
/* this method is used when the server starts to search and load settings to be passed to the Listen() method */ public static ServerSettings Init(string ServerName) { ServerList SL = new ServerList(); string[] ServerList = SL.GetServerList(); // list of all directory paths string ServerPath = ""; // ex: ServerList[0] = Server\Caleb // we want to remove the parent directory from the path so we can retrieve the name // for checking // then we populate ConfigurationFile with elements of the txt file. // we then use a SeverSettings to pass into Listen() //for (int i = 0; i < ServerList.Length; i++) // ServerList[i] = ServerList[i].Remove(0, SL.MainServerDirectory.Length + 1); for (int i = 0; i < ServerList.Length; i++) { if (ServerName == ServerList[i]) { ServerPath = SL.MainServerDirectory + @"\" + ServerName; } } //gets the configuration file from the Servers\myserver directory DirectoryInfo ConfigFileInfo = new DirectoryInfo(ServerPath); FileInfo[] files = ConfigFileInfo.GetFiles(); string ConfigFilePath = ""; for (int i = 0; i < files.Length; i++) { if (files[i].Extension == ".txt" && files[i].Name.Contains("_Config")) { ConfigFilePath = files[i].FullName; } } List<string> ConfigurationFile = Tools.IO.ReadTextFileList(ConfigFilePath); ServerSettings settings = new ServerSettings(); //TODO: could I just iterate through this and add through the structure to make it less hacky? settings.server_name = ConfigurationFile[1]; settings.server_password = ConfigurationFile[2]; settings.backlog = int.Parse(ConfigurationFile[3]); settings.server_ip_address = ConfigurationFile[4]; settings.port_number = int.Parse(ConfigurationFile[5]); return settings; }
// prompts the user to enter in a server name private string GetSeverName() { ServerList serverlist = new ServerList(); Console.WriteLine("Enter the name of the new server: "); while (true) { string input = Console.ReadLine(); if (!(string.IsNullOrEmpty(input))) { if (!(Directory.Exists(serverlist.MainServerDirectory + @"\" + input))) { return input; } else { Console.WriteLine("The server already exists"); } } else { Console.WriteLine("please enter in a name for the server"); } } }
// add the server to the server list and write the configurations private static void CreateNewServer(ServerSettings settings) { ServerList Servers = new ServerList(); Servers.Add(settings); // ServerList creates the configuration file and res folder }