Removed old proto definitions (not tested)

This commit is contained in:
watsonb8
2020-02-02 16:49:01 -05:00
parent 2a7e10364e
commit 51ab2d9c42
48 changed files with 581 additions and 1645 deletions

View File

@ -1,22 +0,0 @@
using System;
namespace Aurora.Services
{
public abstract class BaseService<T> where T : class
{
private static volatile Lazy<T> _instance = new Lazy<T>(() => CreateInstanceOfT());
public static T Instance
{
get { return _instance.Value; }
}
private static T CreateInstanceOfT()
{
return Activator.CreateInstance(typeof(T), true) as T;
}
}
}

View File

@ -0,0 +1,46 @@
using Grpc.Core;
using Aurora.Proto.Party;
using Aurora.Services.Settings;
namespace Aurora.Services.Client
{
public class ClientService : IClientService
{
private RemotePartyService.RemotePartyServiceClient _remotePartyClient;
private Channel _channel;
private ISettingsService _settingsService;
public ClientService(ISettingsService settingsService)
{
this._settingsService = settingsService;
}
public bool IsStarted
{
get
{
return _remotePartyClient != null;
}
}
public RemotePartyService.RemotePartyServiceClient RemotePartyServiceClient
{
get { return this._remotePartyClient; }
}
public void Start(string hostname, string port)
{
_channel = new Channel(string.Format("{0}:{1}", hostname, port), ChannelCredentials.Insecure);
_remotePartyClient = new RemotePartyService.RemotePartyServiceClient(_channel);
}
public async void Close()
{
await _channel.ShutdownAsync();
_remotePartyClient = null;
}
}
}

View File

@ -0,0 +1,15 @@
using Aurora.Proto.Party;
namespace Aurora.Services.Client
{
public interface IClientService
{
bool IsStarted { get; }
RemotePartyService.RemotePartyServiceClient RemotePartyServiceClient { get; }
void Start(string hostname, string port);
void Close();
}
}

View File

@ -1,152 +0,0 @@
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;
}
/// <summary>
/// Asynchronous function for processing events off of the event stream.
/// </summary>
/// <returns></returns>
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<BaseEvent> 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<BaseEvent.DerivedEventOneofCase, EventInfo> events = new Dictionary<BaseEvent.DerivedEventOneofCase, EventInfo>()
{
{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();
}
}
}
}

View File

@ -1,25 +0,0 @@
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;
namespace Aurora.Services.ClientService
{
public class EventInfo
{
public EventInfo(Delegate handler, Type argsType)
{
this.Handler = handler;
ArgsType = argsType;
}
public Delegate Handler { get; private set; }
public Type ArgsType { get; private set; }
}
}

View File

@ -1,16 +0,0 @@
using System;
using Aurora.Proto.Events;
namespace Aurora.Services.ClientService.Events
{
public delegate void MediaPausedEventHandler(object sender, MediaPausedEventArgs e);
public class MediaPausedEventArgs
{
public MediaPausedEvent Event { get; private set; }
public MediaPausedEventArgs(BaseEvent e)
{
Event = e.MediaPausedEvent;
}
}
}

View File

@ -1,16 +0,0 @@
using System;
using Aurora.Proto.Events;
namespace Aurora.Services.ClientService
{
public delegate void MediaResumedEventHandler(object sender, MediaResumedEventArgs e);
public class MediaResumedEventArgs
{
public MediaResumedEvent Event { get; private set; }
public MediaResumedEventArgs(BaseEvent e)
{
Event = e.MediaResumedEvent;
}
}
}

View File

@ -1,16 +0,0 @@
using System;
using Aurora.Proto.Events;
namespace Aurora.Services.ClientService.Events
{
public delegate void NewMediaPlayingEventHandler(object sender, NewMediaPlayingEventArgs e);
public class NewMediaPlayingEventArgs
{
public NewMediaPlayingEvent Event { get; private set; }
public NewMediaPlayingEventArgs(BaseEvent e)
{
Event = e.NewMediaPlayingEvent;
}
}
}

View File

@ -1,16 +0,0 @@
using System;
using Aurora.Proto.Events;
namespace Aurora.Services.ClientService.Events
{
public delegate void PartyMemberJoinedEventHandler(object sender, PartyMemberJoinedEventArgs e);
public class PartyMemberJoinedEventArgs
{
public PartyMemberJoinedEvent Event { get; private set; }
public PartyMemberJoinedEventArgs(BaseEvent e)
{
Event = e.PartyMemberJoinedEvent;
}
}
}

View File

@ -1,16 +0,0 @@
using System;
using Aurora.Proto.Events;
namespace Aurora.Services.ClientService.Events
{
public delegate void PartyMemberLeftEventHandler(object sender, PartyMemberLeftEventArgs e);
public class PartyMemberLeftEventArgs
{
public PartyMemberLeftEvent Event { get; private set; }
public PartyMemberLeftEventArgs(BaseEvent e)
{
Event = e.PartyMemberLeftEvent;
}
}
}

View File

@ -1,42 +0,0 @@
using Aurora.Services.ClientService.Events;
using Aurora.Proto.Events;
using Aurora.Proto.Party;
using Aurora.Proto.Playback;
using Aurora.Proto.Sync;
using System.Threading.Tasks;
namespace Aurora.Services.ClientService
{
public interface IClientService
{
MediaPausedEventHandler OnMediaPaused { get; set; }
NewMediaPlayingEventHandler OnNewMediaPlaying { get; set; }
PartyMemberJoinedEventHandler OnPartyMemberJoined { get; set; }
PartyMemberLeftEventHandler OnPartyMemberLeft { get; set; }
MediaResumedEventHandler OnMediaResumed { get; set; }
RemotePartyService.RemotePartyServiceClient RemotePartyClient { get; }
RemoteEventService.RemoteEventServiceClient RemoteEventClient { get; }
RemotePlaybackService.RemotePlaybackServiceClient RemotePlaybackClient { get; }
RemoteSyncService.RemoteSyncServiceClient RemoteSyncClient { get; }
bool IsStarted { get; }
bool IsHost { get; set; }
void Start(string hostname, string port);
void Close();
/// <summary>
/// Asynchronous function for processing events off of the event stream.
/// </summary>
/// <returns></returns>
Task GetEvents();
void StopEvents();
}
}

View File

@ -2,9 +2,9 @@ using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Aurora.Proto.PartyV2;
using Aurora.Proto.Party;
namespace Aurora.Services.Server.EventManager
namespace Aurora.Services.EventManager
{
public class EventAction
{

View File

@ -1,23 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Aurora.Proto.Events;
using Aurora.Proto.Party;
namespace Aurora.Services.EventManager
{
public class EventAction
{
public EventAction(Action<BaseEvent> callback, Action cancel)
{
Callback = callback;
Cancel = cancel;
}
public Action<BaseEvent> Callback { get; set; }
public Action Cancel { get; set; }
}
public class EventManager : BaseService<EventManager>
public class EventManager : IEventManager
{
#region Fields
private Dictionary<string, List<EventType>> _subscriptionList;
@ -38,32 +27,32 @@ namespace Aurora.Services.EventManager
#region Public Methods
/// <summary>
/// Get the list of event type subscriptions for a given session id.
/// Get the list of event type subscriptions for a given sessionIdentifier id.
/// </summary>
/// <param name="session">Session Id</param>
/// <param name="sessionIdentifier">sessionIdentifier Id</param>
/// <returns></returns>
public List<EventType> GetSubscriptionList(string session)
public List<EventType> GetSubscriptionList(string sessionIdentifier)
{
List<EventType> eventList = new List<EventType>();
if (_subscriptionList.ContainsKey(session))
if (_subscriptionList.ContainsKey(sessionIdentifier))
{
_subscriptionList.TryGetValue(session, out eventList);
_subscriptionList.TryGetValue(sessionIdentifier, out eventList);
}
return eventList;
}
/// <summary>
/// Get the number of event subscriptions for a given session
/// Get the number of event subscriptions for a given sessionIdentifier
/// </summary>
/// <param name="session">Session Id</param>
/// <param name="sessionIdentifier">sessionIdentifier Id</param>
/// <returns></returns>
public int GetSubscriptionCount(string session)
public int GetSubscriptionCount(string sessionIdentifier)
{
List<EventType> eventList = new List<EventType>();
if (_subscriptionList.ContainsKey(session))
if (_subscriptionList.ContainsKey(sessionIdentifier))
{
_subscriptionList.TryGetValue(session, out eventList);
_subscriptionList.TryGetValue(sessionIdentifier, out eventList);
}
return eventList.Count();
@ -72,24 +61,24 @@ namespace Aurora.Services.EventManager
/// <summary>
/// Add a new subscription
/// </summary>
/// <param name="session"></param>
/// <param name="sessionIdentifier"></param>
/// <param name="type"></param>
public bool AddSubscription(string session, EventType type)
public bool AddSubscription(string sessionIdentifier, EventType type)
{
bool success = false;
lock (_subscriptionList)
{
if (!_subscriptionList.ContainsKey(session))
if (!_subscriptionList.ContainsKey(sessionIdentifier))
{
//Add session to subscription list
//Add sessionIdentifier to subscription list
List<EventType> eventList = new List<EventType>();
eventList.Add(type);
_subscriptionList.Add(session, eventList);
_subscriptionList.Add(sessionIdentifier, eventList);
success = true;
}
else
{
_subscriptionList.TryGetValue(session, out List<EventType> eventList);
_subscriptionList.TryGetValue(sessionIdentifier, out List<EventType> eventList);
if (eventList != null)
{
eventList.Add(type);
@ -104,79 +93,82 @@ namespace Aurora.Services.EventManager
/// <summary>
/// Add a list of subscriptions. This unsubscribes from unused events.
/// </summary>
/// <param name="session">The browser session id.</param>
/// <param name="sessionIdentifier">The browser sessionIdentifier id.</param>
/// <param name="types">The list of event types to subscribe to.</param>
public void AddSubscriptionList(string session, List<EventType> types)
public void AddSubscriptionList(string sessionIdentifier, List<EventType> types)
{
RemoveAllSubscriptions(session);
RemoveAllSubscriptions(sessionIdentifier);
foreach (EventType e in types)
{
AddSubscription(session, e);
AddSubscription(sessionIdentifier, e);
}
}
/// <summary>
/// Unsubscribe from a given event type.
/// </summary>
/// <param name="session">Session Id</param>
/// <param name="sessionIdentifier">sessionIdentifier Id</param>
/// <param name="type">Event Type to be removed</param>
public void RemoveSubscription(string session, EventType type)
public void RemoveSubscription(string sessionIdentifier, EventType type)
{
lock (_subscriptionList)
{
if (_subscriptionList.ContainsKey(session))
if (_subscriptionList.ContainsKey(sessionIdentifier))
{
List<EventType> eventTypeList;
_subscriptionList.TryGetValue(session, out eventTypeList);
_subscriptionList.TryGetValue(sessionIdentifier, out eventTypeList);
if (eventTypeList != null && eventTypeList.Contains(type))
{
eventTypeList.Remove(type);
//base.LogInformation(string.Format("Subscription removed for event type {0} subscription on session {1}", type.ToString(), session));
//base.LogInformation(string.Format("Subscription removed for event type {0} subscription on sessionIdentifier {1}", type.ToString(), sessionIdentifier));
}
}
}
}
public void RemoveSubscriptionList(string session, List<EventType> types)
public void RemoveSubscriptionList(string sessionIdentifier, List<EventType> types)
{
foreach (EventType e in types)
{
RemoveSubscription(session, e);
RemoveSubscription(sessionIdentifier, e);
}
}
/// <summary>
/// Remove all subscriptons for a given session.
/// Remove all subscriptons for a given sessionIdentifier.
/// </summary>
/// <param name="session">Session Id</param>
public void RemoveAllSubscriptions(string session)
/// <param name="sessionIdentifier">sessionIdentifier Id</param>
public void RemoveAllSubscriptions(string sessionIdentifier)
{
if (_subscriptionList.ContainsKey(session))
if (_subscriptionList.ContainsKey(sessionIdentifier))
{
_subscriptionList.Remove(session);
_subscriptionList.Remove(sessionIdentifier);
}
}
public void AddEventHandler(Action<BaseEvent> action, Action cancel, string sessionId)
public void AddEventHandler(Action<BaseEvent> action, Action cancel, string sessionIdentifierId)
{
lock (_actionList)
{
_actionList.Add(sessionId, new EventAction(action, cancel));
_actionList.Add(sessionIdentifierId, new EventAction(action, cancel));
}
}
public void RemoveEventHandler(string sessionId)
public void RemoveEventHandler(string sessionIdentifierId)
{
_actionList.Remove(sessionId);
_actionList.Remove(sessionIdentifierId);
}
public void CancelEventStream(string sessionId)
public void CancelEventStream(string sessionIdentifierId)
{
_actionList.TryGetValue(sessionId, out EventAction value);
value.Cancel();
_actionList.TryGetValue(sessionIdentifierId, out EventAction value);
if (value != null)
{
value.Cancel();
}
RemoveEventHandler(sessionId);
RemoveEventHandler(sessionIdentifierId);
}
public void FireEvent(BaseEvent bEvent)

View File

@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using Aurora.Proto.PartyV2;
using Aurora.Proto.Party;
namespace Aurora.Services.Server.EventManager
namespace Aurora.Services.EventManager
{
public interface IEventManager
{

View File

@ -3,8 +3,6 @@ using System.Threading.Tasks;
using System.Threading;
using Grpc.Core;
using Aurora.Models.Media;
using Aurora.Proto.Sync;
using LibVLCSharp.Shared;
namespace Aurora.Services.Player
{

View File

@ -3,7 +3,7 @@ using System.Threading.Tasks;
using System.Threading;
using Grpc.Core;
using Aurora.Models.Media;
using Aurora.Proto.Sync;
using Aurora.Proto.Party;
using LibVLCSharp.Shared;
namespace Aurora.Services.Player
@ -85,7 +85,7 @@ namespace Aurora.Services.Player
}
_currentMedia = media;
await _currentMedia.Load();
var md = new Media(_libvlc, _currentMedia.DataStream);
var md = new LibVLCSharp.Shared.Media(_libvlc, _currentMedia.DataStream);
_mediaPlayer = new MediaPlayer(md);
_mediaPlayer.Stopped += OnStopped;
md.Dispose();
@ -112,15 +112,15 @@ namespace Aurora.Services.Player
RemoteAudio media = _currentMedia as RemoteAudio;
if (!media.FromHost)
{
RemoteSyncService.RemoteSyncServiceClient remoteSyncClient = media.RemoteSyncClient;
RemotePartyService.RemotePartyServiceClient remotePartyServiceClient = media.RemotePartyServiceClient;
//Sync playback in a separate task
//Task completes when host stops syncing (when a song is complete)
Task syncTask = new Task(async () =>
{
CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
using (AsyncServerStreamingCall<Sync> syncStream = remoteSyncClient
.GetMediaSync(new Proto.General.Empty()))
using (AsyncServerStreamingCall<Sync> syncStream = remotePartyServiceClient
.SyncMedia(new SyncMediaRequest() { }))
{
try
{

View File

@ -1,11 +1,11 @@
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using Aurora.Proto.PartyV2;
using Aurora.Proto.Party;
using Aurora.Services.Library;
using Aurora.Services.Settings;
using Aurora.Models.Media;
using Aurora.Services.Server.EventManager;
using Aurora.Services.EventManager;
using Autofac;
namespace Aurora.Services.Server.Controllers

View File

@ -1,7 +1,7 @@
using System;
using System.Threading.Tasks;
using System.Threading;
using Aurora.Proto.PartyV2;
using Aurora.Proto.Party;
using Aurora.Utils;
namespace Aurora.Services.Server.Controllers

View File

@ -1,6 +1,7 @@
using System;
using System.Threading.Tasks;
using Aurora.Proto.PartyV2;
using System.Collections.Generic;
using Aurora.Proto.Party;
using Aurora.Proto.General;
using Aurora.Utils;
@ -8,12 +9,16 @@ namespace Aurora.Services.Server.Controllers
{
public partial class RemotePartyController : RemotePartyService.RemotePartyServiceBase
{
public override Task<ListEventSubscriptionsResponse> ListEventSubscriptions(ListEventSubscriptionsRequest request, Grpc.Core.ServerCallContext context)
public override Task<ListEventSubscriptionsResponse> ListEventSubscriptions(
ListEventSubscriptionsRequest request,
Grpc.Core.ServerCallContext context)
{
throw new NotImplementedException();
}
public override Task<EventSubscription> CreateEventSubscription(CreateEventSubscriptionRequest request, Grpc.Core.ServerCallContext context)
public override Task<EventSubscription> CreateEventSubscription(
CreateEventSubscriptionRequest request,
Grpc.Core.ServerCallContext context)
{
Console.WriteLine(string.Format("SERVER - Subscription from client with id: {0}", request.Parent));
this._eventManager.AddSubscription(Misc.Combine(new string[] { context.Peer, request.Parent }), request.EventSubscription.Type);
@ -21,14 +26,37 @@ namespace Aurora.Services.Server.Controllers
return Task.FromResult(request.EventSubscription);
}
public override Task<Empty> DeleteEventSubscription(DeleteEventSubscriptionRequest request, Grpc.Core.ServerCallContext context)
public override Task<CreateEventSubscriptionListResponse> CreateEventSubscriptionList(
CreateEventSubscriptionListRequest request,
Grpc.Core.ServerCallContext context)
{
Console.WriteLine(string.Format("SERVER - Subscription from client with id: {0}", request.Parent));
List<EventType> eventTypes = new List<EventType>();
foreach (EventSubscription subscription in request.EventSubscriptions)
{
eventTypes.Add(subscription.Type);
}
this._eventManager.AddSubscriptionList(Misc.Combine(new string[] { context.Peer, request.Parent }), eventTypes);
CreateEventSubscriptionListResponse resp = new CreateEventSubscriptionListResponse();
resp.EventSubscriptions.AddRange(request.EventSubscriptions);
return Task.FromResult(resp);
}
public override Task<Empty> DeleteEventSubscription(
DeleteEventSubscriptionRequest request,
Grpc.Core.ServerCallContext context)
{
this._eventManager.RemoveSubscription(Misc.Combine(new string[] { context.Peer, request.Parent }), request.Type);
return Task.FromResult(new Empty());
}
public override Task<Empty> DeleteAllEventSubscriptions(DeleteAllEventSubscriptionsRequest request, Grpc.Core.ServerCallContext context)
public override Task<Empty> DeleteAllEventSubscriptions(
DeleteAllEventSubscriptionsRequest request,
Grpc.Core.ServerCallContext context)
{
this._eventManager.RemoveAllSubscriptions(Misc.Combine(new string[] { context.Peer, request.Parent }));

View File

@ -1,7 +1,7 @@
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using Aurora.Proto.PartyV2;
using Aurora.Proto.Party;
using Aurora.Models.Media;
using Aurora.Proto.General;
using Aurora.Services.Library;

View File

@ -1,7 +1,7 @@
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using Aurora.Proto.PartyV2;
using Aurora.Proto.Party;
using Aurora.Proto.General;
using Aurora.Utils;
using Grpc.Core;

View File

@ -1,7 +1,7 @@
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using Aurora.Proto.PartyV2;
using Aurora.Proto.Party;
using Google.Protobuf.WellKnownTypes;

View File

@ -1,216 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Aurora.Proto.PartyV2;
namespace Aurora.Services.Server.EventManager
{
public class EventManager : IEventManager
{
#region Fields
private Dictionary<string, List<EventType>> _subscriptionList;
private Dictionary<string, EventAction> _actionList;
#endregion Fields
public EventManager()
{
_subscriptionList = new Dictionary<string, List<EventType>>();
_actionList = new Dictionary<string, EventAction>();
}
#region Private Methods
#endregion Private Methods
#region Public Methods
/// <summary>
/// Get the list of event type subscriptions for a given sessionIdentifier id.
/// </summary>
/// <param name="sessionIdentifier">sessionIdentifier Id</param>
/// <returns></returns>
public List<EventType> GetSubscriptionList(string sessionIdentifier)
{
List<EventType> eventList = new List<EventType>();
if (_subscriptionList.ContainsKey(sessionIdentifier))
{
_subscriptionList.TryGetValue(sessionIdentifier, out eventList);
}
return eventList;
}
/// <summary>
/// Get the number of event subscriptions for a given sessionIdentifier
/// </summary>
/// <param name="sessionIdentifier">sessionIdentifier Id</param>
/// <returns></returns>
public int GetSubscriptionCount(string sessionIdentifier)
{
List<EventType> eventList = new List<EventType>();
if (_subscriptionList.ContainsKey(sessionIdentifier))
{
_subscriptionList.TryGetValue(sessionIdentifier, out eventList);
}
return eventList.Count();
}
/// <summary>
/// Add a new subscription
/// </summary>
/// <param name="sessionIdentifier"></param>
/// <param name="type"></param>
public bool AddSubscription(string sessionIdentifier, EventType type)
{
bool success = false;
lock (_subscriptionList)
{
if (!_subscriptionList.ContainsKey(sessionIdentifier))
{
//Add sessionIdentifier to subscription list
List<EventType> eventList = new List<EventType>();
eventList.Add(type);
_subscriptionList.Add(sessionIdentifier, eventList);
success = true;
}
else
{
_subscriptionList.TryGetValue(sessionIdentifier, out List<EventType> eventList);
if (eventList != null)
{
eventList.Add(type);
success = true;
}
}
}
return success;
}
/// <summary>
/// Add a list of subscriptions. This unsubscribes from unused events.
/// </summary>
/// <param name="sessionIdentifier">The browser sessionIdentifier id.</param>
/// <param name="types">The list of event types to subscribe to.</param>
public void AddSubscriptionList(string sessionIdentifier, List<EventType> types)
{
RemoveAllSubscriptions(sessionIdentifier);
foreach (EventType e in types)
{
AddSubscription(sessionIdentifier, e);
}
}
/// <summary>
/// Unsubscribe from a given event type.
/// </summary>
/// <param name="sessionIdentifier">sessionIdentifier Id</param>
/// <param name="type">Event Type to be removed</param>
public void RemoveSubscription(string sessionIdentifier, EventType type)
{
lock (_subscriptionList)
{
if (_subscriptionList.ContainsKey(sessionIdentifier))
{
List<EventType> eventTypeList;
_subscriptionList.TryGetValue(sessionIdentifier, out eventTypeList);
if (eventTypeList != null && eventTypeList.Contains(type))
{
eventTypeList.Remove(type);
//base.LogInformation(string.Format("Subscription removed for event type {0} subscription on sessionIdentifier {1}", type.ToString(), sessionIdentifier));
}
}
}
}
public void RemoveSubscriptionList(string sessionIdentifier, List<EventType> types)
{
foreach (EventType e in types)
{
RemoveSubscription(sessionIdentifier, e);
}
}
/// <summary>
/// Remove all subscriptons for a given sessionIdentifier.
/// </summary>
/// <param name="sessionIdentifier">sessionIdentifier Id</param>
public void RemoveAllSubscriptions(string sessionIdentifier)
{
if (_subscriptionList.ContainsKey(sessionIdentifier))
{
_subscriptionList.Remove(sessionIdentifier);
}
}
public void AddEventHandler(Action<BaseEvent> action, Action cancel, string sessionIdentifierId)
{
lock (_actionList)
{
_actionList.Add(sessionIdentifierId, new EventAction(action, cancel));
}
}
public void RemoveEventHandler(string sessionIdentifierId)
{
_actionList.Remove(sessionIdentifierId);
}
public void CancelEventStream(string sessionIdentifierId)
{
_actionList.TryGetValue(sessionIdentifierId, out EventAction value);
if (value != null)
{
value.Cancel();
}
RemoveEventHandler(sessionIdentifierId);
}
public void FireEvent(BaseEvent bEvent)
{
Dictionary<string, EventAction> actionsCopy = new Dictionary<string, EventAction>();
//Copy actions list
lock (_actionList)
{
foreach (KeyValuePair<string, EventAction> pair in _actionList)
{
actionsCopy.Add(pair.Key, pair.Value);
}
}
lock (_subscriptionList)
{
foreach (KeyValuePair<string, List<EventType>> pair in _subscriptionList)
{
Task.Delay(1000);
//If action list contains an action for id, invoke
if (actionsCopy.ContainsKey(pair.Key))
{
actionsCopy.TryGetValue(pair.Key, out EventAction action);
Task executionTask = new Task(() => action.Callback(bEvent));
//Execute task with exception handler
executionTask.ContinueWith((Task task) =>
{
var exception = executionTask.Exception;
Console.WriteLine(string.Format("SERVER --- Exception occurred firing event"));
this._actionList.Remove(pair.Key);
},
TaskContinuationOptions.OnlyOnFaulted);
executionTask.Start();
}
}
}
}
#endregion Public Methods
}
}

View File

@ -6,8 +6,8 @@ using Grpc.Core;
using Aurora.Services.Server.Controllers;
using Aurora.Services.Settings;
using Aurora.Services.Library;
using Aurora.Services.Server.EventManager;
using Aurora.Proto.PartyV2;
using Aurora.Services.EventManager;
using Aurora.Proto.Party;
namespace Aurora.Services.Server

View File

@ -1,143 +0,0 @@
using System;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using Grpc.Core;
using Aurora.RemoteImpl;
using Aurora.Proto.Events;
using Aurora.Proto.Party;
using Aurora.Proto.Playback;
using Aurora.Proto.Sync;
using Aurora.Services.Settings;
using Autofac;
namespace Aurora.Services
{
public class ServerService : BaseService<ServerService>
{
private int _port;
private string _hostname;
private Grpc.Core.Server _server;
//Implementation class declarations
private RemotePartyServiceImpl _remotePartyServiceImpl;
private RemoteEventServiceImpl _remoteEventImpl;
private RemotePlaybackServiceImpl _remotePlaybackImpl;
private RemoteSyncServiceImpl _remoteSyncImpl;
/// <summary>
/// Constructor. Registers GRPC service implementations.
/// </summary>
public ServerService()
{
string host = GetLocalIPAddress();
using (var scope = App.Container.BeginLifetimeScope())
{
var service = scope.Resolve<ISettingsService>();
this._port = service.DefaultPort;
}
if (string.IsNullOrWhiteSpace(host))
{
throw new Exception("This device must have a valid IP address");
}
_hostname = host;
_server = new Grpc.Core.Server
{
Ports = { new ServerPort(_hostname, _port, ServerCredentials.Insecure) }
};
}
public int Port
{
get { return _port; }
}
public string Hostname
{
get { return _hostname; }
}
public bool Initialized
{
get
{
return (_remoteEventImpl != null &&
_remotePartyServiceImpl != null &&
_server != null);
}
}
/// <summary>
/// Start Server
/// </summary>
public void Start()
{
try
{
Console.WriteLine(string.Format("Starting gRPC server at hostname: {0}, port: {1}", _hostname, _port));
if (!Initialized)
{
//Construct implementations
_remotePartyServiceImpl = new RemotePartyServiceImpl();
_remoteEventImpl = new RemoteEventServiceImpl();
_remotePlaybackImpl = new RemotePlaybackServiceImpl();
_remoteSyncImpl = new RemoteSyncServiceImpl();
// Register grpc RemoteService with singleton server service
RegisterService(RemotePartyService.BindService(_remotePartyServiceImpl));
RegisterService(RemoteEventService.BindService(_remoteEventImpl));
RegisterService(RemotePlaybackService.BindService(_remotePlaybackImpl));
RegisterService(RemoteSyncService.BindService(_remoteSyncImpl));
}
_server.Start();
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Error starting gRPC server: {0}", ex.Message));
}
}
/// <summary>
/// Shutdown server async.
/// </summary>
/// <returns>Task</returns>
public async Task Stop()
{
await _server.ShutdownAsync();
}
public async Task Reset()
{
await Stop();
_server = new Grpc.Core.Server
{
Ports = { new ServerPort("localhost", _port, ServerCredentials.Insecure) }
};
}
private void RegisterService(ServerServiceDefinition definition)
{
_server.Services.Add(definition);
}
public static string GetLocalIPAddress()
{
string returnIp = "";
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
returnIp = ip.ToString();
}
}
return returnIp;
}
}
}

View File

@ -22,7 +22,7 @@ namespace Aurora.Services.Settings
/// The current sessions clientId. This is assigned by the server. This is not persisted.
/// </summary>
/// <value></value>
string ClientId { get; set; }
string ClientName { get; set; }
string LibraryLocation { get; set; }
}

View File

@ -67,7 +67,7 @@ namespace Aurora.Services.Settings
/// The current sessions clientId. This is assigned by the server. This is not persisted.
/// </summary>
/// <value></value>
public string ClientId
public string ClientName
{
get; set;
}