diff --git a/Example.Frontend/Program.cs b/Example.Frontend/Program.cs index fcc3ca0..9e0559f 100644 --- a/Example.Frontend/Program.cs +++ b/Example.Frontend/Program.cs @@ -14,14 +14,21 @@ builder.Modules.Add(new JsonFrontendSerialisationModule()); builder.Modules.Add(new StreamBasedFrontendInteractionModule()); builder.Modules.Add(new NetworkFrontendBridge(new IPEndPoint(IPAddress.Loopback, 4567))); builder.Modules.Add(new StaticSerialisationModuleProducer()); +builder.Modules.Add(new BasicExecutionModule()); +builder.Modules.Add(new CoCodegenMethodRepository()); +builder.Modules.Add(new StreamBasedVirtualBackendInteractionModule()); builder.UseCollectableContextRepository(); builder.Build(); + + TransmissionConfig.RealContextRepository = builder.GetModule(); TransmissionConfig.RemoteEndpointContextRepository = builder.GetModule(); -builder.GetModule().Connect(); -Console.WriteLine(TransmissionConfig.OwnershipRepository.GetOwnershipId()); +builder.GetModule()!.Connect(); +builder.GetModule()!.StartVirtualInteraction(); + +Console.WriteLine(TransmissionConfig.OwnershipRepository!.GetOwnershipId()); var context = builder.GetModule(); var factory = context.GetSingleObject(typeof(IPrinterFactory)) as IPrinterFactory; diff --git a/mROA/Implementation/Backend/JsonSerialisationModule.cs b/mROA/Implementation/Backend/JsonSerialisationModule.cs index ea873c2..56395f5 100644 --- a/mROA/Implementation/Backend/JsonSerialisationModule.cs +++ b/mROA/Implementation/Backend/JsonSerialisationModule.cs @@ -13,8 +13,13 @@ public class JsonSerialisationModule : ISerialisationModule public void HandleIncomingRequest(int clientId, byte[] message) { - var ownership = TransmissionConfig.OwnershipRepository as MultiClientOwnershipRepository ?? throw new Exception("Set ownership repository type is incorrect"); - ownership.RegisterOwnership(clientId); + MultiClientOwnershipRepository? ownership = null; + if (TransmissionConfig.OwnershipRepository is MultiClientOwnershipRepository) + { + ownership = TransmissionConfig.OwnershipRepository as MultiClientOwnershipRepository; + ownership.RegisterOwnership(clientId); + } + NetworkMessage input = JsonSerializer.Deserialize(message)!; Console.WriteLine(Encoding.Default.GetString(input.Data)); if (input.SchemaId == MessageType.CallRequest) @@ -40,7 +45,8 @@ public class JsonSerialisationModule : ISerialisationModule Data = JsonSerializer.SerializeToUtf8Bytes(response, response.GetType()) }, clientId); } - ownership.FreeOwnership(); + + ownership?.FreeOwnership(); } public void PostResponse(NetworkMessage message, int clientId) diff --git a/mROA/Implementation/Backend/StreamBasedInteractionModule.cs b/mROA/Implementation/Backend/StreamBasedInteractionModule.cs index 31479a3..1d25548 100644 --- a/mROA/Implementation/Backend/StreamBasedInteractionModule.cs +++ b/mROA/Implementation/Backend/StreamBasedInteractionModule.cs @@ -4,9 +4,9 @@ namespace mROA.Implementation.Backend; public class StreamBasedInteractionModule : IInteractionModule { - private ISerialisationModule _serialisationModule; + internal ISerialisationModule _serialisationModule; private readonly Dictionary _streams = new(); - private Action? _handler; + internal Action? _handler; public void RegisterSource(Stream stream) { diff --git a/mROA/Implementation/Frontend/StreamBasedVirtualBackendInteractionModule.cs b/mROA/Implementation/Frontend/StreamBasedVirtualBackendInteractionModule.cs index 5c9cd96..c68dda0 100644 --- a/mROA/Implementation/Frontend/StreamBasedVirtualBackendInteractionModule.cs +++ b/mROA/Implementation/Frontend/StreamBasedVirtualBackendInteractionModule.cs @@ -1,25 +1,26 @@ -using mROA.Implementation.Backend; +using mROA.Abstract; +using mROA.Implementation.Backend; namespace mROA.Implementation.Frontend; -public class StreamBasedVirtualBackendInteractionModule : StreamBasedInteractionModule +public class StreamBasedVirtualBackendInteractionModule : StreamBasedInteractionModule, IInteractionModule { - public Stream ServerStream { get; set; } - + private Stream? _serverStream { get; set; } + private StreamBasedFrontendInteractionModule? _interactionModule; public void RegisterSource(Stream stream) { - ServerStream = stream; + } public Stream GetSource(int clientId) { - return ServerStream; + return _serverStream; } public void SendTo(int clientId, 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); } private async Task ListenTo((int id, Stream stream) client, Action action) @@ -40,5 +41,20 @@ public class StreamBasedVirtualBackendInteractionModule : StreamBasedInteraction { } } - + + public void Inject(T dependency) + { + base.Inject(dependency); + if (dependency is StreamBasedFrontendInteractionModule interactionModule) + { + _interactionModule = interactionModule; + } + } + + + public void StartVirtualInteraction() + { + _serverStream = _interactionModule.ServerStream; + _ = ListenTo((0, _serverStream), _handler!); + } } \ No newline at end of file