pull/1/head
a.bozhenov 6 years ago
parent a1f7089b68
commit 4c9b1512e6

@ -18,23 +18,22 @@ namespace TestApp
protected override void StartAction() protected override void StartAction()
{ {
Log.Info("Started"); Log.Info("Started");
AutoregisterInboxes(UseHost(8800));
ReadServiceInfo(); ReadServiceInfo();
AutoregisterInboxes(UseHost(8800));
UseHost(8801).RegisterInbox<ZeroServiceInfo>("metainfo", (c) => UseHost(8801).RegisterInbox<ZeroServiceInfo>("metainfo", (c) =>
{ {
Log.Info("Reqeust for metainfo"); Log.Info("Reqeust for metainfo");
return this.ServiceInfo; return this.ServiceInfo;
}); });
StoreConnection("mytest", new IPEndPoint(IPAddress.Loopback, 8800)); Exchange.RoutesStorage.Set("mytest", new IPEndPoint(IPAddress.Loopback, 8800));
StoreConnection("mymeta", new IPEndPoint(IPAddress.Loopback, 8801)); Exchange.RoutesStorage.Set("mymeta", new IPEndPoint(IPAddress.Loopback, 8801));
int count = 0; Sheduller.RemindEvery(TimeSpan.FromSeconds(1), () =>
Sheduller.RemindWhile(TimeSpan.FromSeconds(1), () =>
{ {
var client = ConnectToService("mytest"); var client = Exchange.GetConnection("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}"));
@ -43,14 +42,11 @@ namespace TestApp
s => Log.Info($"Response: {s}")); s => Log.Info($"Response: {s}"));
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}"));
count++;
return count > 3;
}); });
Sheduller.RemindEvery(TimeSpan.FromSeconds(3), () => Sheduller.RemindEvery(TimeSpan.FromSeconds(3), () =>
{ {
var client = ConnectToService("mymeta"); Exchange.Request<ZeroServiceInfo>("mymeta", "metainfo", info =>
client.Request<ZeroServiceInfo>("metainfo", info =>
{ {
var si = new StringBuilder(); var si = new StringBuilder();
si.AppendLine(info.Name); si.AppendLine(info.Name);

@ -9,7 +9,7 @@ namespace TestApp
Bootstrap.Startup<MyService>(args, Bootstrap.Startup<MyService>(args,
() => Configuration.ReadSetFromIniFile("config.ini")) () => Configuration.ReadSetFromIniFile("config.ini"))
.EnableConsoleLog(ZeroLevel.Services.Logging.LogLevel.System | ZeroLevel.Services.Logging.LogLevel.FullDebug) .EnableConsoleLog(ZeroLevel.Services.Logging.LogLevel.System | ZeroLevel.Services.Logging.LogLevel.FullDebug)
.UseDiscovery() //.UseDiscovery()
.Run() .Run()
.WaitWhileStatus(ZeroServiceStatus.Running) .WaitWhileStatus(ZeroServiceStatus.Running)
.Stop(); .Stop();

@ -44,18 +44,23 @@ namespace ZeroLevel.CollectionUnitTests
{ {
var arr = new int[] { 1, 2, 3 }; var arr = new int[] { 1, 2, 3 };
// Arrange // Arrange
var collection = new RoundRobinOverCollection<int>(arr); var collection = new RoundRobinCollection<int>(arr);
var iter1 = new int[] { 1, 2, 3 }; var iter1 = new int[] { 1, 2, 3 };
var iter2 = new int[] { 2, 3, 1 }; var iter2 = new int[] { 2, 3, 1 };
var iter3 = new int[] { 3, 1, 2 }; var iter3 = new int[] { 3, 1, 2 };
// Act // Act
// Assert // Assert
Assert.True(CollectionComparsionExtensions.OrderingEquals(collection.GenerateSeq().ToArray(), iter1)); Assert.True(CollectionComparsionExtensions.OrderingEquals(collection.GetCurrentSeq().ToArray(), iter1));
Assert.True(CollectionComparsionExtensions.OrderingEquals(collection.GenerateSeq().ToArray(), iter2)); collection.MoveNext();
Assert.True(CollectionComparsionExtensions.OrderingEquals(collection.GenerateSeq().ToArray(), iter3)); Assert.True(CollectionComparsionExtensions.OrderingEquals(collection.GetCurrentSeq().ToArray(), iter2));
Assert.True(CollectionComparsionExtensions.OrderingEquals(collection.GenerateSeq().ToArray(), iter1)); collection.MoveNext();
Assert.True(CollectionComparsionExtensions.OrderingEquals(collection.GenerateSeq().ToArray(), iter2)); Assert.True(CollectionComparsionExtensions.OrderingEquals(collection.GetCurrentSeq().ToArray(), iter3));
Assert.True(CollectionComparsionExtensions.OrderingEquals(collection.GenerateSeq().ToArray(), iter3)); collection.MoveNext();
Assert.True(CollectionComparsionExtensions.OrderingEquals(collection.GetCurrentSeq().ToArray(), iter1));
collection.MoveNext();
Assert.True(CollectionComparsionExtensions.OrderingEquals(collection.GetCurrentSeq().ToArray(), iter2));
collection.MoveNext();
Assert.True(CollectionComparsionExtensions.OrderingEquals(collection.GetCurrentSeq().ToArray(), iter3));
} }
[Fact] [Fact]

@ -31,7 +31,7 @@ namespace ZeroLevel.NetworkUnitTests
}); });
// Act // Act
var client = ConnectToService(IPAddress.Loopback.ToString() + ":6666"); var client = Exchange.GetConnection(IPAddress.Loopback.ToString() + ":6666");
var ir = client.Send<ZeroServiceInfo>("register", info); var ir = client.Send<ZeroServiceInfo>("register", info);
locker.WaitOne(1000); locker.WaitOne(1000);
@ -70,7 +70,7 @@ namespace ZeroLevel.NetworkUnitTests
server.RegisterInbox<IEnumerable<ZeroServiceInfo>>("services", (_) => new[] { info1, info2 }); server.RegisterInbox<IEnumerable<ZeroServiceInfo>>("services", (_) => new[] { info1, info2 });
// Act // Act
var client = ConnectToService(IPAddress.Loopback.ToString() + ":6667"); var client = Exchange.GetConnection(IPAddress.Loopback.ToString() + ":6667");
var ir = client.Request<IEnumerable<ZeroServiceInfo>>("services", response => var ir = client.Request<IEnumerable<ZeroServiceInfo>>("services", response =>
{ {
received = response; received = response;

@ -13,7 +13,7 @@ namespace ZeroLevel.UnitTests
{ {
// Arrange // Arrange
var server = UseHost(8181); var server = UseHost(8181);
var client = ConnectToService("127.0.0.1:8181"); var client = Exchange.GetConnection("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;

@ -12,7 +12,8 @@ namespace ZeroLevel.Services.Applications
: IZeroService : IZeroService
{ {
private readonly ZeroServiceInfo _serviceInfo = new ZeroServiceInfo(); private readonly ZeroServiceInfo _serviceInfo = new ZeroServiceInfo();
private readonly IExchange _exhange;
protected IExchange Exchange => _exhange;
public ZeroServiceInfo ServiceInfo => _serviceInfo; 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; } }
@ -27,11 +28,13 @@ namespace ZeroLevel.Services.Applications
protected BaseZeroService() protected BaseZeroService()
{ {
Name = GetType().Name; Name = GetType().Name;
_exhange = new Exchange(this);
} }
protected BaseZeroService(string name) protected BaseZeroService(string name)
{ {
Name = name; Name = name;
_exhange = new Exchange(this);
} }
protected abstract void StartAction(); protected abstract void StartAction();
@ -119,8 +122,6 @@ namespace ZeroLevel.Services.Applications
#endregion Config #endregion Config
#region Network #region Network
private readonly Exchange _exhange = new Exchange();
public void UseDiscovery() public void UseDiscovery()
{ {
@ -179,30 +180,6 @@ namespace ZeroLevel.Services.Applications
return BaseSocket.NullRouter; return BaseSocket.NullRouter;
} }
public ExClient ConnectToService(string endpoint)
{
if (_state == ZeroServiceStatus.Running
|| _state == ZeroServiceStatus.Initialized)
{
if (_aliases.Contains(endpoint))
{
return GetClient(_aliases.Get(endpoint), true);
}
return GetClient(NetUtils.CreateIPEndPoint(endpoint), true);
}
return null;
}
public ExClient ConnectToService(IPEndPoint endpoint)
{
if (_state == ZeroServiceStatus.Running
|| _state == ZeroServiceStatus.Initialized)
{
return GetClient(endpoint, true);
}
return null;
}
#region Autoregistration inboxes #region Autoregistration inboxes
private static Delegate CreateDelegate(Type delegateType, MethodInfo methodInfo, object target) private static Delegate CreateDelegate(Type delegateType, MethodInfo methodInfo, object target)
{ {

@ -13,6 +13,8 @@ namespace ZeroLevel
string Group { get; } string Group { get; }
string Type { get; } string Type { get; }
ZeroServiceInfo ServiceInfo { get; }
void UseDiscovery(); void UseDiscovery();
void UseDiscovery(string url); void UseDiscovery(string url);
void UseDiscovery(IPEndPoint endpoint); void UseDiscovery(IPEndPoint endpoint);
@ -21,9 +23,6 @@ namespace ZeroLevel
IRouter UseHost(int port); IRouter UseHost(int port);
IRouter UseHost(IPEndPoint endpoint); IRouter UseHost(IPEndPoint endpoint);
ExClient ConnectToService(string url);
ExClient ConnectToService(IPEndPoint endpoint);
void ReadServiceInfo(); void ReadServiceInfo();
void ReadServiceInfo(IConfigurationSet config); void ReadServiceInfo(IConfigurationSet config);

@ -0,0 +1,23 @@
using System.Collections.Generic;
using System.Net;
using ZeroLevel.Models;
namespace ZeroLevel.Network
{
public interface IServiceRoutesStorage
{
void Set(IPEndPoint endpoint);
void Set(IEnumerable<IPEndPoint> endpoints);
void Set(string key, IPEndPoint endpoint);
void Set(string key, IEnumerable<IPEndPoint> endpoints);
void Set(string key, string type, string group, IPEndPoint endpoint);
void Set(string key, string type, string group, IEnumerable<IPEndPoint> endpoints);
InvokeResult<IPEndPoint> Get(string key);
InvokeResult<IEnumerable<IPEndPoint>> GetAll(string key);
InvokeResult<IPEndPoint> GetByType(string type);
InvokeResult<IEnumerable<IPEndPoint>> GetAllByType(string type);
InvokeResult<IPEndPoint> GetByGroup(string group);
InvokeResult<IEnumerable<IPEndPoint>> GetAllByGroup(string group);
}
}

@ -9,21 +9,41 @@ using ZeroLevel.Services.Serialization;
namespace ZeroLevel.Network namespace ZeroLevel.Network
{ {
public interface IExchange
: IClientSet, IDisposable
{
void UseDiscovery();
void UseDiscovery(string endpoint);
void UseDiscovery(IPEndPoint endpoint);
IRouter UseHost();
IRouter UseHost(int port);
IRouter UseHost(IPEndPoint endpoint);
IServiceRoutesStorage RoutesStorage { get; }
ExClient GetConnection(string alias);
ExClient GetConnection(IPEndPoint endpoint);
}
/// <summary> /// <summary>
/// Provides data exchange between services /// Provides data exchange between services
/// </summary> /// </summary>
public sealed class Exchange : internal sealed class Exchange :
IClientSet, IExchange
IDisposable
{ {
private IDiscoveryClient _discoveryClient = null; // Feature расширить до нескольких discovery private IDiscoveryClient _discoveryClient = null; // Feature расширить до нескольких discovery
private readonly ServiceRouteStorage _aliases = new ServiceRouteStorage(); private readonly ServiceRouteStorage _aliases = new ServiceRouteStorage();
private readonly ExClientServerCachee _cachee = new ExClientServerCachee(); private readonly ExClientServerCachee _cachee = new ExClientServerCachee();
public IServiceRoutesStorage RoutesStorage => _aliases;
private readonly IZeroService _owner;
#region Ctor #region Ctor
public Exchange() public Exchange(IZeroService owner)
{ {
_owner = owner;
} }
#endregion Ctor #endregion Ctor
@ -482,16 +502,15 @@ namespace ZeroLevel.Network
} }
RegisterServicesInDiscovery(); RegisterServicesInDiscovery();
_update_discovery_table_task = Sheduller.RemindEvery(_update_discovery_table_period, RegisterServicesInDiscovery); _update_discovery_table_task = Sheduller.RemindEvery(_update_discovery_table_period, RegisterServicesInDiscovery);
_register_in_discovery_table_task = Sheduller.RemindEvery(_register_in_discovery_table_period, () => { }); _register_in_discovery_table_task = Sheduller.RemindEvery(_register_in_discovery_table_period, UpdateServiceListFromDiscovery);
} }
private void RegisterServicesInDiscovery() private void RegisterServicesInDiscovery()
{ {
var services = _serverInstances. var services = _cachee.ServerList.
Values.
Select(s => Select(s =>
{ {
var info = MessageSerializer.Copy(this._serviceInfo); var info = MessageSerializer.Copy(_owner.ServiceInfo);
info.Port = s.LocalEndpoint.Port; info.Port = s.LocalEndpoint.Port;
return info; return info;
}). }).
@ -501,8 +520,45 @@ namespace ZeroLevel.Network
_discoveryClient.Register(service); _discoveryClient.Register(service);
} }
} }
private void UpdateServiceListFromDiscovery()
{
}
#endregion #endregion
public ExClient GetConnection(string alias)
{
var address = _aliases.Get(alias);
if (address.Success)
{
return _cachee.GetClient(address.Value, true);
}
try
{
var endpoint = NetUtils.CreateIPEndPoint(alias);
return _cachee.GetClient(endpoint, true);
}
catch (Exception ex)
{
Log.Error(ex, "[Exchange.GetConnection]");
}
return null;
}
public ExClient GetConnection(IPEndPoint endpoint)
{
try
{
return _cachee.GetClient(endpoint, true);
}
catch (Exception ex)
{
Log.Error(ex, "[Exchange.GetConnection]");
}
return null;
}
#region Host service #region Host service
public IRouter UseHost() public IRouter UseHost()
{ {
@ -521,7 +577,7 @@ namespace ZeroLevel.Network
#endregion #endregion
#region Private #region Private
internal IEnumerable<ExClient> GetClientEnumerator(string serviceKey) private IEnumerable<ExClient> GetClientEnumerator(string serviceKey)
{ {
InvokeResult<IEnumerable<IPEndPoint>> candidates; InvokeResult<IEnumerable<IPEndPoint>> candidates;
try try
@ -556,7 +612,7 @@ namespace ZeroLevel.Network
} }
} }
internal IEnumerable<ExClient> GetClientEnumeratorByType(string serviceType) private IEnumerable<ExClient> GetClientEnumeratorByType(string serviceType)
{ {
InvokeResult<IEnumerable<IPEndPoint>> candidates; InvokeResult<IEnumerable<IPEndPoint>> candidates;
try try
@ -591,7 +647,7 @@ namespace ZeroLevel.Network
} }
} }
internal IEnumerable<ExClient> GetClientEnumeratorByGroup(string serviceGroup) private IEnumerable<ExClient> GetClientEnumeratorByGroup(string serviceGroup)
{ {
InvokeResult<IEnumerable<IPEndPoint>> candidates; InvokeResult<IEnumerable<IPEndPoint>> candidates;
try try
@ -631,7 +687,7 @@ namespace ZeroLevel.Network
/// <param name="serviceKey">Service key</param> /// <param name="serviceKey">Service key</param>
/// <param name="callHandler">Service call code</param> /// <param name="callHandler">Service call code</param>
/// <returns>true - service called succesfully</returns> /// <returns>true - service called succesfully</returns>
internal bool CallService(string serviceKey, Func<ExClient, bool> callHandler) private bool CallService(string serviceKey, Func<ExClient, bool> callHandler)
{ {
InvokeResult<IEnumerable<IPEndPoint>> candidates; InvokeResult<IEnumerable<IPEndPoint>> candidates;
try try
@ -678,7 +734,7 @@ namespace ZeroLevel.Network
return success; return success;
} }
internal InvokeResult CallServiceDirect(string endpoint, Func<ExClient, InvokeResult> callHandler) private InvokeResult CallServiceDirect(string endpoint, Func<ExClient, InvokeResult> callHandler)
{ {
ExClient transport; ExClient transport;
try try

@ -18,6 +18,7 @@ namespace ZeroLevel.Network
*/ */
public sealed class ServiceRouteStorage public sealed class ServiceRouteStorage
: IServiceRoutesStorage
{ {
private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim(); private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();

@ -13,6 +13,8 @@ namespace ZeroLevel.Network
private readonly ConcurrentDictionary<string, SocketServer> _serverInstances = new ConcurrentDictionary<string, SocketServer>(); private readonly ConcurrentDictionary<string, SocketServer> _serverInstances = new ConcurrentDictionary<string, SocketServer>();
private HashSet<string> _clients = new HashSet<string>(); private HashSet<string> _clients = new HashSet<string>();
internal IEnumerable<SocketServer> ServerList => _serverInstances.Values;
public ExClient GetClient(IPEndPoint endpoint, bool use_cachee, IRouter router = null) public ExClient GetClient(IPEndPoint endpoint, bool use_cachee, IRouter router = null)
{ {
if (use_cachee) if (use_cachee)

Loading…
Cancel
Save

Powered by TurnKey Linux.