From 8672a9d5c69f4e50bb4f0c0b42730875054b266c Mon Sep 17 00:00:00 2001 From: Cyberboss Date: Tue, 31 Oct 2017 13:14:08 -0400 Subject: [PATCH] Modify Interface to accept instance switching. Add ITGInstance --- TGCommandLine/ConfigCommands.cs | 2 +- TGControlPanel/ServerPage.cs | 2 +- TGServerService/ServerInstance/Config.cs | 5 -- .../ServerInstance/ServerInstance.cs | 9 +++- TGServiceInterface/Components/Config.cs | 6 --- TGServiceInterface/Components/Instance.cs | 18 ++++++++ TGServiceInterface/DreamDaemonBridge.cs | 4 +- TGServiceInterface/Enumerations.cs | 28 ++++++++++- TGServiceInterface/Interface.cs | 46 ++++++++++++++++++- TGServiceInterface/TGServiceInterface.csproj | 1 + 10 files changed, 102 insertions(+), 19 deletions(-) create mode 100644 TGServiceInterface/Components/Instance.cs diff --git a/TGCommandLine/ConfigCommands.cs b/TGCommandLine/ConfigCommands.cs index 3f203877bb..d3cdb2d800 100644 --- a/TGCommandLine/ConfigCommands.cs +++ b/TGCommandLine/ConfigCommands.cs @@ -88,7 +88,7 @@ namespace TGCommandLine protected override ExitCode Run(IList parameters) { - OutputProc(Interface.GetComponent().ServerDirectory()); + OutputProc(Interface.GetComponent().ServerDirectory()); return ExitCode.Normal; } diff --git a/TGControlPanel/ServerPage.cs b/TGControlPanel/ServerPage.cs index db53e870c9..17d5f167ea 100644 --- a/TGControlPanel/ServerPage.cs +++ b/TGControlPanel/ServerPage.cs @@ -106,7 +106,7 @@ namespace TGControlPanel { updatingFields = true; - ServerPathLabel.Text = "Server Path: " + Config.ServerDirectory(); + ServerPathLabel.Text = "Server Path: " + Interface.GetComponent().ServerDirectory(); SecuritySelector.SelectedIndex = (int)DD.SecurityLevel(); diff --git a/TGServerService/ServerInstance/Config.cs b/TGServerService/ServerInstance/Config.cs index d639f6760c..d59fef5909 100644 --- a/TGServerService/ServerInstance/Config.cs +++ b/TGServerService/ServerInstance/Config.cs @@ -13,11 +13,6 @@ namespace TGServerService /// Used for multithreading safety /// object configLock = new object(); //for atomic reads/writes - /// - public string ServerDirectory() - { - return Config.InstanceDirectory; - } /// [OperationBehavior(Impersonation = ImpersonationOption.Required)] diff --git a/TGServerService/ServerInstance/ServerInstance.cs b/TGServerService/ServerInstance/ServerInstance.cs index 64b05b9a15..9269e108a6 100644 --- a/TGServerService/ServerInstance/ServerInstance.cs +++ b/TGServerService/ServerInstance/ServerInstance.cs @@ -15,7 +15,7 @@ namespace TGServerService /// The class which holds all interface components. There are no safeguards for call race conditions so these must be guarded against internally /// [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.Single)] - sealed partial class ServerInstance : IDisposable, ITGConnectivity + sealed partial class ServerInstance : IDisposable, ITGConnectivity, ITGInstance { /// /// Used to assign the instance to event IDs @@ -120,6 +120,13 @@ namespace TGServerService SendMessage("SERVICE: Update started...", MessageType.DeveloperInfo); } + /// + public string ServerDirectory() + { + return Config.InstanceDirectory; + } + + //mostly generated code with a call to RunDisposals() //you don't need to open this #region IDisposable Support diff --git a/TGServiceInterface/Components/Config.cs b/TGServiceInterface/Components/Config.cs index 587f8a5f79..1055352aad 100644 --- a/TGServiceInterface/Components/Config.cs +++ b/TGServiceInterface/Components/Config.cs @@ -10,12 +10,6 @@ namespace TGServiceInterface.Components [ServiceContract] public interface ITGConfig { - /// - /// Return the directory of the server on the host machine - /// - /// The path to the directory on success, null on failure - [OperationContract] - string ServerDirectory(); /// /// Returns the file contents of the specified server directory diff --git a/TGServiceInterface/Components/Instance.cs b/TGServiceInterface/Components/Instance.cs new file mode 100644 index 0000000000..9172aee46b --- /dev/null +++ b/TGServiceInterface/Components/Instance.cs @@ -0,0 +1,18 @@ +using System.ServiceModel; + +namespace TGServiceInterface.Components +{ + /// + /// Metadata for a server instance + /// + [ServiceContract] + public interface ITGInstance + { + /// + /// Return the directory of the server on the host machine + /// + /// The path to the directory on success, null on failure + [OperationContract] + string ServerDirectory(); + } +} diff --git a/TGServiceInterface/DreamDaemonBridge.cs b/TGServiceInterface/DreamDaemonBridge.cs index cfd5697866..77de577130 100644 --- a/TGServiceInterface/DreamDaemonBridge.cs +++ b/TGServiceInterface/DreamDaemonBridge.cs @@ -24,10 +24,10 @@ namespace TGServiceInterface { var parsedArgs = new List(); parsedArgs.AddRange(args); - Interface.InstanceName = parsedArgs[0]; parsedArgs.RemoveAt(0); using (var I = new Interface()) - I.GetComponent().InteropMessage(String.Join(" ", parsedArgs)); + if(I.ConnectToInstanceImpl(parsedArgs[0], true).HasFlag(InstanceConnectivity.Connected)) + I.GetComponent().InteropMessage(String.Join(" ", parsedArgs)); } catch { } return 0; diff --git a/TGServiceInterface/Enumerations.cs b/TGServiceInterface/Enumerations.cs index 155af7f65f..5ce44bc951 100644 --- a/TGServiceInterface/Enumerations.cs +++ b/TGServiceInterface/Enumerations.cs @@ -1,5 +1,31 @@ -namespace TGServiceInterface +using System; + +namespace TGServiceInterface { + /// + /// Description of the connectivity level to an + /// + [Flags] + public enum InstanceConnectivity + { + /// + /// The connection could not be made, either a communication error occurred or the specified does not exist + /// + None = 0, + /// + /// The connection could be made + /// + Connected = 1, + /// + /// The connected user is authenticated + /// + Authenticated = 2, + /// + /// The connected user is an administrator + /// + Administrator = 4, + } + /// /// The status of a BYOND update job /// diff --git a/TGServiceInterface/Interface.cs b/TGServiceInterface/Interface.cs index 16d0026e7c..976fd567ca 100644 --- a/TGServiceInterface/Interface.cs +++ b/TGServiceInterface/Interface.cs @@ -46,9 +46,9 @@ namespace TGServiceInterface /// - /// The name of the current instance in use + /// The name of the current instance in use. Defaults to /// - public static string InstanceName; + public string InstanceName { get; private set; } /// /// If this is set, we will try and connect to an HTTPS server running at this address @@ -112,6 +112,48 @@ namespace TGServiceInterface HTTPSPassword = password; } + /// + /// Targets as the instance to use with . Closes all connections to any previous instance + /// + /// The name of the instance to connect to + /// The apporopriate value + public InstanceConnectivity ConnectToInstance(string instanceName) + { + return ConnectToInstanceImpl(instanceName, false); + } + + /// + /// Targets as the instance to use with . Closes all connections to any previous instance + /// + /// The name of the instance to connect to + /// If set to , skips the connectivity and authentication checks and returns + /// The apporopriate value + internal InstanceConnectivity ConnectToInstanceImpl(string instanceName, bool skipAuthChecks) { + if (VerifyConnection() != null) + return InstanceConnectivity.None; + var prevInstance = InstanceName; + CloseAllChannels(); + InstanceName = instanceName; + if (skipAuthChecks) + return InstanceConnectivity.Connected; + try + { + GetComponent().VerifyConnection(); + } + catch + { + InstanceName = prevInstance; + return InstanceConnectivity.None; + } + try + { + GetComponent().ServerDirectory(); + return InstanceConnectivity.Authenticated; + } catch { + return InstanceConnectivity.Connected; + } + } + /// /// Sets the function called when a remote login fails due to the server having an invalid SSL cert /// diff --git a/TGServiceInterface/TGServiceInterface.csproj b/TGServiceInterface/TGServiceInterface.csproj index ea546a5b8a..42943a1d0d 100644 --- a/TGServiceInterface/TGServiceInterface.csproj +++ b/TGServiceInterface/TGServiceInterface.csproj @@ -56,6 +56,7 @@ +