You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Services/Network/Exchange.cs

665 lines
27 KiB

6 years ago
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace ZeroLevel.Network
6 years ago
{
/*
6 years ago
/// <summary>
6 years ago
/// Provides data exchange between services
6 years ago
/// </summary>
public sealed class Exchange :
IDisposable
{
private readonly IDiscoveryClient _discoveryClient;
private readonly ExServiceHost _host;
#region Ctor
6 years ago
public Exchange(IDiscoveryClient discoveryClient)
{
this._discoveryClient = discoveryClient ?? throw new ArgumentNullException(nameof(discoveryClient));
this._host = new ExServiceHost(this._discoveryClient);
}
#endregion Ctor
6 years ago
/// <summary>
6 years ago
/// Registration service
6 years ago
/// </summary>
public IExchangeService RegisterService(IExchangeService service)
6 years ago
{
return _host.RegisterService(service);
}
public IExchangeService RegisterService(ExServiceInfo service)
6 years ago
{
return _host.RegisterService(service);
}
#region Balanced send
6 years ago
/// <summary>
6 years ago
/// Sending a message to the service
6 years ago
/// </summary>
6 years ago
/// <param name="serviceKey">Service key</param>
/// <param name="inbox">Inbox name</param>
/// <param name="data">Message</param>
6 years ago
/// <returns></returns>
public bool Send<T>(string serviceKey, string inbox, T data)
{
try
{
return _host.CallService(serviceKey, (endpoint, transport) => transport.Send<T>(inbox, data).Success);
}
catch (Exception ex)
{
Log.SystemError(ex, $"[Exchange] Error send data in service '{serviceKey}'. Inbox '{inbox}'");
}
return false;
}
6 years ago
public bool Send<T>(string serviceKey, T data) => Send(serviceKey, BaseSocket.DEFAULT_MESSAGE_INBOX, data);
#endregion Balanced send
6 years ago
#region Balanced request
6 years ago
public Tresp Request<Treq, Tresp>(string serviceKey, string inbox, Treq data)
{
Tresp response = default(Tresp);
try
{
if (false == _host.CallService(serviceKey, (endpoint, transport) =>
{
try
{
using (var waiter = new ManualResetEventSlim(false))
{
if (false == transport.Request<Treq, Tresp>(inbox, data, resp =>
{
response = resp;
waiter.Set();
}).Success)
{
return false;
}
6 years ago
if (false == waiter.Wait(BaseSocket.MAX_REQUEST_TIME_MS))
6 years ago
{
return false;
}
}
return true;
}
catch (Exception ex)
{
Log.SystemError(ex, $"[Exchange] Error request to service '{serviceKey}'. Inbox '{inbox}'");
}
return false;
}))
{
Log.SystemWarning($"[Exchange] No responce on request. Service key '{serviceKey}'. Inbox '{inbox}'");
}
}
catch (Exception ex)
{
Log.SystemError(ex, $"[Exchange] Error request to service '{serviceKey}'. Inbox '{inbox}'");
}
return response;
}
public Tresp Request<Tresp>(string serviceKey, string inbox)
{
Tresp response = default(Tresp);
try
{
if (false == _host.CallService(serviceKey, (endpoint, transport) =>
{
try
{
using (var waiter = new ManualResetEventSlim(false))
{
if (false == transport.Request<Tresp>(inbox, resp =>
{
response = resp;
waiter.Set();
}).Success)
{
return false;
}
6 years ago
if (false == waiter.Wait(BaseSocket.MAX_REQUEST_TIME_MS))
6 years ago
{
return false;
}
}
return true;
}
catch (Exception ex)
{
Log.SystemError(ex, $"[Exchange] Error request to service '{serviceKey}'. Inbox '{inbox}'");
}
return false;
}))
{
Log.SystemWarning($"[Exchange] No responce on request. Service key '{serviceKey}'. Inbox '{inbox}'");
}
}
catch (Exception ex)
{
Log.SystemError(ex, $"[Exchange] Error request to service '{serviceKey}'. Inbox '{inbox}'");
}
return response;
}
public Tresp Request<Treq, Tresp>(string serviceKey, Treq data) =>
6 years ago
Request<Treq, Tresp>(serviceKey, BaseSocket.DEFAULT_REQUEST_INBOX, data);
6 years ago
public Tresp Request<Tresp>(string serviceKey) =>
6 years ago
Request<Tresp>(serviceKey, BaseSocket.DEFAULT_REQUEST_INBOX);
#endregion Balanced request
6 years ago
#region Direct request
6 years ago
public Tresp RequestDirect<Treq, Tresp>(string endpoint, string serviceKey, string inbox, Treq data)
{
Tresp response = default(Tresp);
try
{
if (false == _host.CallServiceDirect(endpoint, serviceKey, (transport) =>
{
try
{
using (var waiter = new ManualResetEventSlim(false))
{
if (false == transport.Request<Treq, Tresp>(inbox, data, resp =>
{
response = resp;
waiter.Set();
}).Success)
{
return false;
}
6 years ago
if (false == waiter.Wait(BaseSocket.MAX_REQUEST_TIME_MS))
6 years ago
{
return false;
}
}
return true;
}
catch (Exception ex)
{
Log.SystemError(ex, $"[Exchange] Error direct request to '{endpoint}'. Service key '{serviceKey}'. Inbox '{inbox}'");
}
return false;
}))
{
Log.SystemWarning($"[Exchange] No responce on direct request to '{endpoint}'. Service key '{serviceKey}'. Inbox '{inbox}'");
}
}
catch (Exception ex)
{
Log.SystemError(ex, $"[Exchange] Error direct request to '{endpoint}'. Service key '{serviceKey}'. Inbox '{inbox}'");
}
return response;
}
public Tresp RequestDirect<Tresp>(string endpoint, string serviceKey, string inbox)
{
Tresp response = default(Tresp);
try
{
if (false == _host.CallServiceDirect(endpoint, serviceKey, (transport) =>
{
try
{
using (var waiter = new ManualResetEventSlim(false))
{
if (false == transport.Request<Tresp>(inbox, resp =>
{
response = resp;
waiter.Set();
}).Success)
{
return false;
}
6 years ago
if (false == waiter.Wait(BaseSocket.MAX_REQUEST_TIME_MS))
6 years ago
{
return false;
}
}
return true;
}
catch (Exception ex)
{
Log.SystemError(ex, $"[Exchange] Error direct request to '{endpoint}'. Service key '{serviceKey}'. Inbox '{inbox}'");
}
return false;
}))
{
Log.SystemWarning($"[Exchange] No responce on direct request to '{endpoint}'. Service key '{serviceKey}'. Inbox '{inbox}'");
}
}
catch (Exception ex)
{
Log.SystemError(ex, $"[Exchange] Error direct request to service '{serviceKey}'. Inbox '{inbox}'");
}
return response;
}
public Tresp RequestDirect<Treq, Tresp>(string endpoint, string serviceKey, Treq data) =>
6 years ago
RequestDirect<Treq, Tresp>(endpoint, serviceKey, BaseSocket.DEFAULT_REQUEST_INBOX, data);
6 years ago
public Tresp RequestDirect<Tresp>(string endpoint, string serviceKey) =>
6 years ago
RequestDirect<Tresp>(endpoint, serviceKey, BaseSocket.DEFAULT_REQUEST_INBOX);
#endregion Direct request
6 years ago
#region Broadcast
6 years ago
/// <summary>
6 years ago
/// Sending a message to all services with the specified key to the specified handler
6 years ago
/// </summary>
6 years ago
/// <typeparam name="T">Message type</typeparam>
/// <param name="serviceKey">Service key</param>
/// <param name="inbox">Inbox name</param>
/// <param name="data">Message</param>
/// <returns>true - on successful submission</returns>
6 years ago
public bool SendBroadcast<T>(string serviceKey, string inbox, T data)
{
try
{
foreach (var client in _host.GetClientEnumerator(serviceKey))
{
Task.Run(() =>
{
try
{
client.Send(inbox, data);
}
catch (Exception ex)
{
Log.SystemError(ex, $"[Exchange] Error broadcast send data to services '{serviceKey}'. Inbox '{inbox}'");
}
});
}
}
catch (Exception ex)
{
Log.SystemError(ex, $"[Exchange] Error broadcast send data in service '{serviceKey}'. Inbox '{inbox}'");
}
return false;
}
6 years ago
/// <summary>
6 years ago
/// Sending a message to all services with the specified key, to the default handler
6 years ago
/// </summary>
6 years ago
/// <typeparam name="T">Message type</typeparam>
/// <param name="serviceKey">Service key</param>
/// <param name="data">Message</param>
/// <returns>true - on successful submission</returns>
6 years ago
public bool SendBroadcast<T>(string serviceKey, T data) => SendBroadcast(serviceKey, BaseSocket.DEFAULT_MESSAGE_INBOX, data);
6 years ago
/// <summary>
6 years ago
/// Sending a message to all services of a specific type to the specified handler
6 years ago
/// </summary>
6 years ago
/// <typeparam name="T">Message type</typeparam>
/// <param name="serviceType">Service type</param>
/// <param name="inbox">Inbox name</param>
/// <param name="data">Message</param>
/// <returns>true - on successful submission</returns>
6 years ago
public bool SendBroadcastByType<T>(string serviceType, string inbox, T data)
{
try
{
foreach (var client in _host.GetClientEnumeratorByType(serviceType))
{
Task.Run(() =>
{
try
{
client.Send(inbox, data);
}
catch (Exception ex)
{
Log.SystemError(ex, $"[Exchange] Error broadcast send data to services with type '{serviceType}'. Inbox '{inbox}'");
}
});
}
}
catch (Exception ex)
{
Log.SystemError(ex, $"[Exchange] Error broadcast send data to services with type '{serviceType}'. Inbox '{inbox}'");
}
return false;
}
6 years ago
/// <summary>
6 years ago
/// Sending a message to all services of a particular type, to the default handler
6 years ago
/// </summary>
6 years ago
/// <typeparam name="T">Message type</typeparam>
/// <param name="serviceType">Service type</param>
/// <param name="data">Message</param>
/// <returns>true - on successful submission</returns>
6 years ago
public bool SendBroadcastByType<T>(string serviceType, T data) =>
6 years ago
SendBroadcastByType(serviceType, BaseSocket.DEFAULT_MESSAGE_INBOX, data);
6 years ago
/// <summary>
6 years ago
/// Sending a message to all services of a specific group to the specified handler
6 years ago
/// </summary>
6 years ago
/// <typeparam name="T">Message type</typeparam>
/// <param name="serviceGroup">Service group</param>
/// <param name="inbox">Inbox name</param>
/// <param name="data">Message</param>
/// <returns>true - on successful submission</returns>
6 years ago
public bool SendBroadcastByGroup<T>(string serviceGroup, string inbox, T data)
{
try
{
foreach (var client in _host.GetClientEnumeratorByGroup(serviceGroup))
{
Task.Run(() =>
{
try
{
client.Send(inbox, data);
}
catch (Exception ex)
{
Log.SystemError(ex, $"[Exchange] Error broadcast send data to services with type '{serviceGroup}'. Inbox '{inbox}'");
}
});
}
}
catch (Exception ex)
{
Log.SystemError(ex, $"[Exchange] Error broadcast send data to services with type '{serviceGroup}'. Inbox '{inbox}'");
}
return false;
}
6 years ago
/// <summary>
6 years ago
/// Sending a message to all services of a specific group in the default handler
6 years ago
/// </summary>
6 years ago
/// <typeparam name="T">Message type</typeparam>
/// <param name="serviceGroup">Service group</param>
/// <param name="data">Messsage</param>
/// <returns>true - on successful submission</returns>
6 years ago
public bool SendBroadcastByGroup<T>(string serviceGroup, T data) =>
6 years ago
SendBroadcastByGroup(serviceGroup, BaseSocket.DEFAULT_MESSAGE_INBOX, data);
6 years ago
/// <summary>
6 years ago
/// Broadcast polling services by key
6 years ago
/// </summary>
6 years ago
/// <typeparam name="Treq">Request message type</typeparam>
/// <typeparam name="Tresp">Response message type</typeparam>
/// <param name="serviceKey">Service key</param>
/// <param name="inbox">Inbox name</param>
/// <param name="data">Request message</param>
/// <param name="responseHandler">Response handler</param>
/// <returns>true - in case of successful mailing</returns>
6 years ago
public IEnumerable<Tresp> RequestBroadcast<Treq, Tresp>(string serviceKey, string inbox, Treq data)
{
try
{
var clients = _host.GetClientEnumerator(serviceKey).ToList();
return _RequestBroadcast<Treq, Tresp>(clients, inbox, data);
}
catch (Exception ex)
{
Log.SystemError(ex, $"[Exchange] Error broadcast request to service '{serviceKey}'. Inbox '{inbox}'");
}
return Enumerable.Empty<Tresp>();
}
6 years ago
/// <summary>
6 years ago
/// Broadcast polling services by key, without message request
6 years ago
/// </summary>
6 years ago
/// <typeparam name="Tresp">Response message type</typeparam>
/// <param name="serviceKey">Service key</param>
/// <param name="inbox">Inbox name</param>
/// <param name="responseHandler">Response handler</param>
/// <returns>true - in case of successful mailing</returns>
6 years ago
public IEnumerable<Tresp> RequestBroadcast<Tresp>(string serviceKey, string inbox)
{
try
{
var clients = _host.GetClientEnumerator(serviceKey).ToList();
return _RequestBroadcast<Tresp>(clients, inbox);
}
catch (Exception ex)
{
Log.SystemError(ex, $"[Exchange] Error broadcast request to service '{serviceKey}'. Inbox '{inbox}'");
}
return Enumerable.Empty<Tresp>();
}
6 years ago
/// <summary>
6 years ago
/// Broadcast polling services by key, to default handler
6 years ago
/// </summary>
6 years ago
/// <typeparam name="Treq">Request message type</typeparam>
/// <typeparam name="Tresp">Response message type</typeparam>
/// <param name="serviceKey">Service key</param>
/// <param name="data">Request message</param>
/// <param name="responseHandler">Response handler</param>
/// <returns>true - in case of successful mailing</returns>
6 years ago
public IEnumerable<Tresp> RequestBroadcast<Treq, Tresp>(string serviceKey, Treq data) =>
6 years ago
RequestBroadcast<Treq, Tresp>(serviceKey, BaseSocket.DEFAULT_REQUEST_INBOX, data);
6 years ago
/// <summary>
6 years ago
/// Broadcast polling of services by key, without message of request, to default handler
6 years ago
/// </summary>
6 years ago
/// <typeparam name="Tresp">Response message type</typeparam>
/// <param name="serviceKey">Service key</param>
/// <param name="responseHandler">Response handler</param>
/// <returns>true - in case of successful mailing</returns>
6 years ago
public IEnumerable<Tresp> RequestBroadcast<Tresp>(string serviceKey) =>
6 years ago
RequestBroadcast<Tresp>(serviceKey, BaseSocket.DEFAULT_REQUEST_INBOX);
6 years ago
/// <summary>
6 years ago
/// Broadcast polling services by type of service
6 years ago
/// </summary>
6 years ago
/// <typeparam name="Treq">Request message type</typeparam>
/// <typeparam name="Tresp">Response message type</typeparam>
/// <param name="serviceType">Service type</param>
/// <param name="inbox">Inbox name</param>
/// <param name="data">Request message</param>
/// <param name="responseHandler">Response handler</param>
/// <returns>true - in case of successful mailing</returns>
6 years ago
public IEnumerable<Tresp> RequestBroadcastByType<Treq, Tresp>(string serviceType, string inbox, Treq data)
{
try
{
var clients = _host.GetClientEnumeratorByType(serviceType).ToList();
return _RequestBroadcast<Treq, Tresp>(clients, inbox, data);
}
catch (Exception ex)
{
Log.SystemError(ex, $"[Exchange] Error broadcast request to service by type '{serviceType}'. Inbox '{inbox}'");
}
return Enumerable.Empty<Tresp>();
}
6 years ago
/// <summary>
6 years ago
/// Broadcast polling of services by type of service, without a request message
6 years ago
/// </summary>
6 years ago
/// <typeparam name="Tresp">Response message type</typeparam>
/// <param name="serviceType">Service type</param>
/// <param name="inbox">Inbox name</param>
/// <param name="responseHandler">Response handler</param>
/// <returns>true - in case of successful mailing</returns>
6 years ago
public IEnumerable<Tresp> RequestBroadcastByType<Tresp>(string serviceType, string inbox)
{
try
{
var clients = _host.GetClientEnumeratorByType(serviceType).ToList();
return _RequestBroadcast<Tresp>(clients, inbox);
}
catch (Exception ex)
{
Log.SystemError(ex, $"[Exchange] Error broadcast request to service by type '{serviceType}'. Inbox '{inbox}'");
}
return Enumerable.Empty<Tresp>();
}
6 years ago
/// <summary>
6 years ago
/// Broadcast polling services by type of service, in the default handler
6 years ago
/// </summary>
6 years ago
/// <typeparam name="Treq">Request message type</typeparam>
/// <typeparam name="Tresp">Response message type</typeparam>
/// <param name="serviceType">Service type</param>
/// <param name="data">Request message</param>
/// <param name="responseHandler">Response handler</param>
/// <returns>true - in case of successful mailing</returns>
6 years ago
public IEnumerable<Tresp> RequestBroadcastByType<Treq, Tresp>(string serviceType, Treq data) =>
6 years ago
RequestBroadcastByType<Treq, Tresp>(serviceType, BaseSocket.DEFAULT_REQUEST_INBOX, data);
6 years ago
/// <summary>
6 years ago
/// Broadcast polling services by type, without message request, in the default handler
6 years ago
/// </summary>
6 years ago
/// <typeparam name="Tresp">Response message type</typeparam>
/// <param name="serviceType">Service type</param>
/// <param name="responseHandler">Response handler</param>
/// <returns>true - in case of successful mailing</returns>
6 years ago
public IEnumerable<Tresp> RequestBroadcastByType<Tresp>(string serviceType) =>
6 years ago
RequestBroadcastByType<Tresp>(serviceType, BaseSocket.DEFAULT_REQUEST_INBOX);
6 years ago
/// <summary>
6 years ago
/// Broadcast polling services for a group of services
6 years ago
/// </summary>
6 years ago
/// <typeparam name="Treq">Request message type</typeparam>
/// <typeparam name="Tresp">Response message type</typeparam>
/// <param name="serviceGroup">Service group</param>
/// <param name="inbox">Inbox name</param>
/// <param name="data">Request message</param>
/// <param name="responseHandler">Response handler</param>
/// <returns>true - in case of successful mailing</returns>
6 years ago
public IEnumerable<Tresp> RequestBroadcastByGroup<Treq, Tresp>(string serviceGroup, string inbox, Treq data)
{
try
{
var clients = _host.GetClientEnumeratorByGroup(serviceGroup).ToList();
return _RequestBroadcast<Treq, Tresp>(clients, inbox, data);
}
catch (Exception ex)
{
Log.SystemError(ex, $"[Exchange] Error broadcast request to service by type '{serviceGroup}'. Inbox '{inbox}'");
}
return Enumerable.Empty<Tresp>();
}
6 years ago
/// <summary>
6 years ago
/// Broadcast polling services for a group of services, without prompting
6 years ago
/// </summary>
6 years ago
/// <typeparam name="Tresp">Response message type</typeparam>
/// <param name="serviceGroup">Service group</param>
/// <param name="inbox">Inbox name</param>
/// <param name="responseHandler">Response handler</param>
/// <returns>true - in case of successful mailing</returns>
6 years ago
public IEnumerable<Tresp> RequestBroadcastByGroup<Tresp>(string serviceGroup, string inbox)
{
try
{
var clients = _host.GetClientEnumeratorByGroup(serviceGroup).ToList();
return _RequestBroadcast<Tresp>(clients, inbox);
}
catch (Exception ex)
{
Log.SystemError(ex, $"[Exchange] Error broadcast request to service by type '{serviceGroup}'. Inbox '{inbox}'");
}
return Enumerable.Empty<Tresp>();
}
6 years ago
/// <summary>
6 years ago
/// Broadcast polling services by service group to default handler
6 years ago
/// </summary>
6 years ago
/// <typeparam name="Treq">Request message type</typeparam>
/// <typeparam name="Tresp">Response message type</typeparam>
/// <param name="serviceGroup">Service group</param>
/// <param name="data">Request message</param>
/// <param name="responseHandler">Response handler</param>
/// <returns>true - in case of successful mailing</returns>
6 years ago
public IEnumerable<Tresp> RequestBroadcastByGroup<Treq, Tresp>(string serviceGroup, Treq data) =>
6 years ago
RequestBroadcastByGroup<Treq, Tresp>(serviceGroup, BaseSocket.DEFAULT_REQUEST_INBOX, data);
6 years ago
/// <summary>
6 years ago
///Broadcast polling services for a group of services, without sending a request, to the default handler
6 years ago
/// </summary>
6 years ago
/// <typeparam name="Tresp">Response message type</typeparam>
/// <param name="serviceGroup">Service group</param>
/// <param name="responseHandler">Response handler</param>
/// <returns>true - in case of successful mailing</returns>
6 years ago
public IEnumerable<Tresp> RequestBroadcastByGroup<Tresp>(string serviceGroup) =>
6 years ago
RequestBroadcastByGroup<Tresp>(serviceGroup, BaseSocket.DEFAULT_REQUEST_INBOX);
6 years ago
#region Private
private IEnumerable<Tresp> _RequestBroadcast<Treq, Tresp>(List<ExClient> clients, string inbox, Treq data)
6 years ago
{
var response = new List<Tresp>();
using (var waiter = new CountdownEvent(clients.Count))
{
foreach (var client in clients)
{
Task.Run(() =>
{
try
{
if (false == client.Request<Treq, Tresp>(inbox, data, resp => { waiter.Signal(); response.Add(resp); }).Success)
{
waiter.Signal();
}
}
catch (Exception ex)
{
6 years ago
Log.SystemError(ex, $"[Exchange] Error direct request to service '{client.EndPoint}' in broadcast request. Inbox '{inbox}'");
6 years ago
waiter.Signal();
}
});
}
6 years ago
waiter.Wait(BaseSocket.MAX_REQUEST_TIME_MS);
6 years ago
}
return response;
}
private IEnumerable<Tresp> _RequestBroadcast<Tresp>(List<ExClient> clients, string inbox)
6 years ago
{
var response = new List<Tresp>();
using (var waiter = new CountdownEvent(clients.Count))
{
foreach (var client in clients)
{
Task.Run(() =>
{
try
{
if (false == client.Request<Tresp>(inbox, resp => { waiter.Signal(); response.Add(resp); }).Success)
{
waiter.Signal();
}
}
catch (Exception ex)
{
6 years ago
Log.SystemError(ex, $"[Exchange] Error direct request to service '{client.EndPoint}' in broadcast request. Inbox '{inbox}'");
6 years ago
waiter.Signal();
}
});
}
6 years ago
waiter.Wait(BaseSocket.MAX_REQUEST_TIME_MS);
6 years ago
}
return response;
}
#endregion Private
#endregion Broadcast
6 years ago
public void Dispose()
{
this._host.Dispose();
}
}
*/
}

Powered by TurnKey Linux.