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

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

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

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

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

@ -13,7 +13,7 @@ namespace ZeroLevel.UnitTests
{
// Arrange
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_with_request = false;

@ -12,7 +12,8 @@ namespace ZeroLevel.Services.Applications
: IZeroService
{
private readonly ZeroServiceInfo _serviceInfo = new ZeroServiceInfo();
private readonly IExchange _exhange;
protected IExchange Exchange => _exhange;
public ZeroServiceInfo ServiceInfo => _serviceInfo;
public string Name { get { return _serviceInfo.Name; } private set { _serviceInfo.Name = value; } }
@ -27,11 +28,13 @@ namespace ZeroLevel.Services.Applications
protected BaseZeroService()
{
Name = GetType().Name;
_exhange = new Exchange(this);
}
protected BaseZeroService(string name)
{
Name = name;
_exhange = new Exchange(this);
}
protected abstract void StartAction();
@ -119,8 +122,6 @@ namespace ZeroLevel.Services.Applications
#endregion Config
#region Network
private readonly Exchange _exhange = new Exchange();
public void UseDiscovery()
{
@ -179,30 +180,6 @@ namespace ZeroLevel.Services.Applications
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
private static Delegate CreateDelegate(Type delegateType, MethodInfo methodInfo, object target)
{

@ -13,6 +13,8 @@ namespace ZeroLevel
string Group { get; }
string Type { get; }
ZeroServiceInfo ServiceInfo { get; }
void UseDiscovery();
void UseDiscovery(string url);
void UseDiscovery(IPEndPoint endpoint);
@ -21,9 +23,6 @@ namespace ZeroLevel
IRouter UseHost(int port);
IRouter UseHost(IPEndPoint endpoint);
ExClient ConnectToService(string url);
ExClient ConnectToService(IPEndPoint endpoint);
void ReadServiceInfo();
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
{
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>
/// Provides data exchange between services
/// </summary>
public sealed class Exchange :
IClientSet,
IDisposable
internal sealed class Exchange :
IExchange
{
private IDiscoveryClient _discoveryClient = null; // Feature расширить до нескольких discovery
private readonly ServiceRouteStorage _aliases = new ServiceRouteStorage();
private readonly ExClientServerCachee _cachee = new ExClientServerCachee();
public IServiceRoutesStorage RoutesStorage => _aliases;
private readonly IZeroService _owner;
#region Ctor
public Exchange()
public Exchange(IZeroService owner)
{
_owner = owner;
}
#endregion Ctor
@ -482,16 +502,15 @@ namespace ZeroLevel.Network
}
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()
{
var services = _serverInstances.
Values.
var services = _cachee.ServerList.
Select(s =>
{
var info = MessageSerializer.Copy(this._serviceInfo);
var info = MessageSerializer.Copy(_owner.ServiceInfo);
info.Port = s.LocalEndpoint.Port;
return info;
}).
@ -501,8 +520,45 @@ namespace ZeroLevel.Network
_discoveryClient.Register(service);
}
}
private void UpdateServiceListFromDiscovery()
{
}
#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
public IRouter UseHost()
{
@ -521,7 +577,7 @@ namespace ZeroLevel.Network
#endregion
#region Private
internal IEnumerable<ExClient> GetClientEnumerator(string serviceKey)
private IEnumerable<ExClient> GetClientEnumerator(string serviceKey)
{
InvokeResult<IEnumerable<IPEndPoint>> candidates;
try
@ -556,7 +612,7 @@ namespace ZeroLevel.Network
}
}
internal IEnumerable<ExClient> GetClientEnumeratorByType(string serviceType)
private IEnumerable<ExClient> GetClientEnumeratorByType(string serviceType)
{
InvokeResult<IEnumerable<IPEndPoint>> candidates;
try
@ -591,7 +647,7 @@ namespace ZeroLevel.Network
}
}
internal IEnumerable<ExClient> GetClientEnumeratorByGroup(string serviceGroup)
private IEnumerable<ExClient> GetClientEnumeratorByGroup(string serviceGroup)
{
InvokeResult<IEnumerable<IPEndPoint>> candidates;
try
@ -631,7 +687,7 @@ namespace ZeroLevel.Network
/// <param name="serviceKey">Service key</param>
/// <param name="callHandler">Service call code</param>
/// <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;
try
@ -678,7 +734,7 @@ namespace ZeroLevel.Network
return success;
}
internal InvokeResult CallServiceDirect(string endpoint, Func<ExClient, InvokeResult> callHandler)
private InvokeResult CallServiceDirect(string endpoint, Func<ExClient, InvokeResult> callHandler)
{
ExClient transport;
try

@ -7,7 +7,7 @@ using ZeroLevel.Services.Collections;
namespace ZeroLevel.Network
{
/*
/*
One IPEndpoint binded with one service.
Service can have one key, one type, one group.
Therefore IPEndpoint can be binded with one key, one type and one group.
@ -18,6 +18,7 @@ namespace ZeroLevel.Network
*/
public sealed class ServiceRouteStorage
: IServiceRoutesStorage
{
private readonly ReaderWriterLockSlim _lock = new ReaderWriterLockSlim();

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

Loading…
Cancel
Save

Powered by TurnKey Linux.