Рефакторинг модуля выполнения и экстрактора запросов

This commit is contained in:
2025-03-13 20:30:02 +03:00
parent 40a7f86dcf
commit 2dbfdce254
2 changed files with 79 additions and 55 deletions
@@ -36,38 +36,19 @@ namespace mROA.Implementation.Backend
try try
{ {
if (_cancellationRepo is null) ThrowIfNotInjected(contextRepository);
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");
if (command is CancelRequest) if (command is CancelRequest)
{ {
#if TRACE #if TRACE
Console.WriteLine("Final cancelling request"); Console.WriteLine("Final cancelling request");
#endif #endif
var cts = _cancellationRepo.GetCancellation(command.Id); CancelExecution(command);
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
};
} }
var invoker = _methodRepo.GetMethod(command.CommandId); var invoker = _methodRepo!.GetMethod(command.CommandId);
if (invoker == null) if (invoker == null)
throw new Exception($"Command {command.CommandId} not found"); throw new Exception($"Command {command.CommandId} not found");
IContextRepository repository;
var context = command.ObjectId.ContextId != -1 var context = command.ObjectId.ContextId != -1
? contextRepository.GetObject<object>(command.ObjectId) ? contextRepository.GetObject<object>(command.ObjectId)
: contextRepository.GetSingleObject(invoker.SuitableType, command.ObjectId.OwnerId); : contextRepository.GetSingleObject(invoker.SuitableType, command.ObjectId.OwnerId);
@@ -83,7 +64,7 @@ namespace mROA.Implementation.Backend
castedParams = new object[invoker.ParameterTypes.Length]; castedParams = new object[invoker.ParameterTypes.Length];
for (var i = 0; i < castedParams.Length; i++) 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: case AsyncMethodInvoker { IsVoid: false } asyncNonVoidMethodInvoker:
return TypedExecuteAsync(asyncNonVoidMethodInvoker, context, castedParams, command, return TypedExecuteAsync(asyncNonVoidMethodInvoker, context, castedParams, command,
_cancellationRepo, _cancellationRepo!,
representationModule, execContext); representationModule, execContext);
case AsyncMethodInvoker asyncMethodInvoker: case AsyncMethodInvoker asyncMethodInvoker:
return ExecuteAsync(asyncMethodInvoker, context, castedParams, command, _cancellationRepo, return ExecuteAsync(asyncMethodInvoker, context, castedParams, command, _cancellationRepo!,
representationModule, execContext); representationModule, execContext);
default: default:
var result = Execute((invoker as MethodInvoker)!, context, castedParams!, command, execContext); 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, private static ICommandExecution Execute(MethodInvoker invoker, object instance, object?[] parameter,
ICallRequest command, RequestContext executionContext) ICallRequest command, RequestContext executionContext)
{ {
@@ -48,17 +48,7 @@ namespace mROA.Implementation.Frontend
{ {
return Task.Run(() => return Task.Run(() =>
{ {
if (_serializationToolkit == null) ThrowIfNotInjected();
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.");
var multiClientOwnershipRepository = var multiClientOwnershipRepository =
TransmissionConfig.OwnershipRepository as MultiClientOwnershipRepository; TransmissionConfig.OwnershipRepository as MultiClientOwnershipRepository;
multiClientOwnershipRepository?.RegisterOwnership(_representationModule.Id); multiClientOwnershipRepository?.RegisterOwnership(_representationModule.Id);
@@ -99,31 +89,15 @@ namespace mROA.Implementation.Frontend
#if TRACE #if TRACE
Console.WriteLine("Cancelling request"); Console.WriteLine("Cancelling request");
#endif #endif
var req = cancelRequest.Result; HandleCancelRequest(tokenSource, cancelRequest.Result);
tokenSource.Cancel();
_executeModule.Execute(req, _realContextRepository, _representationModule);
} }
else if (defaultRequest.IsCompleted) else if (defaultRequest.IsCompleted)
{ {
tokenSource.Cancel(); HandleCallRequest(tokenSource, defaultRequest.Result);
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());
} }
else else
{ {
tokenSource.Cancel(); HandleEventRequest(tokenSource, eventRequest.Result);
var request = eventRequest.Result;
_executeModule.Execute(request, _remoteContextRepository!, _representationModule);
} }
} }
} }
@@ -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!);
}
} }
} }