тесты написаны, передача по сети работает, объекты правильно сериазилизуются
This commit is contained in:
@@ -0,0 +1,88 @@
|
||||
using System.Net;
|
||||
using System.Net.Sockets;
|
||||
using System.Reflection;
|
||||
using mROA.Implementation;
|
||||
|
||||
namespace mROA.Test;
|
||||
|
||||
public class FrontendFinalTest
|
||||
{
|
||||
private StreamBasedInteractionModule _interactionModule;
|
||||
private StreamBasedFrontendInteractionModule _frontendInteractionModule;
|
||||
private JsonFrontendSerialisationModule _frontendSerialisationModule;
|
||||
private ISerialisationModule _serialisationModule;
|
||||
private IExecuteModule _executeModule;
|
||||
private IMethodRepository _methodRepository;
|
||||
private IContextRepository _contextRepository;
|
||||
bool isTestNotFinished = true;
|
||||
private IContextRepository _frontendContextRepository;
|
||||
|
||||
[SetUp]
|
||||
public void Setup()
|
||||
{
|
||||
var repo = new MethodRepository();
|
||||
repo.CollectForAssembly(Assembly.GetExecutingAssembly());
|
||||
_methodRepository = repo;
|
||||
var repo2 = new ContextRepository();
|
||||
repo2.FillSingletons(Assembly.GetExecutingAssembly());
|
||||
_contextRepository = repo2;
|
||||
_interactionModule = new StreamBasedInteractionModule();
|
||||
|
||||
_serialisationModule = new JsonSerialisationModule(_interactionModule, _methodRepository);
|
||||
|
||||
_executeModule = new LaunchReadyExecutionModule(_methodRepository, _serialisationModule, _contextRepository);
|
||||
TransmissionConfig.BackendRepository = _contextRepository;
|
||||
|
||||
_frontendInteractionModule = new StreamBasedFrontendInteractionModule();
|
||||
_frontendSerialisationModule = new JsonFrontendSerialisationModule(_frontendInteractionModule);
|
||||
_frontendContextRepository = new FrontendContextRepository(new Dictionary<Type, Type> {
|
||||
{ typeof(ITestController), typeof(TestControllerRemoteEndpoint) },
|
||||
{typeof(ITestParameter), typeof(TestParameterRemoteEndpoint)}
|
||||
}, _frontendSerialisationModule);
|
||||
TransmissionConfig.FrontendRepository = _frontendContextRepository;
|
||||
TransmissionConfig.SetupBackendRepository();
|
||||
|
||||
Task.Run(() =>
|
||||
{
|
||||
TcpListener listener = new TcpListener(IPAddress.Loopback, 4567);
|
||||
listener.Start();
|
||||
|
||||
var stream = listener.AcceptTcpClient().GetStream();
|
||||
|
||||
Console.WriteLine("Client connected");
|
||||
|
||||
_interactionModule.RegisterClient(stream);
|
||||
|
||||
while (isTestNotFinished) ;
|
||||
});
|
||||
|
||||
var tcpClient = new TcpClient();
|
||||
tcpClient.Connect(IPAddress.Loopback, 4567);
|
||||
_frontendInteractionModule.ServerStream = tcpClient.GetStream();
|
||||
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void CallTest()
|
||||
{
|
||||
|
||||
|
||||
var singleton = _frontendContextRepository.GetSingleObject(typeof(ITestController)) as ITestController;
|
||||
|
||||
var x = singleton.B();
|
||||
Console.WriteLine(x);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TransmittionTest()
|
||||
{
|
||||
var singleton = _frontendContextRepository.GetSingleObject(typeof(ITestController)) as ITestController;
|
||||
|
||||
var next = singleton.SharedObjectTransmitionTest().Value;
|
||||
var parameter = singleton.GetTestParameter().Value;
|
||||
var x = next.Parametrized(new TestParameter { A = 100, LinkedObject = new (parameter!)});
|
||||
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
using mROA.Implementation;
|
||||
using System.Text.Json;
|
||||
using mROA.Implementation;
|
||||
|
||||
namespace mROA.Test;
|
||||
|
||||
@@ -47,7 +48,7 @@ public class TestController : ITestController
|
||||
|
||||
public int Parametrized(TestParameter parameter)
|
||||
{
|
||||
return parameter.A + parameter.LinkedObject.Value.Test;
|
||||
return parameter.A + parameter.LinkedObject.Value.Test();
|
||||
}
|
||||
|
||||
public TransmittedSharedObject<ITestParameter> GetTestParameter()
|
||||
@@ -59,12 +60,12 @@ public class TestController : ITestController
|
||||
[SharedObjectInterafce]
|
||||
public interface ITestParameter
|
||||
{
|
||||
public int Test { get; }
|
||||
public int Test();
|
||||
}
|
||||
|
||||
public class TestParameterInstance : ITestParameter
|
||||
{
|
||||
public int Test => 10;
|
||||
public int Test() => 10;
|
||||
}
|
||||
|
||||
public class TransmissionTestController : ITestController
|
||||
@@ -102,7 +103,7 @@ public class TransmissionTestController : ITestController
|
||||
|
||||
public int Parametrized(TestParameter parameter)
|
||||
{
|
||||
return parameter.A * parameter.LinkedObject.Value.Test;
|
||||
return parameter.A * parameter.LinkedObject.Value.Test();
|
||||
}
|
||||
|
||||
public TransmittedSharedObject<ITestParameter> GetTestParameter()
|
||||
@@ -117,9 +118,10 @@ public class TestParameter
|
||||
public TransmittedSharedObject<ITestParameter> LinkedObject { get; set; }
|
||||
}
|
||||
|
||||
public class TestControllerRemoteEndoint(int id, ISerialisationModule.IFrontendSerialisationModule serialisationModule)
|
||||
public class TestControllerRemoteEndpoint(int id, ISerialisationModule.IFrontendSerialisationModule serialisationModule)
|
||||
: ITestController, IRemoteObject
|
||||
{
|
||||
|
||||
public void A()
|
||||
{
|
||||
serialisationModule.PostCallRequest(new JsonCallRequest { CommandId = 0, ObjectId = id });
|
||||
@@ -134,46 +136,80 @@ public class TestControllerRemoteEndoint(int id, ISerialisationModule.IFrontendS
|
||||
|
||||
public int B()
|
||||
{
|
||||
TransmissionConfig.SetupBackendRepository();
|
||||
var request = new JsonCallRequest { CommandId = 2, ObjectId = id };
|
||||
serialisationModule.PostCallRequest(request);
|
||||
var response = serialisationModule.GetNextCommandExecution<FinalCommandExecution>(request.CallRequestId)
|
||||
.GetAwaiter().GetResult();
|
||||
return (int)response.Result!;
|
||||
TransmissionConfig.SetupFrontendRepository();
|
||||
var convertation = response.Result! is JsonElement e ? e.Deserialize<int>() : (int)response.Result!;
|
||||
return convertation;
|
||||
}
|
||||
|
||||
public async Task<int> BAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
TransmissionConfig.SetupBackendRepository();
|
||||
var request = new JsonCallRequest { CommandId = 3, ObjectId = id };
|
||||
serialisationModule.PostCallRequest(request);
|
||||
var response = await serialisationModule.GetNextCommandExecution<FinalCommandExecution>(request.CallRequestId);
|
||||
return (int)response.Result!;
|
||||
TransmissionConfig.SetupFrontendRepository();
|
||||
var convertation = response.Result! is JsonElement e ? e.Deserialize<int>() : (int)response.Result!;
|
||||
return convertation;
|
||||
}
|
||||
|
||||
public TransmittedSharedObject<ITestController> SharedObjectTransmitionTest()
|
||||
{
|
||||
TransmissionConfig.SetupBackendRepository();
|
||||
|
||||
var request = new JsonCallRequest { CommandId = 4, ObjectId = id };
|
||||
serialisationModule.PostCallRequest(request);
|
||||
var response = serialisationModule.GetNextCommandExecution<FinalCommandExecution>(request.CallRequestId)
|
||||
.GetAwaiter().GetResult();
|
||||
return (TransmittedSharedObject<ITestController>)response.Result!;
|
||||
TransmissionConfig.SetupFrontendRepository();
|
||||
var convertation = response.Result! is JsonElement e ? e.Deserialize<TransmittedSharedObject<ITestController>>()! : (TransmittedSharedObject<ITestController>)response.Result!;
|
||||
return convertation;
|
||||
}
|
||||
|
||||
public int Parametrized(TestParameter parameter)
|
||||
{
|
||||
var request = new JsonCallRequest { CommandId = 5, ObjectId = id, Parameter = parameter };
|
||||
TransmissionConfig.SetupBackendRepository();
|
||||
|
||||
serialisationModule.PostCallRequest(request);
|
||||
var response = serialisationModule.GetNextCommandExecution<FinalCommandExecution>(request.CallRequestId)
|
||||
.GetAwaiter().GetResult();
|
||||
return (int)response.Result!;
|
||||
|
||||
TransmissionConfig.SetupFrontendRepository();
|
||||
var convertation = response.Result! is JsonElement e ? e.Deserialize<int>() : (int)response.Result!;
|
||||
return convertation;
|
||||
}
|
||||
|
||||
public TransmittedSharedObject<ITestParameter> GetTestParameter()
|
||||
{
|
||||
TransmissionConfig.SetupBackendRepository();
|
||||
var request = new JsonCallRequest { CommandId = 6, ObjectId = id };
|
||||
serialisationModule.PostCallRequest(request);
|
||||
var response = serialisationModule.GetNextCommandExecution<FinalCommandExecution>(request.CallRequestId)
|
||||
.GetAwaiter().GetResult();
|
||||
return (TransmittedSharedObject<ITestParameter>)response.Result!;
|
||||
TransmissionConfig.SetupFrontendRepository();
|
||||
var convertation = response.Result! is JsonElement e ? e.Deserialize<TransmittedSharedObject<ITestParameter>>() : (TransmittedSharedObject<ITestParameter>)response.Result!;
|
||||
return convertation;
|
||||
}
|
||||
|
||||
public int Id => id;
|
||||
}
|
||||
|
||||
public class TestParameterRemoteEndpoint(int id, ISerialisationModule.IFrontendSerialisationModule serialisationModule) : ITestParameter, IRemoteObject{
|
||||
public int Test()
|
||||
{
|
||||
var request = new JsonCallRequest { CommandId = 7, ObjectId = id };
|
||||
serialisationModule.PostCallRequest(request);
|
||||
var response = serialisationModule.GetNextCommandExecution<FinalCommandExecution>(request.CallRequestId)
|
||||
.GetAwaiter().GetResult();
|
||||
TransmissionConfig.SetupFrontendRepository();
|
||||
var convertation = response.Result! is JsonElement e ? e.Deserialize<int>() : (int)response.Result!;
|
||||
TransmissionConfig.SetupBackendRepository();
|
||||
return convertation;
|
||||
}
|
||||
|
||||
public int Id => id;
|
||||
|
||||
@@ -5,12 +5,12 @@
|
||||
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
|
||||
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
|
||||
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
|
||||
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\Mimm\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages</NuGetPackageFolders>
|
||||
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\Mikhail\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages</NuGetPackageFolders>
|
||||
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
|
||||
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.12.0</NuGetToolVersion>
|
||||
</PropertyGroup>
|
||||
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
||||
<SourceRoot Include="C:\Users\Mimm\.nuget\packages\" />
|
||||
<SourceRoot Include="C:\Users\Mikhail\.nuget\packages\" />
|
||||
<SourceRoot Include="C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages\" />
|
||||
</ItemGroup>
|
||||
<ImportGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
||||
@@ -21,6 +21,6 @@
|
||||
<Import Project="$(NuGetPackageRoot)microsoft.net.test.sdk\17.12.0\build\netcoreapp3.1\Microsoft.NET.Test.Sdk.props" Condition="Exists('$(NuGetPackageRoot)microsoft.net.test.sdk\17.12.0\build\netcoreapp3.1\Microsoft.NET.Test.Sdk.props')" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
||||
<PkgNUnit_Analyzers Condition=" '$(PkgNUnit_Analyzers)' == '' ">C:\Users\Mimm\.nuget\packages\nunit.analyzers\4.6.0</PkgNUnit_Analyzers>
|
||||
<PkgNUnit_Analyzers Condition=" '$(PkgNUnit_Analyzers)' == '' ">C:\Users\Mikhail\.nuget\packages\nunit.analyzers\4.6.0</PkgNUnit_Analyzers>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace mROA.Implementation;
|
||||
|
||||
public class FrontendContextRepository(Dictionary<Type, Type> remoteTypes, ISerialisationModule serialisationModule) : IContextRepository
|
||||
public class FrontendContextRepository(Dictionary<Type, Type> remoteTypes, ISerialisationModule.IFrontendSerialisationModule serialisationModule) : IContextRepository
|
||||
{
|
||||
private FrozenDictionary<Type, Type> _remoteTypes = remoteTypes.ToFrozenDictionary();
|
||||
|
||||
@@ -33,7 +33,7 @@ public class FrontendContextRepository(Dictionary<Type, Type> remoteTypes, ISeri
|
||||
|
||||
public object GetSingleObject(Type type)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
return Activator.CreateInstance(remoteTypes[type], -1, serialisationModule)!;
|
||||
}
|
||||
|
||||
public int GetObjectIndex(object o)
|
||||
|
||||
@@ -5,6 +5,17 @@ namespace mROA.Implementation;
|
||||
public static class TransmissionConfig
|
||||
{
|
||||
public static IContextRepository DefaultContextRepository { get; set; }
|
||||
public static IContextRepository BackendRepository { get; set; }
|
||||
public static IContextRepository FrontendRepository { get; set; }
|
||||
|
||||
public static void SetupBackendRepository()
|
||||
{
|
||||
DefaultContextRepository = BackendRepository;
|
||||
}
|
||||
public static void SetupFrontendRepository()
|
||||
{
|
||||
DefaultContextRepository = FrontendRepository;
|
||||
}
|
||||
}
|
||||
|
||||
public class TransmittedSharedObject<T>
|
||||
@@ -17,9 +28,16 @@ public class TransmittedSharedObject<T>
|
||||
[JsonIgnore]
|
||||
public IContextRepository Context { get; set; }
|
||||
|
||||
public TransmittedSharedObject()
|
||||
{
|
||||
}
|
||||
public TransmittedSharedObject(T value)
|
||||
{
|
||||
ContextId = TransmissionConfig.DefaultContextRepository.GetObjectIndex(value);
|
||||
}
|
||||
|
||||
public static implicit operator T(TransmittedSharedObject<T> value) => value.Value!;
|
||||
|
||||
public static implicit operator TransmittedSharedObject<T>(T value) =>
|
||||
new() { ContextId = TransmissionConfig.DefaultContextRepository.GetObjectIndex(value) };
|
||||
|
||||
}
|
||||
@@ -5,12 +5,12 @@
|
||||
<RestoreTool Condition=" '$(RestoreTool)' == '' ">NuGet</RestoreTool>
|
||||
<ProjectAssetsFile Condition=" '$(ProjectAssetsFile)' == '' ">$(MSBuildThisFileDirectory)project.assets.json</ProjectAssetsFile>
|
||||
<NuGetPackageRoot Condition=" '$(NuGetPackageRoot)' == '' ">$(UserProfile)\.nuget\packages\</NuGetPackageRoot>
|
||||
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\Mimm\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages</NuGetPackageFolders>
|
||||
<NuGetPackageFolders Condition=" '$(NuGetPackageFolders)' == '' ">C:\Users\Mikhail\.nuget\packages\;C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages</NuGetPackageFolders>
|
||||
<NuGetProjectStyle Condition=" '$(NuGetProjectStyle)' == '' ">PackageReference</NuGetProjectStyle>
|
||||
<NuGetToolVersion Condition=" '$(NuGetToolVersion)' == '' ">6.12.0</NuGetToolVersion>
|
||||
</PropertyGroup>
|
||||
<ItemGroup Condition=" '$(ExcludeRestorePackageImports)' != 'true' ">
|
||||
<SourceRoot Include="C:\Users\Mimm\.nuget\packages\" />
|
||||
<SourceRoot Include="C:\Users\Mikhail\.nuget\packages\" />
|
||||
<SourceRoot Include="C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages\" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user