diff --git a/TestApp/MyService.cs b/TestApp/MyService.cs index 531a8c6..0986396 100644 --- a/TestApp/MyService.cs +++ b/TestApp/MyService.cs @@ -1,5 +1,6 @@ using System; using System.Net; +using System.Text; using ZeroLevel; using ZeroLevel.Network; using ZeroLevel.Services.Applications; @@ -19,7 +20,14 @@ namespace TestApp Log.Info("Started"); AutoregisterInboxes(UseHost(8800)); - var client = ConnectToService(new IPEndPoint(IPAddress.Loopback, 8800)); + ReadServiceInfo(); + + UseHost(8801).RegisterInbox("metainfo", (c) => 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), () => { @@ -32,6 +40,19 @@ namespace TestApp client.Request("now", s => Log.Info($"Response date: {s}")); client.Request(BaseSocket.DEFAULT_REQUEST_WITHOUT_ARGS_INBOX, s => Log.Info($"Response ip: {s}")); }); + + Sheduller.RemindEvery(TimeSpan.FromSeconds(15), () => + { + ConnectToService("mymeta").Request("metainfo", info => + { + var si = new StringBuilder(); + si.AppendLine(info.Name); + si.AppendLine(info.ServiceKey); + si.AppendLine(info.Version); + + Log.Info("Service info:\r\n{0}", si.ToString()); + }); + }); } [ExchangeHandler("pum")] diff --git a/ZeroLevel/Services/BaseZeroService.cs b/ZeroLevel/Services/BaseZeroService.cs index bbf61e3..b84464e 100644 --- a/ZeroLevel/Services/BaseZeroService.cs +++ b/ZeroLevel/Services/BaseZeroService.cs @@ -15,6 +15,8 @@ namespace ZeroLevel.Services.Applications { private readonly ZeroServiceInfo _serviceInfo = new ZeroServiceInfo(); + public ZeroServiceInfo ServiceInfo => _serviceInfo; + public string Name { get { return _serviceInfo.Name; } private set { _serviceInfo.Name = value; } } public string Key { get { return _serviceInfo.ServiceKey; } private set { _serviceInfo.ServiceKey = value; } } public string Version { get { return _serviceInfo.Version; } private set { _serviceInfo.Version = value; } } @@ -171,7 +173,7 @@ namespace ZeroLevel.Services.Applications _discoveryClient = null; } var discovery = Configuration.Default.First("discovery"); - _discoveryClient = new DiscoveryClient(GetClient(NetUtils.CreateIPEndPoint(discovery), _null_router, false)); + _discoveryClient = new DiscoveryClient(GetClient(NetUtils.CreateIPEndPoint(discovery), false, _null_router)); RestartDiscoveryTasks(); } } @@ -186,7 +188,7 @@ namespace ZeroLevel.Services.Applications _discoveryClient.Dispose(); _discoveryClient = null; } - _discoveryClient = new DiscoveryClient(GetClient(NetUtils.CreateIPEndPoint(endpoint), _null_router, false)); + _discoveryClient = new DiscoveryClient(GetClient(NetUtils.CreateIPEndPoint(endpoint), false, _null_router)); RestartDiscoveryTasks(); } } @@ -201,7 +203,7 @@ namespace ZeroLevel.Services.Applications _discoveryClient.Dispose(); _discoveryClient = null; } - _discoveryClient = new DiscoveryClient(GetClient(endpoint, _null_router, false)); + _discoveryClient = new DiscoveryClient(GetClient(endpoint, false, _null_router)); RestartDiscoveryTasks(); } } @@ -241,7 +243,11 @@ namespace ZeroLevel.Services.Applications if (_state == ZeroServiceStatus.Running || _state == ZeroServiceStatus.Initialized) { - return GetClient(NetUtils.CreateIPEndPoint(endpoint), new Router(), true); + if (_aliases.Contains(endpoint)) + { + return GetClient(_aliases.GetAddress(endpoint), true); + } + return GetClient(NetUtils.CreateIPEndPoint(endpoint), true); } return null; } @@ -251,7 +257,7 @@ namespace ZeroLevel.Services.Applications if (_state == ZeroServiceStatus.Running || _state == ZeroServiceStatus.Initialized) { - return GetClient(endpoint, new Router(), true); + return GetClient(endpoint, true); } return null; } @@ -519,7 +525,7 @@ namespace ZeroLevel.Services.Applications #region Utils - private ExClient GetClient(IPEndPoint endpoint, IRouter router, bool use_cachee) + private ExClient GetClient(IPEndPoint endpoint, bool use_cachee, IRouter router = null) { if (use_cachee) { @@ -536,11 +542,11 @@ namespace ZeroLevel.Services.Applications instance.Dispose(); instance = null; } - instance = new ExClient(new SocketClient(endpoint, router)); + instance = new ExClient(new SocketClient(endpoint, router ?? new Router())); _clientInstances[key] = instance; return instance; } - return new ExClient(new SocketClient(endpoint, router)); + return new ExClient(new SocketClient(endpoint, router ?? new Router())); } private SocketServer GetServer(IPEndPoint endpoint, IRouter router) diff --git a/ZeroLevel/Services/Network/Alias.cs b/ZeroLevel/Services/Network/Alias.cs index 86a4e48..8b88f9c 100644 --- a/ZeroLevel/Services/Network/Alias.cs +++ b/ZeroLevel/Services/Network/Alias.cs @@ -130,6 +130,8 @@ namespace ZeroLevel.Network private readonly ConcurrentDictionary> _aliases = new ConcurrentDictionary>(); + public bool Contains(string alias) => _aliases.ContainsKey(alias); + public void Set(string alias, T address) { if (_aliases.ContainsKey(alias) == false) @@ -141,7 +143,6 @@ namespace ZeroLevel.Network } else { - _aliases[alias].Clear(); _aliases[alias].Add(address); } } @@ -158,7 +159,6 @@ namespace ZeroLevel.Network } else { - _aliases[alias].Clear(); foreach (var address in addresses) _aliases[alias].Add(address); } diff --git a/ZeroLevel/Services/Network/SocketServer.cs b/ZeroLevel/Services/Network/SocketServer.cs index a06ce93..da7c093 100644 --- a/ZeroLevel/Services/Network/SocketServer.cs +++ b/ZeroLevel/Services/Network/SocketServer.cs @@ -94,11 +94,6 @@ namespace ZeroLevel.Network } } - private void Connection_OnIncomingData(ISocketClient client, byte[] data, int identity) - { - throw new NotImplementedException(); - } - private void Connection_OnDisconnect(ISocketClient client) { client.OnDisconnect -= Connection_OnDisconnect;