From 4f0304cb4325f6a8d3c9750b4a7a8a304e27563b Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Sat, 15 Feb 2025 09:06:29 +0300 Subject: [PATCH] =?UTF-8?q?=D0=B1=D0=B0=D0=B3=20=D1=80=D0=B5=D1=88=D0=B5?= =?UTF-8?q?=D0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Example.Frontend/Program.cs | 4 +-- mROA.Codegen/mROASourceGenerator.cs | 34 ++++++++++--------- .../Backend/BasicExecutionModule.cs | 2 ++ .../Backend/JsonSerialisationModule.cs | 1 + .../JsonFrontendSerialisationModule.cs | 24 ++++++++++--- 5 files changed, 41 insertions(+), 24 deletions(-) diff --git a/Example.Frontend/Program.cs b/Example.Frontend/Program.cs index 6be01c8..fcc3ca0 100644 --- a/Example.Frontend/Program.cs +++ b/Example.Frontend/Program.cs @@ -28,14 +28,12 @@ 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(); -Thread.Sleep(100); Console.WriteLine("Printer name : {0}", name); Console.WriteLine("Registered printer"); + factory.Register(new SharedObject(new ClientBasedPrinter())); -Thread.Sleep(100); Console.WriteLine("Collecting all printers"); var names = factory.CollectAllNames(); Console.ReadLine(); diff --git a/mROA.Codegen/mROASourceGenerator.cs b/mROA.Codegen/mROASourceGenerator.cs index b3122de..4c505b7 100644 --- a/mROA.Codegen/mROASourceGenerator.cs +++ b/mROA.Codegen/mROASourceGenerator.cs @@ -56,17 +56,17 @@ namespace {Namespace} // Go through all attributes of the class. foreach (AttributeListSyntax attributeListSyntax in classDeclarationSyntax.AttributeLists) - foreach (AttributeSyntax attributeSyntax in attributeListSyntax.Attributes) - { - if (context.SemanticModel.GetSymbolInfo(attributeSyntax).Symbol is not IMethodSymbol attributeSymbol) - continue; // if we can't get the symbol, ignore it + foreach (AttributeSyntax attributeSyntax in attributeListSyntax.Attributes) + { + if (context.SemanticModel.GetSymbolInfo(attributeSyntax).Symbol is not IMethodSymbol attributeSymbol) + continue; // if we can't get the symbol, ignore it - string attributeName = attributeSymbol.ContainingType.ToDisplayString(); + string attributeName = attributeSymbol.ContainingType.ToDisplayString(); - // Check the full name of the [Report] attribute. - if (attributeName == "mROA.Implementation.Attributes.SharedObjectInterfaceAttribute") - return (classDeclarationSyntax, true); - } + // Check the full name of the [Report] attribute. + if (attributeName == "mROA.Implementation.Attributes.SharedObjectInterfaceAttribute") + return (classDeclarationSyntax, true); + } return (classDeclarationSyntax, false); } @@ -141,8 +141,7 @@ namespace {Namespace} sb.AppendLine( $"\t\tvar defaultCallRequestCodegen = new DefaultCallRequest {{ CommandId = {index}, ObjectId = id }};"); } - - sb.AppendLine("\t\tSystem.Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(defaultCallRequestCodegen));"); + //Post created request sb.AppendLine("\t\tserialisationModule.PostCallRequest(defaultCallRequestCodegen);"); @@ -154,7 +153,6 @@ namespace {Namespace} } else if (method.ReturnType.OriginalDefinition.ToString() == "System.Threading.Tasks.Task") { - var type = method.ReturnType.ToString(); type = type.Substring(type.IndexOf('<') + 1); type = type.Substring(0, type.Length - 1); @@ -169,6 +167,11 @@ namespace {Namespace} $"\t\tvar response = serialisationModule.GetFinalCommandExecution<{type}>(defaultCallRequestCodegen.CallRequestId).GetAwaiter().GetResult();"); sb.AppendLine($"\t\treturn ({type})response.Result;"); } + else + { + sb.AppendLine( + "\t\tserialisationModule.GetNextCommandExecution(defaultCallRequestCodegen.CallRequestId).Wait();"); + } sb.AppendLine("\t}"); @@ -198,16 +201,16 @@ partial class {className} (int id, ISerialisationModule.IFrontendSerialisationMo // Add the source code to the compilation. context.AddSource($"{className}.g.cs", SourceText.From(code, Encoding.UTF8)); - frontendContextRepo.Add($"{{ typeof({classSymbol.ToDisplayString()}), typeof({namespaceName}.{className}) }}"); + frontendContextRepo.Add( + $"{{ typeof({classSymbol.ToDisplayString()}), typeof({namespaceName}.{className}) }}"); } if (methods.Count != 0) { - var methodsStringed = methods.Select(i => $"typeof({i.Item1}).GetMethod(\"{i.Item2.Name}\", [{string.Join(", ", i.Item2.Parameters.Select(p => $"typeof({p.Type.ToDisplayString()})"))}])") .ToList(); - + var coCodegenRepoCode = @$"// using System.Collections.Generic; using System.Reflection; @@ -249,7 +252,6 @@ public class CoCodegenMethodRepository : IMethodRepository if (frontendContextRepo.Count != 0) { - var fronendRepoCode = @$"// using System.Collections.Frozen; using mROA.Implementation; diff --git a/mROA/Implementation/Backend/BasicExecutionModule.cs b/mROA/Implementation/Backend/BasicExecutionModule.cs index 274b872..200c948 100644 --- a/mROA/Implementation/Backend/BasicExecutionModule.cs +++ b/mROA/Implementation/Backend/BasicExecutionModule.cs @@ -15,6 +15,8 @@ public class BasicExecutionModule : IExecuteModule public ICommandExecution Execute(ICallRequest command, IContextRepository _contextRepo) { Console.WriteLine($"Executing command: {command.CommandId}"); + Thread.Sleep(100); + if (_methodRepo is null) throw new NullReferenceException("Method repository was not defined"); diff --git a/mROA/Implementation/Backend/JsonSerialisationModule.cs b/mROA/Implementation/Backend/JsonSerialisationModule.cs index ea873c2..d68e282 100644 --- a/mROA/Implementation/Backend/JsonSerialisationModule.cs +++ b/mROA/Implementation/Backend/JsonSerialisationModule.cs @@ -27,6 +27,7 @@ public class JsonSerialisationModule : ISerialisationModule command.Parameter = jsElement.Deserialize(parameter); } + Console.WriteLine($"Calling command execution {command.CommandId}"); var response = _executeModule!.Execute(command, _contextRepo); response.ClientId = clientId; var resultType = response is FinalCommandExecution diff --git a/mROA/Implementation/Frontend/JsonFrontendSerialisationModule.cs b/mROA/Implementation/Frontend/JsonFrontendSerialisationModule.cs index dca2e19..3a9b30d 100644 --- a/mROA/Implementation/Frontend/JsonFrontendSerialisationModule.cs +++ b/mROA/Implementation/Frontend/JsonFrontendSerialisationModule.cs @@ -1,3 +1,4 @@ +using System.Text; using System.Text.Json; using mROA.Abstract; @@ -15,11 +16,20 @@ public class JsonFrontendSerialisationModule throw new Exception("Interaction module not initialized"); var receiveMessage = await _interactionModule.ReceiveMessage(); - var parsed = JsonSerializer.Deserialize(receiveMessage)!; - while (parsed.CallRequestId != requestId) + var message = JsonSerializer.Deserialize(receiveMessage)!; + + while (message.Id != requestId) { receiveMessage = await _interactionModule.ReceiveMessage(); - parsed = JsonSerializer.Deserialize(receiveMessage)!; + message = JsonSerializer.Deserialize(receiveMessage)!; + } + + var parsed = JsonSerializer.Deserialize(message.Data)!; + + if (message.SchemaId == MessageType.ErrorCommandExecution) + { + throw new RemoteException(JsonSerializer.Deserialize(message.Data)!.Exception) + { CallRequestId = requestId }; } return parsed; @@ -31,11 +41,14 @@ public class JsonFrontendSerialisationModule throw new Exception("Interaction module not initialized"); var receiveMessage = await _interactionModule.ReceiveMessage(); + Console.WriteLine($"Received message: {Encoding.UTF8.GetString(receiveMessage)}"); var message = JsonSerializer.Deserialize(receiveMessage)!; while (message.Id != requestId) { receiveMessage = await _interactionModule.ReceiveMessage(); + Console.WriteLine($"Received message again: {Encoding.UTF8.GetString(receiveMessage)}"); + message = JsonSerializer.Deserialize(receiveMessage)!; } @@ -53,6 +66,8 @@ public class JsonFrontendSerialisationModule if (_interactionModule is null) throw new Exception("Interaction module not initialized"); + Console.WriteLine($"PostCallRequest: {JsonSerializer.Serialize(callRequest)}"); + var post = JsonSerializer.SerializeToUtf8Bytes(callRequest, callRequest.GetType()); _interactionModule.PostMessage(JsonSerializer.SerializeToUtf8Bytes(new NetworkMessage { @@ -60,9 +75,8 @@ public class JsonFrontendSerialisationModule Data = post, SchemaId = MessageType.CallRequest })); - - } + public void Inject(T dependency) { if (dependency is IInteractionModule.IFrontendInteractionModule interactionModule)