From cc5f82c7e791a095d2cf33bc1c33146cd1cec545 Mon Sep 17 00:00:00 2001 From: Mihail Mitrovanov Date: Mon, 5 May 2025 14:37:17 +0300 Subject: [PATCH] Prefinal local endpoint context override --- Example.Backend/Program.cs | 5 ++-- mROA/Abstract/IContextRepository.cs | 2 +- .../Backend/BasicExecutionModule.cs | 10 ++++---- .../Backend/ContextRepository.cs | 2 +- .../Backend/HubRequestExtractor.cs | 24 +++++++++---------- .../Backend/MultiClientContextRepository.cs | 4 ++-- .../ComplexContextRepository.cs | 2 +- .../Implementation/RemoteContextRepository.cs | 4 ++-- mROA/Implementation/SharedObjectShell.cs | 2 +- 9 files changed, 27 insertions(+), 28 deletions(-) diff --git a/Example.Backend/Program.cs b/Example.Backend/Program.cs index 4f5a7ab..408f880 100644 --- a/Example.Backend/Program.cs +++ b/Example.Backend/Program.cs @@ -7,7 +7,6 @@ using mROA.Codegen; using mROA.Implementation; using mROA.Implementation.Backend; using mROA.Implementation.Bootstrap; -using mROA.Implementation.Frontend; class Program { @@ -24,11 +23,11 @@ class Program builder.GetModule()!); builder.Modules.Add(new UdpGateway(listening)); builder.Modules.Add(new ConnectionHub()); - builder.Modules.Add(new HubRequestExtractor(typeof(RequestExtractor))); + builder.Modules.Add(new HubRequestExtractor()); builder.UseBasicExecution(); builder.Modules.Add(new CreativeRepresentationModuleProducer( - new IInjectableModule[] { builder.GetModule()! }, + new IInjectableModule[] { builder.GetModule()! }, typeof(RepresentationModule))); builder.Modules.Add(new RemoteContextRepository()); // builder.UseCollectableContextRepository(typeof(PrinterFactory).Assembly); diff --git a/mROA/Abstract/IContextRepository.cs b/mROA/Abstract/IContextRepository.cs index 920a317..c4d6c3e 100644 --- a/mROA/Abstract/IContextRepository.cs +++ b/mROA/Abstract/IContextRepository.cs @@ -8,7 +8,7 @@ namespace mROA.Abstract int HostId { get; set; } int ResisterObject(object o, IEndPointContext context); void ClearObject(ComplexObjectIdentifier id); - T GetObject(ComplexObjectIdentifier id); + T GetObject(ComplexObjectIdentifier id, IEndPointContext context); object GetSingleObject(Type type, int ownerId); int GetObjectIndex(object o, IEndPointContext context); } diff --git a/mROA/Implementation/Backend/BasicExecutionModule.cs b/mROA/Implementation/Backend/BasicExecutionModule.cs index 1360230..f161e20 100644 --- a/mROA/Implementation/Backend/BasicExecutionModule.cs +++ b/mROA/Implementation/Backend/BasicExecutionModule.cs @@ -49,7 +49,7 @@ namespace mROA.Implementation.Backend if (invoker == null) throw new Exception($"Command {command.CommandId} not found"); - var context = GetContext(command, contextRepository, invoker); + var context = GetContext(command, contextRepository, invoker, endPointContext); if (context == null) throw new NullReferenceException("Instance can't be null"); @@ -96,10 +96,10 @@ namespace mROA.Implementation.Backend } private static object GetContext(ICallRequest command, IContextRepository contextRepository, - IMethodInvoker invoker) + IMethodInvoker invoker, IEndPointContext endPointContext) { var context = command.ObjectId.ContextId != -1 - ? contextRepository.GetObject(command.ObjectId) + ? contextRepository.GetObject(command.ObjectId, endPointContext) : contextRepository.GetSingleObject(invoker.SuitableType, command.ObjectId.OwnerId); return context; } @@ -152,8 +152,8 @@ namespace mROA.Implementation.Backend { return new AsyncCommandExecution(); } - - if (invoker.IsVoid ) + + if (invoker.IsVoid) { return new FinalCommandExecution { diff --git a/mROA/Implementation/Backend/ContextRepository.cs b/mROA/Implementation/Backend/ContextRepository.cs index b4ecc98..5448101 100644 --- a/mROA/Implementation/Backend/ContextRepository.cs +++ b/mROA/Implementation/Backend/ContextRepository.cs @@ -44,7 +44,7 @@ namespace mROA.Implementation.Backend _storage.Free(id.ContextId); } - public T GetObject(ComplexObjectIdentifier id) + public T GetObject(ComplexObjectIdentifier id, IEndPointContext context) { var value = _storage.GetValue(id.ContextId); diff --git a/mROA/Implementation/Backend/HubRequestExtractor.cs b/mROA/Implementation/Backend/HubRequestExtractor.cs index 7d760fe..9c0a2d3 100644 --- a/mROA/Implementation/Backend/HubRequestExtractor.cs +++ b/mROA/Implementation/Backend/HubRequestExtractor.cs @@ -1,5 +1,5 @@ -using System; using mROA.Abstract; +using mROA.Implementation.Frontend; namespace mROA.Implementation.Backend { @@ -12,12 +12,6 @@ namespace mROA.Implementation.Backend private IMethodRepository? _methodRepository; private IContextualSerializationToolKit? _serializationToolkit; private IExecuteModule? _executeModule; - private readonly Type _extractorType; - - public HubRequestExtractor(Type extractorType) - { - _extractorType = extractorType; - } public void Inject(T dependency) { @@ -49,7 +43,7 @@ namespace mROA.Implementation.Backend private void HubOnOnConnected(IRepresentationModule interaction) { var extractor = CreateExtractor(interaction); - extractor.StartExtraction().ContinueWith(t => OnDisconnected(interaction)); + extractor.StartExtraction().ContinueWith(_ => OnDisconnected(interaction)); } private void OnDisconnected(IRepresentationModule representationModule) @@ -60,16 +54,22 @@ namespace mROA.Implementation.Backend private IRequestExtractor CreateExtractor(IRepresentationModule interaction) { - var extractor = (IRequestExtractor)Activator.CreateInstance(_extractorType)!; + var extractor = new RequestExtractor(); + var context = new EndPointContext + { + HostId = 0, OwnerId = interaction.Id + }; extractor.Inject(interaction); + if (_contextRepository is IContextRepositoryHub contextHub) - extractor.Inject(contextHub.GetRepository(interaction.Id)); + context.RealRepository = contextHub.GetRepository(interaction.Id); else - extractor.Inject(interaction); + context.RealRepository = _contextRepository!; + + context.RemoteRepository = _remoteContextRepository!; extractor.Inject(_methodRepository); extractor.Inject(_serializationToolkit); extractor.Inject(_executeModule); - extractor.Inject(_remoteContextRepository); return extractor; } } diff --git a/mROA/Implementation/Backend/MultiClientContextRepository.cs b/mROA/Implementation/Backend/MultiClientContextRepository.cs index a0b3893..370f105 100644 --- a/mROA/Implementation/Backend/MultiClientContextRepository.cs +++ b/mROA/Implementation/Backend/MultiClientContextRepository.cs @@ -32,10 +32,10 @@ namespace mROA.Implementation.Backend repository.ClearObject(id); } - public T GetObject(ComplexObjectIdentifier id) + public T GetObject(ComplexObjectIdentifier id, IEndPointContext context) { var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()); - return repository.GetObject(id); + return repository.GetObject(id, context); } public object GetSingleObject(Type type, int ownerId) diff --git a/mROA/Implementation/ComplexContextRepository.cs b/mROA/Implementation/ComplexContextRepository.cs index 8f44c78..bcd8285 100644 --- a/mROA/Implementation/ComplexContextRepository.cs +++ b/mROA/Implementation/ComplexContextRepository.cs @@ -52,7 +52,7 @@ namespace mROA.Implementation _storages.Find(i => i.Key == id.OwnerId).Value.Free(id.ContextId); } - public T GetObject(ComplexObjectIdentifier id) + public T GetObject(ComplexObjectIdentifier id, IEndPointContext context) { throw new NotImplementedException(); } diff --git a/mROA/Implementation/RemoteContextRepository.cs b/mROA/Implementation/RemoteContextRepository.cs index 43dceda..a6af325 100644 --- a/mROA/Implementation/RemoteContextRepository.cs +++ b/mROA/Implementation/RemoteContextRepository.cs @@ -23,7 +23,7 @@ namespace mROA.Implementation throw new NotSupportedException(); } - public T GetObject(ComplexObjectIdentifier id) + public T GetObject(ComplexObjectIdentifier id, IEndPointContext context) { var index = _producedRemoteEndpoints.Find(i => i.Identifier.Equals(id)); if (index is not null) @@ -35,7 +35,7 @@ namespace mROA.Implementation var representationModule = _representationProducer.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId()); var remote = (T)Activator.CreateInstance(remoteType, id.ContextId, - representationModule)!; + representationModule, context)!; _producedRemoteEndpoints.Add((remote as RemoteObjectBase)!); diff --git a/mROA/Implementation/SharedObjectShell.cs b/mROA/Implementation/SharedObjectShell.cs index 46a632a..d595a23 100644 --- a/mROA/Implementation/SharedObjectShell.cs +++ b/mROA/Implementation/SharedObjectShell.cs @@ -77,7 +77,7 @@ namespace mROA.Implementation set { _identifier = value; - Value = GetDefaultContextRepository().GetObject(Identifier); + Value = GetDefaultContextRepository().GetObject(Identifier, EndPointContext); } }