From d6423136d21aee762bcc5499867f9803e0d5acbd Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Mon, 10 Mar 2025 23:28:41 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D0=B0=D0=BB=D0=B8=D0=B7=D0=BE?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=BE=20=D1=83=D0=BD=D0=B8=D0=B2=D0=B5=D1=80?= =?UTF-8?q?=D1=81=D0=B0=D0=BB=D1=8C=D0=BD=D0=BE=D0=B5=20=D1=85=D1=80=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mROA.Codegen/mROASourceGenerator.cs | 2 +- mROA/Abstract/IContextRepository.cs | 5 +- mROA/Abstract/IStorage.cs | 10 ++++ .../Backend/BasicExecutionModule.cs | 4 +- .../Backend/ContextRepository.cs | 14 +++-- .../Backend/MultiClientContextRepository.cs | 14 ++--- mROA/Implementation/ComplexRepository.cs | 44 +++++++++++++++ mROA/Implementation/ExtensibleStorage.cs | 55 +++++++++++++++++++ .../Implementation/RemoteContextRepository.cs | 6 +- 9 files changed, 132 insertions(+), 22 deletions(-) create mode 100644 mROA/Abstract/IStorage.cs create mode 100644 mROA/Implementation/ComplexRepository.cs create mode 100644 mROA/Implementation/ExtensibleStorage.cs diff --git a/mROA.Codegen/mROASourceGenerator.cs b/mROA.Codegen/mROASourceGenerator.cs index 1fd6df4..ed5abd2 100644 --- a/mROA.Codegen/mROASourceGenerator.cs +++ b/mROA.Codegen/mROASourceGenerator.cs @@ -481,7 +481,7 @@ namespace {classSymbol.ContainingNamespace.ToDisplayString()} {callFilter} var request = new DefaultCallRequest {{ - CommandId = {index}, ObjectId = new UniversalObjectIdentifier(index, context.OwnerId), Parameters = new object[] {{ {transferParameters} }} + CommandId = {index}, ObjectId = new ComplexObjectIdentifier(index, context.OwnerId), Parameters = new object[] {{ {transferParameters} }} }}; module.PostCallMessageAsync(request.Id, MessageType.EventRequest, request); }}; diff --git a/mROA/Abstract/IContextRepository.cs b/mROA/Abstract/IContextRepository.cs index 75668c9..9f064e1 100644 --- a/mROA/Abstract/IContextRepository.cs +++ b/mROA/Abstract/IContextRepository.cs @@ -5,10 +5,11 @@ namespace mROA.Abstract { public interface IContextRepository : IInjectableModule { + int HostId { get; set; } int ResisterObject(object o, IEndPointContext context); - void ClearObject(int id); + void ClearObject(ComplexObjectIdentifier id); T GetObjectByShell(SharedObjectShellShell sharedObjectShellShell); - T? GetObject(int id); + T? GetObject(ComplexObjectIdentifier id); object GetSingleObject(Type type); int GetObjectIndex(object o, IEndPointContext context); } diff --git a/mROA/Abstract/IStorage.cs b/mROA/Abstract/IStorage.cs new file mode 100644 index 0000000..1ba6de3 --- /dev/null +++ b/mROA/Abstract/IStorage.cs @@ -0,0 +1,10 @@ +namespace mROA.Abstract +{ + public interface IStorage + { + T GetValue(int index); + int GetIndex(T value); + int Place(T value); + void Free(int index); + } +} \ No newline at end of file diff --git a/mROA/Implementation/Backend/BasicExecutionModule.cs b/mROA/Implementation/Backend/BasicExecutionModule.cs index 28158f9..d5ec4ab 100644 --- a/mROA/Implementation/Backend/BasicExecutionModule.cs +++ b/mROA/Implementation/Backend/BasicExecutionModule.cs @@ -69,7 +69,7 @@ namespace mROA.Implementation.Backend IContextRepository repository; var context = command.ObjectId.ContextId != -1 - ? contextRepository.GetObject(command.ObjectId.ContextId) + ? contextRepository.GetObject(command.ObjectId) : contextRepository.GetSingleObject(invoker.SuitableType); if (context == null) @@ -104,7 +104,7 @@ namespace mROA.Implementation.Backend #if TRACE Console.WriteLine("Disposing object"); #endif - contextRepository.ClearObject(command.ObjectId.ContextId); + contextRepository.ClearObject(command.ObjectId); } return result; diff --git a/mROA/Implementation/Backend/ContextRepository.cs b/mROA/Implementation/Backend/ContextRepository.cs index d00fd4c..fe1a780 100644 --- a/mROA/Implementation/Backend/ContextRepository.cs +++ b/mROA/Implementation/Backend/ContextRepository.cs @@ -31,6 +31,8 @@ namespace mROA.Implementation.Backend _storage = new object[StartupSize]; } + public int HostId { get; set; } + public int ResisterObject(object o, IEndPointContext context) { if (!_lastIndexFinder.IsCompleted) @@ -47,10 +49,10 @@ namespace mROA.Implementation.Backend return last; } - public void ClearObject(int id) + public void ClearObject(ComplexObjectIdentifier id) { - _storage[id] = null; - _lastIndexFinder = Task.FromResult(id); + _storage[id.ContextId] = null; + _lastIndexFinder = Task.FromResult(id.ContextId); } public T GetObjectByShell(SharedObjectShellShell sharedObjectShellShell) @@ -58,11 +60,11 @@ namespace mROA.Implementation.Backend return (T)GetObject(sharedObjectShellShell.Identifier.ContextId); } - public T GetObject(int id) + public T? GetObject(ComplexObjectIdentifier id) { - return id == -1 || _storage.Length <= id + return id.ContextId == -1 || _storage.Length <= id.ContextId ? throw new NullReferenceException("Cannot find that object. It is null") - : (T)_storage[id]!; + : (T)_storage[id.ContextId]!; } public object GetSingleObject(Type type) diff --git a/mROA/Implementation/Backend/MultiClientContextRepository.cs b/mROA/Implementation/Backend/MultiClientContextRepository.cs index 5a5ac0e..1647dbc 100644 --- a/mROA/Implementation/Backend/MultiClientContextRepository.cs +++ b/mROA/Implementation/Backend/MultiClientContextRepository.cs @@ -18,13 +18,15 @@ namespace mROA.Implementation.Backend { } + public int HostId { get; set; } + public int ResisterObject(object o, IEndPointContext context) { var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()); return repository.ResisterObject(o, context); } - public void ClearObject(int id) + public void ClearObject(ComplexObjectIdentifier id) { var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()); repository.ClearObject(id); @@ -33,10 +35,10 @@ namespace mROA.Implementation.Backend public T GetObjectByShell(SharedObjectShellShell sharedObjectShellShell) { var repository = GetRepository(sharedObjectShellShell.Identifier.OwnerId); - return repository.GetObject(sharedObjectShellShell.Identifier.ContextId); + return repository.GetObject(sharedObjectShellShell.Identifier)!; } - public T? GetObject(int id) + public T? GetObject(ComplexObjectIdentifier id) { var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()); return repository.GetObject(id); @@ -69,11 +71,5 @@ namespace mROA.Implementation.Backend _repositories.Add(clientId, created); return created; } - - public object GetObject(int id) - { - var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()); - return repository.GetObject(id); - } } } \ No newline at end of file diff --git a/mROA/Implementation/ComplexRepository.cs b/mROA/Implementation/ComplexRepository.cs new file mode 100644 index 0000000..756969c --- /dev/null +++ b/mROA/Implementation/ComplexRepository.cs @@ -0,0 +1,44 @@ +using System; +using mROA.Abstract; + +namespace mROA.Implementation +{ + public class ComplexRepository : IContextRepository + { + public void Inject(T dependency) + { + throw new NotImplementedException(); + } + + public int HostId { get; set; } + public int ResisterObject(object o, IEndPointContext context) + { + throw new NotImplementedException(); + } + + public void ClearObject(ComplexObjectIdentifier id) + { + throw new NotImplementedException(); + } + + public T GetObjectByShell(SharedObjectShellShell sharedObjectShellShell) + { + throw new NotImplementedException(); + } + + public T? GetObject(ComplexObjectIdentifier id) + { + throw new NotImplementedException(); + } + + public object GetSingleObject(Type type) + { + throw new NotImplementedException(); + } + + public int GetObjectIndex(object o, IEndPointContext context) + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/mROA/Implementation/ExtensibleStorage.cs b/mROA/Implementation/ExtensibleStorage.cs new file mode 100644 index 0000000..02d12a4 --- /dev/null +++ b/mROA/Implementation/ExtensibleStorage.cs @@ -0,0 +1,55 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using mROA.Abstract; + +namespace mROA.Implementation +{ + public class ExtensibleStorage : IStorage + { + private const int StartupSize = 1024; + private const int GrowSize = 128; + private T?[] _array = new T?[StartupSize]; + private readonly LinkedList _freePlaces = new(Enumerable.Range(0, StartupSize)); + + public T GetValue(int index) + { + return _array[index]!; + } + + public int GetIndex(T value) + { + return Array.IndexOf(_array, value); + } + + public int Place(T value) + { + if (_freePlaces.Count == 0) + { + Grow(); + } + + var index = _freePlaces.First.Value; + + _array[index] = value; + + return index; + } + + private void Grow() + { + foreach (var index in Enumerable.Range(_array.Length, GrowSize)) + _freePlaces.AddLast(index); + + T?[] nextStorage = new T[_array.Length + GrowSize]; + Array.Copy(_array, nextStorage, _array.Length); + _array = nextStorage; + } + + public void Free(int index) + { + _freePlaces.AddFirst(index); + _array[index] = default; + } + } +} \ No newline at end of file diff --git a/mROA/Implementation/RemoteContextRepository.cs b/mROA/Implementation/RemoteContextRepository.cs index 9f34c1d..71097ed 100644 --- a/mROA/Implementation/RemoteContextRepository.cs +++ b/mROA/Implementation/RemoteContextRepository.cs @@ -9,12 +9,14 @@ namespace mROA.Implementation public static Dictionary RemoteTypes = new(); private IRepresentationModuleProducer? _representationProducer; + public int HostId { get; set; } + public int ResisterObject(object o, IEndPointContext context) { throw new NotSupportedException(); } - public void ClearObject(int id) + public void ClearObject(ComplexObjectIdentifier id) { throw new NotSupportedException(); } @@ -31,7 +33,7 @@ namespace mROA.Implementation return remote; } - public T GetObject(int id) + public T? GetObject(ComplexObjectIdentifier id) { if (_representationProducer == null) throw new NullReferenceException("representation producer is not initialized");