diff --git a/mROA.Test/StreamTest.cs b/mROA.Test/StreamTest.cs index e799071..54dc398 100644 --- a/mROA.Test/StreamTest.cs +++ b/mROA.Test/StreamTest.cs @@ -1,28 +1,56 @@ +using System.Net; +using System.Net.Sockets; using mROA.Implementation; +using mROA.Implementation.Test; namespace mROA.Test; public class StreamTest { private StreamBasedInteractionModule _interactionModule; + private StreamBasedFrontendInteractionModule _frontendInteractionModule; + private JsonFrontendSerialisationModule _frontendSerialisationModule; private ISerialisationModule _serialisationModule; private IExecuteModule _executeModule; - private IMethodRepository _methodRepository; - private IContextRepository _contextRepository; + [SetUp] public void Setup() { _interactionModule = new StreamBasedInteractionModule(); - - _serialisationModule = new JsonSerialisationModule(_interactionModule, _methodRepository); + + _serialisationModule = new JsonSerialisationModule(_interactionModule, new MockMethodRepository()); _executeModule = new MockExecModule(); _serialisationModule.SetExecuteModule(_executeModule); + + _frontendInteractionModule = new StreamBasedFrontendInteractionModule(); + _frontendSerialisationModule = new JsonFrontendSerialisationModule(_frontendInteractionModule); } [Test] public void StreamingTest() { + bool isTestNotFinished = true; + Task.Run(() => + { + TcpListener listener = new TcpListener(IPAddress.Loopback, 4567); + listener.Start(); + + var stream = listener.AcceptTcpClient().GetStream(); + + Console.WriteLine("Client connected"); + + _interactionModule.RegisterClient(stream); + + while (isTestNotFinished) ; + }); + var tcpClient = new TcpClient(); + tcpClient.Connect(IPAddress.Loopback, 4567); + _frontendInteractionModule.ServerStream = tcpClient.GetStream(); + var req = new JsonCallRequest { CommandId = 1, ObjectId = -1 }; + _frontendSerialisationModule.PostCallRequest(req); + var res =_frontendSerialisationModule.GetNextCommandExecution(req.CallRequestId).Result; + isTestNotFinished = false; } } \ No newline at end of file diff --git a/mROA/Abstract/ICommandExecution.cs b/mROA/Abstract/ICommandExecution.cs index bee8e42..3bf1fd0 100644 --- a/mROA/Abstract/ICommandExecution.cs +++ b/mROA/Abstract/ICommandExecution.cs @@ -3,6 +3,6 @@ namespace mROA; public interface ICommandExecution { Guid CallRequestId { get; init; } - int ClientId { get; } + int ClientId { get; set; } int CommandId { get; } } \ No newline at end of file diff --git a/mROA/Abstract/ISerialisationModule.cs b/mROA/Abstract/ISerialisationModule.cs index 5a050b7..56fbc77 100644 --- a/mROA/Abstract/ISerialisationModule.cs +++ b/mROA/Abstract/ISerialisationModule.cs @@ -9,7 +9,7 @@ public interface ISerialisationModule void SetExecuteModule(IExecuteModule executeModule); public interface IFrontendSerialisationModule { - ICommandExecution GetNextCommandExecution(Guid requestId) where T : ICommandExecution; + T GetNextCommandExecution(Guid requestId) where T : ICommandExecution; void PostCallRequest(ICallRequest callRequest); } } \ No newline at end of file diff --git a/mROA/Implementation/CallRequest.cs b/mROA/Implementation/CallRequest.cs index c9b6907..a772b60 100644 --- a/mROA/Implementation/CallRequest.cs +++ b/mROA/Implementation/CallRequest.cs @@ -2,18 +2,16 @@ public interface ICallRequest { - Guid CallRequestId { get; } + Guid CallRequestId { get; internal set; } int CommandId { get; } - int ClientId { get; } int ObjectId { get; } object Parameter { get; } } public class JsonCallRequest : ICallRequest { - public Guid CallRequestId { get; } = Guid.NewGuid(); + public Guid CallRequestId { get; set; } = Guid.NewGuid(); public int CommandId { get; set; } - public int ClientId { get; set; } public int ObjectId { get; set; } = -1; public object? Parameter { get; set; } } diff --git a/mROA/Implementation/JsonFrontendSerialisationModule.cs b/mROA/Implementation/JsonFrontendSerialisationModule.cs index 080d423..7b835eb 100644 --- a/mROA/Implementation/JsonFrontendSerialisationModule.cs +++ b/mROA/Implementation/JsonFrontendSerialisationModule.cs @@ -6,7 +6,7 @@ namespace mROA.Implementation; public class JsonFrontendSerialisationModule(IInteractionModule.IFrontendInteractionModule interactionModule) : ISerialisationModule.IFrontendSerialisationModule { - public ICommandExecution GetNextCommandExecution(Guid requestId) where T : ICommandExecution + public T GetNextCommandExecution(Guid requestId) where T : ICommandExecution { var receiveMessage = interactionModule.ReceiveMessage(); var parsed = JsonSerializer.Deserialize(receiveMessage); diff --git a/mROA/Implementation/JsonSerialisationModule.cs b/mROA/Implementation/JsonSerialisationModule.cs index eacaba1..b547f62 100644 --- a/mROA/Implementation/JsonSerialisationModule.cs +++ b/mROA/Implementation/JsonSerialisationModule.cs @@ -21,7 +21,6 @@ public class JsonSerialisationModule : ISerialisationModule public void HandleIncomingRequest(int clientId, byte[] command) { JsonCallRequest request = JsonSerializer.Deserialize(command); - request.ClientId = clientId; if (request.Parameter is not null) { @@ -30,6 +29,7 @@ public class JsonSerialisationModule : ISerialisationModule } var response = _executeModule.Execute(request); + response.ClientId = clientId; PostResponse(response); } diff --git a/mROA/Implementation/LaunchReadyExecutionModule.cs b/mROA/Implementation/LaunchReadyExecutionModule.cs index 5ec3b29..128b2d2 100644 --- a/mROA/Implementation/LaunchReadyExecutionModule.cs +++ b/mROA/Implementation/LaunchReadyExecutionModule.cs @@ -45,7 +45,7 @@ public class LaunchReadyExecutionModule : IExecuteModule var finalResult = currentCommand.Invoke(context, parameter is null ? [] : [parameter]); return new FinalCommandExecution { - CommandId = command.CommandId, Result = finalResult, ClientId = command.ClientId, + CommandId = command.CommandId, Result = finalResult, CallRequestId = command.CallRequestId }; } @@ -58,7 +58,7 @@ public class LaunchReadyExecutionModule : IExecuteModule var result = (Task)currentCommand.Invoke(context, parameter is null ? [token] : [parameter, token])!; var exec = new AsyncCommandExecution(tokenSource) - { CommandId = command.CommandId, ClientId = command.ClientId, CallRequestId = command.CallRequestId }; + { CommandId = command.CommandId, CallRequestId = command.CallRequestId }; result.ContinueWith(_ => { PostFinalizedCallback(exec, null); }, token); @@ -74,7 +74,7 @@ public class LaunchReadyExecutionModule : IExecuteModule var result = (Task)currentCommand.Invoke(context, parameter is null ? [token] : [parameter, token])!; var exec = new AsyncCommandExecution(tokenSource) - { CommandId = command.CommandId, ClientId = command.ClientId, CallRequestId = command.CallRequestId }; + { CommandId = command.CommandId, CallRequestId = command.CallRequestId }; result.ContinueWith(task => { diff --git a/mROA/Implementation/StreamBasedFrontendInteractionModule.cs b/mROA/Implementation/StreamBasedFrontendInteractionModule.cs index e9c0b09..6e9dd91 100644 --- a/mROA/Implementation/StreamBasedFrontendInteractionModule.cs +++ b/mROA/Implementation/StreamBasedFrontendInteractionModule.cs @@ -2,30 +2,25 @@ namespace mROA.Implementation; public class StreamBasedFrontendInteractionModule : IInteractionModule.IFrontendInteractionModule { - private Stream _serverStream; - - public StreamBasedFrontendInteractionModule(Stream serverStream) - { - _serverStream = serverStream; - } + public Stream ServerStream { get; set; } public byte[] ReceiveMessage() { const int bufferSize = ushort.MaxValue; - byte[] buffer = new byte[bufferSize]; - if (!_serverStream.CanRead) throw new IOException("Server is not connected."); - - _serverStream.ReadExactly(buffer, 0, 2); - var len = BitConverter.ToUInt16(buffer, 0); - _serverStream.ReadExactly(buffer, 0, len); + byte[] buffer = new byte[bufferSize]; + if (!ServerStream.CanRead) throw new IOException("Server is not connected."); - return buffer[..len]; + ServerStream.ReadExactly(buffer, 0, 2); + var len = BitConverter.ToUInt16(buffer, 0); + ServerStream.ReadExactly(buffer, 0, len); + + return buffer[..len]; } public void PostMessage(byte[] message) { - _serverStream.Write(BitConverter.GetBytes((ushort)message.Length), 0, sizeof(ushort)); - _serverStream.Write(message, 0, message.Length); + ServerStream.Write(BitConverter.GetBytes((ushort)message.Length), 0, sizeof(ushort)); + ServerStream.Write(message, 0, message.Length); } } \ No newline at end of file diff --git a/mROA/Implementation/Test/ConsoleFrontendInteractionModule.cs b/mROA/Implementation/Test/ConsoleFrontendInteractionModule.cs deleted file mode 100644 index 40323c3..0000000 --- a/mROA/Implementation/Test/ConsoleFrontendInteractionModule.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System.Text; - -namespace mROA.Implementation; - -public class ConsoleFrontendInteractionModule : IInteractionModule.IFrontendInteractionModule -{ - public byte[] ReceiveMessage() - { - return []; - } - - public void PostMessage(byte[] message) - { - Console.WriteLine(Encoding.UTF8.GetString(message)); - } -} \ No newline at end of file diff --git a/mROA/Implementation/Test/MockExecModule.cs b/mROA/Implementation/Test/MockExecModule.cs index 928fb54..36e9f02 100644 --- a/mROA/Implementation/Test/MockExecModule.cs +++ b/mROA/Implementation/Test/MockExecModule.cs @@ -5,6 +5,6 @@ public class MockExecModule : IExecuteModule public ICommandExecution Execute(ICallRequest command) { return new FinalCommandExecution - { ClientId = command.ClientId, CommandId = command.CommandId, Result = new { A = "wqer", B = 5 }, CallRequestId = command.CallRequestId }; + { CommandId = command.CommandId, Result = new { A = "wqer", B = 5 }, CallRequestId = command.CallRequestId }; } } \ No newline at end of file diff --git a/mROA/Implementation/Test/MockMethodRepository.cs b/mROA/Implementation/Test/MockMethodRepository.cs new file mode 100644 index 0000000..0f30dd5 --- /dev/null +++ b/mROA/Implementation/Test/MockMethodRepository.cs @@ -0,0 +1,25 @@ +using System.Reflection; + +namespace mROA.Implementation.Test; + +public class MockMethodRepository : IMethodRepository +{ + public MethodInfo GetMethod(int id) + { + return GetType().GetMethod("MockMethod")!; + } + + public int RegisterMethod(MethodInfo method) + { + return 0; + } + + public IEnumerable GetMethods() + { + return [GetType().GetMethod("MockMethod")!]; + } + + private void MockMethod(object x) + { + } +} \ No newline at end of file