реализован производитель модулей сериализации

This commit is contained in:
2025-02-14 20:24:04 +03:00
parent c370142e7a
commit b4d4837f9d
5 changed files with 56 additions and 4 deletions
+2 -1
View File
@@ -5,6 +5,7 @@ using mROA.Codegen;
using mROA.Implementation;
using mROA.Implementation.Backend;
using mROA.Implementation.Bootstrap;
using mROA.Implementation.Frontend;
var builder = new FullMixBuilder();
@@ -14,7 +15,7 @@ builder.UseStreamInteraction();
builder.UseBasicExecution();
builder.UseCollectableContextRepository(typeof(PrinterFactory).Assembly);
builder.SetupMethodsRepository(new CoCodegenMethodRepository());
builder.Modules.Add(new StaticSerialisationModuleProducer());
builder.Modules.Add(new CreativeSerializationModuleProducer([], typeof(JsonFrontendSerialisationModule)));
builder.Build();
+1
View File
@@ -4,6 +4,7 @@ public interface IInteractionModule : IInjectableModule
{
void SendTo(int clientId, byte[] message);
void RegisterSource(Stream stream);
Stream GetSource(int clientId);
public interface IFrontendInteractionModule : IInjectableModule
{
int ClientId { get; }
@@ -16,6 +16,11 @@ public class StreamBasedInteractionModule : IInteractionModule
_serialisationModule.SendWelcomeMessage(id);
}
public Stream GetSource(int clientId)
{
return _streams.GetValueOrDefault(clientId, Stream.Null);
}
public void SendTo(int clientId, byte[] message)
{
if (!_streams.TryGetValue(clientId, out var stream))
@@ -0,0 +1,46 @@
using mROA.Abstract;
using mROA.Implementation.Backend;
using mROA.Implementation.Frontend;
namespace mROA.Implementation;
public class CreativeSerializationModuleProducer : ISerialisationModuleProducer
{
private Type _serializationModuleType;
private IInjectableModule[] _creationModules;
private StreamBasedInteractionModule? _interactionModule;
public CreativeSerializationModuleProducer(IInjectableModule[] creationModules, Type serializationModuleType)
{
_creationModules = creationModules;
_serializationModuleType = serializationModuleType;
}
public void Inject<T>(T dependency)
{
if (dependency is StreamBasedInteractionModule interactionModule)
_interactionModule = interactionModule;
}
public ISerialisationModule.IFrontendSerialisationModule Produce(int ownership)
{
if (_interactionModule == null)
throw new NullReferenceException("Interaction module is null");
var produced =
Activator.CreateInstance(_serializationModuleType) as ISerialisationModule.IFrontendSerialisationModule ??
throw new Exception("Bad serialization module type");
foreach (var creationModule in _creationModules)
produced.Inject(creationModule);
var interactionModule = new StreamBasedFrontendInteractionModule
{
ClientId = ownership,
ServerStream = _interactionModule.GetSource(ownership)
};
produced.Inject(interactionModule);
return produced;
}
}
@@ -1,4 +1,3 @@
using System.Text;
using System.Text.Json;
using mROA.Abstract;
@@ -8,7 +7,6 @@ public class JsonFrontendSerialisationModule
: ISerialisationModule.IFrontendSerialisationModule
{
private IInteractionModule.IFrontendInteractionModule? _interactionModule;
public int ClientId => _interactionModule!.ClientId;
public async Task<T> GetNextCommandExecution<T>(Guid requestId) where T : ICommandExecution
@@ -62,8 +60,9 @@ public class JsonFrontendSerialisationModule
Data = post,
SchemaId = MessageType.CallRequest
}));
}
public void Inject<T>(T dependency)
{
if (dependency is IInteractionModule.IFrontendInteractionModule interactionModule)