diff --git a/mROA.Cbor/CborSerializationToolkit.cs b/mROA.Cbor/CborSerializationToolkit.cs index 12016df..341b49d 100644 --- a/mROA.Cbor/CborSerializationToolkit.cs +++ b/mROA.Cbor/CborSerializationToolkit.cs @@ -65,6 +65,50 @@ namespace mROA.Cbor return Convert.ChangeType(nonCasted, type); } + public void Inject(T dependency) + { + } + + public byte[] Serialize(T objectToSerialize) + { + return Serialize(objectToSerialize, typeof(T)); + } + + public byte[] Serialize(object objectToSerialize, Type type) + { + return Serialize(objectToSerialize, context: null); + } + + public T Deserialize(byte[] rawData) + { + return Deserialize(rawData: rawData, context: null); + } + + public object? Deserialize(byte[] rawData, Type type) + { + return Deserialize(rawData: rawData, type, context: null); + } + + public T Deserialize(Span rawData) + { + return Deserialize(rawData.ToArray().AsMemory(), context: null); + } + + public object? Deserialize(Span rawData, Type type) + { + return Deserialize(rawData: rawData.ToArray(), type: type); + } + + public T Cast(object nonCasted) + { + return Cast(nonCasted: nonCasted, context: null); + } + + public object Cast(object nonCasted, Type type) + { + return Cast(nonCasted: nonCasted, type: type, context: null); + } + private void WriteData(object? obj, CborWriter writer, IEndPointContext? context) { switch (obj) @@ -322,7 +366,7 @@ namespace mROA.Cbor var identifier = reader.ReadUInt64(); reader.ReadEndArray(); - so.Identifier = UniversalObjectIdentifier.FromFlat(identifier); + so.Identifier = ComplexObjectIdentifier.FromFlat(identifier); return so.UniversalValue; } @@ -387,49 +431,5 @@ namespace mROA.Cbor return finalProperties; } - - public void Inject(T dependency) - { - } - - public byte[] Serialize(T objectToSerialize) - { - return Serialize(objectToSerialize, typeof(T)); - } - - public byte[] Serialize(object objectToSerialize, Type type) - { - return Serialize(objectToSerialize, context: null); - } - - public T Deserialize(byte[] rawData) - { - return Deserialize(rawData: rawData, context: null); - } - - public object? Deserialize(byte[] rawData, Type type) - { - return Deserialize(rawData: rawData, type, context: null); - } - - public T Deserialize(Span rawData) - { - return Deserialize(rawData.ToArray().AsMemory(), context: null); - } - - public object? Deserialize(Span rawData, Type type) - { - return Deserialize(rawData: rawData.ToArray(), type: type); - } - - public T Cast(object nonCasted) - { - return Cast(nonCasted: nonCasted, context: null); - } - - public object Cast(object nonCasted, Type type) - { - return Cast(nonCasted: nonCasted, type: type, context: null); - } } } \ No newline at end of file diff --git a/mROA.Cbor/PreParsedValue.cs b/mROA.Cbor/PreParsedValue.cs index 8140f47..9737b7e 100644 --- a/mROA.Cbor/PreParsedValue.cs +++ b/mROA.Cbor/PreParsedValue.cs @@ -12,16 +12,15 @@ namespace mROA.Cbor public class PreParsedValue : IPreParsedValue { - private List _properties { get; set; } - public PreParsedValue(List properties) { _properties = properties; } + private List _properties { get; set; } + public object? ToObject(Type type, IEndPointContext? context) { - if (type.IsInterface) { var sharedShell = typeof(SharedObjectShellShell<>).MakeGenericType(type); @@ -31,10 +30,10 @@ namespace mROA.Cbor if (context != null) so.EndPointContext = context; - so.Identifier = UniversalObjectIdentifier.FromFlat((ulong)_properties[0]); + so.Identifier = ComplexObjectIdentifier.FromFlat((ulong)_properties[0]); return so.UniversalValue; } - + var instance = Activator.CreateInstance(type); if (instance == null) return null; @@ -48,7 +47,10 @@ namespace mROA.Cbor for (var index = 0; index < properties.Count; index++) { var property = properties[index]; - property.SetValue(instance, _properties[index] is IPreParsedValue ppv ? ppv.ToObject(property.PropertyType, context) : _properties[index]); + property.SetValue(instance, + _properties[index] is IPreParsedValue ppv + ? ppv.ToObject(property.PropertyType, context) + : _properties[index]); } return instance; @@ -57,13 +59,13 @@ namespace mROA.Cbor public class ParsedValue : IPreParsedValue { + private object? _value; + public ParsedValue(object? value) { _value = value; } - private object? _value; - public object? ToObject(Type type, IEndPointContext? context) { diff --git a/mROA.Test/UnSOization.cs b/mROA.Test/UnSOization.cs index afdcb9e..d30c025 100644 --- a/mROA.Test/UnSOization.cs +++ b/mROA.Test/UnSOization.cs @@ -4,12 +4,12 @@ namespace mROA.Test; public class UnSOization { - private UniversalObjectIdentifier _uoi; - + private ComplexObjectIdentifier _uoi; + [SetUp] public void Setup() { - _uoi = new UniversalObjectIdentifier + _uoi = new ComplexObjectIdentifier { ContextId = -123, OwnerId = 123 }; @@ -19,8 +19,8 @@ public class UnSOization public void FlatTest() { var flat = _uoi.Flat; - var next = new UniversalObjectIdentifier { Flat = flat }; - + var next = new ComplexObjectIdentifier { Flat = flat }; + Assert.That(_uoi, Is.EqualTo(next)); } } \ No newline at end of file diff --git a/mROA/Abstract/IContextRepository.cs b/mROA/Abstract/IContextRepository.cs index 0acbb60..75668c9 100644 --- a/mROA/Abstract/IContextRepository.cs +++ b/mROA/Abstract/IContextRepository.cs @@ -7,7 +7,7 @@ namespace mROA.Abstract { int ResisterObject(object o, IEndPointContext context); void ClearObject(int id); - T GetObjectBySharedObject(SharedObjectShellShell sharedObjectShellShell); + T GetObjectByShell(SharedObjectShellShell sharedObjectShellShell); T? GetObject(int id); object GetSingleObject(Type type); int GetObjectIndex(object o, IEndPointContext context); diff --git a/mROA/Implementation/Backend/ContextRepository.cs b/mROA/Implementation/Backend/ContextRepository.cs index d97bdc2..d00fd4c 100644 --- a/mROA/Implementation/Backend/ContextRepository.cs +++ b/mROA/Implementation/Backend/ContextRepository.cs @@ -53,7 +53,7 @@ namespace mROA.Implementation.Backend _lastIndexFinder = Task.FromResult(id); } - public T GetObjectBySharedObject(SharedObjectShellShell sharedObjectShellShell) + public T GetObjectByShell(SharedObjectShellShell sharedObjectShellShell) { return (T)GetObject(sharedObjectShellShell.Identifier.ContextId); } diff --git a/mROA/Implementation/Backend/MultiClientContextRepository.cs b/mROA/Implementation/Backend/MultiClientContextRepository.cs index 4b4194d..5a5ac0e 100644 --- a/mROA/Implementation/Backend/MultiClientContextRepository.cs +++ b/mROA/Implementation/Backend/MultiClientContextRepository.cs @@ -6,23 +6,14 @@ namespace mROA.Implementation.Backend { public class MultiClientContextRepository : IContextRepository, IContextRepositoryHub { - private Dictionary _repositories = new(); private readonly Func _produceRepository; + private Dictionary _repositories = new(); public MultiClientContextRepository(Func produceRepository) { _produceRepository = produceRepository; } - 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) { } @@ -39,18 +30,12 @@ namespace mROA.Implementation.Backend repository.ClearObject(id); } - public T GetObjectBySharedObject(SharedObjectShellShell sharedObjectShellShell) + public T GetObjectByShell(SharedObjectShellShell sharedObjectShellShell) { var repository = GetRepository(sharedObjectShellShell.Identifier.OwnerId); return repository.GetObject(sharedObjectShellShell.Identifier.ContextId); } - public object GetObject(int id) - { - var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()); - return repository.GetObject(id); - } - public T? GetObject(int id) { var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()); @@ -74,5 +59,21 @@ namespace mROA.Implementation.Backend var repository = GetRepositoryByClientId(clientId); return repository; } + + 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 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/CallRequest.cs b/mROA/Implementation/CallRequest.cs index 86d234f..6a909b0 100644 --- a/mROA/Implementation/CallRequest.cs +++ b/mROA/Implementation/CallRequest.cs @@ -9,7 +9,7 @@ namespace mROA.Implementation { Guid Id { get; } int CommandId { get; } - UniversalObjectIdentifier ObjectId { get; } + ComplexObjectIdentifier ObjectId { get; } object?[]? Parameters { get; } } @@ -17,7 +17,7 @@ namespace mROA.Implementation { public Guid Id { get; set; } = Guid.NewGuid(); public int CommandId { get; set; } - public UniversalObjectIdentifier ObjectId { get; set; } = UniversalObjectIdentifier.Null; + public ComplexObjectIdentifier ObjectId { get; set; } = ComplexObjectIdentifier.Null; public object?[]? Parameters { get; set; } @@ -31,7 +31,7 @@ namespace mROA.Implementation { public Guid Id { get; set; } public int CommandId { get; set; } = -2; - public UniversalObjectIdentifier ObjectId { get; set; } = UniversalObjectIdentifier.Null; + public ComplexObjectIdentifier ObjectId { get; set; } = ComplexObjectIdentifier.Null; public object?[]? Parameters { get; set; } = null; public override string ToString() diff --git a/mROA/Implementation/UniversalObjectIdentifier.cs b/mROA/Implementation/ComplexObjectIdentifier.cs similarity index 66% rename from mROA/Implementation/UniversalObjectIdentifier.cs rename to mROA/Implementation/ComplexObjectIdentifier.cs index cd78859..a1004d3 100644 --- a/mROA/Implementation/UniversalObjectIdentifier.cs +++ b/mROA/Implementation/ComplexObjectIdentifier.cs @@ -3,19 +3,19 @@ using System; namespace mROA.Implementation { #pragma warning disable CS8618, CS9264 - public struct UniversalObjectIdentifier : IEquatable + public struct ComplexObjectIdentifier : IEquatable { public int ContextId; public int OwnerId; - public UniversalObjectIdentifier(int contextId, int ownerId) + public ComplexObjectIdentifier(int contextId, int ownerId) { ContextId = contextId; OwnerId = ownerId; } - public static UniversalObjectIdentifier Null = new UniversalObjectIdentifier { ContextId = -2, OwnerId = -1 }; - public static UniversalObjectIdentifier FromFlat(ulong flat) => new() { Flat = flat }; + public static ComplexObjectIdentifier Null = new ComplexObjectIdentifier { ContextId = -2, OwnerId = -1 }; + public static ComplexObjectIdentifier FromFlat(ulong flat) => new() { Flat = flat }; public override string ToString() { @@ -34,14 +34,14 @@ namespace mROA.Implementation } } - public bool Equals(UniversalObjectIdentifier other) + public bool Equals(ComplexObjectIdentifier other) { return ContextId == other.ContextId && OwnerId == other.OwnerId; } public override bool Equals(object? obj) { - return obj is UniversalObjectIdentifier other && Equals(other); + return obj is ComplexObjectIdentifier other && Equals(other); } public override int GetHashCode() diff --git a/mROA/Implementation/RemoteContextRepository.cs b/mROA/Implementation/RemoteContextRepository.cs index e86e74f..9f34c1d 100644 --- a/mROA/Implementation/RemoteContextRepository.cs +++ b/mROA/Implementation/RemoteContextRepository.cs @@ -6,8 +6,8 @@ namespace mROA.Implementation { public class RemoteContextRepository : IContextRepository { - private IRepresentationModuleProducer? _representationProducer; public static Dictionary RemoteTypes = new(); + private IRepresentationModuleProducer? _representationProducer; public int ResisterObject(object o, IEndPointContext context) { @@ -19,7 +19,7 @@ namespace mROA.Implementation throw new NotSupportedException(); } - public T GetObjectBySharedObject(SharedObjectShellShell sharedObjectShellShell) + public T GetObjectByShell(SharedObjectShellShell sharedObjectShellShell) { if (_representationProducer == null) throw new NullReferenceException("representation producer is not initialized"); @@ -31,18 +31,14 @@ namespace mROA.Implementation return remote; } - public object GetObject(int id) - { - throw new NotSupportedException(); - } - public T GetObject(int id) { if (_representationProducer == null) throw new NullReferenceException("representation producer is not initialized"); if (!RemoteTypes.TryGetValue(typeof(T), out var remoteType)) throw new NotSupportedException(); - var representationModule = _representationProducer.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId()); + var representationModule = + _representationProducer.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId()); var remote = (T)Activator.CreateInstance(remoteType, id, representationModule)!; return remote; @@ -53,7 +49,8 @@ namespace mROA.Implementation if (_representationProducer == null) throw new NullReferenceException("representation producer is not initialized"); - var representationModule = _representationProducer.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId()); + var representationModule = + _representationProducer.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId()); return Activator.CreateInstance(RemoteTypes[type], -1, representationModule)!; } @@ -73,5 +70,10 @@ namespace mROA.Implementation if (dependency is IRepresentationModuleProducer serialisationModule) _representationProducer = serialisationModule; } + + public object GetObject(int id) + { + throw new NotSupportedException(); + } } } \ No newline at end of file diff --git a/mROA/Implementation/RemoteObjectBase.cs b/mROA/Implementation/RemoteObjectBase.cs index b180099..3ee8774 100644 --- a/mROA/Implementation/RemoteObjectBase.cs +++ b/mROA/Implementation/RemoteObjectBase.cs @@ -10,25 +10,34 @@ namespace mROA.Implementation { public abstract class RemoteObjectBase : IDisposable { - private readonly UniversalObjectIdentifier _identifier; + private readonly ComplexObjectIdentifier _identifier; private readonly IRepresentationModule _representationModule; protected RemoteObjectBase(int id, IRepresentationModule representationModule) { - _identifier = new UniversalObjectIdentifier { ContextId = id, OwnerId = representationModule.Id }; + _identifier = new ComplexObjectIdentifier { ContextId = id, OwnerId = representationModule.Id }; _representationModule = representationModule; } public int Id => _identifier.ContextId; public int OwnerId => _identifier.OwnerId; - public UniversalObjectIdentifier Identifier => _identifier; + public ComplexObjectIdentifier Identifier => _identifier; + + public void Dispose() + { + if (_identifier.IsStatic) + return; + CallAsync(-1).Wait(); + } + protected async Task GetResultAsync(int methodId, object?[]? parameters = null, CancellationToken cancellationToken = default) { var request = new DefaultCallRequest - { CommandId = methodId, ObjectId = _identifier, Parameters = parameters - }; - + { + CommandId = methodId, ObjectId = _identifier, Parameters = parameters + }; + await _representationModule.PostCallMessageAsync(request.Id, MessageType.CallRequest, request); var localTokenSource = new CancellationTokenSource(); @@ -53,7 +62,7 @@ namespace mROA.Implementation }); localTokenSource.Cancel(); }); - + Task.WaitAny(new Task[] { successResponse, errorResponse @@ -80,8 +89,9 @@ namespace mROA.Implementation CancellationToken cancellationToken = default) { var request = new DefaultCallRequest - { CommandId = methodId, ObjectId = _identifier, Parameters = parameters - }; + { + CommandId = methodId, ObjectId = _identifier, Parameters = parameters + }; await _representationModule.PostCallMessageAsync(request.Id, MessageType.CallRequest, request); var localTokenSource = new CancellationTokenSource(); @@ -122,13 +132,6 @@ namespace mROA.Implementation throw errorResponse.Result.GetException(); } - public void Dispose() - { - if (_identifier.IsStatic) - return; - CallAsync(-1).Wait(); - } - public override string ToString() { return _identifier.ToString(); diff --git a/mROA/Implementation/SharedObjectShell.cs b/mROA/Implementation/SharedObjectShell.cs index 3fba873..6073d66 100644 --- a/mROA/Implementation/SharedObjectShell.cs +++ b/mROA/Implementation/SharedObjectShell.cs @@ -11,13 +11,13 @@ namespace mROA.Implementation public interface ISharedObjectShell { IEndPointContext EndPointContext { get; set; } - UniversalObjectIdentifier Identifier { get; set; } + ComplexObjectIdentifier Identifier { get; set; } object UniversalValue { get; set; } } public class SharedObjectShellShell : ISharedObjectShell where T : notnull { - private UniversalObjectIdentifier _identifier = UniversalObjectIdentifier.Null; + private ComplexObjectIdentifier _identifier = ComplexObjectIdentifier.Null; private T _value; @@ -64,7 +64,7 @@ namespace mROA.Implementation OwnerFunc = TransmissionConfig.OwnershipRepository.GetOwnershipId }; - public UniversalObjectIdentifier Identifier + public ComplexObjectIdentifier Identifier { get { @@ -74,7 +74,7 @@ namespace mROA.Implementation set { _identifier = value; - Value = GetDefaultContextRepository().GetObjectBySharedObject(this); + Value = GetDefaultContextRepository().GetObjectByShell(this); } }