diff --git a/src/Tgstation.Server.Host/Components/Watchdog/InteropCommand.cs b/src/Tgstation.Server.Host/Components/Interop/CommCommand.cs similarity index 59% rename from src/Tgstation.Server.Host/Components/Watchdog/InteropCommand.cs rename to src/Tgstation.Server.Host/Components/Interop/CommCommand.cs index 67f9bd550c..cc9ab8571a 100644 --- a/src/Tgstation.Server.Host/Components/Watchdog/InteropCommand.cs +++ b/src/Tgstation.Server.Host/Components/Interop/CommCommand.cs @@ -1,14 +1,14 @@ using System.Collections.Generic; -namespace Tgstation.Server.Host.Components.Watchdog +namespace Tgstation.Server.Host.Components.Interop { /// /// Represents a command from DD /// - sealed class InteropCommand + sealed class CommCommand { /// - /// The raw JSON decond of the + /// The raw JSON decond of the /// public IReadOnlyDictionary Parameters { get; set; } } diff --git a/src/Tgstation.Server.Host/Components/Watchdog/InteropContext.cs b/src/Tgstation.Server.Host/Components/Interop/CommContext.cs similarity index 77% rename from src/Tgstation.Server.Host/Components/Watchdog/InteropContext.cs rename to src/Tgstation.Server.Host/Components/Interop/CommContext.cs index 47bc455eff..19ee6d7777 100644 --- a/src/Tgstation.Server.Host/Components/Watchdog/InteropContext.cs +++ b/src/Tgstation.Server.Host/Components/Interop/CommContext.cs @@ -7,49 +7,49 @@ using System.Threading; using System.Threading.Tasks; using Tgstation.Server.Host.IO; -namespace Tgstation.Server.Host.Components.Watchdog +namespace Tgstation.Server.Host.Components.Interop { /// - sealed class InteropContext : IInteropContext + sealed class CommContext : ICommContext { /// - /// The for the + /// The for the /// readonly IIOManager ioManager; /// - /// The for the + /// The for the /// - readonly ILogger logger; + readonly ILogger logger; /// - /// The for the + /// The for the /// readonly FileSystemWatcher fileSystemWatcher; /// - /// The for the + /// The for the /// readonly CancellationTokenSource cancellationTokenSource; /// - /// The for the + /// The for the /// readonly CancellationToken cancellationToken; /// - /// The for the + /// The for the /// - IInteropHandler handler; + ICommHandler handler; /// - /// Construct an + /// Construct an /// /// The value of /// The value of /// The path to watch /// The filter to watch for - public InteropContext(IIOManager ioManager, ILogger logger, string directory, string filter) + public CommContext(IIOManager ioManager, ILogger logger, string directory, string filter) { this.ioManager = ioManager ?? throw new ArgumentNullException(nameof(ioManager)); this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); @@ -93,10 +93,10 @@ namespace Tgstation.Server.Host.Components.Watchdog var fileBytes = await ioManager.ReadAllBytes(e.FullPath, cancellationToken).ConfigureAwait(false); var file = Encoding.UTF8.GetString(fileBytes); - InteropCommand command; + CommCommand command; try { - command = JsonConvert.DeserializeObject(file); + command = JsonConvert.DeserializeObject(file); } catch (JsonSerializationException ex) { @@ -114,7 +114,7 @@ namespace Tgstation.Server.Host.Components.Watchdog } /// - public void RegisterHandler(IInteropHandler handler) + public void RegisterHandler(ICommHandler handler) { if (this.handler != null) throw new InvalidOperationException("RegisterHandler already called!"); diff --git a/src/Tgstation.Server.Host/Components/Watchdog/InteropConstants.cs b/src/Tgstation.Server.Host/Components/Interop/Constants.cs similarity index 92% rename from src/Tgstation.Server.Host/Components/Watchdog/InteropConstants.cs rename to src/Tgstation.Server.Host/Components/Interop/Constants.cs index 0235e8da67..00b516bd94 100644 --- a/src/Tgstation.Server.Host/Components/Watchdog/InteropConstants.cs +++ b/src/Tgstation.Server.Host/Components/Interop/Constants.cs @@ -1,6 +1,6 @@ -namespace Tgstation.Server.Host.Components.Watchdog +namespace Tgstation.Server.Host.Components.Interop { - static class InteropConstants + static class Constants { //interop values, match them up with the appropriate api.dm diff --git a/src/Tgstation.Server.Host/Components/Interop/ICommContext.cs b/src/Tgstation.Server.Host/Components/Interop/ICommContext.cs new file mode 100644 index 0000000000..c3f034551c --- /dev/null +++ b/src/Tgstation.Server.Host/Components/Interop/ICommContext.cs @@ -0,0 +1,16 @@ +using System; + +namespace Tgstation.Server.Host.Components.Interop +{ + /// + /// Represents a registration of an interop session + /// + interface ICommContext : IDisposable + { + /// + /// Register a with the + /// + /// The to register + void RegisterHandler(ICommHandler handler); + } +} \ No newline at end of file diff --git a/src/Tgstation.Server.Host/Components/Watchdog/IInteropHandler.cs b/src/Tgstation.Server.Host/Components/Interop/ICommHandler.cs similarity index 55% rename from src/Tgstation.Server.Host/Components/Watchdog/IInteropHandler.cs rename to src/Tgstation.Server.Host/Components/Interop/ICommHandler.cs index 1ab32d5d8d..53a9256bf7 100644 --- a/src/Tgstation.Server.Host/Components/Watchdog/IInteropHandler.cs +++ b/src/Tgstation.Server.Host/Components/Interop/ICommHandler.cs @@ -1,19 +1,19 @@ using System.Threading; using System.Threading.Tasks; -namespace Tgstation.Server.Host.Components.Watchdog +namespace Tgstation.Server.Host.Components.Interop { /// - /// Handles s + /// Handles s /// - interface IInteropHandler + interface ICommHandler { /// /// Handle a /// - /// The to handle + /// The to handle /// The for the operation /// A representing the running operation - Task HandleInterop(InteropCommand command, CancellationToken cancellationToken); + Task HandleInterop(CommCommand command, CancellationToken cancellationToken); } } \ No newline at end of file diff --git a/src/Tgstation.Server.Host/Components/Watchdog/InteropInfo.cs b/src/Tgstation.Server.Host/Components/Interop/JsonFile.cs similarity index 94% rename from src/Tgstation.Server.Host/Components/Watchdog/InteropInfo.cs rename to src/Tgstation.Server.Host/Components/Interop/JsonFile.cs index 6530632ff8..861cb8cebb 100644 --- a/src/Tgstation.Server.Host/Components/Watchdog/InteropInfo.cs +++ b/src/Tgstation.Server.Host/Components/Interop/JsonFile.cs @@ -1,12 +1,12 @@ using System.Collections.Generic; using Tgstation.Server.Api.Models.Internal; -namespace Tgstation.Server.Host.Components.Watchdog +namespace Tgstation.Server.Host.Components.Interop { /// /// Representation of the initial json passed to DreamDaemon /// - sealed class InteropInfo + sealed class JsonFile { /// /// The code used by the server to authenticate command Topics diff --git a/src/Tgstation.Server.Host/Components/Watchdog/InteropInfoBase.cs b/src/Tgstation.Server.Host/Components/Interop/JsonSubFileList.cs similarity index 72% rename from src/Tgstation.Server.Host/Components/Watchdog/InteropInfoBase.cs rename to src/Tgstation.Server.Host/Components/Interop/JsonSubFileList.cs index 7a494a4953..2e3066cd15 100644 --- a/src/Tgstation.Server.Host/Components/Watchdog/InteropInfoBase.cs +++ b/src/Tgstation.Server.Host/Components/Interop/JsonSubFileList.cs @@ -1,12 +1,12 @@ using System; using System.ComponentModel.DataAnnotations; -namespace Tgstation.Server.Host.Components.Watchdog +namespace Tgstation.Server.Host.Components.Interop { /// /// Information used in for reattaching and interop /// - public class InteropInfoBase + public class JsonSubFileList { /// /// Path to the chat commands json file @@ -27,15 +27,15 @@ namespace Tgstation.Server.Host.Components.Watchdog public string ServerCommandsJson { get; set; } /// - /// Construct an + /// Construct an /// - protected InteropInfoBase() { } + protected JsonSubFileList() { } /// - /// Construct an from a + /// Construct an from a /// - /// An to copy - public InteropInfoBase(InteropInfoBase copy) + /// An to copy + public JsonSubFileList(JsonSubFileList copy) { if (copy == null) throw new ArgumentNullException(nameof(copy)); diff --git a/src/Tgstation.Server.Host/Components/Watchdog/TestMerge.cs b/src/Tgstation.Server.Host/Components/Interop/TestMerge.cs similarity index 93% rename from src/Tgstation.Server.Host/Components/Watchdog/TestMerge.cs rename to src/Tgstation.Server.Host/Components/Interop/TestMerge.cs index e72d6cfc5c..9f2b4e247f 100644 --- a/src/Tgstation.Server.Host/Components/Watchdog/TestMerge.cs +++ b/src/Tgstation.Server.Host/Components/Interop/TestMerge.cs @@ -1,6 +1,6 @@ using Tgstation.Server.Api.Models.Internal; -namespace Tgstation.Server.Host.Components.Watchdog +namespace Tgstation.Server.Host.Components.Interop { /// /// This model mirrors /datum/tgs_revision_information/test_merge diff --git a/src/Tgstation.Server.Host/Components/Watchdog/IInteropContext.cs b/src/Tgstation.Server.Host/Components/Watchdog/IInteropContext.cs deleted file mode 100644 index 0a1572cf64..0000000000 --- a/src/Tgstation.Server.Host/Components/Watchdog/IInteropContext.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; - -namespace Tgstation.Server.Host.Components.Watchdog -{ - /// - /// Represents a registration of an interop session - /// - interface IInteropContext : IDisposable - { - /// - /// Register a with the - /// - /// The to register - void RegisterHandler(IInteropHandler handler); - } -} \ No newline at end of file diff --git a/src/Tgstation.Server.Host/Components/Watchdog/SessionController.cs b/src/Tgstation.Server.Host/Components/Watchdog/SessionController.cs index 4a6e5a8c03..e5b24240d5 100644 --- a/src/Tgstation.Server.Host/Components/Watchdog/SessionController.cs +++ b/src/Tgstation.Server.Host/Components/Watchdog/SessionController.cs @@ -10,12 +10,13 @@ using System.Threading.Tasks; using Tgstation.Server.Api.Models; using Tgstation.Server.Host.Components.Byond; using Tgstation.Server.Host.Components.Chat; +using Tgstation.Server.Host.Components.Interop; using Tgstation.Server.Host.Core; namespace Tgstation.Server.Host.Components.Watchdog { /// - sealed class SessionController : ISessionController, IInteropHandler + sealed class SessionController : ISessionController, ICommHandler { /// public bool IsPrimary @@ -93,9 +94,9 @@ namespace Tgstation.Server.Host.Components.Watchdog readonly IByondTopicSender byondTopicSender; /// - /// The for the + /// The for the /// - readonly IInteropContext interopContext; + readonly ICommContext interopContext; /// /// The for the @@ -166,7 +167,7 @@ namespace Tgstation.Server.Host.Components.Watchdog /// The value of /// The value of /// The value of - public SessionController(ReattachInformation reattachInformation, IProcess process, IByondExecutableLock byondLock, IByondTopicSender byondTopicSender, IJsonTrackingContext chatJsonTrackingContext, IInteropContext interopContext, IChat chat, ILogger logger) + public SessionController(ReattachInformation reattachInformation, IProcess process, IByondExecutableLock byondLock, IByondTopicSender byondTopicSender, IJsonTrackingContext chatJsonTrackingContext, ICommContext interopContext, IChat chat, ILogger logger) { this.chatJsonTrackingContext = chatJsonTrackingContext; //null valid this.reattachInformation = reattachInformation ?? throw new ArgumentNullException(nameof(reattachInformation)); @@ -247,7 +248,7 @@ namespace Tgstation.Server.Host.Components.Watchdog } /// - public Task HandleInterop(InteropCommand command, CancellationToken cancellationToken) + public Task HandleInterop(CommCommand command, CancellationToken cancellationToken) { if (command == null) throw new ArgumentNullException(nameof(command)); @@ -255,17 +256,17 @@ namespace Tgstation.Server.Host.Components.Watchdog var query = command.Parameters; object content; - if (query.TryGetValue(InteropConstants.DMParameterCommand, out var method)) + if (query.TryGetValue(Constants.DMParameterCommand, out var method)) { content = new object(); switch (method) { - case InteropConstants.DMCommandIdentify: + case Constants.DMCommandIdentify: lock (this) if (portClosed) - content = new Dictionary { { InteropConstants.DMParameterData, nextPort } }; + content = new Dictionary { { Constants.DMParameterData, nextPort } }; break; - case InteropConstants.DMCommandOnline: + case Constants.DMCommandOnline: lock (this) if (portClosed) { @@ -275,13 +276,13 @@ namespace Tgstation.Server.Host.Components.Watchdog portClosed = false; } break; - case InteropConstants.DMCommandApiValidate: + case Constants.DMCommandApiValidate: apiValidated = true; break; - case InteropConstants.DMCommandWorldReboot: + case Constants.DMCommandWorldReboot: if (ClosePortOnReboot) { - content = new Dictionary { { InteropConstants.DMParameterData, 0 } }; + content = new Dictionary { { Constants.DMParameterData, 0 } }; portClosed = true; } else @@ -299,7 +300,7 @@ namespace Tgstation.Server.Host.Components.Watchdog content = new ErrorMessage { Message = "Missing command parameter!" }; var json = JsonConvert.SerializeObject(content); - return SendCommand(String.Format(CultureInfo.InvariantCulture, "{0}&{1}={2}", byondTopicSender.SanitizeString(InteropConstants.DMTopicInteropResponse), byondTopicSender.SanitizeString(InteropConstants.DMParameterData), byondTopicSender.SanitizeString(json)), cancellationToken); + return SendCommand(String.Format(CultureInfo.InvariantCulture, "{0}&{1}={2}", byondTopicSender.SanitizeString(Constants.DMTopicInteropResponse), byondTopicSender.SanitizeString(Constants.DMParameterData), byondTopicSender.SanitizeString(json)), cancellationToken); } /// @@ -332,9 +333,9 @@ namespace Tgstation.Server.Host.Components.Watchdog { var commandString = String.Format(CultureInfo.InvariantCulture, "?{0}={1}&{2}={3}", - byondTopicSender.SanitizeString(InteropConstants.DMInteropAccessIdentifier), + byondTopicSender.SanitizeString(Constants.DMInteropAccessIdentifier), byondTopicSender.SanitizeString(reattachInformation.AccessIdentifier), - byondTopicSender.SanitizeString(InteropConstants.DMParameterCommand), + byondTopicSender.SanitizeString(Constants.DMParameterCommand), //intentionally don't sanitize command, that's up to the caller command); @@ -352,7 +353,7 @@ namespace Tgstation.Server.Host.Components.Watchdog } } - async Task SetPortImpl(ushort port, CancellationToken cancellationToken) => await SendCommand(String.Format(CultureInfo.InvariantCulture, "{0}&{1}={2}", byondTopicSender.SanitizeString(InteropConstants.DMTopicChangePort), byondTopicSender.SanitizeString(InteropConstants.DMParameterData), byondTopicSender.SanitizeString(port.ToString(CultureInfo.InvariantCulture))), cancellationToken).ConfigureAwait(false) == InteropConstants.DMResponseSuccess; + async Task SetPortImpl(ushort port, CancellationToken cancellationToken) => await SendCommand(String.Format(CultureInfo.InvariantCulture, "{0}&{1}={2}", byondTopicSender.SanitizeString(Constants.DMTopicChangePort), byondTopicSender.SanitizeString(Constants.DMParameterData), byondTopicSender.SanitizeString(port.ToString(CultureInfo.InvariantCulture))), cancellationToken).ConfigureAwait(false) == Constants.DMResponseSuccess; /// public async Task ClosePort(CancellationToken cancellationToken) @@ -399,7 +400,7 @@ namespace Tgstation.Server.Host.Components.Watchdog if (RebootState == newRebootState) return true; - return await SendCommand(String.Format(CultureInfo.InvariantCulture, "{0}&{1}={2}", InteropConstants.DMTopicChangeReboot, InteropConstants.DMParameterData, (int)newRebootState), cancellationToken).ConfigureAwait(false) == InteropConstants.DMResponseSuccess; + return await SendCommand(String.Format(CultureInfo.InvariantCulture, "{0}&{1}={2}", Constants.DMTopicChangeReboot, Constants.DMParameterData, (int)newRebootState), cancellationToken).ConfigureAwait(false) == Constants.DMResponseSuccess; } /// diff --git a/src/Tgstation.Server.Host/Components/Watchdog/SessionControllerFactory.cs b/src/Tgstation.Server.Host/Components/Watchdog/SessionControllerFactory.cs index e915aa4084..92d109e28b 100644 --- a/src/Tgstation.Server.Host/Components/Watchdog/SessionControllerFactory.cs +++ b/src/Tgstation.Server.Host/Components/Watchdog/SessionControllerFactory.cs @@ -12,6 +12,7 @@ using Tgstation.Server.Api.Models; using Tgstation.Server.Api.Models.Internal; using Tgstation.Server.Host.Components.Byond; using Tgstation.Server.Host.Components.Chat; +using Tgstation.Server.Host.Components.Interop; using Tgstation.Server.Host.Core; using Tgstation.Server.Host.IO; using Tgstation.Server.Host.Security; @@ -130,7 +131,7 @@ namespace Tgstation.Server.Host.Components.Watchdog //i changed this back from guids, hopefully i don't regret that string JsonFile(string name) => String.Format(CultureInfo.InvariantCulture, "{0}.{1}", name, JsonPostfix); - var interopInfo = new InteropInfo + var interopInfo = new JsonFile { AccessIdentifier = accessIdentifier, ApiValidateOnly = apiValidate, @@ -141,7 +142,7 @@ namespace Tgstation.Server.Host.Components.Watchdog Revision = dmbProvider.CompileJob.RevisionInformation }; - interopInfo.TestMerges.AddRange(dmbProvider.CompileJob.RevisionInformation.ActiveTestMerges.Select(x => x.TestMerge).Select(x => new TestMerge(x))); + interopInfo.TestMerges.AddRange(dmbProvider.CompileJob.RevisionInformation.ActiveTestMerges.Select(x => x.TestMerge).Select(x => new Interop.TestMerge(x))); var interopJsonFile = JsonFile("interop"); @@ -166,9 +167,9 @@ namespace Tgstation.Server.Host.Components.Watchdog try { //more sanitization here cause it uses the same scheme - var parameters = String.Format(CultureInfo.InvariantCulture, "{2}={0}&{3}={1}", byondTopicSender.SanitizeString(application.Version.ToString()), byondTopicSender.SanitizeString(interopJsonFile), byondTopicSender.SanitizeString(InteropConstants.DMParamHostVersion), byondTopicSender.SanitizeString(InteropConstants.DMParamInfoJson)); + var parameters = String.Format(CultureInfo.InvariantCulture, "{2}={0}&{3}={1}", byondTopicSender.SanitizeString(application.Version.ToString()), byondTopicSender.SanitizeString(interopJsonFile), byondTopicSender.SanitizeString(Constants.DMParamHostVersion), byondTopicSender.SanitizeString(Constants.DMParamInfoJson)); - var context = new InteropContext(ioManager, loggerFactory.CreateLogger(), basePath, interopInfo.ServerCommandsJson); + var context = new CommContext(ioManager, loggerFactory.CreateLogger(), basePath, interopInfo.ServerCommandsJson); try { var arguments = String.Format(CultureInfo.InvariantCulture, "{0} -port {1} {2}-close -{3} -verbose -public -params \"{4}\"", @@ -232,7 +233,7 @@ namespace Tgstation.Server.Host.Components.Watchdog var byondLock = await byond.UseExecutables(Version.Parse(reattachInformation.Dmb.CompileJob.ByondVersion), cancellationToken).ConfigureAwait(false); try { - var context = new InteropContext(ioManager, loggerFactory.CreateLogger(), basePath, reattachInformation.ServerCommandsJson); + var context = new CommContext(ioManager, loggerFactory.CreateLogger(), basePath, reattachInformation.ServerCommandsJson); try { var process = processExecutor.GetProcess(reattachInformation.ProcessId); diff --git a/src/Tgstation.Server.Host/Components/Watchdog/Watchdog.cs b/src/Tgstation.Server.Host/Components/Watchdog/Watchdog.cs index 8a043aa2a0..4bbc1c6dbb 100644 --- a/src/Tgstation.Server.Host/Components/Watchdog/Watchdog.cs +++ b/src/Tgstation.Server.Host/Components/Watchdog/Watchdog.cs @@ -11,6 +11,7 @@ using System.Threading.Tasks; using Tgstation.Server.Api.Models.Internal; using Tgstation.Server.Host.Components.Chat; using Tgstation.Server.Host.Components.Compiler; +using Tgstation.Server.Host.Components.Interop; using Tgstation.Server.Host.Core; namespace Tgstation.Server.Host.Components.Watchdog @@ -787,7 +788,7 @@ namespace Tgstation.Server.Host.Components.Watchdog if (!Running) return true; - var builder = new StringBuilder(InteropConstants.DMTopicEvent); + var builder = new StringBuilder(Constants.DMTopicEvent); foreach (var I in parameters) { builder.Append("&"); @@ -825,7 +826,7 @@ namespace Tgstation.Server.Host.Components.Watchdog if (!Running) return "ERROR: Server offline!"; - var command = String.Format(CultureInfo.InvariantCulture, "{0}&{1}={2}", byondTopicSender.SanitizeString(InteropConstants.DMTopicChatCommand), byondTopicSender.SanitizeString(InteropConstants.DMParameterData), byondTopicSender.SanitizeString(JsonConvert.SerializeObject(arguments))); + var command = String.Format(CultureInfo.InvariantCulture, "{0}&{1}={2}", byondTopicSender.SanitizeString(Constants.DMTopicChatCommand), byondTopicSender.SanitizeString(Constants.DMParameterData), byondTopicSender.SanitizeString(JsonConvert.SerializeObject(arguments))); var activeServer = AlphaIsActive ? alphaServer : bravoServer; return await activeServer.SendCommand(command, cancellationToken).ConfigureAwait(false) ?? "ERROR: Bad topic exchange!"; diff --git a/src/Tgstation.Server.Host/Models/ReattachInformationBase.cs b/src/Tgstation.Server.Host/Models/ReattachInformationBase.cs index 33a9ebdbe8..3d808a7f96 100644 --- a/src/Tgstation.Server.Host/Models/ReattachInformationBase.cs +++ b/src/Tgstation.Server.Host/Models/ReattachInformationBase.cs @@ -1,5 +1,6 @@ using System; using System.ComponentModel.DataAnnotations; +using Tgstation.Server.Host.Components.Interop; using Tgstation.Server.Host.Components.Watchdog; namespace Tgstation.Server.Host.Models @@ -7,7 +8,7 @@ namespace Tgstation.Server.Host.Models /// /// Base class for /// - public abstract class ReattachInformationBase : InteropInfoBase + public abstract class ReattachInformationBase : JsonSubFileList { /// /// Used to identify and authenticate the DreamDaemon instance