From a78494a7b167e7b60c7710afd7b7e1da9d7e84f9 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Sun, 12 Jan 2025 00:43:51 +0300 Subject: [PATCH] mock exe mod and keyboard input --- mROA/Abstract/IExecuteModule.cs | 4 +- mROA/Abstract/IInputModule.cs | 4 +- mROA/Abstract/IInteractionModule.cs | 1 - mROA/Implementation/InputModule.cs | 26 +++++++++- .../PrepairedExecutionModule.cs | 48 +++++++++++++++---- .../Test/ConsoleInteractionChanel.cs | 18 +++++++ mROA/Implementation/Test/MockExecModule.cs | 10 ++++ 7 files changed, 97 insertions(+), 14 deletions(-) create mode 100644 mROA/Implementation/Test/ConsoleInteractionChanel.cs create mode 100644 mROA/Implementation/Test/MockExecModule.cs diff --git a/mROA/Abstract/IExecuteModule.cs b/mROA/Abstract/IExecuteModule.cs index df9c495..4df615a 100644 --- a/mROA/Abstract/IExecuteModule.cs +++ b/mROA/Abstract/IExecuteModule.cs @@ -1,6 +1,8 @@ +using mROA.Implementation; + namespace mROA; public interface IExecuteModule { - ICommandExecution Execute(int objectId, int commandId, object parameter); + ICommandExecution Execute(ICallRequest command); } \ No newline at end of file diff --git a/mROA/Abstract/IInputModule.cs b/mROA/Abstract/IInputModule.cs index 0c55f50..c794cec 100644 --- a/mROA/Abstract/IInputModule.cs +++ b/mROA/Abstract/IInputModule.cs @@ -1,7 +1,9 @@ +using mROA.Implementation; + namespace mROA; public interface IInputModule { - void HandleIncomingRequest(object command); + void HandleIncomingRequest(int clientId, string command); void PostResponse(ICommandExecution call); } \ No newline at end of file diff --git a/mROA/Abstract/IInteractionModule.cs b/mROA/Abstract/IInteractionModule.cs index e5c5878..ad2c005 100644 --- a/mROA/Abstract/IInteractionModule.cs +++ b/mROA/Abstract/IInteractionModule.cs @@ -2,6 +2,5 @@ namespace mROA; public interface IInteractionModule { - void SetInteractionHandler(Action<(int clientId, byte[] message)> handler); void SendTo(int clientId, byte[] message); } \ No newline at end of file diff --git a/mROA/Implementation/InputModule.cs b/mROA/Implementation/InputModule.cs index aa5bc0c..3ce3836 100644 --- a/mROA/Implementation/InputModule.cs +++ b/mROA/Implementation/InputModule.cs @@ -7,9 +7,33 @@ namespace mROA.Implementation; public class InputModule(IInteractionModule dataSource, IExecuteModule executeModule) : IInputModule { - public void HandleIncomingRequest(object command) + public void HandleIncomingRequest(int clientId, string command) { + var type = JsonDocument.Parse(command).RootElement.GetProperty("RequestTypeId").GetInt32(); + + ICallRequest? request; + + switch (type) + { + case 0: + request = JsonSerializer.Deserialize(command); + break; + case 1: + request = JsonSerializer.Deserialize(command); + break; + case 2: + request = JsonSerializer.Deserialize(command); + break; + default: + request = JsonSerializer.Deserialize(command); + break; + } + + var response = executeModule.Execute(request); + var texted = JsonSerializer.Serialize(response); + var binary = Encoding.UTF8.GetBytes(texted); + dataSource.SendTo(clientId, binary); } public void PostResponse(ICommandExecution call) diff --git a/mROA/Implementation/PrepairedExecutionModule.cs b/mROA/Implementation/PrepairedExecutionModule.cs index 931967b..6d8fd0e 100644 --- a/mROA/Implementation/PrepairedExecutionModule.cs +++ b/mROA/Implementation/PrepairedExecutionModule.cs @@ -8,31 +8,59 @@ public class PrepairedExecutionModule( IContextRepository contextRepo) : IExecuteModule { - public ICommandExecution Execute(int objectId, int commandId, object parameter) + public ICommandExecution Execute(ICallRequest command) { - var command = methodRepo.GetMethod(commandId); - if (command == null) - throw new Exception($"Command {commandId} not found"); + var currentCommand = methodRepo.GetMethod(command.CommandId); + if (currentCommand == null) + throw new Exception($"Command {command.CommandId} not found"); - if (command.ReturnType != typeof(Task)) + var context = command is CallRequest request ? contextRepo.GetObject(request.ObjectId) : null; + var parameter = command is ParametrizedCallRequest callRequest ? callRequest.Parameter : null; + + if (currentCommand.ReturnType == typeof(Task<>)) { var tokenSource = new CancellationTokenSource(); var token = tokenSource.Token; - var result = command.Invoke(contextRepo.GetObject(objectId), [parameter, token]) as Task; - var exec = new AsyncCommandExecution(tokenSource) { CommandId = commandId }; + var result = + currentCommand.Invoke(context, parameter is null ? [token] : [parameter, token]) as Task; + var exec = new AsyncCommandExecution(tokenSource) + { CommandId = command.CommandId, ClientId = command.ClientId }; result.ContinueWith(task => { var result = task.Result; inputModule.PostResponse(new FinalCommandExecution - { ExecutionId = exec.ExecutionId, Result = result, CommandId = commandId }); + { + ExecutionId = exec.ExecutionId, Result = result, CommandId = command.CommandId, + ClientId = command.ClientId + }); + }, token); + + return exec; + } + else + { + var tokenSource = new CancellationTokenSource(); + var token = tokenSource.Token; + + var result = currentCommand.Invoke(context, parameter is null ? [token] : [parameter, token]) as Task; + var exec = new AsyncCommandExecution(tokenSource) + { CommandId = command.CommandId, ClientId = command.ClientId }; + + result.ContinueWith(_ => + { + inputModule.PostResponse(new FinalCommandExecution + { + ExecutionId = exec.ExecutionId, Result = null, CommandId = command.CommandId, + ClientId = command.ClientId + }); }, token); return exec; } - var finalResult = command.Invoke(contextRepo.GetObject(objectId), [parameter]); - return new FinalCommandExecution { CommandId = commandId, Result = finalResult }; + var finalResult = currentCommand.Invoke(context, parameter is null ? [] : [parameter]); + return new FinalCommandExecution { CommandId = command.CommandId, Result = finalResult }; } } \ No newline at end of file diff --git a/mROA/Implementation/Test/ConsoleInteractionChanel.cs b/mROA/Implementation/Test/ConsoleInteractionChanel.cs new file mode 100644 index 0000000..6f8e1aa --- /dev/null +++ b/mROA/Implementation/Test/ConsoleInteractionChanel.cs @@ -0,0 +1,18 @@ +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/MockExecModule.cs b/mROA/Implementation/Test/MockExecModule.cs new file mode 100644 index 0000000..697e1c2 --- /dev/null +++ b/mROA/Implementation/Test/MockExecModule.cs @@ -0,0 +1,10 @@ +namespace mROA.Implementation; + +public class MockExecModule : IExecuteModule +{ + public ICommandExecution Execute(ICallRequest command) + { + return new FinalCommandExecution + { ClientId = command.ClientId, CommandId = command.CommandId, Result = new { A = "wqer", B = 5 } }; + } +} \ No newline at end of file