mock exe mod and keyboard input
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
using mROA.Implementation;
|
||||
|
||||
namespace mROA;
|
||||
|
||||
public interface IExecuteModule
|
||||
{
|
||||
ICommandExecution Execute(int objectId, int commandId, object parameter);
|
||||
ICommandExecution Execute(ICallRequest command);
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
@@ -2,6 +2,5 @@ namespace mROA;
|
||||
|
||||
public interface IInteractionModule
|
||||
{
|
||||
void SetInteractionHandler(Action<(int clientId, byte[] message)> handler);
|
||||
void SendTo(int clientId, byte[] message);
|
||||
}
|
||||
@@ -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<StaticCallRequest>(command);
|
||||
break;
|
||||
case 1:
|
||||
request = JsonSerializer.Deserialize<CallRequest>(command);
|
||||
break;
|
||||
case 2:
|
||||
request = JsonSerializer.Deserialize<ParametrizedCallRequest>(command);
|
||||
break;
|
||||
default:
|
||||
request = JsonSerializer.Deserialize<StaticCallRequest>(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)
|
||||
|
||||
@@ -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<object>;
|
||||
|
||||
var exec = new AsyncCommandExecution(tokenSource) { CommandId = commandId };
|
||||
var result =
|
||||
currentCommand.Invoke(context, parameter is null ? [token] : [parameter, token]) as Task<object>;
|
||||
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 };
|
||||
}
|
||||
}
|
||||
@@ -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));
|
||||
}
|
||||
}
|
||||
@@ -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 } };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user