From ec5d594e4a959a1c20be599458c10c112d2d6d4b Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Tue, 18 Feb 2025 17:45:01 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=20=D0=BC=D1=83=D0=BB=D1=8C=D1=82=D0=B8=D0=BA=D0=BB=D0=B8?= =?UTF-8?q?=D0=B5=D0=BD=D1=82=D1=81=D0=BA=D0=B8=D0=B9=20=D1=80=D0=B5=D0=BF?= =?UTF-8?q?=D0=BE=D0=B7=D0=B8=D1=82=D0=BE=D1=80=D0=B8=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Example.Backend/Program.cs | 10 +++- Example.Frontend/Program.cs | 2 +- mROA/Abstract/IContextRepositoryHub.cs | 6 ++ .../Backend/MultiClientContextRepository.cs | 56 +++++++++++++++++++ mROA/Implementation/SharedObject.cs | 25 ++++++++- 5 files changed, 93 insertions(+), 6 deletions(-) create mode 100644 mROA/Abstract/IContextRepositoryHub.cs create mode 100644 mROA/Implementation/Backend/MultiClientContextRepository.cs diff --git a/Example.Backend/Program.cs b/Example.Backend/Program.cs index ff62dcf..7342e57 100644 --- a/Example.Backend/Program.cs +++ b/Example.Backend/Program.cs @@ -19,14 +19,20 @@ builder.Modules.Add(new HubRequestExtractor()); builder.UseBasicExecution(); builder.Modules.Add(new RemoteContextRepository()); -builder.UseCollectableContextRepository(typeof(PrinterFactory).Assembly); +// builder.UseCollectableContextRepository(typeof(PrinterFactory).Assembly); +builder.Modules.Add(new MultiClientContextRepository(i => +{ + var repo = new ContextRepository(); + repo.FillSingletons(typeof(PrinterFactory).Assembly); + return repo; +})); builder.SetupMethodsRepository(new CoCodegenMethodRepository()); builder.Modules.Add(new CreativeSerializationModuleProducer([builder.GetModule()!], typeof(RepresentationModule))); builder.Build(); new RemoteTypeBinder(); -TransmissionConfig.RealContextRepository = builder.GetModule(); +TransmissionConfig.RealContextRepository = builder.GetModule(); TransmissionConfig.RemoteEndpointContextRepository = builder.GetModule(); TransmissionConfig.OwnershipRepository = new MultiClientOwnershipRepository(); diff --git a/Example.Frontend/Program.cs b/Example.Frontend/Program.cs index 5f47471..a0996ba 100644 --- a/Example.Frontend/Program.cs +++ b/Example.Frontend/Program.cs @@ -27,7 +27,7 @@ TransmissionConfig.RemoteEndpointContextRepository = builder.GetModule()!.Connect(); _ = builder.GetModule()!.StartExtraction(); -Console.WriteLine(TransmissionConfig.OwnershipRepository!.GetOwnershipId()); +Console.WriteLine(TransmissionConfig.OwnershipRepository.GetOwnershipId()); var context = builder.GetModule(); var factory = context.GetSingleObject(typeof(IPrinterFactory)) as IPrinterFactory; diff --git a/mROA/Abstract/IContextRepositoryHub.cs b/mROA/Abstract/IContextRepositoryHub.cs new file mode 100644 index 0000000..8a34bf2 --- /dev/null +++ b/mROA/Abstract/IContextRepositoryHub.cs @@ -0,0 +1,6 @@ +namespace mROA.Abstract; + +public interface IContextRepositoryHub +{ + IContextRepository GetRepository(int clientId); +} \ No newline at end of file diff --git a/mROA/Implementation/Backend/MultiClientContextRepository.cs b/mROA/Implementation/Backend/MultiClientContextRepository.cs new file mode 100644 index 0000000..b00dbb6 --- /dev/null +++ b/mROA/Implementation/Backend/MultiClientContextRepository.cs @@ -0,0 +1,56 @@ +using mROA.Abstract; + +namespace mROA.Implementation.Backend; + +public class MultiClientContextRepository(Func produceRepository) : IContextRepository, IContextRepositoryHub +{ + private Dictionary _repositories = new(); + + private IContextRepository GetRepositoryByClientId(int clientId) + { + if (_repositories.TryGetValue(clientId, out var repository)) + return repository; + + var created = produceRepository(clientId); + _repositories.Add(clientId, created); + return created; + } + public void Inject(T dependency) + { + } + + public int ResisterObject(object o) + { + return GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()).ResisterObject(o); + } + + public void ClearObject(int id) + { + GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()).ClearObject(id); + } + + public object GetObject(int id) + { + return GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()).GetObject(id); + } + + public T? GetObject(int id) + { + return GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()).GetObject(id); + } + + public object GetSingleObject(Type type) + { + return GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()).GetSingleObject(type); + } + + public int GetObjectIndex(object o) + { + return GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()).GetObjectIndex(o); + } + + public IContextRepository GetRepository(int clientId) + { + return _repositories[clientId]; + } +} \ No newline at end of file diff --git a/mROA/Implementation/SharedObject.cs b/mROA/Implementation/SharedObject.cs index e39d614..a122257 100644 --- a/mROA/Implementation/SharedObject.cs +++ b/mROA/Implementation/SharedObject.cs @@ -5,9 +5,28 @@ namespace mROA.Implementation; public static class TransmissionConfig { - public static IContextRepository? RealContextRepository { get; set; } - public static IContextRepository? RemoteEndpointContextRepository { get; set; } - public static IOwnershipRepository? OwnershipRepository { get; set; } + private static IContextRepository? _realContextRepository; + private static IContextRepository? _remoteEndpointContextRepository; + private static IOwnershipRepository? _ownershipRepository; + + public static IContextRepository RealContextRepository + { + get => _realContextRepository ?? throw new NullReferenceException("RealContextRepository is null"); + set => _realContextRepository = value; + } + + public static IContextRepository RemoteEndpointContextRepository + { + get => _remoteEndpointContextRepository ?? throw new NullReferenceException("RemoteEndpointContextRepository is null"); + set => _remoteEndpointContextRepository = value; + } + + public static IOwnershipRepository OwnershipRepository + { + get => _ownershipRepository ?? throw new NullReferenceException("OwnershipRepository is null"); + set => _ownershipRepository = value; + } + } public class SharedObject where T : notnull