diff --git a/Example.Backend/Printer.cs b/Example.Backend/Printer.cs index b456160..85cf27b 100644 --- a/Example.Backend/Printer.cs +++ b/Example.Backend/Printer.cs @@ -3,10 +3,10 @@ using mROA.Implementation; namespace Example.Backend; - public class Printer : IPrinter { public string Name; + public string GetName() { return Name; @@ -15,6 +15,7 @@ public class Printer : IPrinter public async Task> Print(string text, CancellationToken cancellationToken = default) { // throw new Exception("The method or operation is not implemented."); - return new Page {Text = text}; + // var sharedObject = new Page { Text = text }; + return new Page { Text = text }; } } \ No newline at end of file diff --git a/Example.Frontend/Program.cs b/Example.Frontend/Program.cs index 1f73baa..f4800df 100644 --- a/Example.Frontend/Program.cs +++ b/Example.Frontend/Program.cs @@ -32,7 +32,7 @@ _ = builder.GetModule()!.StartExtraction(); Console.WriteLine(TransmissionConfig.OwnershipRepository.GetOwnershipId()); var context = builder.GetModule(); -var factory = context.GetSingleObject(typeof(IPrinterFactory)) as IPrinterFactory; +var factory = context.GetSingleObject(); //правильный порядок команд 8-5-10-7 var printer = factory.Create("Test"); diff --git a/mROA/Abstract/IContextRepository.cs b/mROA/Abstract/IContextRepository.cs index 754c39f..6829405 100644 --- a/mROA/Abstract/IContextRepository.cs +++ b/mROA/Abstract/IContextRepository.cs @@ -6,6 +6,7 @@ public interface IContextRepository : IInjectableModule void ClearObject(int id); object GetObject(int id); T? GetObject(int id); + T GetSingleObject(); object GetSingleObject(Type type); int GetObjectIndex(object o); } \ No newline at end of file diff --git a/mROA/Abstract/ISerialisationModule.cs b/mROA/Abstract/ISerialisationModule.cs index d18e78e..f83f40b 100644 --- a/mROA/Abstract/ISerialisationModule.cs +++ b/mROA/Abstract/ISerialisationModule.cs @@ -21,13 +21,13 @@ public interface ISerialisationModule : IInjectableModule public interface IRepresentationModule : IInjectableModule { int Id { get; } - Task GetMessageAsync(Guid? requestId = null, MessageType? messageType = null, CancellationToken token = default); - T GetMessage(Guid? requestId = null, MessageType? messageType = null); + Task GetMessageAsync(Guid? requestId = null, EMessageType? messageType = null, CancellationToken token = default); + T GetMessage(Guid? requestId = null, EMessageType? messageType = null); T GetMessage(Predicate filter); Task GetRawMessage(Predicate filter, CancellationToken token = default); - Task PostCallMessageAsync(Guid id, MessageType messageType, T payload) where T : notnull; - Task PostCallMessageAsync(Guid id, MessageType messageType, object payload, Type payloadType); - void PostCallMessage(Guid id, MessageType messageType, T payload) where T : notnull; - void PostCallMessage(Guid id, MessageType messageType, object payload, Type payloadType); + Task PostCallMessageAsync(Guid id, EMessageType eMessageType, T payload) where T : notnull; + Task PostCallMessageAsync(Guid id, EMessageType eMessageType, object payload, Type payloadType); + void PostCallMessage(Guid id, EMessageType eMessageType, T payload) where T : notnull; + void PostCallMessage(Guid id, EMessageType eMessageType, object payload, Type payloadType); } \ No newline at end of file diff --git a/mROA/Implementation/Backend/BasicExecutionModule.cs b/mROA/Implementation/Backend/BasicExecutionModule.cs index e3d9903..ac64004 100644 --- a/mROA/Implementation/Backend/BasicExecutionModule.cs +++ b/mROA/Implementation/Backend/BasicExecutionModule.cs @@ -28,6 +28,7 @@ public class BasicExecutionModule : IExecuteModule var context = command.ObjectId != -1 ? contextRepository.GetObject(command.ObjectId) : contextRepository.GetSingleObject(currentCommand.DeclaringType!); + var parameter = command.Parameter; if (currentCommand.ReturnType.BaseType == typeof(Task) && diff --git a/mROA/Implementation/Backend/ContextRepository.cs b/mROA/Implementation/Backend/ContextRepository.cs index 8ee451d..6a3fe1e 100644 --- a/mROA/Implementation/Backend/ContextRepository.cs +++ b/mROA/Implementation/Backend/ContextRepository.cs @@ -54,12 +54,21 @@ public class ContextRepository : IContextRepository public T GetObject(int id) { - return id == -1 || _storage.Length <= id ? throw new NullReferenceException("Cannot find that object. It is null"): (T)_storage[id]!; + return id == -1 || _storage.Length <= id + ? throw new NullReferenceException("Cannot find that object. It is null") + : (T)_storage[id]!; + } + + public T GetSingleObject() + { + var result = GetSingleObject(typeof(T)); + return (T)result; } public object GetSingleObject(Type type) { - return _singletons!.GetValueOrDefault(type.GetHashCode()) ?? throw new ArgumentException("Unregistered singleton type"); + return _singletons!.GetValueOrDefault(type.GetHashCode()) ?? + throw new ArgumentException("Unregistered singleton type"); } public int GetObjectIndex(object o) @@ -81,8 +90,8 @@ public class ContextRepository : IContextRepository _storage = nextStorage; return _storage.Length; } + public void Inject(T dependency) { } - } \ No newline at end of file diff --git a/mROA/Implementation/Backend/MultiClientContextRepository.cs b/mROA/Implementation/Backend/MultiClientContextRepository.cs index 5bcce54..ff9da0e 100644 --- a/mROA/Implementation/Backend/MultiClientContextRepository.cs +++ b/mROA/Implementation/Backend/MultiClientContextRepository.cs @@ -39,6 +39,12 @@ public class MultiClientContextRepository(Func produceR return GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()).GetObject(id); } + public T GetSingleObject() + { + var result = GetSingleObject(typeof(T)); + return (T)result; + } + public object GetSingleObject(Type type) { return GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()).GetSingleObject(type); diff --git a/mROA/Implementation/Backend/NetworkGatewayModule.cs b/mROA/Implementation/Backend/NetworkGatewayModule.cs index 5ae5206..45ac183 100644 --- a/mROA/Implementation/Backend/NetworkGatewayModule.cs +++ b/mROA/Implementation/Backend/NetworkGatewayModule.cs @@ -71,7 +71,7 @@ public class NetworkGatewayModule : IGatewayModule interaction.StartInfiniteReceiving(); interaction.PostMessage(new NetworkMessage { - Id = Guid.NewGuid(), SchemaId = MessageType.IdAssigning, + Id = Guid.NewGuid(), MessageType = EMessageType.IdAssigning, Data = _serialization.Serialize(new IdAssingnment { Id = interaction.ConnectionId }) }); _hub.RegisterInteraction(interaction); diff --git a/mROA/Implementation/Frontend/NetworkFrontendBridge.cs b/mROA/Implementation/Frontend/NetworkFrontendBridge.cs index 5c1920b..9aa3b0c 100644 --- a/mROA/Implementation/Frontend/NetworkFrontendBridge.cs +++ b/mROA/Implementation/Frontend/NetworkFrontendBridge.cs @@ -37,9 +37,9 @@ public class NetworkFrontendBridge(IPEndPoint ipEndPoint) : IFrontendBridge var handle = _interactionModule.CurrentReceivingHandle; handle.WaitOne(); var welcomeMessage = _interactionModule.LastMessage; - if (welcomeMessage.SchemaId != MessageType.IdAssigning) + if (welcomeMessage.MessageType != EMessageType.IdAssigning) { - throw new Exception($"Incorrect message type. Must be IdAssigning, current : {welcomeMessage.SchemaId.ToString()}"); + throw new Exception($"Incorrect message type. Must be IdAssigning, current : {welcomeMessage.MessageType.ToString()}"); } _interactionModule.HandleMessage(welcomeMessage); TransmissionConfig.OwnershipRepository = new StaticOwnershipRepository(_serialization.Deserialize(welcomeMessage.Data)!.Id); diff --git a/mROA/Implementation/Frontend/RequestExtractor.cs b/mROA/Implementation/Frontend/RequestExtractor.cs index 7da79bd..6a6e52f 100644 --- a/mROA/Implementation/Frontend/RequestExtractor.cs +++ b/mROA/Implementation/Frontend/RequestExtractor.cs @@ -61,7 +61,7 @@ public class RequestExtractor : IRequestExtractor { var request = - _representationModule!.GetMessage(m => m.Id != lastCommandId && m.SchemaId == MessageType.CallRequest); + _representationModule!.GetMessage(m => m.Id != lastCommandId && m.MessageType == EMessageType.CallRequest); lastCommandId = request.Id; if (request.Parameter is not null) { @@ -74,8 +74,8 @@ public class RequestExtractor : IRequestExtractor var result = _executeModule.Execute(request, _contextRepository); var resultType = result is FinalCommandExecution - ? MessageType.FinishedCommandExecution - : MessageType.ExceptionCommandExecution; + ? EMessageType.FinishedCommandExecution + : EMessageType.ExceptionCommandExecution; _representationModule.PostCallMessage(request.Id, resultType, result, result.GetType()); } diff --git a/mROA/Implementation/NetworkMessage.cs b/mROA/Implementation/NetworkMessage.cs index 5571251..b6bb898 100644 --- a/mROA/Implementation/NetworkMessage.cs +++ b/mROA/Implementation/NetworkMessage.cs @@ -4,24 +4,20 @@ namespace mROA.Implementation; -public class NetworkMessage +public sealed class NetworkMessage { - public static readonly NetworkMessage Null = new() { SchemaId = MessageType.Unknown, Id = Guid.Empty, Data = [] }; + public static readonly NetworkMessage Null = new() { MessageType = EMessageType.Unknown, Id = Guid.Empty, Data = [] }; public Guid Id { get; init; } - - [JsonConverter(typeof(JsonStringEnumConverter))] - public MessageType SchemaId { get; init; } - + public EMessageType MessageType { get; init; } public required byte[] Data { get; init; } - - public bool IsValidMessage(Guid? requestId = null, MessageType? messageType = null) + public bool IsValidMessage(Guid? requestId = null, EMessageType? messageType = null) { return (requestId is null || Id == requestId) && - (messageType is null || SchemaId == messageType); + (messageType is null || MessageType == messageType); } } -public enum MessageType +public enum EMessageType { Unknown, FinishedCommandExecution, diff --git a/mROA/Implementation/RemoteContextRepository.cs b/mROA/Implementation/RemoteContextRepository.cs index 37eac71..c470092 100644 --- a/mROA/Implementation/RemoteContextRepository.cs +++ b/mROA/Implementation/RemoteContextRepository.cs @@ -31,7 +31,12 @@ public class RemoteContextRepository : IContextRepository var remote = (T)Activator.CreateInstance(remoteType, id, _representationProducer.Produce(TransmissionConfig.OwnershipRepository.GetOwnershipId()))!; return remote; } - + public T GetSingleObject() + { + var obj = GetSingleObject(typeof(T)); + return (T)obj; + } + public object GetSingleObject(Type type) { if (_representationProducer == null) diff --git a/mROA/Implementation/RemoteObjectBase.cs b/mROA/Implementation/RemoteObjectBase.cs index 3794a0e..af66e18 100644 --- a/mROA/Implementation/RemoteObjectBase.cs +++ b/mROA/Implementation/RemoteObjectBase.cs @@ -14,27 +14,26 @@ public abstract class RemoteObjectBase(int id, IRepresentationModule representat { var request = new DefaultCallRequest { CommandId = methodId, ObjectId = id, Parameter = parameter, ParameterType = parameter?.GetType() }; - await representationModule.PostCallMessageAsync(request.Id, MessageType.CallRequest, request); - + await representationModule.PostCallMessageAsync(request.Id, EMessageType.CallRequest, request); + var localTokenSource = new CancellationTokenSource(); - + var successResponse = - representationModule.GetMessageAsync>( - messageType: MessageType.FinishedCommandExecution, requestId: request.Id, token: localTokenSource.Token); - var errorResponse = - representationModule.GetMessageAsync( - messageType: MessageType.ExceptionCommandExecution, requestId: request.Id, token: localTokenSource.Token); + representationModule.GetMessageAsync>(request.Id, + EMessageType.FinishedCommandExecution, localTokenSource.Token); + var errorResponse = + representationModule.GetMessageAsync(request.Id, + EMessageType.ExceptionCommandExecution, localTokenSource.Token); + Task.WaitAny(successResponse, errorResponse); - - if (successResponse.IsCompletedSuccessfully) { await localTokenSource.CancelAsync(); return successResponse.Result.Result!; } - + await localTokenSource.CancelAsync(); throw errorResponse.Result.GetException(); } @@ -43,14 +42,14 @@ public abstract class RemoteObjectBase(int id, IRepresentationModule representat { var request = new DefaultCallRequest { CommandId = methodId, ObjectId = id, Parameter = parameter, ParameterType = parameter?.GetType() }; - await representationModule.PostCallMessageAsync(request.Id, MessageType.CallRequest, request); + await representationModule.PostCallMessageAsync(request.Id, EMessageType.CallRequest, request); var successResponse = representationModule.GetMessageAsync( - messageType: MessageType.FinishedCommandExecution, requestId: request.Id); + messageType: EMessageType.FinishedCommandExecution, requestId: request.Id); var errorResponse = representationModule.GetMessageAsync( - messageType: MessageType.ExceptionCommandExecution, requestId: request.Id); + messageType: EMessageType.ExceptionCommandExecution, requestId: request.Id); Task.WaitAny(successResponse, errorResponse); diff --git a/mROA/Implementation/RepresentationModule.cs b/mROA/Implementation/RepresentationModule.cs index 038a9ac..6c3e08f 100644 --- a/mROA/Implementation/RepresentationModule.cs +++ b/mROA/Implementation/RepresentationModule.cs @@ -22,16 +22,17 @@ public class RepresentationModule : IRepresentationModule public int Id => (_interaction ?? throw new NullReferenceException("Interaction is not initialized")).ConnectionId; - public async Task GetMessageAsync(Guid? requestId, MessageType? messageType, + public async Task GetMessageAsync(Guid? requestId, EMessageType? messageType, CancellationToken token = default) { if (_serialization == null) throw new NullReferenceException("Serialization toolkit is not initialized"); - return _serialization.Deserialize(await GetRawMessage(m => m.IsValidMessage(requestId, messageType), token))!; + var raw = await GetRawMessage(m => m.IsValidMessage(requestId, messageType), token); + return _serialization.Deserialize(raw)!; } - public T GetMessage(Guid? requestId = null, MessageType? messageType = null) + public T GetMessage(Guid? requestId = null, EMessageType? messageType = null) { return GetMessage(m => m.IsValidMessage(requestId, messageType)); } @@ -40,8 +41,8 @@ public class RepresentationModule : IRepresentationModule { if (_serialization == null) throw new NullReferenceException("Serialization toolkit is not initialized"); - - return _serialization.Deserialize(GetRawMessage(filter).GetAwaiter().GetResult())!; + var raw = GetRawMessage(filter).GetAwaiter().GetResult(); + return _serialization.Deserialize(raw)!; } public async Task GetRawMessage(Predicate filter, CancellationToken token = default) @@ -50,10 +51,7 @@ public class RepresentationModule : IRepresentationModule if (_interaction == null) throw new NullReferenceException("Interaction toolkit is not initialized"); - - // Console.WriteLine( - // $"{DateTime.Now.TimeOfDay} {Environment.CurrentManagedThreadId} Representation : Reading"); - + if (filter(_interaction.LastMessage)) { _interaction.HandleMessage(_interaction.LastMessage); @@ -71,20 +69,16 @@ public class RepresentationModule : IRepresentationModule while (!token.IsCancellationRequested) { - // Console.WriteLine( - // $"{DateTime.Now.TimeOfDay} {Environment.CurrentManagedThreadId} Representation : Receiving message..."); var handle = _interaction.CurrentReceivingHandle; handle.WaitOne(); message = _interaction.LastMessage; - // Console.WriteLine( - // $"{DateTime.Now.TimeOfDay} {Environment.CurrentManagedThreadId} Representation : Message received {message.SchemaId} - {message.Id}"); - if (!filter(message)) continue; + if (!filter(message)) + continue; message = _interaction.LastMessage; - // Console.WriteLine( - // $"{DateTime.Now.TimeOfDay} {Environment.CurrentManagedThreadId} Representation : Message received Successfully {message.SchemaId} - {message.Id}"); + _interaction.HandleMessage(message); return message.Data; } @@ -92,29 +86,30 @@ public class RepresentationModule : IRepresentationModule return []; } - public async Task PostCallMessageAsync(Guid id, MessageType messageType, T payload) where T : notnull + public async Task PostCallMessageAsync(Guid id, EMessageType eMessageType, T payload) where T : notnull { - await PostCallMessageAsync(id, messageType, payload, typeof(T)); + await PostCallMessageAsync(id, eMessageType, payload, typeof(T)); } - public async Task PostCallMessageAsync(Guid id, MessageType messageType, object payload, Type payloadType) + public async Task PostCallMessageAsync(Guid id, EMessageType eMessageType, object payload, Type payloadType) { if (_interaction == null) throw new NullReferenceException("Interaction toolkit is not initialized"); if (_serialization == null) throw new NullReferenceException("Serialization toolkit is not initialized"); - await _interaction.PostMessage(new NetworkMessage - { Id = id, SchemaId = messageType, Data = _serialization.Serialize(payload, payloadType) }); + var message = new NetworkMessage + { Id = id, MessageType = eMessageType, Data = _serialization.Serialize(payload, payloadType) }; + await _interaction.PostMessage(message); } - public void PostCallMessage(Guid id, MessageType messageType, T payload) where T : notnull + public void PostCallMessage(Guid id, EMessageType eMessageType, T payload) where T : notnull { - PostCallMessageAsync(id, messageType, payload).GetAwaiter().GetResult(); + PostCallMessageAsync(id, eMessageType, payload).GetAwaiter().GetResult(); } - public void PostCallMessage(Guid id, MessageType messageType, object payload, Type payloadType) + public void PostCallMessage(Guid id, EMessageType eMessageType, object payload, Type payloadType) { - PostCallMessageAsync(id, messageType, payload, payloadType).GetAwaiter().GetResult(); + PostCallMessageAsync(id, eMessageType, payload, payloadType).GetAwaiter().GetResult(); } } \ No newline at end of file