Прокидывание контекста конечной точки в регистрацию объекта

This commit is contained in:
2025-03-10 11:19:08 +03:00
parent b1424fbe32
commit ce14258bf7
6 changed files with 76 additions and 73 deletions
+2 -2
View File
@@ -5,11 +5,11 @@ namespace mROA.Abstract
{ {
public interface IContextRepository : IInjectableModule public interface IContextRepository : IInjectableModule
{ {
int ResisterObject(object o); int ResisterObject(object o, IEndPointContext context);
void ClearObject(int id); void ClearObject(int id);
T GetObjectBySharedObject<T>(SharedObjectShellShell<T> sharedObjectShellShell); T GetObjectBySharedObject<T>(SharedObjectShellShell<T> sharedObjectShellShell);
T? GetObject<T>(int id); T? GetObject<T>(int id);
object GetSingleObject(Type type); object GetSingleObject(Type type);
int GetObjectIndex(object o); int GetObjectIndex(object o, IEndPointContext context);
} }
} }
@@ -39,7 +39,7 @@ namespace mROA.Implementation.Backend
Activator.CreateInstance); Activator.CreateInstance);
} }
public int ResisterObject(object o) public int ResisterObject(object o, IEndPointContext context)
{ {
if (!_lastIndexFinder.IsCompleted) if (!_lastIndexFinder.IsCompleted)
_lastIndexFinder.Wait(); _lastIndexFinder.Wait();
@@ -79,10 +79,10 @@ namespace mROA.Implementation.Backend
return _singletons.GetValueOrDefault(type.GetHashCode()) ?? throw new ArgumentException("Unregistered singleton type"); return _singletons.GetValueOrDefault(type.GetHashCode()) ?? throw new ArgumentException("Unregistered singleton type");
} }
public int GetObjectIndex(object o) public int GetObjectIndex(object o, IEndPointContext context)
{ {
var index = Array.IndexOf(_storage, o); var index = Array.IndexOf(_storage, o);
return index == -1 ? ResisterObject(o) : index; return index == -1 ? ResisterObject(o, context) : index;
} }
private int FindLastIndex() private int FindLastIndex()
@@ -27,10 +27,10 @@ namespace mROA.Implementation.Backend
{ {
} }
public int ResisterObject(object o) public int ResisterObject(object o, IEndPointContext context)
{ {
var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()); var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId());
return repository.ResisterObject(o); return repository.ResisterObject(o, context);
} }
public void ClearObject(int id) public void ClearObject(int id)
@@ -63,10 +63,10 @@ namespace mROA.Implementation.Backend
return repository.GetSingleObject(type); return repository.GetSingleObject(type);
} }
public int GetObjectIndex(object o) public int GetObjectIndex(object o, IEndPointContext context)
{ {
var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId()); var repository = GetRepositoryByClientId(TransmissionConfig.OwnershipRepository.GetOwnershipId());
return repository.GetObjectIndex(o); return repository.GetObjectIndex(o, context);
} }
public IContextRepository GetRepository(int clientId) public IContextRepository GetRepository(int clientId)
@@ -39,84 +39,87 @@ namespace mROA.Implementation.Frontend
} }
} }
public async Task StartExtraction() public Task StartExtraction()
{ {
if (_serializationToolkit == null) return Task.Run(() =>
throw new NullReferenceException("Serializing toolkit is null.");
if (_executeModule == null)
throw new NullReferenceException("Execute module is null.");
if (_contextRepository == 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.");
await Task.Yield();
var multiClientOwnershipRepository =
TransmissionConfig.OwnershipRepository as MultiClientOwnershipRepository;
multiClientOwnershipRepository?.RegisterOwnership(_representationModule.Id);
try
{ {
while (true) if (_serializationToolkit == null)
throw new NullReferenceException("Serializing toolkit is null.");
if (_executeModule == null)
throw new NullReferenceException("Execute module is null.");
if (_contextRepository == 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.");
// await Task.Yield();
var multiClientOwnershipRepository =
TransmissionConfig.OwnershipRepository as MultiClientOwnershipRepository;
multiClientOwnershipRepository?.RegisterOwnership(_representationModule.Id);
try
{ {
while (true)
{
#if TRACE #if TRACE
Console.WriteLine("Waiting for request..."); Console.WriteLine("Waiting for request...");
#endif #endif
var tokenSource = new CancellationTokenSource(); var tokenSource = new CancellationTokenSource();
var token = tokenSource.Token; var token = tokenSource.Token;
var defaultRequest = var defaultRequest =
_representationModule!.GetMessageAsync<DefaultCallRequest>( _representationModule!.GetMessageAsync<DefaultCallRequest>(
messageType: MessageType.CallRequest, token: token); messageType: MessageType.CallRequest, token: token);
var cancelRequest = var cancelRequest =
_representationModule!.GetMessageAsync<CancelRequest>( _representationModule!.GetMessageAsync<CancelRequest>(
messageType: MessageType.CancelRequest, token: token); messageType: MessageType.CancelRequest, token: token);
Task.WaitAny(defaultRequest, cancelRequest); Task.WaitAny(defaultRequest, cancelRequest);
#if TRACE #if TRACE
Console.WriteLine("Request received"); Console.WriteLine("Request received");
#endif #endif
if (cancelRequest.IsCompleted) if (cancelRequest.IsCompleted)
{ {
#if TRACE #if TRACE
Console.WriteLine("Cancelling request"); Console.WriteLine("Cancelling request");
#endif #endif
var req = cancelRequest.Result; var req = cancelRequest.Result;
tokenSource.Cancel(); tokenSource.Cancel();
_executeModule.Execute(req, _contextRepository, _representationModule); _executeModule.Execute(req, _contextRepository, _representationModule);
}
else
{
tokenSource.Cancel();
var request = defaultRequest.Result;
var result = _executeModule.Execute(request, _contextRepository, _representationModule);
var resultType = MessageType.Unknown;
switch (result)
{
case FinalCommandExecution:
resultType = MessageType.FinishedCommandExecution;
break;
case AsyncCommandExecution:
resultType = MessageType.AsyncCommandExecution;
break;
case ExceptionCommandExecution:
resultType = MessageType.ExceptionCommandExecution;
break;
} }
else
{
tokenSource.Cancel();
var request = defaultRequest.Result;
_representationModule.PostCallMessage(request.Id, resultType, result, result.GetType()); var result = _executeModule.Execute(request, _contextRepository, _representationModule);
var resultType = MessageType.Unknown;
switch (result)
{
case FinalCommandExecution:
resultType = MessageType.FinishedCommandExecution;
break;
case AsyncCommandExecution:
resultType = MessageType.AsyncCommandExecution;
break;
case ExceptionCommandExecution:
resultType = MessageType.ExceptionCommandExecution;
break;
}
_representationModule.PostCallMessage(request.Id, resultType, result, result.GetType());
}
} }
} }
} catch
catch {
{ multiClientOwnershipRepository?.FreeOwnership();
multiClientOwnershipRepository?.FreeOwnership(); }
} });
} }
} }
} }
@@ -9,7 +9,7 @@ namespace mROA.Implementation
private IRepresentationModuleProducer? _representationProducer; private IRepresentationModuleProducer? _representationProducer;
public static Dictionary<Type, Type> RemoteTypes = new(); public static Dictionary<Type, Type> RemoteTypes = new();
public int ResisterObject(object o) public int ResisterObject(object o, IEndPointContext context)
{ {
throw new NotSupportedException(); throw new NotSupportedException();
} }
@@ -58,7 +58,7 @@ namespace mROA.Implementation
representationModule)!; representationModule)!;
} }
public int GetObjectIndex(object o) public int GetObjectIndex(object o, IEndPointContext context)
{ {
if (o is RemoteObjectBase remote) if (o is RemoteObjectBase remote)
{ {
+1 -1
View File
@@ -49,7 +49,7 @@ namespace mROA.Implementation
else else
{ {
_identifier.OwnerId = EndPointContext.HostId; _identifier.OwnerId = EndPointContext.HostId;
_identifier.ContextId = EndPointContext.RealRepository.GetObjectIndex(Value); _identifier.ContextId = EndPointContext.RealRepository.GetObjectIndex(Value, EndPointContext);
} }
} }
} }