using System; using System.Threading.Tasks; using System.Threading; using Grpc.Core; using Aurora.Proto.Events; using Aurora.Proto.Party; using Aurora.Proto.Playback; using Aurora.Proto.Sync; using Aurora.Services.ClientService.Events; using System.Collections.Generic; using Aurora.Services.Settings; namespace Aurora.Services.ClientService { public class ClientService : IClientService { private RemotePartyService.RemotePartyServiceClient _remotePartyClient; private RemoteEventService.RemoteEventServiceClient _remoteEventsClient; private RemotePlaybackService.RemotePlaybackServiceClient _remotePlaybackClient; private RemoteSyncService.RemoteSyncServiceClient _remoteSyncClient; private Channel _channel; private CancellationTokenSource _eventCancellationTokenSource; private ISettingsService _settingsService; public ClientService(ISettingsService settingsService) { this._settingsService = settingsService; } public MediaPausedEventHandler OnMediaPaused { get; set; } public NewMediaPlayingEventHandler OnNewMediaPlaying { get; set; } public PartyMemberJoinedEventHandler OnPartyMemberJoined { get; set; } public PartyMemberLeftEventHandler OnPartyMemberLeft { get; set; } public MediaResumedEventHandler OnMediaResumed { get; set; } public RemotePartyService.RemotePartyServiceClient RemotePartyClient { get { return _remotePartyClient; } } public RemoteEventService.RemoteEventServiceClient RemoteEventClient { get { return _remoteEventsClient; } } public RemotePlaybackService.RemotePlaybackServiceClient RemotePlaybackClient { get { return _remotePlaybackClient; } } public RemoteSyncService.RemoteSyncServiceClient RemoteSyncClient { get { return _remoteSyncClient; } } public bool IsStarted { get { return _remoteEventsClient != null && _remotePartyClient != null; } } public bool IsHost { get; set; } public void Start(string hostname, string port) { _channel = new Channel(string.Format("{0}:{1}", hostname, port), ChannelCredentials.Insecure); _remotePartyClient = new RemotePartyService.RemotePartyServiceClient(_channel); _remoteEventsClient = new RemoteEventService.RemoteEventServiceClient(_channel); _remotePlaybackClient = new RemotePlaybackService.RemotePlaybackServiceClient(_channel); _remoteSyncClient = new RemoteSyncService.RemoteSyncServiceClient(_channel); } public async void Close() { _eventCancellationTokenSource.Cancel(); await _channel.ShutdownAsync(); _remotePartyClient = null; _remoteEventsClient = null; _remotePlaybackClient = null; _remoteSyncClient = null; } /// /// Asynchronous function for processing events off of the event stream. /// /// public async Task GetEvents() { _eventCancellationTokenSource = new CancellationTokenSource(); string clientId = this._settingsService.ClientId; Console.WriteLine(string.Format("CLIENT {0} - GetEvents called from client with id", clientId)); using (AsyncServerStreamingCall eventStream = _remoteEventsClient .GetEvents(new EventsRequest { ClientId = this._settingsService.ClientId })) { try { while (!_eventCancellationTokenSource.Token.IsCancellationRequested && await eventStream.ResponseStream.MoveNext(_eventCancellationTokenSource.Token)) { try { BaseEvent e = new BaseEvent(eventStream.ResponseStream.Current); Dictionary events = new Dictionary() { {BaseEvent.DerivedEventOneofCase.MediaPausedEvent, new EventInfo(this.OnMediaPaused, typeof(MediaPausedEventArgs))}, {BaseEvent.DerivedEventOneofCase.MediaResumedEvent, new EventInfo(this.OnMediaResumed, typeof(MediaResumedEventArgs))}, {BaseEvent.DerivedEventOneofCase.NewMediaPlayingEvent, new EventInfo(this.OnNewMediaPlaying, typeof(NewMediaPlayingEventArgs))}, {BaseEvent.DerivedEventOneofCase.PartyMemberJoinedEvent, new EventInfo(this.OnPartyMemberJoined, typeof(PartyMemberJoinedEventArgs))}, {BaseEvent.DerivedEventOneofCase.PartyMemberLeftEvent, new EventInfo(this.OnPartyMemberLeft, typeof(PartyMemberLeftEventArgs))} }; events.TryGetValue(e.DerivedEventCase, out EventInfo eventInfo); if (eventInfo != null && eventInfo.Handler != null) { eventInfo.Handler.DynamicInvoke(new object[] { this, Activator.CreateInstance(eventInfo.ArgsType, new object[] { e }) }); } } catch (Exception ex) { Console.WriteLine("Exception while parsing event ---" + ex.Message); } } } catch (Exception ex) { Console.WriteLine(string.Format("EXCEPTION while parsing events --- " + ex.Message)); } } } public void StopEvents() { if (_eventCancellationTokenSource != null && !_eventCancellationTokenSource.IsCancellationRequested) { _eventCancellationTokenSource.Cancel(); } } } }