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

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
{
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<object>(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)
{
@@ -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!);
}
}
}