pull/1/head
unknown 5 years ago
parent 00c4cff5d6
commit 47d8d6329d

@ -1,5 +1,6 @@
using System; using System;
using System.Net; using System.Net;
using System.Text;
using ZeroLevel; using ZeroLevel;
using ZeroLevel.Network; using ZeroLevel.Network;
using ZeroLevel.Services.Applications; using ZeroLevel.Services.Applications;
@ -19,7 +20,14 @@ namespace TestApp
Log.Info("Started"); Log.Info("Started");
AutoregisterInboxes(UseHost(8800)); AutoregisterInboxes(UseHost(8800));
var client = ConnectToService(new IPEndPoint(IPAddress.Loopback, 8800)); ReadServiceInfo();
UseHost(8801).RegisterInbox<ZeroServiceInfo>("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), () => Sheduller.RemindEvery(TimeSpan.FromSeconds(5), () =>
{ {
@ -32,6 +40,19 @@ namespace TestApp
client.Request<string>("now", s => Log.Info($"Response date: {s}")); client.Request<string>("now", s => Log.Info($"Response date: {s}"));
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), () =>
{
ConnectToService("mymeta").Request<ZeroServiceInfo>("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")] [ExchangeHandler("pum")]

@ -15,6 +15,8 @@ namespace ZeroLevel.Services.Applications
{ {
private readonly ZeroServiceInfo _serviceInfo = new ZeroServiceInfo(); private readonly ZeroServiceInfo _serviceInfo = new ZeroServiceInfo();
public ZeroServiceInfo ServiceInfo => _serviceInfo;
public string Name { get { return _serviceInfo.Name; } private set { _serviceInfo.Name = value; } } 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 Key { get { return _serviceInfo.ServiceKey; } private set { _serviceInfo.ServiceKey = value; } }
public string Version { get { return _serviceInfo.Version; } private set { _serviceInfo.Version = value; } } public string Version { get { return _serviceInfo.Version; } private set { _serviceInfo.Version = value; } }
@ -171,7 +173,7 @@ namespace ZeroLevel.Services.Applications
_discoveryClient = null; _discoveryClient = null;
} }
var discovery = Configuration.Default.First("discovery"); 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(); RestartDiscoveryTasks();
} }
} }
@ -186,7 +188,7 @@ namespace ZeroLevel.Services.Applications
_discoveryClient.Dispose(); _discoveryClient.Dispose();
_discoveryClient = null; _discoveryClient = null;
} }
_discoveryClient = new DiscoveryClient(GetClient(NetUtils.CreateIPEndPoint(endpoint), _null_router, false)); _discoveryClient = new DiscoveryClient(GetClient(NetUtils.CreateIPEndPoint(endpoint), false, _null_router));
RestartDiscoveryTasks(); RestartDiscoveryTasks();
} }
} }
@ -201,7 +203,7 @@ namespace ZeroLevel.Services.Applications
_discoveryClient.Dispose(); _discoveryClient.Dispose();
_discoveryClient = null; _discoveryClient = null;
} }
_discoveryClient = new DiscoveryClient(GetClient(endpoint, _null_router, false)); _discoveryClient = new DiscoveryClient(GetClient(endpoint, false, _null_router));
RestartDiscoveryTasks(); RestartDiscoveryTasks();
} }
} }
@ -241,7 +243,11 @@ namespace ZeroLevel.Services.Applications
if (_state == ZeroServiceStatus.Running if (_state == ZeroServiceStatus.Running
|| _state == ZeroServiceStatus.Initialized) || _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; return null;
} }
@ -251,7 +257,7 @@ namespace ZeroLevel.Services.Applications
if (_state == ZeroServiceStatus.Running if (_state == ZeroServiceStatus.Running
|| _state == ZeroServiceStatus.Initialized) || _state == ZeroServiceStatus.Initialized)
{ {
return GetClient(endpoint, new Router(), true); return GetClient(endpoint, true);
} }
return null; return null;
} }
@ -519,7 +525,7 @@ namespace ZeroLevel.Services.Applications
#region Utils #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) if (use_cachee)
{ {
@ -536,11 +542,11 @@ namespace ZeroLevel.Services.Applications
instance.Dispose(); instance.Dispose();
instance = null; instance = null;
} }
instance = new ExClient(new SocketClient(endpoint, router)); instance = new ExClient(new SocketClient(endpoint, router ?? new Router()));
_clientInstances[key] = instance; _clientInstances[key] = instance;
return 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) private SocketServer GetServer(IPEndPoint endpoint, IRouter router)

@ -130,6 +130,8 @@ namespace ZeroLevel.Network
private readonly ConcurrentDictionary<string, _RoundRobinCollection<T>> _aliases = new ConcurrentDictionary<string, _RoundRobinCollection<T>>(); private readonly ConcurrentDictionary<string, _RoundRobinCollection<T>> _aliases = new ConcurrentDictionary<string, _RoundRobinCollection<T>>();
public bool Contains(string alias) => _aliases.ContainsKey(alias);
public void Set(string alias, T address) public void Set(string alias, T address)
{ {
if (_aliases.ContainsKey(alias) == false) if (_aliases.ContainsKey(alias) == false)
@ -141,7 +143,6 @@ namespace ZeroLevel.Network
} }
else else
{ {
_aliases[alias].Clear();
_aliases[alias].Add(address); _aliases[alias].Add(address);
} }
} }
@ -158,7 +159,6 @@ namespace ZeroLevel.Network
} }
else else
{ {
_aliases[alias].Clear();
foreach (var address in addresses) foreach (var address in addresses)
_aliases[alias].Add(address); _aliases[alias].Add(address);
} }

@ -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) private void Connection_OnDisconnect(ISocketClient client)
{ {
client.OnDisconnect -= Connection_OnDisconnect; client.OnDisconnect -= Connection_OnDisconnect;

Loading…
Cancel
Save

Powered by TurnKey Linux.