Переименование UOI в COI

This commit is contained in:
2025-03-10 21:56:22 +03:00
parent 96aa828808
commit 5e21d941df
11 changed files with 123 additions and 115 deletions
+45 -45
View File
@@ -65,6 +65,50 @@ namespace mROA.Cbor
return Convert.ChangeType(nonCasted, type);
}
public void Inject<T>(T dependency)
{
}
public byte[] Serialize<T>(T objectToSerialize)
{
return Serialize(objectToSerialize, typeof(T));
}
public byte[] Serialize(object objectToSerialize, Type type)
{
return Serialize(objectToSerialize, context: null);
}
public T Deserialize<T>(byte[] rawData)
{
return Deserialize<T>(rawData: rawData, context: null);
}
public object? Deserialize(byte[] rawData, Type type)
{
return Deserialize(rawData: rawData, type, context: null);
}
public T Deserialize<T>(Span<byte> rawData)
{
return Deserialize<T>(rawData.ToArray().AsMemory(), context: null);
}
public object? Deserialize(Span<byte> rawData, Type type)
{
return Deserialize(rawData: rawData.ToArray(), type: type);
}
public T Cast<T>(object nonCasted)
{
return Cast<T>(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>(T dependency)
{
}
public byte[] Serialize<T>(T objectToSerialize)
{
return Serialize(objectToSerialize, typeof(T));
}
public byte[] Serialize(object objectToSerialize, Type type)
{
return Serialize(objectToSerialize, context: null);
}
public T Deserialize<T>(byte[] rawData)
{
return Deserialize<T>(rawData: rawData, context: null);
}
public object? Deserialize(byte[] rawData, Type type)
{
return Deserialize(rawData: rawData, type, context: null);
}
public T Deserialize<T>(Span<byte> rawData)
{
return Deserialize<T>(rawData.ToArray().AsMemory(), context: null);
}
public object? Deserialize(Span<byte> rawData, Type type)
{
return Deserialize(rawData: rawData.ToArray(), type: type);
}
public T Cast<T>(object nonCasted)
{
return Cast<T>(nonCasted: nonCasted, context: null);
}
public object Cast(object nonCasted, Type type)
{
return Cast(nonCasted: nonCasted, type: type, context: null);
}
}
}
+10 -8
View File
@@ -12,16 +12,15 @@ namespace mROA.Cbor
public class PreParsedValue : IPreParsedValue
{
private List<object> _properties { get; set; }
public PreParsedValue(List<object> properties)
{
_properties = properties;
}
private List<object> _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)
{
+5 -5
View File
@@ -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));
}
}
+1 -1
View File
@@ -7,7 +7,7 @@ namespace mROA.Abstract
{
int ResisterObject<T>(object o, IEndPointContext context);
void ClearObject(int id);
T GetObjectBySharedObject<T>(SharedObjectShellShell<T> sharedObjectShellShell);
T GetObjectByShell<T>(SharedObjectShellShell<T> sharedObjectShellShell);
T? GetObject<T>(int id);
object GetSingleObject(Type type);
int GetObjectIndex<T>(object o, IEndPointContext context);
@@ -53,7 +53,7 @@ namespace mROA.Implementation.Backend
_lastIndexFinder = Task.FromResult(id);
}
public T GetObjectBySharedObject<T>(SharedObjectShellShell<T> sharedObjectShellShell)
public T GetObjectByShell<T>(SharedObjectShellShell<T> sharedObjectShellShell)
{
return (T)GetObject(sharedObjectShellShell.Identifier.ContextId);
}
@@ -6,23 +6,14 @@ namespace mROA.Implementation.Backend
{
public class MultiClientContextRepository : IContextRepository, IContextRepositoryHub
{
private Dictionary<int, IContextRepository> _repositories = new();
private readonly Func<int, IContextRepository> _produceRepository;
private Dictionary<int, IContextRepository> _repositories = new();
public MultiClientContextRepository(Func<int, IContextRepository> 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>(T dependency)
{
}
@@ -39,18 +30,12 @@ namespace mROA.Implementation.Backend
repository.ClearObject(id);
}
public T GetObjectBySharedObject<T>(SharedObjectShellShell<T> sharedObjectShellShell)
public T GetObjectByShell<T>(SharedObjectShellShell<T> sharedObjectShellShell)
{
var repository = GetRepository(sharedObjectShellShell.Identifier.OwnerId);
return repository.GetObject<T>(sharedObjectShellShell.Identifier.ContextId);
}
public object GetObject(int id)
{
var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId());
return repository.GetObject<object>(id);
}
public T? GetObject<T>(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<object>(id);
}
}
}
+3 -3
View File
@@ -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()
@@ -3,19 +3,19 @@ using System;
namespace mROA.Implementation
{
#pragma warning disable CS8618, CS9264
public struct UniversalObjectIdentifier : IEquatable<UniversalObjectIdentifier>
public struct ComplexObjectIdentifier : IEquatable<ComplexObjectIdentifier>
{
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()
+11 -9
View File
@@ -6,8 +6,8 @@ namespace mROA.Implementation
{
public class RemoteContextRepository : IContextRepository
{
private IRepresentationModuleProducer? _representationProducer;
public static Dictionary<Type, Type> RemoteTypes = new();
private IRepresentationModuleProducer? _representationProducer;
public int ResisterObject<T>(object o, IEndPointContext context)
{
@@ -19,7 +19,7 @@ namespace mROA.Implementation
throw new NotSupportedException();
}
public T GetObjectBySharedObject<T>(SharedObjectShellShell<T> sharedObjectShellShell)
public T GetObjectByShell<T>(SharedObjectShellShell<T> 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<T>(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();
}
}
}
+19 -16
View File
@@ -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<T> GetResultAsync<T>(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();
+4 -4
View File
@@ -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<T> : 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);
}
}