From b4d4837f9dcc82135d8a6d7ab4c7b3faaf516216 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Fri, 14 Feb 2025 20:24:04 +0300 Subject: [PATCH] =?UTF-8?q?=D1=80=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=20=D0=BF=D1=80=D0=BE=D0=B8=D0=B7=D0=B2=D0=BE?= =?UTF-8?q?=D0=B4=D0=B8=D1=82=D0=B5=D0=BB=D1=8C=20=D0=BC=D0=BE=D0=B4=D1=83?= =?UTF-8?q?=D0=BB=D0=B5=D0=B9=20=D1=81=D0=B5=D1=80=D0=B8=D0=B0=D0=BB=D0=B8?= =?UTF-8?q?=D0=B7=D0=B0=D1=86=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Example.Backend/Program.cs | 3 +- mROA/Abstract/IInteractionModule.cs | 1 + .../Backend/StreamBasedInteractionModule.cs | 5 ++ .../CreativeSerializationModuleProducer.cs | 46 +++++++++++++++++++ .../JsonFrontendSerialisationModule.cs | 5 +- 5 files changed, 56 insertions(+), 4 deletions(-) create mode 100644 mROA/Implementation/CreativeSerializationModuleProducer.cs diff --git a/Example.Backend/Program.cs b/Example.Backend/Program.cs index 0d019ca..fc73cc0 100644 --- a/Example.Backend/Program.cs +++ b/Example.Backend/Program.cs @@ -5,6 +5,7 @@ using mROA.Codegen; using mROA.Implementation; using mROA.Implementation.Backend; using mROA.Implementation.Bootstrap; +using mROA.Implementation.Frontend; var builder = new FullMixBuilder(); @@ -14,7 +15,7 @@ builder.UseStreamInteraction(); builder.UseBasicExecution(); builder.UseCollectableContextRepository(typeof(PrinterFactory).Assembly); builder.SetupMethodsRepository(new CoCodegenMethodRepository()); -builder.Modules.Add(new StaticSerialisationModuleProducer()); +builder.Modules.Add(new CreativeSerializationModuleProducer([], typeof(JsonFrontendSerialisationModule))); builder.Build(); diff --git a/mROA/Abstract/IInteractionModule.cs b/mROA/Abstract/IInteractionModule.cs index 127ab65..b0400a6 100644 --- a/mROA/Abstract/IInteractionModule.cs +++ b/mROA/Abstract/IInteractionModule.cs @@ -4,6 +4,7 @@ public interface IInteractionModule : IInjectableModule { void SendTo(int clientId, byte[] message); void RegisterSource(Stream stream); + Stream GetSource(int clientId); public interface IFrontendInteractionModule : IInjectableModule { int ClientId { get; } diff --git a/mROA/Implementation/Backend/StreamBasedInteractionModule.cs b/mROA/Implementation/Backend/StreamBasedInteractionModule.cs index 04a2e7f..31479a3 100644 --- a/mROA/Implementation/Backend/StreamBasedInteractionModule.cs +++ b/mROA/Implementation/Backend/StreamBasedInteractionModule.cs @@ -16,6 +16,11 @@ public class StreamBasedInteractionModule : IInteractionModule _serialisationModule.SendWelcomeMessage(id); } + public Stream GetSource(int clientId) + { + return _streams.GetValueOrDefault(clientId, Stream.Null); + } + public void SendTo(int clientId, byte[] message) { if (!_streams.TryGetValue(clientId, out var stream)) diff --git a/mROA/Implementation/CreativeSerializationModuleProducer.cs b/mROA/Implementation/CreativeSerializationModuleProducer.cs new file mode 100644 index 0000000..c7e7a81 --- /dev/null +++ b/mROA/Implementation/CreativeSerializationModuleProducer.cs @@ -0,0 +1,46 @@ +using mROA.Abstract; +using mROA.Implementation.Backend; +using mROA.Implementation.Frontend; + +namespace mROA.Implementation; + +public class CreativeSerializationModuleProducer : ISerialisationModuleProducer +{ + private Type _serializationModuleType; + private IInjectableModule[] _creationModules; + private StreamBasedInteractionModule? _interactionModule; + + public CreativeSerializationModuleProducer(IInjectableModule[] creationModules, Type serializationModuleType) + { + _creationModules = creationModules; + _serializationModuleType = serializationModuleType; + } + + + public void Inject(T dependency) + { + if (dependency is StreamBasedInteractionModule interactionModule) + _interactionModule = interactionModule; + } + + public ISerialisationModule.IFrontendSerialisationModule Produce(int ownership) + { + if (_interactionModule == null) + throw new NullReferenceException("Interaction module is null"); + + var produced = + Activator.CreateInstance(_serializationModuleType) as ISerialisationModule.IFrontendSerialisationModule ?? + throw new Exception("Bad serialization module type"); + + foreach (var creationModule in _creationModules) + produced.Inject(creationModule); + + var interactionModule = new StreamBasedFrontendInteractionModule + { + ClientId = ownership, + ServerStream = _interactionModule.GetSource(ownership) + }; + produced.Inject(interactionModule); + return produced; + } +} \ No newline at end of file diff --git a/mROA/Implementation/Frontend/JsonFrontendSerialisationModule.cs b/mROA/Implementation/Frontend/JsonFrontendSerialisationModule.cs index efc057c..dca2e19 100644 --- a/mROA/Implementation/Frontend/JsonFrontendSerialisationModule.cs +++ b/mROA/Implementation/Frontend/JsonFrontendSerialisationModule.cs @@ -1,4 +1,3 @@ -using System.Text; using System.Text.Json; using mROA.Abstract; @@ -8,7 +7,6 @@ public class JsonFrontendSerialisationModule : ISerialisationModule.IFrontendSerialisationModule { private IInteractionModule.IFrontendInteractionModule? _interactionModule; - public int ClientId => _interactionModule!.ClientId; public async Task GetNextCommandExecution(Guid requestId) where T : ICommandExecution @@ -62,8 +60,9 @@ public class JsonFrontendSerialisationModule Data = post, SchemaId = MessageType.CallRequest })); + + } - public void Inject(T dependency) { if (dependency is IInteractionModule.IFrontendInteractionModule interactionModule)