using System; using System.Threading.Tasks; using System.Collections.Generic; using System.Linq; using Aurora.Services; using Aurora.Proto.Events; using Aurora.Proto.General; namespace Aurora.RemoteImpl { public class RemoteEventServiceImpl : RemoteEventService.RemoteEventServiceBase { public RemoteEventServiceImpl() { } /// /// RPC for getting event stream for a particular client. /// /// Empty /// The response stream /// gRPC client context /// public async override Task GetEvents(EventsRequest request, Grpc.Core.IServerStreamWriter responseStream, Grpc.Core.ServerCallContext context) { try { while (EventManager.Instance.GetSubscriptionCount(Combine(new string[] { context.Peer, request.ClientId })) > 0) { Console.WriteLine("Peer " + context.Peer); //TODO this causes crashes when two or more members are connected //TODO Change this to events based stream instead of a poll based... List events = EventManager.Instance.GetSessionEvents(Combine(new string[] { context.Peer, request.ClientId })); foreach (BaseEvent currentEvent in events) { await responseStream.WriteAsync(currentEvent); } } } catch (Exception ex) { Console.WriteLine("Exception caught:" + ex.Message); } } /// /// RPC for subscribing to remote events /// /// SubscribeRequest /// gRPC client context /// public override Task SubscribeToEvents(SubscribeRequest request, Grpc.Core.ServerCallContext context) { EventManager.Instance.AddSubscriptionList(Combine(new string[] { context.Peer, request.ClientId }), request.EventTypes.ToList()); return Task.FromResult(new SubscriptionResponse { Successful = true }); } /// /// RPC for unsubscibing from events /// /// UnsubscribeRequest /// gRPC client context /// public override Task UnsubscribeFromEvents(UnsubscribeRequest request, Grpc.Core.ServerCallContext context) { EventType[] eventTypes = null; request.EventTypes.CopyTo(eventTypes, 0); EventManager.Instance.RemoveSubscriptionList(Combine(new string[] { context.Peer, request.ClientId }), eventTypes.ToList()); return Task.FromResult(new SubscriptionResponse { Successful = true }); } /// /// RPC for unsubscribing from all events /// /// UnsubscribeAllRequest /// gRPC client context /// public override Task UnsubscribeFromAll(UnsubscribeAllRequest request, Grpc.Core.ServerCallContext context) { EventManager.Instance.RemoveAllSubscriptions(Combine(new string[] { context.Peer, request.ClientId })); return Task.FromResult(new SubscriptionResponse { Successful = true }); } private string Combine(string[] args) { string outString = ""; foreach (string arg in args) { if (arg == args.Last()) { outString += arg; } else { outString += arg + ":"; } } return outString; } } }