2019-07-07 21:12:13 +00:00
|
|
|
using System;
|
|
|
|
using System.Threading.Tasks;
|
2019-07-15 16:14:38 +00:00
|
|
|
using System.Threading;
|
2019-07-07 21:12:13 +00:00
|
|
|
using System.Collections.Generic;
|
|
|
|
using Aurora.Services;
|
2019-07-15 16:14:38 +00:00
|
|
|
using System.Linq;
|
|
|
|
using Aurora.Services.EventManager;
|
2019-07-07 21:12:13 +00:00
|
|
|
using Aurora.Proto.Events;
|
|
|
|
|
|
|
|
namespace Aurora.RemoteImpl
|
|
|
|
{
|
|
|
|
public class RemoteEventServiceImpl : RemoteEventService.RemoteEventServiceBase
|
|
|
|
{
|
|
|
|
public RemoteEventServiceImpl()
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// RPC for getting event stream for a particular client.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">Empty</param>
|
|
|
|
/// <param name="responseStream">The response stream</param>
|
|
|
|
/// <param name="context">gRPC client context</param>
|
|
|
|
/// <returns></returns>
|
2019-11-11 20:10:08 +00:00
|
|
|
public async override Task GetEvents(EventsRequest request,
|
|
|
|
Grpc.Core.IServerStreamWriter<BaseEvent> responseStream,
|
|
|
|
Grpc.Core.ServerCallContext context)
|
2019-07-07 21:12:13 +00:00
|
|
|
{
|
2019-07-15 16:14:38 +00:00
|
|
|
string peerId = Combine(new string[] { context.Peer, request.ClientId });
|
|
|
|
Console.WriteLine(string.Format("SERVER - Events request received from peer: {0}", peerId));
|
|
|
|
|
|
|
|
AutoResetEvent are = new AutoResetEvent(false);
|
|
|
|
Action<BaseEvent> callback = (BaseEvent bEvent) =>
|
2019-07-12 15:34:06 +00:00
|
|
|
{
|
2019-07-15 16:14:38 +00:00
|
|
|
Console.WriteLine(string.Format("SERVER - Event fired for peer: {0}", peerId));
|
2019-11-29 17:37:57 +00:00
|
|
|
//TODO need to remove callback if stream no longer exists IE. Client crashed or stopped
|
2019-07-15 16:14:38 +00:00
|
|
|
responseStream.WriteAsync(bEvent);
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
EventManager.Instance.AddEventHandler(callback, Combine(new string[] { context.Peer, request.ClientId }));
|
|
|
|
are.WaitOne();
|
2019-07-07 21:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// RPC for subscribing to remote events
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">SubscribeRequest</param>
|
|
|
|
/// <param name="context">gRPC client context</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public override Task<SubscriptionResponse> SubscribeToEvents(SubscribeRequest request, Grpc.Core.ServerCallContext context)
|
|
|
|
{
|
2019-07-15 16:14:38 +00:00
|
|
|
Console.WriteLine(string.Format("SERVER - Subscription from client with id: {0}", request.ClientId));
|
2019-07-12 15:34:06 +00:00
|
|
|
EventManager.Instance.AddSubscriptionList(Combine(new string[] { context.Peer, request.ClientId }), request.EventTypes.ToList());
|
2019-07-07 21:12:13 +00:00
|
|
|
|
|
|
|
return Task.FromResult(new SubscriptionResponse { Successful = true });
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// RPC for unsubscibing from events
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">UnsubscribeRequest</param>
|
|
|
|
/// <param name="context">gRPC client context</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public override Task<SubscriptionResponse> UnsubscribeFromEvents(UnsubscribeRequest request, Grpc.Core.ServerCallContext context)
|
|
|
|
{
|
|
|
|
EventType[] eventTypes = null;
|
|
|
|
request.EventTypes.CopyTo(eventTypes, 0);
|
|
|
|
|
2019-07-12 15:34:06 +00:00
|
|
|
EventManager.Instance.RemoveSubscriptionList(Combine(new string[] { context.Peer, request.ClientId }), eventTypes.ToList());
|
2019-07-07 21:12:13 +00:00
|
|
|
|
|
|
|
return Task.FromResult(new SubscriptionResponse { Successful = true });
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// RPC for unsubscribing from all events
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="request">UnsubscribeAllRequest</param>
|
|
|
|
/// <param name="context">gRPC client context</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public override Task<SubscriptionResponse> UnsubscribeFromAll(UnsubscribeAllRequest request, Grpc.Core.ServerCallContext context)
|
|
|
|
{
|
2019-07-12 15:34:06 +00:00
|
|
|
EventManager.Instance.RemoveAllSubscriptions(Combine(new string[] { context.Peer, request.ClientId }));
|
2019-07-07 21:12:13 +00:00
|
|
|
|
|
|
|
return Task.FromResult(new SubscriptionResponse { Successful = true });
|
|
|
|
}
|
2019-07-12 15:34:06 +00:00
|
|
|
|
|
|
|
private string Combine(string[] args)
|
|
|
|
{
|
|
|
|
string outString = "";
|
|
|
|
foreach (string arg in args)
|
|
|
|
{
|
|
|
|
if (arg == args.Last())
|
|
|
|
{
|
|
|
|
outString += arg;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
outString += arg + ":";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return outString;
|
|
|
|
}
|
2019-07-07 21:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|