diff --git a/Example.Frontend/ClientBasedPrinter.cs b/Example.Frontend/ClientBasedPrinter.cs index f49a243..58a97c8 100644 --- a/Example.Frontend/ClientBasedPrinter.cs +++ b/Example.Frontend/ClientBasedPrinter.cs @@ -8,7 +8,7 @@ public class ClientBasedPrinter : IPrinter public string GetName() { Console.WriteLine("ClientBasedPrinter called from server!!!!!!!!!!! Vova likes that:)"); - return "ClientBasedPrinter"; + return "ClientBasedPrinter from mroa"; } public async Task> Print(string text, CancellationToken cancellationToken) diff --git a/Example.Frontend/Program.cs b/Example.Frontend/Program.cs index 4a7c009..10ae566 100644 --- a/Example.Frontend/Program.cs +++ b/Example.Frontend/Program.cs @@ -28,7 +28,7 @@ TransmissionConfig.RealContextRepository = builder.GetModule( TransmissionConfig.RemoteEndpointContextRepository = builder.GetModule(); builder.GetModule()!.Connect(); - _ = builder.GetModule()!.StartExtraction(); + // _ = builder.GetModule()!.StartExtraction(); Console.WriteLine(TransmissionConfig.OwnershipRepository!.GetOwnershipId()); var context = builder.GetModule(); @@ -36,18 +36,27 @@ var factory = context.GetSingleObject(typeof(IPrinterFactory)) as IPrinterFactor //правильный порядок команд 8-5-10-7 var printer = factory.Create("Test"); +Thread.Sleep(100); + var name = printer.Value.GetName(); Console.WriteLine("Printer name : {0}", name); - +Thread.Sleep(100); factory.Register(new SharedObject(new ClientBasedPrinter())); +Thread.Sleep(100); + Console.WriteLine("Registered printer"); var registred = factory.GetFirstPrinter(); +Thread.Sleep(100); +Console.WriteLine("First printer"); + Console.WriteLine(registred.Value); Console.WriteLine("Collecting all printers"); var names = factory.CollectAllNames(); -Console.WriteLine(names); +Thread.Sleep(100); + +Console.WriteLine(string.Join(", ", names)); Console.ReadLine(); // var page = await printer.Value.Print("Test Page", new CancellationToken()); diff --git a/mROA/Implementation/Backend/BasicExecutionModule.cs b/mROA/Implementation/Backend/BasicExecutionModule.cs index 946ba96..31097c8 100644 --- a/mROA/Implementation/Backend/BasicExecutionModule.cs +++ b/mROA/Implementation/Backend/BasicExecutionModule.cs @@ -48,7 +48,7 @@ public class BasicExecutionModule : IExecuteModule return new TypedFinalCommandExecution { CommandId = command.CommandId, Result = finalResult, - Id = command.CallRequestId, + Id = command.Id, Type = currentCommand.ReturnType }; } @@ -56,7 +56,7 @@ public class BasicExecutionModule : IExecuteModule { return new ExceptionCommandExecution { - Id = command.CallRequestId, CommandId = command.CommandId, + Id = command.Id, CommandId = command.CommandId, Exception = e.ToString() }; } @@ -75,13 +75,13 @@ public class BasicExecutionModule : IExecuteModule result.Wait(token); - return new FinalCommandExecution { CommandId = command.CommandId, Id = command.CallRequestId }; + return new FinalCommandExecution { CommandId = command.CommandId, Id = command.Id }; } catch (Exception e) { return new ExceptionCommandExecution { - Id = command.CallRequestId, CommandId = command.CommandId, + Id = command.Id, CommandId = command.CommandId, Exception = e.ToString() }; } @@ -102,7 +102,7 @@ public class BasicExecutionModule : IExecuteModule var finalResult = result.GetType().GetProperty("Result")?.GetValue(result); return new TypedFinalCommandExecution { - Id = command.CallRequestId, + Id = command.Id, Result = finalResult, CommandId = command.CommandId, Type = finalResult?.GetType() @@ -112,7 +112,7 @@ public class BasicExecutionModule : IExecuteModule { return new ExceptionCommandExecution { - Id = command.CallRequestId, CommandId = command.CommandId, + Id = command.Id, CommandId = command.CommandId, Exception = e.ToString() }; } diff --git a/mROA/Implementation/CallRequest.cs b/mROA/Implementation/CallRequest.cs index 0b5f17d..6ef0aeb 100644 --- a/mROA/Implementation/CallRequest.cs +++ b/mROA/Implementation/CallRequest.cs @@ -4,7 +4,7 @@ namespace mROA.Implementation; public interface ICallRequest { - Guid CallRequestId { get; } + Guid Id { get; } int CommandId { get; } int ObjectId { get; } object? Parameter { get; } @@ -12,7 +12,7 @@ public interface ICallRequest public class DefaultCallRequest : ICallRequest { - public Guid CallRequestId { get; set; } = Guid.NewGuid(); + public Guid Id { get; set; } = Guid.NewGuid(); public int CommandId { get; init; } public int ObjectId { get; init; } = -1; diff --git a/mROA/Implementation/Frontend/RequestExtractor.cs b/mROA/Implementation/Frontend/RequestExtractor.cs index 64620f5..e50d997 100644 --- a/mROA/Implementation/Frontend/RequestExtractor.cs +++ b/mROA/Implementation/Frontend/RequestExtractor.cs @@ -61,6 +61,8 @@ public class RequestExtractor : IRequestExtractor var request = _representationModule!.GetMessage(messageType: MessageType.CallRequest); + Console.WriteLine("Executing {0}", request.Id); + if (request.Parameter is not null) { var parameterType = _methodRepository!.GetMethod(request.CommandId).GetParameters().First() @@ -75,7 +77,7 @@ public class RequestExtractor : IRequestExtractor ? MessageType.FinishedCommandExecution : MessageType.ExceptionCommandExecution; - _representationModule.PostCallMessage(request.CallRequestId, resultType, result, result.GetType()); + _representationModule.PostCallMessage(request.Id, resultType, result, result.GetType()); } } catch diff --git a/mROA/Implementation/NetworkMessage.cs b/mROA/Implementation/NetworkMessage.cs index 066efe2..405168d 100644 --- a/mROA/Implementation/NetworkMessage.cs +++ b/mROA/Implementation/NetworkMessage.cs @@ -1,4 +1,5 @@ using System.Text.Json; +using System.Text.Json.Serialization; using mROA.Abstract; namespace mROA.Implementation; @@ -6,6 +7,7 @@ namespace mROA.Implementation; public class NetworkMessage { public Guid Id { get; set; } + [JsonConverter(typeof(JsonStringEnumConverter))] public MessageType SchemaId { get; set; } public byte[] Data { get; set; } } diff --git a/mROA/Implementation/NextGenerationInteractionModule.cs b/mROA/Implementation/NextGenerationInteractionModule.cs index d2e4105..918315b 100644 --- a/mROA/Implementation/NextGenerationInteractionModule.cs +++ b/mROA/Implementation/NextGenerationInteractionModule.cs @@ -1,4 +1,6 @@ using System.Security.Cryptography; +using System.Text; +using System.Text.Json; using mROA.Abstract; namespace mROA.Implementation; @@ -11,7 +13,9 @@ public class NextGenerationInteractionModule : INextGenerationInteractionModule private Task? _currentReceiving; private const int BufferSize = ushort.MaxValue; private readonly byte[] _buffer = new byte[BufferSize]; + private List _messageBuffer = []; + public void Inject(T dependency) { switch (dependency) @@ -28,10 +32,11 @@ public class NextGenerationInteractionModule : INextGenerationInteractionModule public Task GetNextMessageReceiving() { - if (_currentReceiving is { IsCompleted: false }) + Console.WriteLine(_currentReceiving?.Status); + if (_currentReceiving is { Status: TaskStatus.Running }) return _currentReceiving; - _currentReceiving = GetNextMessage(); + _currentReceiving = Task.Run(GetNextMessage); return _currentReceiving; } @@ -40,12 +45,15 @@ public class NextGenerationInteractionModule : INextGenerationInteractionModule if (BaseStream == null) throw new NullReferenceException("BaseStream is null"); + Console.WriteLine("Sending {0}", JsonSerializer.Serialize(message)); + + var rawMessage = _serialization.Serialize(message); await BaseStream.WriteAsync(BitConverter.GetBytes((ushort)rawMessage.Length).AsMemory(0, sizeof(ushort))); await BaseStream.WriteAsync(rawMessage); } - private async Task GetNextMessage() + private NetworkMessage GetNextMessage() { if (BaseStream == null) throw new NullReferenceException("BaseStream is null"); @@ -53,10 +61,17 @@ public class NextGenerationInteractionModule : INextGenerationInteractionModule if (_serialization == null) throw new NullReferenceException("Serialization toolkit is null"); - await BaseStream.ReadExactlyAsync(_buffer, 0, 2); + + Console.WriteLine("Receiving message"); + BaseStream.ReadExactly(_buffer, 0, 2); var len = BitConverter.ToUInt16(_buffer, 0); - await BaseStream.ReadExactlyAsync(_buffer, 0, len); + BaseStream.ReadExactly(_buffer, 0, len); + + Console.WriteLine("Receiving {0}", Encoding.Default.GetString(_buffer[..len])); - return _serialization.Deserialize(_buffer[..len])!; + var message = JsonSerializer.Deserialize(Encoding.Default.GetString(_buffer[..len])); + _messageBuffer.Add(message!); + + return message!; } } \ No newline at end of file diff --git a/mROA/Implementation/RemoteObjectBase.cs b/mROA/Implementation/RemoteObjectBase.cs index 286d3bc..fecf8eb 100644 --- a/mROA/Implementation/RemoteObjectBase.cs +++ b/mROA/Implementation/RemoteObjectBase.cs @@ -20,14 +20,14 @@ public abstract class RemoteObjectBase { var request = new DefaultCallRequest { CommandId = methodId, ObjectId = _id, Parameter = parameter, ParameterType = parameter?.GetType() }; - await _representationModule.PostCallMessageAsync(request.CallRequestId, MessageType.CallRequest, request); + await _representationModule.PostCallMessageAsync(request.Id, MessageType.CallRequest, request); var successResponse = _representationModule.GetMessageAsync>( - messageType: MessageType.FinishedCommandExecution, requestId: request.CallRequestId); + messageType: MessageType.FinishedCommandExecution, requestId: request.Id); var errorResponse = _representationModule.GetMessageAsync( - messageType: MessageType.ExceptionCommandExecution, requestId: request.CallRequestId); + messageType: MessageType.ExceptionCommandExecution, requestId: request.Id); Task.WaitAny(successResponse, errorResponse); if (successResponse.IsCompletedSuccessfully) @@ -40,14 +40,14 @@ public abstract class RemoteObjectBase { var request = new DefaultCallRequest { CommandId = methodId, ObjectId = _id, Parameter = parameter, ParameterType = parameter?.GetType() }; - await _representationModule.PostCallMessageAsync(request.CallRequestId, MessageType.CallRequest, request); + await _representationModule.PostCallMessageAsync(request.Id, MessageType.CallRequest, request); var successResponse = _representationModule.GetMessageAsync( - messageType: MessageType.FinishedCommandExecution, requestId: request.CallRequestId); + messageType: MessageType.FinishedCommandExecution, requestId: request.Id); var errorResponse = _representationModule.GetMessageAsync( - messageType: MessageType.ExceptionCommandExecution, requestId: request.CallRequestId); + messageType: MessageType.ExceptionCommandExecution, requestId: request.Id); Task.WaitAny(successResponse, errorResponse); diff --git a/mROA/Implementation/RepresentationModule.cs b/mROA/Implementation/RepresentationModule.cs index dda2a35..6b6c266 100644 --- a/mROA/Implementation/RepresentationModule.cs +++ b/mROA/Implementation/RepresentationModule.cs @@ -38,7 +38,6 @@ public class RepresentationModule : IRepresentationModule while (true) { var message = await _interaction.GetNextMessageReceiving(); - Console.WriteLine("Message received {0}", JsonSerializer.Serialize(message)); if ((requestId is null || message.Id == requestId) && (messageType is null || message.SchemaId == messageType)) return message.Data;