From 809cc05b0a9ec751d95f43183ce8c0a41b305473 Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Fri, 14 Feb 2025 22:26:55 +0300 Subject: [PATCH] =?UTF-8?q?=D0=BF=D1=80=D0=B5=D0=B4=D1=84=D0=B8=D0=BD?= =?UTF-8?q?=D0=B0=D0=BB=D1=8C=D0=BD=D0=BE=D0=B5=20=D0=B8=D0=B7=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=81=D0=B8=D0=B3=D0=BD=D0=B0?= =?UTF-8?q?=D1=82=D1=83=D1=80=D1=8B=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D1=84?= =?UTF-8?q?=D0=B5=D0=B9=D1=81=D0=B0=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8F?= =?UTF-8?q?=20=D0=B8=D1=81=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Example.Backend/PrinterFactory.cs | 5 ++ Example.Backend/Program.cs | 4 +- Example.Frontend/ClientBasedPrinter.cs | 1 + Example.Frontend/Program.cs | 74 +++++++++---------- Example.Shared/IPrinterFactory.cs | 2 + mROA/Abstract/IExecuteModule.cs | 2 +- .../Backend/BasicExecutionModule.cs | 13 +--- .../Backend/JsonSerialisationModule.cs | 27 +++++-- mROA/Implementation/SharedObject.cs | 2 +- 9 files changed, 69 insertions(+), 61 deletions(-) diff --git a/Example.Backend/PrinterFactory.cs b/Example.Backend/PrinterFactory.cs index be94843..c04470d 100644 --- a/Example.Backend/PrinterFactory.cs +++ b/Example.Backend/PrinterFactory.cs @@ -24,4 +24,9 @@ public class PrinterFactory : IPrinterFactory { return new SharedObject(_printers.Find(i => i.GetName() == printerName)!); } + + public string[] CollectAllNames() + { + return _printers.Select(i => i.GetName()).ToArray(); + } } \ No newline at end of file diff --git a/Example.Backend/Program.cs b/Example.Backend/Program.cs index fc73cc0..c6c2b4b 100644 --- a/Example.Backend/Program.cs +++ b/Example.Backend/Program.cs @@ -13,12 +13,14 @@ builder.UseJsonSerialisation(); builder.UseNetworkGateway(new IPEndPoint(IPAddress.Loopback, 4567)); builder.UseStreamInteraction(); builder.UseBasicExecution(); +builder.Modules.Add(new RemoteContextRepository()); builder.UseCollectableContextRepository(typeof(PrinterFactory).Assembly); builder.SetupMethodsRepository(new CoCodegenMethodRepository()); builder.Modules.Add(new CreativeSerializationModuleProducer([], typeof(JsonFrontendSerialisationModule))); builder.Build(); - +new RemoteTypeBinder(); +TransmissionConfig.RemoteEndpointContextRepository = builder.GetModule(); var gateway = builder.GetModule() ; diff --git a/Example.Frontend/ClientBasedPrinter.cs b/Example.Frontend/ClientBasedPrinter.cs index 18daf1a..f49a243 100644 --- a/Example.Frontend/ClientBasedPrinter.cs +++ b/Example.Frontend/ClientBasedPrinter.cs @@ -7,6 +7,7 @@ public class ClientBasedPrinter : IPrinter { public string GetName() { + Console.WriteLine("ClientBasedPrinter called from server!!!!!!!!!!! Vova likes that:)"); return "ClientBasedPrinter"; } diff --git a/Example.Frontend/Program.cs b/Example.Frontend/Program.cs index 74ce553..69e91b8 100644 --- a/Example.Frontend/Program.cs +++ b/Example.Frontend/Program.cs @@ -1,9 +1,4 @@ -// See https://aka.ms/new-console-template for more information - - -using System.Diagnostics; -using System.Net; -using System.Text; +using System.Net; using Example.Frontend; using Example.Shared; using mROA.Codegen; @@ -12,47 +7,48 @@ using mROA.Implementation.Backend; using mROA.Implementation.Bootstrap; using mROA.Implementation.Frontend; - -var mixer = new FullMixBuilder(); +var builder = new FullMixBuilder(); new RemoteTypeBinder(); -mixer.Modules.Add(new RemoteContextRepository()); -mixer.Modules.Add(new JsonFrontendSerialisationModule()); -mixer.Modules.Add(new StreamBasedFrontendInteractionModule()); -mixer.Modules.Add(new NetworkFrontendBridge(new IPEndPoint(IPAddress.Loopback, 4567))); -mixer.Modules.Add(new StaticSerialisationModuleProducer()); -mixer.UseCollectableContextRepository(); -mixer.Build(); +builder.Modules.Add(new RemoteContextRepository()); +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.UseCollectableContextRepository(); +builder.Build(); -TransmissionConfig.RealContextRepository = mixer.GetModule(); -TransmissionConfig.RemoteEndpointContextRepository = mixer.GetModule(); -mixer.GetModule().Connect(); +TransmissionConfig.RealContextRepository = builder.GetModule(); +TransmissionConfig.RemoteEndpointContextRepository = builder.GetModule(); +builder.GetModule().Connect(); Console.WriteLine(TransmissionConfig.OwnershipRepository.GetOwnershipId()); -var context = mixer.GetModule(); +var context = builder.GetModule(); var factory = context.GetSingleObject(typeof(IPrinterFactory)) as IPrinterFactory; var printer = factory.Create("Test"); -var name = printer.Value.GetName(); -Console.WriteLine("Printer name : {0}", name); +// var name = printer.Value.GetName(); +// Console.WriteLine("Printer name : {0}", name); -// factory.Register(new SharedObject(new ClientBasedPrinter())); +factory.Register(new SharedObject(new ClientBasedPrinter())); -var page = await printer.Value.Print("Test Page", new CancellationToken()); -var data = page.Value.GetData(); -Console.WriteLine("Data : {0}", Encoding.UTF8.GetString(data)); - -var loadSingleton = context.GetSingleObject(typeof(ILoadTest)) as ILoadTest; - -const int iterations = 10000; -var timer = Stopwatch.StartNew(); -var x = 0; -for (int i = 0; i < iterations; i++) -{ - x = loadSingleton.Next(x); -} - -timer.Stop(); -Console.WriteLine("X is {0}", x); -Console.WriteLine("Time : {0}", timer.Elapsed.TotalMilliseconds); \ No newline at end of file +// var names = factory.CollectAllNames(); +Console.ReadLine(); +// var page = await printer.Value.Print("Test Page", new CancellationToken()); +// var data = page.Value.GetData(); +// Console.WriteLine("Data : {0}", Encoding.UTF8.GetString(data)); +// +// var loadSingleton = context.GetSingleObject(typeof(ILoadTest)) as ILoadTest; +// +// const int iterations = 10000; +// var timer = Stopwatch.StartNew(); +// var x = 0; +// for (int i = 0; i < iterations; i++) +// { +// x = loadSingleton.Next(x); +// } +// +// timer.Stop(); +// Console.WriteLine("X is {0}", x); +// Console.WriteLine("Time : {0}", timer.Elapsed.TotalMilliseconds); \ No newline at end of file diff --git a/Example.Shared/IPrinterFactory.cs b/Example.Shared/IPrinterFactory.cs index c937ed9..e85fc69 100644 --- a/Example.Shared/IPrinterFactory.cs +++ b/Example.Shared/IPrinterFactory.cs @@ -10,5 +10,7 @@ public interface IPrinterFactory SharedObject Create(string printerName); void Register(SharedObject printer); SharedObject GetPrinterByName(string printerName); + string[] CollectAllNames(); + } diff --git a/mROA/Abstract/IExecuteModule.cs b/mROA/Abstract/IExecuteModule.cs index 36e376d..cc5a843 100644 --- a/mROA/Abstract/IExecuteModule.cs +++ b/mROA/Abstract/IExecuteModule.cs @@ -4,5 +4,5 @@ namespace mROA.Abstract; public interface IExecuteModule : IInjectableModule { - ICommandExecution Execute(ICallRequest command); + ICommandExecution Execute(ICallRequest command, IContextRepository executionContextRepository); } \ No newline at end of file diff --git a/mROA/Implementation/Backend/BasicExecutionModule.cs b/mROA/Implementation/Backend/BasicExecutionModule.cs index cf05375..f8b2fff 100644 --- a/mROA/Implementation/Backend/BasicExecutionModule.cs +++ b/mROA/Implementation/Backend/BasicExecutionModule.cs @@ -6,22 +6,13 @@ namespace mROA.Implementation.Backend; public class BasicExecutionModule : IExecuteModule { private IMethodRepository? _methodRepo; - private IContextRepository? _contextRepo; public void Inject(T dependency) { - switch (dependency) - { - case IMethodRepository methodRepo: - _methodRepo = methodRepo; - break; - case IContextRepository contextRepo: - _contextRepo = contextRepo; - break; - } + if (dependency is IMethodRepository methodRepo) _methodRepo = methodRepo; } - public ICommandExecution Execute(ICallRequest command) + public ICommandExecution Execute(ICallRequest command, IContextRepository _contextRepo) { if (_methodRepo is null) throw new NullReferenceException("Method repository was not defined"); diff --git a/mROA/Implementation/Backend/JsonSerialisationModule.cs b/mROA/Implementation/Backend/JsonSerialisationModule.cs index f945b95..ec9cd52 100644 --- a/mROA/Implementation/Backend/JsonSerialisationModule.cs +++ b/mROA/Implementation/Backend/JsonSerialisationModule.cs @@ -8,6 +8,7 @@ public class JsonSerialisationModule : ISerialisationModule private IInteractionModule? _dataSource; private IExecuteModule? _executeModule; private IMethodRepository? _methodRepository; + private IContextRepository? _contextRepo; public void HandleIncomingRequest(int clientId, byte[] message) { @@ -21,10 +22,11 @@ public class JsonSerialisationModule : ISerialisationModule if (command.Parameter is not null) { var parameter = _methodRepository!.GetMethod(command.CommandId).GetParameters().First().ParameterType; - command.Parameter = ((JsonElement)command.Parameter).Deserialize(parameter); + var jsElement = (JsonElement)command.Parameter; + command.Parameter = jsElement.Deserialize(parameter); } - var response = _executeModule!.Execute(command); + var response = _executeModule!.Execute(command, _contextRepo); response.ClientId = clientId; var resultType = response is FinalCommandExecution ? MessageType.FinishedCommandExecution @@ -52,11 +54,20 @@ public class JsonSerialisationModule : ISerialisationModule public void Inject(T dependency) { - if (dependency is IInteractionModule interactionModule) - _dataSource = interactionModule; - if (dependency is IExecuteModule executeModule) - _executeModule = executeModule; - if (dependency is IMethodRepository methodRepository) - _methodRepository = methodRepository; + switch (dependency) + { + case IInteractionModule interactionModule: + _dataSource = interactionModule; + break; + case IExecuteModule executeModule: + _executeModule = executeModule; + break; + case IMethodRepository methodRepository: + _methodRepository = methodRepository; + break; + case IContextRepository contextRepository: + _contextRepo = contextRepository; + break; + } } } \ No newline at end of file diff --git a/mROA/Implementation/SharedObject.cs b/mROA/Implementation/SharedObject.cs index 0a4d18b..0864440 100644 --- a/mROA/Implementation/SharedObject.cs +++ b/mROA/Implementation/SharedObject.cs @@ -13,7 +13,7 @@ public static class TransmissionConfig public class SharedObject where T : notnull { private IContextRepository GetDefaultContextRepository() => - (OwnerId == TransmissionConfig.OwnershipRepository!.GetOwnershipId() + (OwnerId == TransmissionConfig.OwnershipRepository!.GetHostOwnershipId() ? TransmissionConfig.RealContextRepository : TransmissionConfig.RemoteEndpointContextRepository) ?? throw new NullReferenceException(