From cfdafa9d69eb68cfa59eb7ed5ee89e168a4bb11d Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Mon, 30 Oct 2017 12:36:01 -0400 Subject: [PATCH] Implements the instance manipulation functions --- TGServerService/Service.cs | 183 ++++++++++++++++++++++- TGServiceInterface/Components/Service.cs | 18 +-- 2 files changed, 186 insertions(+), 15 deletions(-) diff --git a/TGServerService/Service.cs b/TGServerService/Service.cs index 8295fa2756..f7dc75c97f 100644 --- a/TGServerService/Service.cs +++ b/TGServerService/Service.cs @@ -397,37 +397,208 @@ namespace TGServerService /// public string CreateInstance(string Name, string path) { - throw new NotImplementedException(); + if (File.Exists(path) || Directory.Exists(path)) + return "Cannot create instance at pre-existing path!"; + var Config = Properties.Settings.Default; + lock (this) + { + if (Config.InstancePaths.Contains(path)) + return String.Format("Instance at {0} already exists!", path); + try + { + var ic = new InstanceConfig(path); + Directory.CreateDirectory(path); + ic.Save(); + Properties.Settings.Default.InstancePaths.Add(path); + } + catch (Exception e) + { + return e.ToString(); + } + return SetupOneInstance(path); + } + } + + /// + /// Starts and onlines an instance located at + /// + /// The path to the instance + /// on success, error message on failure + string SetupOneInstance(string path) + { + try + { + var host = SetupInstance(path); + if (host != null) + host.Open(); + return null; + } + catch (Exception e) + { + return "Instance set up but an error occurred while starting it: " + e.ToString(); + } } /// public string ImportInstance(string path) { - throw new NotImplementedException(); + var Config = Properties.Settings.Default; + lock (this) + { + if (Config.InstancePaths.Contains(path)) + return String.Format("Instance at {0} already exists!", path); + if(!Directory.Exists(path)) + return String.Format("There is no instance located at {0}!", path); + InstanceConfig ic; + try + { + ic = InstanceConfig.Load(path); + ic.Save(); + Properties.Settings.Default.InstancePaths.Add(path); + } + catch (Exception e) + { + return e.ToString(); + } + return SetupOneInstance(path); + } } /// public bool InstanceEnabled(string Name) { - throw new NotImplementedException(); + lock(this) + { + return hosts.ContainsKey(Name); + } } /// public string SetInstanceEnabled(string Name, bool enabled) { - throw new NotImplementedException(); + return SetInstanceEnabledImpl(Name, enabled, out string path); + } + + + /// + /// Sets a 's enabled status + /// + /// The whom's status should be changed + /// to enable the , to disable it + /// The path to the modified + /// on success, error message on failure + string SetInstanceEnabledImpl(string Name, bool enabled, out string path) + { + path = null; + lock (this) + { + var hostIsOnline = hosts.ContainsKey(Name); + if (enabled) + { + if (hostIsOnline) + return null; + //now this is a bit awkward because we need to check each instance config for the one named Name + string LastCheckedConfig = null; + try + { + foreach (var I in Properties.Settings.Default.InstancePaths) + { + LastCheckedConfig = I; + var ic = InstanceConfig.Load(I); + if (ic.Name == Name) + { + path = I; + return SetupOneInstance(I); + } + } + } + catch (Exception e) + { + return String.Format("An error occurred while checking instance config at {0}! Error: ", LastCheckedConfig, e.ToString()); + } + return String.Format("Instance {0} does not exist!", Name); + } + else + { + if (!hostIsOnline) + return null; + var host = hosts[Name]; + hosts.Remove(Name); + var inst = (ServerInstance)host.SingletonInstance; + host.Close(); + path = inst.ServerDirectory(); + inst.Dispose(); + return null; + } + } } /// public string RenameInstance(string name, string new_name) { - throw new NotImplementedException(); + if (name == new_name) + return null; + lock (this) + { + //we have to check em all anyway + InstanceConfig the_droid_were_looking_for = null; + foreach (var I in Properties.Settings.Default.InstancePaths) + { + var ic = InstanceConfig.Load(I); + if (ic.Name == name) + the_droid_were_looking_for = ic; + else if (ic.Name == new_name) + return String.Format("There is already another instance named {0}!", new_name); + } + if (the_droid_were_looking_for == null) + return String.Format("There is no instance named {0}!", name); + var ie = InstanceEnabled(name); + if(ie) + SetInstanceEnabled(name, false); + the_droid_were_looking_for.Name = new_name; + string result = ""; + try + { + the_droid_were_looking_for.Save(); + result = null; + } + catch(Exception e) + { + result = "Could not save instance config! Error: " + e.ToString(); + } + finally + { + if (ie) + result = SetInstanceEnabled(new_name, true) + " "+ result; + } + return result; + } } /// public string DetachInstance(string name) { - throw new NotImplementedException(); + lock (this) + { + var res = SetInstanceEnabledImpl(name, false, out string path); + if (res != null) + return res; + var Config = Properties.Settings.Default.InstancePaths; + if (path == null) //gotta find it ourselves + foreach (var I in Config) + { + var ic = InstanceConfig.Load(I); + if (ic.Name == name) + { + path = I; + break; + } + } + if (path == null) + return String.Format("No instance named {0} exists!", name); + Config.Remove(path); + return null; + } } } } diff --git a/TGServiceInterface/Components/Service.cs b/TGServiceInterface/Components/Service.cs index 98c1987220..695bae9849 100644 --- a/TGServiceInterface/Components/Service.cs +++ b/TGServiceInterface/Components/Service.cs @@ -42,7 +42,7 @@ namespace TGServiceInterface.Components /// /// List instances /// - /// + /// A of instance names relating to their paths [OperationContract] IDictionary ListInstances(); @@ -51,7 +51,7 @@ namespace TGServiceInterface.Components /// /// The name of the instance /// The path to the instance - /// null on success, error message on failure + /// on success, error message on failure [OperationContract] string CreateInstance(string Name, string path); @@ -59,7 +59,7 @@ namespace TGServiceInterface.Components /// Registers an existing server instance /// /// The path to the instance - /// null on success, error message on failure + /// on success, error message on failure [OperationContract] string ImportInstance(string path); @@ -67,7 +67,7 @@ namespace TGServiceInterface.Components /// Checks if an instance is online /// /// The name of the instance - /// true if the Instance exists and is online, false otherwise + /// if the Instance exists and is online, otherwise [OperationContract] bool InstanceEnabled(string Name); @@ -75,17 +75,17 @@ namespace TGServiceInterface.Components /// Sets an instance's enabled status /// /// The instance whom's status should be changed - /// true to enable the instance, false to disable it - /// null on success, error message on failure + /// to enable the instance, to disable it + /// on success, error message on failure [OperationContract] string SetInstanceEnabled(string Name, bool enabled); /// - /// Renames an instance + /// Renames an instance, this will restart the instance if it is enabled /// /// The current name of the instance /// The new name of the instance - /// null on success, error message on failure + /// on success, error message on failure [OperationContract] string RenameInstance(string name, string new_name); @@ -93,7 +93,7 @@ namespace TGServiceInterface.Components /// Disables and unregisters an instance, allowing the folder and data to be manipulated manually /// /// The instance to detach - /// null on success, error message on failure + /// on success, error message on failure [OperationContract] string DetachInstance(string name);