diff --git a/ZeroLevel.Discovery/App.config b/ZeroLevel.Discovery/App.config index 8ac5084..eec1b5b 100644 --- a/ZeroLevel.Discovery/App.config +++ b/ZeroLevel.Discovery/App.config @@ -14,6 +14,10 @@ + + + + \ No newline at end of file diff --git a/ZeroLevel.Discovery/DiscoveryService.cs b/ZeroLevel.Discovery/DiscoveryService.cs index b36e4c9..4a0a592 100644 --- a/ZeroLevel.Discovery/DiscoveryService.cs +++ b/ZeroLevel.Discovery/DiscoveryService.cs @@ -40,7 +40,7 @@ namespace ZeroLevel.Discovery _exInbox.RegisterInbox>("services", (_, __) => routeTable.Get()); _exInbox.RegisterInbox("register", (info, _, __) => routeTable.Append(info)); - Log.Info($"TCP server started on port {socketPort}"); + Log.Info($"TCP server started {_exInbox.Endpoint.Address}:{socketPort}"); } public override void StopAction() diff --git a/ZeroLevel.Discovery/ZeroLevel.Discovery.csproj b/ZeroLevel.Discovery/ZeroLevel.Discovery.csproj index 3028b5e..8d11bee 100644 --- a/ZeroLevel.Discovery/ZeroLevel.Discovery.csproj +++ b/ZeroLevel.Discovery/ZeroLevel.Discovery.csproj @@ -42,8 +42,8 @@ ..\packages\Microsoft.Owin.Hosting.4.0.1\lib\net45\Microsoft.Owin.Hosting.dll - - ..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll + + C:\Users\a.bozhenov\source\repos\ZeroTests\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll ..\packages\Owin.1.0\lib\net40\Owin.dll diff --git a/ZeroLevel.Discovery/obj/Debug/ZeroLevel.Discovery.csproj.CoreCompileInputs.cache b/ZeroLevel.Discovery/obj/Debug/ZeroLevel.Discovery.csproj.CoreCompileInputs.cache index 9e86bbc..e40d599 100644 --- a/ZeroLevel.Discovery/obj/Debug/ZeroLevel.Discovery.csproj.CoreCompileInputs.cache +++ b/ZeroLevel.Discovery/obj/Debug/ZeroLevel.Discovery.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -e6d14b54c463a8096220ac67f9a339de12a3fb4a +baf2f9d75e4982e81a51362dec180283f7786a6a diff --git a/ZeroLevel.Discovery/obj/Debug/ZeroLevel.Discovery.csprojAssemblyReference.cache b/ZeroLevel.Discovery/obj/Debug/ZeroLevel.Discovery.csprojAssemblyReference.cache index 34790e8..f9dc362 100644 Binary files a/ZeroLevel.Discovery/obj/Debug/ZeroLevel.Discovery.csprojAssemblyReference.cache and b/ZeroLevel.Discovery/obj/Debug/ZeroLevel.Discovery.csprojAssemblyReference.cache differ diff --git a/ZeroLevel.Discovery/packages.config b/ZeroLevel.Discovery/packages.config index b1d1905..584a933 100644 --- a/ZeroLevel.Discovery/packages.config +++ b/ZeroLevel.Discovery/packages.config @@ -7,6 +7,6 @@ - + \ No newline at end of file diff --git a/ZeroLevel/Services/Application/BaseWindowsExService.cs b/ZeroLevel/Services/Application/BaseWindowsExService.cs index 65aeeae..ad468ba 100644 --- a/ZeroLevel/Services/Application/BaseWindowsExService.cs +++ b/ZeroLevel/Services/Application/BaseWindowsExService.cs @@ -29,7 +29,26 @@ namespace ZeroLevel.Services.Applications var discoveryProtocol = _config.FirstOrDefault("discoveryProtocol", "socket"); _exchange = new Exchange(new DiscoveryClient(discoveryProtocol, discovery)); + } + private IExService _self_service = null; + private readonly object _self_create_lock = new object(); + protected IExService Self + { + get + { + if (_self_service == null) + { + lock (_self_create_lock) + { + if (_self_service == null) + { + _self_service = _exchange.RegisterService(this); + } + } + } + return _self_service; + } } #region Config diff --git a/ZeroLevel/Services/Application/BaseWindowsService.cs b/ZeroLevel/Services/Application/BaseWindowsService.cs index 20a7936..2572f5f 100644 --- a/ZeroLevel/Services/Application/BaseWindowsService.cs +++ b/ZeroLevel/Services/Application/BaseWindowsService.cs @@ -28,9 +28,13 @@ namespace ZeroLevel.Services.Applications { InteraciveModeWorkingFlag.Reset(); OnStart(args); - while (false == InteraciveModeWorkingFlag.WaitOne(2000)) + try { + while (false == InteraciveModeWorkingFlag.WaitOne(2000)) + { + } } + catch { } } #region IZeroService diff --git a/ZeroLevel/Services/Network/Contract/IExService.cs b/ZeroLevel/Services/Network/Contract/IExService.cs index c8437e7..9b5824f 100644 --- a/ZeroLevel/Services/Network/Contract/IExService.cs +++ b/ZeroLevel/Services/Network/Contract/IExService.cs @@ -16,5 +16,12 @@ namespace ZeroLevel.Network /// Replier without request /// void RegisterInbox(string inbox, Func handler); + +/* +DEFAULT INBOXES +*/ + void RegisterInbox(Action handler); + void RegisterInbox(Func handler); + void RegisterInbox(Func handler); } } \ No newline at end of file diff --git a/ZeroLevel/Services/Network/NetUtils.cs b/ZeroLevel/Services/Network/NetUtils.cs index f1ff5de..32ab76a 100644 --- a/ZeroLevel/Services/Network/NetUtils.cs +++ b/ZeroLevel/Services/Network/NetUtils.cs @@ -53,23 +53,28 @@ namespace ZeroLevel.Network return port; } - public static IPAddress GetNonLoopbackAddress() + public static IPAddress GetNonLoopbackAddress(bool ignore_virtual_devices = true, bool ignore_docker_devices = true) { if (NetworkInterface.GetIsNetworkAvailable()) { foreach (var adapter in NetworkInterface.GetAllNetworkInterfaces()) { - if (adapter.Description.IndexOf("VirtualBox", StringComparison.OrdinalIgnoreCase) >= 0) - continue; - if (adapter.NetworkInterfaceType != NetworkInterfaceType.Ethernet) - continue; - foreach (UnicastIPAddressInformation address in adapter.GetIPProperties().UnicastAddresses) + if (adapter.OperationalStatus == OperationalStatus.Up) { - if (address.Address.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) + if (ignore_virtual_devices && (adapter.Description.IndexOf("Virtual", StringComparison.OrdinalIgnoreCase) >= 0 || adapter.Name.IndexOf("Virtual", StringComparison.OrdinalIgnoreCase) >= 0)) + continue; + if (ignore_docker_devices && (adapter.Description.IndexOf("Docker", StringComparison.OrdinalIgnoreCase) >= 0 || adapter.Name.IndexOf("Docker", StringComparison.OrdinalIgnoreCase) >= 0)) + continue; + if (adapter.NetworkInterfaceType != NetworkInterfaceType.Ethernet) + continue; + foreach (UnicastIPAddressInformation address in adapter.GetIPProperties().UnicastAddresses) { - if (!IPAddress.IsLoopback(address.Address)) + if (address.Address.AddressFamily == AddressFamily.InterNetwork) { - return address.Address; + if (!IPAddress.IsLoopback(address.Address)) + { + return address.Address; + } } } } diff --git a/ZeroLevel/Services/Network/Services/DiscoveryClient.cs b/ZeroLevel/Services/Network/Services/DiscoveryClient.cs index 76041fe..10f9d6e 100644 --- a/ZeroLevel/Services/Network/Services/DiscoveryClient.cs +++ b/ZeroLevel/Services/Network/Services/DiscoveryClient.cs @@ -66,10 +66,42 @@ namespace ZeroLevel.Network _discoveryServerClient.ForceConnect(); if (_discoveryServerClient.Status == ZTransportStatus.Working) { - IEnumerable records = null; try { - var ir = _discoveryServerClient.Request>("services", response => records = response); + var ir = _discoveryServerClient.Request>("services", records => + { + if (records == null) + { + Log.Warning("[DiscoveryClient] UpdateServiceListInfo. Discrovery response is empty"); + return; + } + _lock.EnterWriteLock(); + try + { + _tableByGroups.Clear(); + _tableByTypes.Clear(); + var keysToRemove = new List(_tableByKey.Keys); + foreach (var info in records) + { + var key = info.ServiceKey.Trim().ToLowerInvariant(); + UpdateOrAddRecord(key, info); + keysToRemove.Remove(key); + } + foreach (var key in keysToRemove) + { + _tableByKey.TryRemove(key, out RoundRobinCollection removed); + removed.Dispose(); + } + } + catch (Exception ex) + { + Log.Error(ex, "[DiscoveryClient] UpdateServiceListInfo. Update local routing table error."); + } + finally + { + _lock.ExitWriteLock(); + } + }); if (!ir.Success) { Log.Warning($"[DiscoveryClient] UpdateServiceListInfo. Error request to inbox 'services'. {ir.Comment}"); @@ -81,37 +113,6 @@ namespace ZeroLevel.Network Log.Error(ex, "[DiscoveryClient] UpdateServiceListInfo. Discrovery service response is absent"); return; } - if (records == null) - { - Log.Warning("[DiscoveryClient] UpdateServiceListInfo. Discrovery response is empty"); - return; - } - _lock.EnterWriteLock(); - try - { - _tableByGroups.Clear(); - _tableByTypes.Clear(); - var keysToRemove = new List(_tableByKey.Keys); - foreach (var info in records) - { - var key = info.ServiceKey.Trim().ToLowerInvariant(); - UpdateOrAddRecord(key, info); - keysToRemove.Remove(key); - } - foreach (var key in keysToRemove) - { - _tableByKey.TryRemove(key, out RoundRobinCollection removed); - removed.Dispose(); - } - } - catch (Exception ex) - { - Log.Error(ex, "[DiscoveryClient] UpdateServiceListInfo. Update local routing table error."); - } - finally - { - _lock.ExitWriteLock(); - } } else { diff --git a/ZeroLevel/obj/Debug/ZeroLevel.csproj.CoreCompileInputs.cache b/ZeroLevel/obj/Debug/ZeroLevel.csproj.CoreCompileInputs.cache index b62c01d..9fb1091 100644 --- a/ZeroLevel/obj/Debug/ZeroLevel.csproj.CoreCompileInputs.cache +++ b/ZeroLevel/obj/Debug/ZeroLevel.csproj.CoreCompileInputs.cache @@ -1 +1 @@ -35db55177c262262ace2e47d49fa51ff7b48a736 +44e8c5c8420f166261f61c4c11d319a8acdc2c79