From c9cd6ec0c032f648e0729a91fcbc4a65f23303f6 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Sun, 12 Jan 2025 01:06:09 +0300 Subject: [PATCH] =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mROA.sln | 6 ++++ mROA/Abstract/IInteractionModule.cs | 1 + mROA/Implementation/FinalCommandExecution.cs | 6 ++-- mROA/Implementation/InputModule.cs | 30 ++++++++++++++----- .../Test/ConsoleInteractionChanel.cs | 18 ----------- .../Test/ProgramlyInteractionChanel.cs | 20 +++++++++++++ 6 files changed, 53 insertions(+), 28 deletions(-) delete mode 100644 mROA/Implementation/Test/ConsoleInteractionChanel.cs create mode 100644 mROA/Implementation/Test/ProgramlyInteractionChanel.cs diff --git a/mROA.sln b/mROA.sln index 6f4953b..b65ee37 100644 --- a/mROA.sln +++ b/mROA.sln @@ -2,6 +2,8 @@ Microsoft Visual Studio Solution File, Format Version 12.00 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mROA", "mROA\mROA.csproj", "{AE4E93F6-7B91-41FC-9BEA-2C8C2CBE1CB6}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mROA.Test", "mROA.Test\mROA.Test.csproj", "{D0E5760B-BB6E-453A-B396-A972CD94F133}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -12,5 +14,9 @@ Global {AE4E93F6-7B91-41FC-9BEA-2C8C2CBE1CB6}.Debug|Any CPU.Build.0 = Debug|Any CPU {AE4E93F6-7B91-41FC-9BEA-2C8C2CBE1CB6}.Release|Any CPU.ActiveCfg = Release|Any CPU {AE4E93F6-7B91-41FC-9BEA-2C8C2CBE1CB6}.Release|Any CPU.Build.0 = Release|Any CPU + {D0E5760B-BB6E-453A-B396-A972CD94F133}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D0E5760B-BB6E-453A-B396-A972CD94F133}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D0E5760B-BB6E-453A-B396-A972CD94F133}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D0E5760B-BB6E-453A-B396-A972CD94F133}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection EndGlobal diff --git a/mROA/Abstract/IInteractionModule.cs b/mROA/Abstract/IInteractionModule.cs index ad2c005..5658e6d 100644 --- a/mROA/Abstract/IInteractionModule.cs +++ b/mROA/Abstract/IInteractionModule.cs @@ -2,5 +2,6 @@ namespace mROA; public interface IInteractionModule { + void SetMessageHandler(Action handler); void SendTo(int clientId, byte[] message); } \ No newline at end of file diff --git a/mROA/Implementation/FinalCommandExecution.cs b/mROA/Implementation/FinalCommandExecution.cs index bbf70c3..d6fd1b7 100644 --- a/mROA/Implementation/FinalCommandExecution.cs +++ b/mROA/Implementation/FinalCommandExecution.cs @@ -2,7 +2,7 @@ public class FinalCommandExecution : ICommandExecution { - public Guid ExecutionId { get; init; } = new Guid(); + public Guid ExecutionId { get; init; } = Guid.NewGuid(); public int ClientId { get; set; } public int CommandId { get; set; } public object? Result { get; set; } @@ -10,7 +10,7 @@ public class FinalCommandExecution : ICommandExecution public class ExeptionCommandExecution : ICommandExecution { - public Guid ExecutionId { get; init; } = new Guid(); + public Guid ExecutionId { get; init; } = Guid.NewGuid(); public int ClientId { get; set; } public int CommandId { get; set; } public string Reason { get; set; } @@ -18,7 +18,7 @@ public class ExeptionCommandExecution : ICommandExecution public class AsyncCommandExecution : ICommandExecution { - public Guid ExecutionId { get; init; } = new Guid(); + public Guid ExecutionId { get; init; } = Guid.NewGuid(); public int ClientId { get; set; } public int CommandId { get; set; } diff --git a/mROA/Implementation/InputModule.cs b/mROA/Implementation/InputModule.cs index 3ce3836..2cd4a89 100644 --- a/mROA/Implementation/InputModule.cs +++ b/mROA/Implementation/InputModule.cs @@ -4,9 +4,18 @@ using System.Text.Json.Serialization; namespace mROA.Implementation; -public class InputModule(IInteractionModule dataSource, IExecuteModule executeModule) - : IInputModule +public class InputModule : IInputModule { + private readonly IInteractionModule _dataSource; + private readonly IExecuteModule _executeModule; + + public InputModule(IInteractionModule dataSource, IExecuteModule executeModule) + { + _dataSource = dataSource; + _executeModule = executeModule; + _dataSource.SetMessageHandler(HandleIncomingRequest); + } + public void HandleIncomingRequest(int clientId, string command) { var type = JsonDocument.Parse(command).RootElement.GetProperty("RequestTypeId").GetInt32(); @@ -29,17 +38,24 @@ public class InputModule(IInteractionModule dataSource, IExecuteModule executeMo break; } - var response = executeModule.Execute(request); - - var texted = JsonSerializer.Serialize(response); + request.ClientId = clientId; + var response = _executeModule.Execute(request); + var texted = string.Empty; + if (response is FinalCommandExecution finalCommand) + { + texted = JsonSerializer.Serialize(finalCommand); + }else if (response is AsyncCommandExecution asyncCommand) + { + texted = JsonSerializer.Serialize(asyncCommand); + } var binary = Encoding.UTF8.GetBytes(texted); - dataSource.SendTo(clientId, binary); + _dataSource.SendTo(clientId, binary); } public void PostResponse(ICommandExecution call) { var texted = JsonSerializer.Serialize(call); var binary = Encoding.UTF8.GetBytes(texted); - dataSource.SendTo(call.ClientId, binary); + _dataSource.SendTo(call.ClientId, binary); } } \ No newline at end of file diff --git a/mROA/Implementation/Test/ConsoleInteractionChanel.cs b/mROA/Implementation/Test/ConsoleInteractionChanel.cs deleted file mode 100644 index 6f8e1aa..0000000 --- a/mROA/Implementation/Test/ConsoleInteractionChanel.cs +++ /dev/null @@ -1,18 +0,0 @@ -namespace mROA.Implementation; - -public class ConsoleInteractionChanel(IInputModule inputModule) : IInteractionModule -{ - public void StartListeningKeyboard() - { - while (true) - { - var input = Console.ReadLine(); - inputModule.HandleIncomingRequest(123, input); - } - } - - public void SendTo(int clientId, byte[] message) - { - Console.WriteLine("{0}: {1}", clientId, System.Text.Encoding.UTF8.GetString(message)); - } -} \ No newline at end of file diff --git a/mROA/Implementation/Test/ProgramlyInteractionChanel.cs b/mROA/Implementation/Test/ProgramlyInteractionChanel.cs new file mode 100644 index 0000000..b2116e4 --- /dev/null +++ b/mROA/Implementation/Test/ProgramlyInteractionChanel.cs @@ -0,0 +1,20 @@ +namespace mROA.Implementation; + +public class ProgramlyInteractionChanel : IInteractionModule +{ + public void PassCommand(int clientId, string message) => _handler(clientId, message); + private Action _handler; + + public List OutputBuffer = new List(); + + public void SetMessageHandler(Action handler) + { + _handler = handler; + } + + public void SendTo(int clientId, byte[] message) + { + Console.WriteLine("{0}: {1}", clientId, System.Text.Encoding.UTF8.GetString(message)); + OutputBuffer.Add(System.Text.Encoding.UTF8.GetString(message)); + } +} \ No newline at end of file