2019-11-02 19:47:41 +00:00
|
|
|
using System;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using System.Threading;
|
|
|
|
using Grpc.Core;
|
|
|
|
using Aurora.Proto.Events;
|
|
|
|
using Aurora.Proto.Party;
|
2019-11-11 20:10:08 +00:00
|
|
|
using Aurora.Proto.Playback;
|
|
|
|
using Aurora.Proto.Sync;
|
2019-11-29 17:37:57 +00:00
|
|
|
using Aurora.Services.ClientService.Events;
|
|
|
|
using System.Collections.Generic;
|
2019-11-02 19:47:41 +00:00
|
|
|
|
|
|
|
namespace Aurora.Services.ClientService
|
|
|
|
{
|
2019-11-29 17:37:57 +00:00
|
|
|
|
2019-11-02 19:47:41 +00:00
|
|
|
public class ClientService : BaseService<ClientService>
|
|
|
|
{
|
|
|
|
private RemotePartyService.RemotePartyServiceClient _remotePartyClient;
|
|
|
|
private RemoteEventService.RemoteEventServiceClient _remoteEventsClient;
|
2019-11-11 20:10:08 +00:00
|
|
|
private RemotePlaybackService.RemotePlaybackServiceClient _remotePlaybackClient;
|
|
|
|
private RemoteSyncService.RemoteSyncServiceClient _remoteSyncClient;
|
|
|
|
|
2019-11-02 19:47:41 +00:00
|
|
|
private Channel _channel;
|
|
|
|
CancellationTokenSource _eventCancellationTokenSource;
|
|
|
|
|
|
|
|
public ClientService()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-11-29 17:37:57 +00:00
|
|
|
public MediaPausedEventHandler OnMediaPaused;
|
|
|
|
public NewMediaPlayingEventHandler OnNewMediaPlaying;
|
|
|
|
public PartyMemberJoinedEventHandler OnPartyMemberJoined;
|
|
|
|
public PartyMemberLeftEventHandler OnPartyMemberLeft;
|
|
|
|
public MediaResumedEventHandler OnMediaResumed;
|
2019-11-02 19:47:41 +00:00
|
|
|
|
|
|
|
public RemotePartyService.RemotePartyServiceClient RemotePartyClient
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _remotePartyClient;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public RemoteEventService.RemoteEventServiceClient RemoteEventClient
|
|
|
|
{
|
|
|
|
get { return _remoteEventsClient; }
|
|
|
|
}
|
|
|
|
|
2019-11-11 20:10:08 +00:00
|
|
|
public RemotePlaybackService.RemotePlaybackServiceClient RemotePlaybackClient
|
|
|
|
{
|
|
|
|
get { return _remotePlaybackClient; }
|
|
|
|
}
|
|
|
|
|
|
|
|
public RemoteSyncService.RemoteSyncServiceClient RemoteSyncClient
|
|
|
|
{
|
|
|
|
get { return _remoteSyncClient; }
|
|
|
|
}
|
|
|
|
|
2019-11-02 19:47:41 +00:00
|
|
|
public bool IsStarted
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _remoteEventsClient != null &&
|
2019-11-04 04:17:34 +00:00
|
|
|
_remotePartyClient != null;
|
2019-11-02 19:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-09 19:55:09 +00:00
|
|
|
public bool IsHost { get; set; }
|
|
|
|
|
2019-11-02 19:47:41 +00:00
|
|
|
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);
|
2019-11-11 20:10:08 +00:00
|
|
|
_remotePlaybackClient = new RemotePlaybackService.RemotePlaybackServiceClient(_channel);
|
|
|
|
_remoteSyncClient = new RemoteSyncService.RemoteSyncServiceClient(_channel);
|
2019-11-02 19:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async void Close()
|
|
|
|
{
|
|
|
|
_eventCancellationTokenSource.Cancel();
|
|
|
|
await _channel.ShutdownAsync();
|
|
|
|
|
|
|
|
_remotePartyClient = null;
|
|
|
|
_remoteEventsClient = null;
|
2019-11-11 20:10:08 +00:00
|
|
|
_remotePlaybackClient = null;
|
|
|
|
_remoteSyncClient = null;
|
2019-11-02 19:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Asynchronous function for processing events off of the event stream.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
public async Task GetEvents()
|
|
|
|
{
|
2019-11-30 01:51:48 +00:00
|
|
|
_eventCancellationTokenSource = new CancellationTokenSource();
|
2019-11-02 19:47:41 +00:00
|
|
|
string clientId = SettingsService.Instance.ClientId;
|
2020-01-14 00:57:02 +00:00
|
|
|
Console.WriteLine(string.Format("CLIENT {0} - GetEvents called from client with id", clientId));
|
2019-11-02 19:47:41 +00:00
|
|
|
using (AsyncServerStreamingCall<BaseEvent> eventStream = _remoteEventsClient
|
|
|
|
.GetEvents(new EventsRequest { ClientId = SettingsService.Instance.ClientId }))
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
while (!_eventCancellationTokenSource.Token.IsCancellationRequested &&
|
|
|
|
await eventStream.ResponseStream.MoveNext(_eventCancellationTokenSource.Token))
|
|
|
|
{
|
2019-11-29 17:37:57 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
BaseEvent e = new BaseEvent(eventStream.ResponseStream.Current);
|
|
|
|
|
|
|
|
Dictionary<BaseEvent.DerivedEventOneofCase, EventInfo> events = new Dictionary<BaseEvent.DerivedEventOneofCase, EventInfo>()
|
2019-11-02 19:47:41 +00:00
|
|
|
{
|
2019-11-29 17:37:57 +00:00
|
|
|
{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 }) });
|
|
|
|
}
|
2019-11-02 19:47:41 +00:00
|
|
|
}
|
2019-11-29 17:37:57 +00:00
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2020-01-14 00:57:02 +00:00
|
|
|
Console.WriteLine("Exception while parsing event ---" + ex.Message);
|
2019-11-29 17:37:57 +00:00
|
|
|
}
|
|
|
|
|
2019-11-02 19:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2020-01-14 00:57:02 +00:00
|
|
|
Console.WriteLine(string.Format("EXCEPTION while parsing events --- " + ex.Message));
|
2019-11-02 19:47:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-07 03:32:43 +00:00
|
|
|
|
|
|
|
public void StopEvents()
|
|
|
|
{
|
2019-12-02 00:14:36 +00:00
|
|
|
if (_eventCancellationTokenSource != null && !_eventCancellationTokenSource.IsCancellationRequested)
|
2019-11-07 03:32:43 +00:00
|
|
|
{
|
|
|
|
_eventCancellationTokenSource.Cancel();
|
|
|
|
}
|
|
|
|
}
|
2019-11-02 19:47:41 +00:00
|
|
|
}
|
|
|
|
}
|