From 2dbfdce254f10df65103f1dddd33c9081ba385eb Mon Sep 17 00:00:00 2001 From: Mikhail Mitrofanov Date: Thu, 13 Mar 2025 20:30:02 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3=20=D0=BC=D0=BE=D0=B4=D1=83=D0=BB=D1=8F?= =?UTF-8?q?=20=D0=B2=D1=8B=D0=BF=D0=BE=D0=BB=D0=BD=D0=B5=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20=D0=B8=20=D1=8D=D0=BA=D1=81=D1=82=D1=80=D0=B0=D0=BA=D1=82?= =?UTF-8?q?=D0=BE=D1=80=D0=B0=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=D0=BE?= =?UTF-8?q?=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Backend/BasicExecutionModule.cs | 57 ++++++++------ .../Frontend/RequestExtractor.cs | 77 +++++++++++-------- 2 files changed, 79 insertions(+), 55 deletions(-) diff --git a/mROA/Implementation/Backend/BasicExecutionModule.cs b/mROA/Implementation/Backend/BasicExecutionModule.cs index e36ac30..21e4663 100644 --- a/mROA/Implementation/Backend/BasicExecutionModule.cs +++ b/mROA/Implementation/Backend/BasicExecutionModule.cs @@ -36,38 +36,19 @@ namespace mROA.Implementation.Backend try { - if (_cancellationRepo is null) - throw new NullReferenceException("Method repository was not defined"); - - if (_methodRepo is null) - throw new NullReferenceException("Method repository was not defined"); - - if (contextRepository is null) - throw new NullReferenceException("Context repository was not defined"); - + ThrowIfNotInjected(contextRepository); if (command is CancelRequest) { #if TRACE Console.WriteLine("Final cancelling request"); #endif - var cts = _cancellationRepo.GetCancellation(command.Id); - if (cts == null) - throw new NullReferenceException("Can't find cancellation for this request"); - cts.Cancel(); - _cancellationRepo.FreeCancelation(command.Id); - - return new FinalCommandExecution - { - Id = command.Id - }; + CancelExecution(command); } - var invoker = _methodRepo.GetMethod(command.CommandId); + var invoker = _methodRepo!.GetMethod(command.CommandId); if (invoker == null) throw new Exception($"Command {command.CommandId} not found"); - IContextRepository repository; - var context = command.ObjectId.ContextId != -1 ? contextRepository.GetObject(command.ObjectId) : contextRepository.GetSingleObject(invoker.SuitableType, command.ObjectId.OwnerId); @@ -83,7 +64,7 @@ namespace mROA.Implementation.Backend castedParams = new object[invoker.ParameterTypes.Length]; for (var i = 0; i < castedParams.Length; i++) { - castedParams[i] = _serialization.Cast(command.Parameters![i], invoker.ParameterTypes[i]); + castedParams[i] = _serialization!.Cast(command.Parameters![i], invoker.ParameterTypes[i]); } } @@ -93,10 +74,10 @@ namespace mROA.Implementation.Backend { case AsyncMethodInvoker { IsVoid: false } asyncNonVoidMethodInvoker: return TypedExecuteAsync(asyncNonVoidMethodInvoker, context, castedParams, command, - _cancellationRepo, + _cancellationRepo!, representationModule, execContext); case AsyncMethodInvoker asyncMethodInvoker: - return ExecuteAsync(asyncMethodInvoker, context, castedParams, command, _cancellationRepo, + return ExecuteAsync(asyncMethodInvoker, context, castedParams, command, _cancellationRepo!, representationModule, execContext); default: var result = Execute((invoker as MethodInvoker)!, context, castedParams!, command, execContext); @@ -121,6 +102,32 @@ namespace mROA.Implementation.Backend } } + private void ThrowIfNotInjected(IContextRepository contextRepository) + { + if (_cancellationRepo is null) + throw new NullReferenceException("Method repository was not defined"); + + if (_methodRepo is null) + throw new NullReferenceException("Method repository was not defined"); + + if (contextRepository is null) + throw new NullReferenceException("Context repository was not defined"); + } + + private FinalCommandExecution CancelExecution(ICallRequest command) + { + var cts = _cancellationRepo!.GetCancellation(command.Id); + if (cts == null) + throw new NullReferenceException("Can't find cancellation for this request"); + cts.Cancel(); + _cancellationRepo.FreeCancelation(command.Id); + + return new FinalCommandExecution + { + Id = command.Id + }; + } + private static ICommandExecution Execute(MethodInvoker invoker, object instance, object?[] parameter, ICallRequest command, RequestContext executionContext) { diff --git a/mROA/Implementation/Frontend/RequestExtractor.cs b/mROA/Implementation/Frontend/RequestExtractor.cs index baf6be3..83aecb1 100644 --- a/mROA/Implementation/Frontend/RequestExtractor.cs +++ b/mROA/Implementation/Frontend/RequestExtractor.cs @@ -48,17 +48,7 @@ namespace mROA.Implementation.Frontend { return Task.Run(() => { - if (_serializationToolkit == null) - throw new NullReferenceException("Serializing toolkit is null."); - if (_executeModule == null) - throw new NullReferenceException("Execute module is null."); - if (_realContextRepository == null) - throw new NullReferenceException("Context repository is null."); - if (_representationModule == null) - throw new NullReferenceException("Representation module is null."); - if (_methodRepository == null) - throw new NullReferenceException("Method repository is null."); - + ThrowIfNotInjected(); var multiClientOwnershipRepository = TransmissionConfig.OwnershipRepository as MultiClientOwnershipRepository; multiClientOwnershipRepository?.RegisterOwnership(_representationModule.Id); @@ -99,31 +89,15 @@ namespace mROA.Implementation.Frontend #if TRACE Console.WriteLine("Cancelling request"); #endif - var req = cancelRequest.Result; - tokenSource.Cancel(); - _executeModule.Execute(req, _realContextRepository, _representationModule); + HandleCancelRequest(tokenSource, cancelRequest.Result); } else if (defaultRequest.IsCompleted) { - tokenSource.Cancel(); - var request = defaultRequest.Result; - - var result = _executeModule.Execute(request, _realContextRepository, _representationModule); - - var resultType = result switch - { - FinalCommandExecution => MessageType.FinishedCommandExecution, - AsyncCommandExecution => MessageType.AsyncCommandExecution, - ExceptionCommandExecution => MessageType.ExceptionCommandExecution, - _ => MessageType.Unknown - }; - _representationModule.PostCallMessage(request.Id, resultType, result, result.GetType()); + HandleCallRequest(tokenSource, defaultRequest.Result); } else { - tokenSource.Cancel(); - var request = eventRequest.Result; - _executeModule.Execute(request, _remoteContextRepository!, _representationModule); + HandleEventRequest(tokenSource, eventRequest.Result); } } } @@ -133,5 +107,48 @@ namespace mROA.Implementation.Frontend } }); } + + private void ThrowIfNotInjected() + { + if (_serializationToolkit == null) + throw new NullReferenceException("Serializing toolkit is null."); + if (_executeModule == null) + throw new NullReferenceException("Execute module is null."); + if (_realContextRepository == null) + throw new NullReferenceException("Context repository is null."); + if (_representationModule == null) + throw new NullReferenceException("Representation module is null."); + if (_methodRepository == null) + throw new NullReferenceException("Method repository is null."); + } + + private void HandleCancelRequest(CancellationTokenSource tokenSource, CancelRequest req) + { + tokenSource.Cancel(); + _executeModule!.Execute(req, _realContextRepository!, _representationModule!); + } + + private void HandleCallRequest(CancellationTokenSource tokenSource, DefaultCallRequest request) + { + tokenSource.Cancel(); + + var result = _executeModule!.Execute(request, _realContextRepository!, _representationModule!); + + var resultType = result switch + { + FinalCommandExecution => MessageType.FinishedCommandExecution, + AsyncCommandExecution => MessageType.AsyncCommandExecution, + ExceptionCommandExecution => MessageType.ExceptionCommandExecution, + _ => MessageType.Unknown + }; + + _representationModule!.PostCallMessage(request.Id, resultType, result, result.GetType()); + } + + private void HandleEventRequest(CancellationTokenSource tokenSource, DefaultCallRequest request) + { + tokenSource.Cancel(); + _executeModule!.Execute(request, _remoteContextRepository!, _representationModule!); + } } } \ No newline at end of file