From 4f721fdbc8462dfb310513f7613c93994bb6f0a7 Mon Sep 17 00:00:00 2001 From: "a.bozhenov" Date: Thu, 4 Jul 2019 21:42:23 +0300 Subject: [PATCH] Fixes --- FileTransferClient/MainWindow.xaml.cs | 2 +- FileTransferServer/MainWindow.xaml.cs | 3 +- TestApp/MyService.cs | 16 ++++--- ZeroLevel.Discovery/DiscoveryService.cs | 11 ++--- ZeroLevel.Discovery/RouteTable.cs | 2 +- ZeroLevel.UnitTests/EncryptionTests.cs | 31 ------------ ZeroLevel.UnitTests/ExchangeTests.cs | 39 ++++++++------- ZeroLevel.UnitTests/NetworkTest.cs | 25 +++++++--- ZeroLevel/Services/Network/ExClient.cs | 47 +++++++++++++++++-- .../Network/FileTransfer/BaseFileTransfer.cs | 6 +-- .../Network/FileTransfer/FileClientFactory.cs | 5 +- 11 files changed, 104 insertions(+), 83 deletions(-) diff --git a/FileTransferClient/MainWindow.xaml.cs b/FileTransferClient/MainWindow.xaml.cs index 49a9234..e8bcee3 100644 --- a/FileTransferClient/MainWindow.xaml.cs +++ b/FileTransferClient/MainWindow.xaml.cs @@ -1,6 +1,6 @@ using Microsoft.Win32; using System.Windows; -using ZeroLevel.Services.Network.FileTransfer; +using ZeroLevel.Network.FileTransfer; namespace FileTransferClient { diff --git a/FileTransferServer/MainWindow.xaml.cs b/FileTransferServer/MainWindow.xaml.cs index 57ce261..02d1b86 100644 --- a/FileTransferServer/MainWindow.xaml.cs +++ b/FileTransferServer/MainWindow.xaml.cs @@ -1,6 +1,6 @@ using System.IO; using System.Windows; -using ZeroLevel.Services.Network.FileTransfer; +using ZeroLevel.Network.FileTransfer; namespace FileTransferServer { @@ -39,7 +39,6 @@ namespace FileTransferServer private void Button_Click_1(object sender, RoutedEventArgs e) { - _server?.Dispose(); _server = null; } } diff --git a/TestApp/MyService.cs b/TestApp/MyService.cs index 0986396..459f2f0 100644 --- a/TestApp/MyService.cs +++ b/TestApp/MyService.cs @@ -22,15 +22,18 @@ namespace TestApp AutoregisterInboxes(UseHost(8800)); ReadServiceInfo(); - UseHost(8801).RegisterInbox("metainfo", (c) => this.ServiceInfo); + UseHost(8801).RegisterInbox("metainfo", (c) => + { + Log.Info("Reqeust for metainfo"); + return this.ServiceInfo; + }); StoreConnection("mytest", new IPEndPoint(IPAddress.Loopback, 8800)); 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(BaseSocket.DEFAULT_MESSAGE_INBOX, "'This is message'"); client.Request("d2s", DateTime.Now, s => Log.Info($"Response: {s}")); @@ -41,9 +44,10 @@ namespace TestApp client.Request(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("metainfo", info => + var client = ConnectToService("mymeta"); + client.Request("metainfo", info => { var si = new StringBuilder(); si.AppendLine(info.Name); diff --git a/ZeroLevel.Discovery/DiscoveryService.cs b/ZeroLevel.Discovery/DiscoveryService.cs index 50f12e1..b2e1caa 100644 --- a/ZeroLevel.Discovery/DiscoveryService.cs +++ b/ZeroLevel.Discovery/DiscoveryService.cs @@ -8,7 +8,7 @@ namespace ZeroLevel.Discovery public sealed class DiscoveryService : BaseZeroService { - private IExService _exInbox; + private IRouter _exInbox; public DiscoveryService() : base("Discovery") @@ -18,20 +18,15 @@ namespace ZeroLevel.Discovery protected override void StartAction() { var routeTable = new RouteTable(); - Injector.Default.Register(routeTable); - var socketPort = Configuration.Default.First("socketport"); - _exInbox = ExchangeTransportFactory.GetServer(socketPort); + _exInbox = UseHost(socketPort); _exInbox.RegisterInbox>("services", (_, __) => routeTable.Get()); - _exInbox.RegisterInbox("register", (info, _, client) => routeTable.Append(info, client)); - - Log.Info($"TCP server started {_exInbox.Endpoint.Address}:{socketPort}"); + _exInbox.RegisterInbox("register", (client, info) => routeTable.Append(info, client)); } protected override void StopAction() { - _exInbox.Dispose(); } } } \ No newline at end of file diff --git a/ZeroLevel.Discovery/RouteTable.cs b/ZeroLevel.Discovery/RouteTable.cs index 6d4a16d..a412cd0 100644 --- a/ZeroLevel.Discovery/RouteTable.cs +++ b/ZeroLevel.Discovery/RouteTable.cs @@ -149,7 +149,7 @@ namespace ZeroLevel.Discovery Save(); } - public InvokeResult Append(ExServiceInfo serviceInfo, ISocketClient client) + public InvokeResult Append(ZeroServiceInfo serviceInfo, ISocketClient client) { InvokeResult result = null; var endpoint = $"{client.Endpoint.Address}:{serviceInfo.Port}"; diff --git a/ZeroLevel.UnitTests/EncryptionTests.cs b/ZeroLevel.UnitTests/EncryptionTests.cs index 653fd6e..2bdc54f 100644 --- a/ZeroLevel.UnitTests/EncryptionTests.cs +++ b/ZeroLevel.UnitTests/EncryptionTests.cs @@ -2,7 +2,6 @@ using System.Linq; using Xunit; using ZeroLevel.DocumentObjectModel; -using ZeroLevel.Network; using ZeroLevel.Services.Encryption; using ZeroLevel.Services.Serialization; using ZeroLevel.UnitTests.Models; @@ -47,35 +46,5 @@ namespace ZeroLevel.EncryptionUnitTests Assert.True(obf_deviation >= deviation); Assert.True(comparator(instance, clone)); } - - [Fact] - public void NetworkStreamDataObfuscatorTest() - { - // Arrange - var comparator = new Func((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(data); - - // Assert - Assert.Equal(deviation, deobf_deviation); - Assert.NotEqual(deviation, obf_deviation); - Assert.True(obf_deviation >= deviation); - Assert.True(comparator(instance, clone)); - } } } diff --git a/ZeroLevel.UnitTests/ExchangeTests.cs b/ZeroLevel.UnitTests/ExchangeTests.cs index f5f6425..6deb4b2 100644 --- a/ZeroLevel.UnitTests/ExchangeTests.cs +++ b/ZeroLevel.UnitTests/ExchangeTests.cs @@ -2,17 +2,18 @@ using System.Net; using System.Threading; using Xunit; -using ZeroLevel.Network; +using ZeroLevel.Services.Applications; namespace ZeroLevel.NetworkUnitTests { public class ExchangeTests + : BaseZeroService { [Fact] public void HandleMessageTest() { // Arrange - var info = new ExServiceInfo + var info = new ZeroServiceInfo { ServiceGroup = "MyServiceGroup", ServiceKey = "MyServiceKey", @@ -20,18 +21,18 @@ namespace ZeroLevel.NetworkUnitTests Version = "1.1.1.1" }; var locker = new ManualResetEvent(false); - var server = ExchangeTransportFactory.GetServer(6666); - ExServiceInfo received = null; + var server = UseHost(6666); + ZeroServiceInfo received = null; - server.RegisterInbox("register", (i, _, __) => + server.RegisterInbox("register", (c, i) => { received = i; locker.Set(); }); // Act - var client = ExchangeTransportFactory.GetClient(IPAddress.Loopback.ToString() + ":6666"); - var ir = client.Send("register", info); + var client = ConnectToService(IPAddress.Loopback.ToString() + ":6666"); + var ir = client.Send("register", info); locker.WaitOne(1000); @@ -42,21 +43,20 @@ namespace ZeroLevel.NetworkUnitTests // Dispose locker.Dispose(); client.Dispose(); - server.Dispose(); } [Fact] public void RequestMessageTest() { // Arrange - var info1 = new ExServiceInfo + var info1 = new ZeroServiceInfo { ServiceGroup = "MyServiceGroup", ServiceKey = "MyServiceKey", ServiceType = "MyServiceType", Version = "1.1.1.1" }; - var info2 = new ExServiceInfo + var info2 = new ZeroServiceInfo { ServiceGroup = "MyServiceGroup", ServiceKey = "MyServiceKey2", @@ -64,14 +64,14 @@ namespace ZeroLevel.NetworkUnitTests Version = "1.1.0.1" }; var locker = new ManualResetEvent(false); - var server = ExchangeTransportFactory.GetServer(6666); - IEnumerable received = null; + var server = UseHost(6667); + IEnumerable received = null; - server.RegisterInbox>("services", (_, __) => new[] { info1, info2 }); + server.RegisterInbox>("services", (_) => new[] { info1, info2 }); // Act - var client = ExchangeTransportFactory.GetClient(IPAddress.Loopback.ToString() + ":6666"); - var ir = client.Request>("services", response => + var client = ConnectToService(IPAddress.Loopback.ToString() + ":6667"); + var ir = client.Request>("services", response => { received = response; locker.Set(); @@ -86,7 +86,14 @@ namespace ZeroLevel.NetworkUnitTests // Dispose locker.Dispose(); client.Dispose(); - server.Dispose(); + } + + protected override void StartAction() + { + } + + protected override void StopAction() + { } } } diff --git a/ZeroLevel.UnitTests/NetworkTest.cs b/ZeroLevel.UnitTests/NetworkTest.cs index c963de8..3d194fe 100644 --- a/ZeroLevel.UnitTests/NetworkTest.cs +++ b/ZeroLevel.UnitTests/NetworkTest.cs @@ -1,18 +1,19 @@ using System; using System.Threading; using Xunit; -using ZeroLevel.Network; +using ZeroLevel.Services.Applications; namespace ZeroLevel.UnitTests { public class NetworkTest + : BaseZeroService { [Fact] public void ClientServerTest() { // Arrange - var server = ExchangeTransportFactory.GetServer(8181); - var client = ExchangeTransportFactory.GetClient("127.0.0.1:8181"); + var server = UseHost(8181); + var client = ConnectToService("127.0.0.1:8181"); bool got_message_no_request = false; bool got_message_with_request = false; @@ -21,10 +22,10 @@ namespace ZeroLevel.UnitTests using (var signal = new ManualResetEvent(false)) { - server.RegisterInbox("empty", (_, __) => { signal.Set(); got_message_no_request = true; }); - server.RegisterInbox((_, __, ___) => { signal.Set(); got_message_with_request = true; }); - server.RegisterInbox("get_response", (_, __) => { return "Hello"; }); - server.RegisterInbox("convert", (num, _, __) => { return num.ToString(); }); + server.RegisterInbox("empty", (_) => { signal.Set(); got_message_no_request = true; }); + server.RegisterInbox((_, ___) => { signal.Set(); got_message_with_request = true; }); + server.RegisterInbox("get_response", (_) => "Hello"); + server.RegisterInbox("convert", (__, num) => num.ToString()); // Act 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_with_request, "No response with request"); } + + protected override void StartAction() + { + throw new NotImplementedException(); + } + + protected override void StopAction() + { + throw new NotImplementedException(); + } } } diff --git a/ZeroLevel/Services/Network/ExClient.cs b/ZeroLevel/Services/Network/ExClient.cs index aebe15f..f2a1ba5 100644 --- a/ZeroLevel/Services/Network/ExClient.cs +++ b/ZeroLevel/Services/Network/ExClient.cs @@ -1,10 +1,6 @@ using System; -using System.Linq; -using System.Linq.Expressions; using System.Net; -using System.Reflection; using ZeroLevel.Models; -using ZeroLevel.Services.Invokation; using ZeroLevel.Services.Serialization; namespace ZeroLevel.Network @@ -52,6 +48,20 @@ namespace ZeroLevel.Network return InvokeResult.Succeeding(); } + public InvokeResult Send(T message) + { + try + { + _client.Send(Frame.FromPool(BaseSocket.DEFAULT_MESSAGE_INBOX, MessageSerializer.SerializeCompatible(message))); + } + catch (Exception ex) + { + Log.Error(ex, "[NetworkNode.Send]"); + return InvokeResult.Fault(ex.Message); + } + return InvokeResult.Succeeding(); + } + public InvokeResult Send(string inbox, T message) { try @@ -108,6 +118,20 @@ namespace ZeroLevel.Network return InvokeResult.Succeeding(); } + public InvokeResult Request(Action callback) + { + try + { + _client.Request(Frame.FromPool(BaseSocket.DEFAULT_REQUEST_INBOX), f => callback(MessageSerializer.DeserializeCompatible(f))); + } + catch (Exception ex) + { + Log.Error(ex, "[NetworkNode.Request]"); + return InvokeResult.Fault(ex.Message); + } + return InvokeResult.Succeeding(); + } + public InvokeResult Request(string inbox, Trequest request, Action callback) { try @@ -123,6 +147,21 @@ namespace ZeroLevel.Network return InvokeResult.Succeeding(); } + public InvokeResult Request(Trequest request, Action callback) + { + try + { + _client.Request(Frame.FromPool(BaseSocket.DEFAULT_REQUEST_WITHOUT_ARGS_INBOX, MessageSerializer.SerializeCompatible(request)), + f => callback(MessageSerializer.DeserializeCompatible(f))); + } + catch (Exception ex) + { + Log.Error(ex, "[NetworkNode.Request]"); + return InvokeResult.Fault(ex.Message); + } + return InvokeResult.Succeeding(); + } + public void Dispose() { _client.Dispose(); diff --git a/ZeroLevel/Services/Network/FileTransfer/BaseFileTransfer.cs b/ZeroLevel/Services/Network/FileTransfer/BaseFileTransfer.cs index 6b316dd..8a078ce 100644 --- a/ZeroLevel/Services/Network/FileTransfer/BaseFileTransfer.cs +++ b/ZeroLevel/Services/Network/FileTransfer/BaseFileTransfer.cs @@ -14,17 +14,13 @@ namespace ZeroLevel.Network.FileTransfer private ObjectPool _taskPool = new ObjectPool(() => new FileTransferTask(), 100); private BlockingCollection _tasks = new BlockingCollection(); 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); _uploadFileThread = new Thread(UploadFileProcessing); _uploadFileThread.IsBackground = true; _uploadFileThread.Start(); - /*_maxParallelFileTransfer = maxParallelFileTransfer; - _currentFileTransfers = 0;*/ } protected void PushTransferTask(string filePath, Action completeHandler = null, Action errorHandler = null, ExClient client = null) diff --git a/ZeroLevel/Services/Network/FileTransfer/FileClientFactory.cs b/ZeroLevel/Services/Network/FileTransfer/FileClientFactory.cs index eab08fc..a8f5cd0 100644 --- a/ZeroLevel/Services/Network/FileTransfer/FileClientFactory.cs +++ b/ZeroLevel/Services/Network/FileTransfer/FileClientFactory.cs @@ -6,8 +6,9 @@ namespace ZeroLevel.Network.FileTransfer { public static IFileClient Create(string serverEndpoint, string baseFolder, ClientFolderNameMapper nameMapper = null) { - return null;/* CreateFileServerClient(ExchangeTransportFactory.GetClient(serverEndpoint), baseFolder, - nameMapper ?? (c => FSUtils.FileNameCorrection($"{c.Endpoint.Address}_{c.Endpoint.Port}")), true);*/ + var client = new ExClient(new SocketClient(NetUtils.CreateIPEndPoint(serverEndpoint), new Router())); + 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)