pull/1/head
a.bozhenov 5 years ago
parent 12f9e7beb3
commit 4f721fdbc8

@ -1,6 +1,6 @@
using Microsoft.Win32; using Microsoft.Win32;
using System.Windows; using System.Windows;
using ZeroLevel.Services.Network.FileTransfer; using ZeroLevel.Network.FileTransfer;
namespace FileTransferClient namespace FileTransferClient
{ {

@ -1,6 +1,6 @@
using System.IO; using System.IO;
using System.Windows; using System.Windows;
using ZeroLevel.Services.Network.FileTransfer; using ZeroLevel.Network.FileTransfer;
namespace FileTransferServer namespace FileTransferServer
{ {
@ -39,7 +39,6 @@ namespace FileTransferServer
private void Button_Click_1(object sender, RoutedEventArgs e) private void Button_Click_1(object sender, RoutedEventArgs e)
{ {
_server?.Dispose();
_server = null; _server = null;
} }
} }

@ -22,15 +22,18 @@ namespace TestApp
AutoregisterInboxes(UseHost(8800)); AutoregisterInboxes(UseHost(8800));
ReadServiceInfo(); ReadServiceInfo();
UseHost(8801).RegisterInbox<ZeroServiceInfo>("metainfo", (c) => this.ServiceInfo); UseHost(8801).RegisterInbox<ZeroServiceInfo>("metainfo", (c) =>
{
Log.Info("Reqeust for metainfo");
return this.ServiceInfo;
});
StoreConnection("mytest", new IPEndPoint(IPAddress.Loopback, 8800)); StoreConnection("mytest", new IPEndPoint(IPAddress.Loopback, 8800));
StoreConnection("mymeta", new IPEndPoint(IPAddress.Loopback, 8801)); StoreConnection("mymeta", new IPEndPoint(IPAddress.Loopback, 8801));
var client = ConnectToService("mytest");
Sheduller.RemindEvery(TimeSpan.FromSeconds(5), () => Sheduller.RemindEvery(TimeSpan.FromSeconds(1), () =>
{ {
var client = ConnectToService("mytest");
client.Send("pum"); client.Send("pum");
client.Send<string>(BaseSocket.DEFAULT_MESSAGE_INBOX, "'This is message'"); client.Send<string>(BaseSocket.DEFAULT_MESSAGE_INBOX, "'This is message'");
client.Request<DateTime, string>("d2s", DateTime.Now, s => Log.Info($"Response: {s}")); client.Request<DateTime, string>("d2s", DateTime.Now, s => Log.Info($"Response: {s}"));
@ -41,9 +44,10 @@ namespace TestApp
client.Request<string>(BaseSocket.DEFAULT_REQUEST_WITHOUT_ARGS_INBOX, s => Log.Info($"Response ip: {s}")); client.Request<string>(BaseSocket.DEFAULT_REQUEST_WITHOUT_ARGS_INBOX, s => Log.Info($"Response ip: {s}"));
}); });
Sheduller.RemindEvery(TimeSpan.FromSeconds(15), () => Sheduller.RemindEvery(TimeSpan.FromSeconds(3), () =>
{ {
ConnectToService("mymeta").Request<ZeroServiceInfo>("metainfo", info => var client = ConnectToService("mymeta");
client.Request<ZeroServiceInfo>("metainfo", info =>
{ {
var si = new StringBuilder(); var si = new StringBuilder();
si.AppendLine(info.Name); si.AppendLine(info.Name);

@ -8,7 +8,7 @@ namespace ZeroLevel.Discovery
public sealed class DiscoveryService public sealed class DiscoveryService
: BaseZeroService : BaseZeroService
{ {
private IExService _exInbox; private IRouter _exInbox;
public DiscoveryService() public DiscoveryService()
: base("Discovery") : base("Discovery")
@ -18,20 +18,15 @@ namespace ZeroLevel.Discovery
protected override void StartAction() protected override void StartAction()
{ {
var routeTable = new RouteTable(); var routeTable = new RouteTable();
Injector.Default.Register<RouteTable>(routeTable); Injector.Default.Register<RouteTable>(routeTable);
var socketPort = Configuration.Default.First<int>("socketport"); var socketPort = Configuration.Default.First<int>("socketport");
_exInbox = ExchangeTransportFactory.GetServer(socketPort); _exInbox = UseHost(socketPort);
_exInbox.RegisterInbox<IEnumerable<ServiceEndpointsInfo>>("services", (_, __) => routeTable.Get()); _exInbox.RegisterInbox<IEnumerable<ServiceEndpointsInfo>>("services", (_, __) => routeTable.Get());
_exInbox.RegisterInbox<ExServiceInfo, InvokeResult>("register", (info, _, client) => routeTable.Append(info, client)); _exInbox.RegisterInbox<ZeroServiceInfo, InvokeResult>("register", (client, info) => routeTable.Append(info, client));
Log.Info($"TCP server started {_exInbox.Endpoint.Address}:{socketPort}");
} }
protected override void StopAction() protected override void StopAction()
{ {
_exInbox.Dispose();
} }
} }
} }

@ -149,7 +149,7 @@ namespace ZeroLevel.Discovery
Save(); Save();
} }
public InvokeResult Append(ExServiceInfo serviceInfo, ISocketClient client) public InvokeResult Append(ZeroServiceInfo serviceInfo, ISocketClient client)
{ {
InvokeResult result = null; InvokeResult result = null;
var endpoint = $"{client.Endpoint.Address}:{serviceInfo.Port}"; var endpoint = $"{client.Endpoint.Address}:{serviceInfo.Port}";

@ -2,7 +2,6 @@
using System.Linq; using System.Linq;
using Xunit; using Xunit;
using ZeroLevel.DocumentObjectModel; using ZeroLevel.DocumentObjectModel;
using ZeroLevel.Network;
using ZeroLevel.Services.Encryption; using ZeroLevel.Services.Encryption;
using ZeroLevel.Services.Serialization; using ZeroLevel.Services.Serialization;
using ZeroLevel.UnitTests.Models; using ZeroLevel.UnitTests.Models;
@ -47,35 +46,5 @@ namespace ZeroLevel.EncryptionUnitTests
Assert.True(obf_deviation >= deviation); Assert.True(obf_deviation >= deviation);
Assert.True(comparator(instance, clone)); Assert.True(comparator(instance, clone));
} }
[Fact]
public void NetworkStreamDataObfuscatorTest()
{
// Arrange
var comparator = new Func<Document, Document, bool>((left, right) =>
{
var l_bin = MessageSerializer.Serialize(left);
var r_bin = MessageSerializer.Serialize(right);
return ArrayExtensions.UnsafeEquals(l_bin, r_bin);
});
var instance = CompositeInstanceFactory.MakeDocument();
var data = MessageSerializer.Serialize(instance);
byte initial = 66;
// Act
var deviation = CalculateDeviation(data);
NetworkStreamFastObfuscator.HashData(data, initial);
var obf_deviation = CalculateDeviation(data);
NetworkStreamFastObfuscator.DeHashData(data, initial);
var deobf_deviation = CalculateDeviation(data);
var clone = MessageSerializer.Deserialize<Document>(data);
// Assert
Assert.Equal(deviation, deobf_deviation);
Assert.NotEqual(deviation, obf_deviation);
Assert.True(obf_deviation >= deviation);
Assert.True(comparator(instance, clone));
}
} }
} }

@ -2,17 +2,18 @@
using System.Net; using System.Net;
using System.Threading; using System.Threading;
using Xunit; using Xunit;
using ZeroLevel.Network; using ZeroLevel.Services.Applications;
namespace ZeroLevel.NetworkUnitTests namespace ZeroLevel.NetworkUnitTests
{ {
public class ExchangeTests public class ExchangeTests
: BaseZeroService
{ {
[Fact] [Fact]
public void HandleMessageTest() public void HandleMessageTest()
{ {
// Arrange // Arrange
var info = new ExServiceInfo var info = new ZeroServiceInfo
{ {
ServiceGroup = "MyServiceGroup", ServiceGroup = "MyServiceGroup",
ServiceKey = "MyServiceKey", ServiceKey = "MyServiceKey",
@ -20,18 +21,18 @@ namespace ZeroLevel.NetworkUnitTests
Version = "1.1.1.1" Version = "1.1.1.1"
}; };
var locker = new ManualResetEvent(false); var locker = new ManualResetEvent(false);
var server = ExchangeTransportFactory.GetServer(6666); var server = UseHost(6666);
ExServiceInfo received = null; ZeroServiceInfo received = null;
server.RegisterInbox<ExServiceInfo>("register", (i, _, __) => server.RegisterInbox<ZeroServiceInfo>("register", (c, i) =>
{ {
received = i; received = i;
locker.Set(); locker.Set();
}); });
// Act // Act
var client = ExchangeTransportFactory.GetClient(IPAddress.Loopback.ToString() + ":6666"); var client = ConnectToService(IPAddress.Loopback.ToString() + ":6666");
var ir = client.Send<ExServiceInfo>("register", info); var ir = client.Send<ZeroServiceInfo>("register", info);
locker.WaitOne(1000); locker.WaitOne(1000);
@ -42,21 +43,20 @@ namespace ZeroLevel.NetworkUnitTests
// Dispose // Dispose
locker.Dispose(); locker.Dispose();
client.Dispose(); client.Dispose();
server.Dispose();
} }
[Fact] [Fact]
public void RequestMessageTest() public void RequestMessageTest()
{ {
// Arrange // Arrange
var info1 = new ExServiceInfo var info1 = new ZeroServiceInfo
{ {
ServiceGroup = "MyServiceGroup", ServiceGroup = "MyServiceGroup",
ServiceKey = "MyServiceKey", ServiceKey = "MyServiceKey",
ServiceType = "MyServiceType", ServiceType = "MyServiceType",
Version = "1.1.1.1" Version = "1.1.1.1"
}; };
var info2 = new ExServiceInfo var info2 = new ZeroServiceInfo
{ {
ServiceGroup = "MyServiceGroup", ServiceGroup = "MyServiceGroup",
ServiceKey = "MyServiceKey2", ServiceKey = "MyServiceKey2",
@ -64,14 +64,14 @@ namespace ZeroLevel.NetworkUnitTests
Version = "1.1.0.1" Version = "1.1.0.1"
}; };
var locker = new ManualResetEvent(false); var locker = new ManualResetEvent(false);
var server = ExchangeTransportFactory.GetServer(6666); var server = UseHost(6667);
IEnumerable<ExServiceInfo> received = null; IEnumerable<ZeroServiceInfo> received = null;
server.RegisterInbox<IEnumerable<ExServiceInfo>>("services", (_, __) => new[] { info1, info2 }); server.RegisterInbox<IEnumerable<ZeroServiceInfo>>("services", (_) => new[] { info1, info2 });
// Act // Act
var client = ExchangeTransportFactory.GetClient(IPAddress.Loopback.ToString() + ":6666"); var client = ConnectToService(IPAddress.Loopback.ToString() + ":6667");
var ir = client.Request<IEnumerable<ExServiceInfo>>("services", response => var ir = client.Request<IEnumerable<ZeroServiceInfo>>("services", response =>
{ {
received = response; received = response;
locker.Set(); locker.Set();
@ -86,7 +86,14 @@ namespace ZeroLevel.NetworkUnitTests
// Dispose // Dispose
locker.Dispose(); locker.Dispose();
client.Dispose(); client.Dispose();
server.Dispose(); }
protected override void StartAction()
{
}
protected override void StopAction()
{
} }
} }
} }

@ -1,18 +1,19 @@
using System; using System;
using System.Threading; using System.Threading;
using Xunit; using Xunit;
using ZeroLevel.Network; using ZeroLevel.Services.Applications;
namespace ZeroLevel.UnitTests namespace ZeroLevel.UnitTests
{ {
public class NetworkTest public class NetworkTest
: BaseZeroService
{ {
[Fact] [Fact]
public void ClientServerTest() public void ClientServerTest()
{ {
// Arrange // Arrange
var server = ExchangeTransportFactory.GetServer(8181); var server = UseHost(8181);
var client = ExchangeTransportFactory.GetClient("127.0.0.1:8181"); var client = ConnectToService("127.0.0.1:8181");
bool got_message_no_request = false; bool got_message_no_request = false;
bool got_message_with_request = false; bool got_message_with_request = false;
@ -21,10 +22,10 @@ namespace ZeroLevel.UnitTests
using (var signal = new ManualResetEvent(false)) using (var signal = new ManualResetEvent(false))
{ {
server.RegisterInbox("empty", (_, __) => { signal.Set(); got_message_no_request = true; }); server.RegisterInbox("empty", (_) => { signal.Set(); got_message_no_request = true; });
server.RegisterInbox<string>((_, __, ___) => { signal.Set(); got_message_with_request = true; }); server.RegisterInbox<string>((_, ___) => { signal.Set(); got_message_with_request = true; });
server.RegisterInbox<string>("get_response", (_, __) => { return "Hello"; }); server.RegisterInbox<string>("get_response", (_) => "Hello");
server.RegisterInbox<int, string>("convert", (num, _, __) => { return num.ToString(); }); server.RegisterInbox<int, string>("convert", (__, num) => num.ToString());
// Act // Act
signal.Reset(); signal.Reset();
@ -52,5 +53,15 @@ namespace ZeroLevel.UnitTests
Assert.True(got_response_message_no_request, "No response without request"); Assert.True(got_response_message_no_request, "No response without request");
Assert.True(got_response_message_with_request, "No response with request"); Assert.True(got_response_message_with_request, "No response with request");
} }
protected override void StartAction()
{
throw new NotImplementedException();
}
protected override void StopAction()
{
throw new NotImplementedException();
}
} }
} }

@ -1,10 +1,6 @@
using System; using System;
using System.Linq;
using System.Linq.Expressions;
using System.Net; using System.Net;
using System.Reflection;
using ZeroLevel.Models; using ZeroLevel.Models;
using ZeroLevel.Services.Invokation;
using ZeroLevel.Services.Serialization; using ZeroLevel.Services.Serialization;
namespace ZeroLevel.Network namespace ZeroLevel.Network
@ -52,6 +48,20 @@ namespace ZeroLevel.Network
return InvokeResult.Succeeding(); return InvokeResult.Succeeding();
} }
public InvokeResult Send<T>(T message)
{
try
{
_client.Send(Frame.FromPool(BaseSocket.DEFAULT_MESSAGE_INBOX, MessageSerializer.SerializeCompatible<T>(message)));
}
catch (Exception ex)
{
Log.Error(ex, "[NetworkNode.Send]");
return InvokeResult.Fault(ex.Message);
}
return InvokeResult.Succeeding();
}
public InvokeResult Send<T>(string inbox, T message) public InvokeResult Send<T>(string inbox, T message)
{ {
try try
@ -108,6 +118,20 @@ namespace ZeroLevel.Network
return InvokeResult.Succeeding(); return InvokeResult.Succeeding();
} }
public InvokeResult Request<Tresponse>(Action<Tresponse> callback)
{
try
{
_client.Request(Frame.FromPool(BaseSocket.DEFAULT_REQUEST_INBOX), f => callback(MessageSerializer.DeserializeCompatible<Tresponse>(f)));
}
catch (Exception ex)
{
Log.Error(ex, "[NetworkNode.Request]");
return InvokeResult.Fault(ex.Message);
}
return InvokeResult.Succeeding();
}
public InvokeResult Request<Trequest, Tresponse>(string inbox, Trequest request, Action<Tresponse> callback) public InvokeResult Request<Trequest, Tresponse>(string inbox, Trequest request, Action<Tresponse> callback)
{ {
try try
@ -123,6 +147,21 @@ namespace ZeroLevel.Network
return InvokeResult.Succeeding(); return InvokeResult.Succeeding();
} }
public InvokeResult Request<Trequest, Tresponse>(Trequest request, Action<Tresponse> callback)
{
try
{
_client.Request(Frame.FromPool(BaseSocket.DEFAULT_REQUEST_WITHOUT_ARGS_INBOX, MessageSerializer.SerializeCompatible<Trequest>(request)),
f => callback(MessageSerializer.DeserializeCompatible<Tresponse>(f)));
}
catch (Exception ex)
{
Log.Error(ex, "[NetworkNode.Request]");
return InvokeResult.Fault(ex.Message);
}
return InvokeResult.Succeeding();
}
public void Dispose() public void Dispose()
{ {
_client.Dispose(); _client.Dispose();

@ -14,17 +14,13 @@ namespace ZeroLevel.Network.FileTransfer
private ObjectPool<FileTransferTask> _taskPool = new ObjectPool<FileTransferTask>(() => new FileTransferTask(), 100); private ObjectPool<FileTransferTask> _taskPool = new ObjectPool<FileTransferTask>(() => new FileTransferTask(), 100);
private BlockingCollection<FileTransferTask> _tasks = new BlockingCollection<FileTransferTask>(); private BlockingCollection<FileTransferTask> _tasks = new BlockingCollection<FileTransferTask>();
private readonly Thread _uploadFileThread; private readonly Thread _uploadFileThread;
/*private int _maxParallelFileTransfer;
private int _currentFileTransfers;*/
internal BaseFileTransfer(string baseFolder/*, int maxParallelFileTransfer = 6*/) internal BaseFileTransfer(string baseFolder)
{ {
_receiver = new FileReceiver(baseFolder); _receiver = new FileReceiver(baseFolder);
_uploadFileThread = new Thread(UploadFileProcessing); _uploadFileThread = new Thread(UploadFileProcessing);
_uploadFileThread.IsBackground = true; _uploadFileThread.IsBackground = true;
_uploadFileThread.Start(); _uploadFileThread.Start();
/*_maxParallelFileTransfer = maxParallelFileTransfer;
_currentFileTransfers = 0;*/
} }
protected void PushTransferTask(string filePath, Action<string> completeHandler = null, Action<string, string> errorHandler = null, ExClient client = null) protected void PushTransferTask(string filePath, Action<string> completeHandler = null, Action<string, string> errorHandler = null, ExClient client = null)

@ -6,8 +6,9 @@ namespace ZeroLevel.Network.FileTransfer
{ {
public static IFileClient Create(string serverEndpoint, string baseFolder, ClientFolderNameMapper nameMapper = null) public static IFileClient Create(string serverEndpoint, string baseFolder, ClientFolderNameMapper nameMapper = null)
{ {
return null;/* CreateFileServerClient(ExchangeTransportFactory.GetClient(serverEndpoint), baseFolder, var client = new ExClient(new SocketClient(NetUtils.CreateIPEndPoint(serverEndpoint), new Router()));
nameMapper ?? (c => FSUtils.FileNameCorrection($"{c.Endpoint.Address}_{c.Endpoint.Port}")), true);*/ return CreateFileServerClient(client, baseFolder,
nameMapper ?? (c => FSUtils.FileNameCorrection($"{c.Endpoint.Address}_{c.Endpoint.Port}")), true);
} }
public static IFileClient Create(ExClient client, string baseFolder, ClientFolderNameMapper nameMapper = null) public static IFileClient Create(ExClient client, string baseFolder, ClientFolderNameMapper nameMapper = null)

Loading…
Cancel
Save

Powered by TurnKey Linux.