From 51ab2d9c42cd1a4267c664993d3ef08ab51d9a6a Mon Sep 17 00:00:00 2001 From: watsonb8 Date: Sun, 2 Feb 2020 16:49:01 -0500 Subject: [PATCH] Removed old proto definitions (not tested) --- Aurora.test/ControllerTests/EventTests.cs | 4 +- .../ControllerTests/MediaControllerTest.cs | 2 +- .../ControllerTests/MembersControllerTest.cs | 2 +- .../ControllerTests/PartyControllerTest.cs | 2 +- Aurora.test/ControllerTests/Setup.cs | 4 +- .../Models/Mock/SettingsServiceMock.cs | 2 +- Aurora/App.xaml.cs | 4 +- Aurora/Aurora.csproj | 4 - .../Components/MemberList/MemberList.xaml.cs | 6 +- Aurora/Design/Views/Party/PartyViewModel.cs | 226 ++++++++------ Aurora/Models/Media/RemoteAudio.cs | 18 +- Aurora/Models/PartyMember.cs | 8 +- Aurora/Proto/events.proto | 77 ----- Aurora/Proto/party.proto | 288 ++++++++++++++++-- Aurora/Proto/party.v2.proto | 276 ----------------- Aurora/Proto/playback.proto | 13 - Aurora/Proto/sync.proto | 15 - Aurora/RemoteImpl/RemoteEventImpl.cs | 97 ------ Aurora/RemoteImpl/RemotePartyImpl.cs | 148 --------- Aurora/RemoteImpl/RemotePlaybackImpl.cs | 57 ---- Aurora/RemoteImpl/RemoteSyncImpl.cs | 56 ---- Aurora/Services/BaseService.cs | 22 -- Aurora/Services/Client/ClientService.cs | 46 +++ Aurora/Services/Client/IClientService.cs | 15 + .../Services/ClientService/ClientService.cs | 152 --------- Aurora/Services/ClientService/EventInfo.cs | 25 -- .../ClientService/Events/MediaPausedEvent.cs | 16 - .../ClientService/Events/MediaResumedEvent.cs | 16 - .../Events/NewMediaPlayingEvent.cs | 16 - .../Events/PartyMemberJoinedEvent.cs | 16 - .../Events/PartyMemberLeftEvent.cs | 16 - .../Services/ClientService/IClientService.cs | 42 --- .../{Server => }/EventManager/EventAction.cs | 4 +- Aurora/Services/EventManager/EventManager.cs | 98 +++--- .../EventManager/IEventManager.cs | 4 +- Aurora/Services/Player/IPlayer.cs | 2 - Aurora/Services/Player/PlayerService.cs | 10 +- .../Server/Controllers/Constructor.cs | 4 +- .../Server/Controllers/EventController.cs | 2 +- .../EventSubscriptionController.cs | 38 ++- .../Server/Controllers/MediaController.cs | 2 +- .../Server/Controllers/MemberController.cs | 2 +- .../Server/Controllers/PartyController.cs | 2 +- .../Server/EventManager/EventManager.cs | 216 ------------- .../Services/Server/Server/ServerService.cs | 4 +- Aurora/Services/ServerService.cs | 143 --------- Aurora/Services/Settings/ISettingsService.cs | 2 +- Aurora/Services/Settings/SettingsService.cs | 2 +- 48 files changed, 581 insertions(+), 1645 deletions(-) delete mode 100644 Aurora/Proto/events.proto delete mode 100644 Aurora/Proto/party.v2.proto delete mode 100644 Aurora/Proto/playback.proto delete mode 100644 Aurora/Proto/sync.proto delete mode 100644 Aurora/RemoteImpl/RemoteEventImpl.cs delete mode 100644 Aurora/RemoteImpl/RemotePartyImpl.cs delete mode 100644 Aurora/RemoteImpl/RemotePlaybackImpl.cs delete mode 100644 Aurora/RemoteImpl/RemoteSyncImpl.cs delete mode 100644 Aurora/Services/BaseService.cs create mode 100644 Aurora/Services/Client/ClientService.cs create mode 100644 Aurora/Services/Client/IClientService.cs delete mode 100644 Aurora/Services/ClientService/ClientService.cs delete mode 100644 Aurora/Services/ClientService/EventInfo.cs delete mode 100644 Aurora/Services/ClientService/Events/MediaPausedEvent.cs delete mode 100644 Aurora/Services/ClientService/Events/MediaResumedEvent.cs delete mode 100644 Aurora/Services/ClientService/Events/NewMediaPlayingEvent.cs delete mode 100644 Aurora/Services/ClientService/Events/PartyMemberJoinedEvent.cs delete mode 100644 Aurora/Services/ClientService/Events/PartyMemberLeftEvent.cs delete mode 100644 Aurora/Services/ClientService/IClientService.cs rename Aurora/Services/{Server => }/EventManager/EventAction.cs (84%) rename Aurora/Services/{Server => }/EventManager/IEventManager.cs (96%) delete mode 100644 Aurora/Services/Server/EventManager/EventManager.cs delete mode 100644 Aurora/Services/ServerService.cs diff --git a/Aurora.test/ControllerTests/EventTests.cs b/Aurora.test/ControllerTests/EventTests.cs index 16fba6f..513331c 100644 --- a/Aurora.test/ControllerTests/EventTests.cs +++ b/Aurora.test/ControllerTests/EventTests.cs @@ -1,8 +1,8 @@ using NUnit.Framework; -using Aurora.Proto.PartyV2; +using Aurora.Proto.Party; using Aurora.Services.Server; -using Aurora.Services.Server.EventManager; +using Aurora.Services.EventManager; using Grpc.Core; using System.Threading.Tasks; using System.Threading; diff --git a/Aurora.test/ControllerTests/MediaControllerTest.cs b/Aurora.test/ControllerTests/MediaControllerTest.cs index 9819313..cf9e4ba 100644 --- a/Aurora.test/ControllerTests/MediaControllerTest.cs +++ b/Aurora.test/ControllerTests/MediaControllerTest.cs @@ -1,5 +1,5 @@ using NUnit.Framework; -using Aurora.Proto.PartyV2; +using Aurora.Proto.Party; using Aurora.Services.Server; using Grpc.Core; using System.Threading.Tasks; diff --git a/Aurora.test/ControllerTests/MembersControllerTest.cs b/Aurora.test/ControllerTests/MembersControllerTest.cs index a12111f..212ffc2 100644 --- a/Aurora.test/ControllerTests/MembersControllerTest.cs +++ b/Aurora.test/ControllerTests/MembersControllerTest.cs @@ -1,5 +1,5 @@ using NUnit.Framework; -using Aurora.Proto.PartyV2; +using Aurora.Proto.Party; using Aurora.Services.Server; using Grpc.Core; using System.Threading.Tasks; diff --git a/Aurora.test/ControllerTests/PartyControllerTest.cs b/Aurora.test/ControllerTests/PartyControllerTest.cs index a08cda5..9c10b8d 100644 --- a/Aurora.test/ControllerTests/PartyControllerTest.cs +++ b/Aurora.test/ControllerTests/PartyControllerTest.cs @@ -1,6 +1,6 @@ using System.Threading.Tasks; using NUnit.Framework; -using Aurora.Proto.PartyV2; +using Aurora.Proto.Party; using Aurora.Services.Server; using Grpc.Core; using Autofac; diff --git a/Aurora.test/ControllerTests/Setup.cs b/Aurora.test/ControllerTests/Setup.cs index 46d53c8..f011bf7 100644 --- a/Aurora.test/ControllerTests/Setup.cs +++ b/Aurora.test/ControllerTests/Setup.cs @@ -1,9 +1,9 @@ using Autofac; -using Aurora.Proto.PartyV2; +using Aurora.Proto.Party; using Aurora.Services.Server; using Aurora.Services.Library; using Aurora.Services.Settings; -using Aurora.Services.Server.EventManager; +using Aurora.Services.EventManager; using Aurora.test.Models.Mock; using System.IO; using Grpc.Core; diff --git a/Aurora.test/Models/Mock/SettingsServiceMock.cs b/Aurora.test/Models/Mock/SettingsServiceMock.cs index bcc3c08..6126c92 100644 --- a/Aurora.test/Models/Mock/SettingsServiceMock.cs +++ b/Aurora.test/Models/Mock/SettingsServiceMock.cs @@ -28,7 +28,7 @@ namespace Aurora.test.Models.Mock /// The current sessions clientId. This is assigned by the server. This is not persisted. /// /// - public string ClientId { get; set; } + public string ClientName { get; set; } public string LibraryLocation { get; set; } } diff --git a/Aurora/App.xaml.cs b/Aurora/App.xaml.cs index b38d816..47ce0e9 100644 --- a/Aurora/App.xaml.cs +++ b/Aurora/App.xaml.cs @@ -6,8 +6,7 @@ using Aurora.Design.Views.Party; using Aurora.Design.Views.Profile; using Aurora.Design.Views.Songs; using Aurora.Design.Views.Stations; -using Aurora.Services.ClientService; -using Aurora.Services.Server.EventManager; +using Aurora.Services.EventManager; using Autofac; using LibVLCSharp.Shared; using Xamarin.Forms; @@ -30,7 +29,6 @@ namespace Aurora // _builder.RegisterInstance(new PlayerService()).SingleInstance(); _builder.RegisterType().As().SingleInstance(); _builder.RegisterType().As().SingleInstance(); - _builder.RegisterType().As().SingleInstance(); _builder.RegisterType().As().SingleInstance(); _builder.RegisterType().As().SingleInstance(); _builder.RegisterType().SingleInstance(); diff --git a/Aurora/Aurora.csproj b/Aurora/Aurora.csproj index 85cc85a..06593eb 100644 --- a/Aurora/Aurora.csproj +++ b/Aurora/Aurora.csproj @@ -62,10 +62,6 @@ - - - - diff --git a/Aurora/Design/Components/MemberList/MemberList.xaml.cs b/Aurora/Design/Components/MemberList/MemberList.xaml.cs index 7a61fe4..0d09652 100644 --- a/Aurora/Design/Components/MemberList/MemberList.xaml.cs +++ b/Aurora/Design/Components/MemberList/MemberList.xaml.cs @@ -24,7 +24,7 @@ namespace Aurora.Design.Components.MemberList /// public static readonly BindableProperty MembersProperty = BindableProperty.Create(propertyName: "Members", - returnType: typeof(ObservableCollection), + returnType: typeof(ObservableCollection), declaringType: typeof(MemberList), defaultBindingMode: BindingMode.Default, propertyChanged: OnMembersChanged); @@ -33,11 +33,11 @@ namespace Aurora.Design.Components.MemberList /// Backing property for MembersProperty /// /// - public ObservableCollection Members + public ObservableCollection Members { get { - return (ObservableCollection)GetValue(MembersProperty); + return (ObservableCollection)GetValue(MembersProperty); } set { diff --git a/Aurora/Design/Views/Party/PartyViewModel.cs b/Aurora/Design/Views/Party/PartyViewModel.cs index be1e970..d775fa1 100644 --- a/Aurora/Design/Views/Party/PartyViewModel.cs +++ b/Aurora/Design/Views/Party/PartyViewModel.cs @@ -1,19 +1,18 @@ using System; using System.Collections.ObjectModel; +using System.Collections.Generic; using System.Threading.Tasks; +using System.Threading; using System.Linq; using Xamarin.Forms; -using Aurora.Services; -using Aurora.Proto.General; using Aurora.Proto.Party; -using Aurora.Proto.Events; -using Aurora.Services.ClientService; -using Aurora.Services.ClientService.Events; -using Aurora.Services.Player; -using Aurora.Services.EventManager; using Aurora.Models.Media; +using Aurora.Services.Client; using Aurora.Design.Views.Party.NewPartyDialog; using Aurora.Services.Settings; +using Aurora.Services.Server; +using Aurora.Services.EventManager; +using Grpc.Core; namespace Aurora.Design.Views.Party { @@ -25,25 +24,37 @@ namespace Aurora.Design.Views.Party Hosting, Connecting, } - + public delegate void EventHandler(BaseEvent e); public class PartyViewModel : BaseViewModel { private PartyState _state; private string _hostname = ""; - private ObservableCollection _members; + private ObservableCollection _members; private ObservableCollection _queue; private BaseMedia _selectedMedia; - private IClientService _client; + // private IClientService _client; private ISettingsService _settingsService; + private IClientService _clientService; + private IServerService _serverService; + private IEventManager _eventManager; + + private CancellationTokenSource _eventCancellationTokenSource; + + private Dictionary _eventHandlers; private int _selectedTabIndex; - public PartyViewModel(ISettingsService settingsService, IClientService clientService) + public PartyViewModel( + ISettingsService settingsService, + IServerService serverService, + IEventManager eventManager) { - _members = new ObservableCollection(); + _members = new ObservableCollection(); _queue = new ObservableCollection(); this._settingsService = settingsService; + this._serverService = serverService; + this._eventManager = eventManager; SetState(PartyState.SelectingHost); @@ -51,19 +62,15 @@ namespace Aurora.Design.Views.Party LeavePartyCommand = new Command(OnLeavePartyCommandExecute, CanLeavePartyCommandExecute); - _client = clientService; - - _client.OnMediaPaused += this.OnRemoteMediaPaused; - _client.OnMediaResumed += this.OnRemoteMediaResumed; - _client.OnNewMediaPlaying += this.OnNewRemoteMediaPlaying; - _client.OnPartyMemberJoined += this.OnPartyMemberJoined; - _client.OnPartyMemberLeft += this.OnPartyMemberLeft; - - } - - ~PartyViewModel() - { - //Task.Run(ServerService.Instance.Stop); + //Setup event handlers + _eventHandlers = new Dictionary() + { + {BaseEvent.DerivedEventOneofCase.MediaPausedEvent, this.OnRemoteMediaPaused}, + {BaseEvent.DerivedEventOneofCase.MediaResumedEvent, this.OnRemoteMediaResumed}, + {BaseEvent.DerivedEventOneofCase.NewMediaPlayingEvent, this.OnNewRemoteMediaPlaying}, + {BaseEvent.DerivedEventOneofCase.MemberCreatedEvent, this.OnPartyMemberJoined}, + {BaseEvent.DerivedEventOneofCase.MemberDeletedEvent, this.OnPartyMemberLeft} + }; } #region Properties @@ -78,7 +85,7 @@ namespace Aurora.Design.Views.Party /// Publc property for the members list /// /// - public ObservableCollection Members + public ObservableCollection Members { get { @@ -140,7 +147,7 @@ namespace Aurora.Design.Views.Party if (this._state == PartyState.Hosting || this._state == PartyState.InParty) { - await _client.GetEvents().ConfigureAwait(false); + await this.GetEvents().ConfigureAwait(false); } else { @@ -177,7 +184,7 @@ namespace Aurora.Design.Views.Party /// public override Task OnInactive() { - _client.StopEvents(); + this._eventCancellationTokenSource.Cancel(); return Task.FromResult(null); } @@ -186,7 +193,7 @@ namespace Aurora.Design.Views.Party /// /// /// - public void OnRemoteMediaPaused(object sender, MediaPausedEventArgs args) + public void OnRemoteMediaPaused(BaseEvent e) { StopPlaying(); } @@ -196,9 +203,9 @@ namespace Aurora.Design.Views.Party /// /// /// - public void OnNewRemoteMediaPlaying(object sender, NewMediaPlayingEventArgs args) + public void OnNewRemoteMediaPlaying(BaseEvent e) { - PlayFromBeginning(GetMediaFromQueue(args.Event.Media.Id)); + PlayFromBeginning(GetMediaFromQueue(e.NewMediaPlayingEvent.Media.Name)); } /// @@ -206,7 +213,7 @@ namespace Aurora.Design.Views.Party /// /// /// - public void OnRemoteMediaResumed(object sender, MediaResumedEventArgs args) + public void OnRemoteMediaResumed(BaseEvent e) { PlayResume(); } @@ -216,17 +223,9 @@ namespace Aurora.Design.Views.Party /// /// /// - public void OnPartyMemberJoined(object sender, PartyMemberJoinedEventArgs args) + public void OnPartyMemberJoined(BaseEvent e) { - PartyMember member = new PartyMember - { - UserName = args.Event.Member.UserName, - Id = args.Event.Member.Id, - IpAddress = args.Event.Member.IpAddress, - Port = args.Event.Member.Port - }; - - Members.Add(member); + Members.Add(e.MemberCreatedEvent.Member); } /// @@ -234,10 +233,10 @@ namespace Aurora.Design.Views.Party /// /// /// - public void OnPartyMemberLeft(object sender, PartyMemberLeftEventArgs args) + public void OnPartyMemberLeft(BaseEvent e) { - var found = Members.Where(x => x.Id == args.Event.Member.Id); - foreach (PartyMember member in found) + var found = Members.Where(x => x.Name == e.MemberDeletedEvent.MemberName); + foreach (Member member in found) { _members.Remove(member); } @@ -249,14 +248,14 @@ namespace Aurora.Design.Views.Party private async void OnJoinCommandExecute() { SetState(PartyState.Connecting); - _client.Start(_hostname, this._settingsService.DefaultPort.ToString()); + _clientService.Start(_hostname, this._settingsService.DefaultPort.ToString()); await JoinParty(false); //TODO add cancellation token try { SetState(PartyState.InParty); - await _client.GetEvents().ConfigureAwait(true); + await GetEvents().ConfigureAwait(true); } catch (Exception ex) { @@ -273,10 +272,9 @@ namespace Aurora.Design.Views.Party { //Change state SetState(PartyState.Connecting); - ServerService.Instance.Start(); + _serverService.Start("test", "asdf"); string localHost = ServerService.GetLocalIPAddress(); - _client.IsHost = true; - _client.Start(localHost, this._settingsService.DefaultPort.ToString()); + _clientService.Start(localHost, this._settingsService.DefaultPort.ToString()); await JoinParty(true); @@ -284,7 +282,7 @@ namespace Aurora.Design.Views.Party try { SetState(PartyState.Hosting); - await _client.GetEvents().ConfigureAwait(true); + // await _clientService.RemotePartyServiceClient.GetEvents().ConfigureAwait(true); } catch (Exception ex) { @@ -299,7 +297,10 @@ namespace Aurora.Design.Views.Party private async void OnLeavePartyCommandExecute() { - await _client.RemotePartyClient.LeavePartyAsync(new LeavePartyRequest()); + await _clientService.RemotePartyServiceClient.DeleteMemberAsync(new DeleteMemberRequest() + { + Name = _settingsService.ClientName + }); } private bool CanLeavePartyCommandExecute() @@ -315,7 +316,7 @@ namespace Aurora.Design.Views.Party AudioMetadata meta = _selectedMedia.Metadata as AudioMetadata; MediaPausedEvent mediaPaused = new MediaPausedEvent(); - EventManager.Instance.FireEvent(new BaseEvent() + _eventManager.FireEvent(new BaseEvent() { MediaPausedEvent = mediaPaused }); @@ -326,7 +327,7 @@ namespace Aurora.Design.Views.Party AudioMetadata meta = _selectedMedia.Metadata as AudioMetadata; MediaResumedEvent mediaResumed = new MediaResumedEvent(); - EventManager.Instance.FireEvent(new BaseEvent() + _eventManager.FireEvent(new BaseEvent() { MediaResumedEvent = mediaResumed }); @@ -357,16 +358,17 @@ namespace Aurora.Design.Views.Party AudioMetadata meta = _selectedMedia.Metadata as AudioMetadata; NewMediaPlayingEvent mediaPlaying = new NewMediaPlayingEvent() { - Media = new RemoteMediaData() + Media = new Media() { - Id = _selectedMedia.Id, + //TODO need full resource name + Name = _selectedMedia.Id, Title = meta.Title, Artist = meta.Artist, Album = meta.Album, } }; - EventManager.Instance.FireEvent(new BaseEvent() + _eventManager.FireEvent(new BaseEvent() { NewMediaPlayingEvent = mediaPlaying }); @@ -389,24 +391,31 @@ namespace Aurora.Design.Views.Party { try { - JoinPartyResponse resp = await _client.RemotePartyClient.JoinPartyAsync(new JoinPartyRequest + Member resp = await _clientService.RemotePartyServiceClient.CreateMemberAsync(new CreateMemberRequest { - UserName = this._settingsService.Username, + Member = new Member() + { + UserName = this._settingsService.Username, + } }); - this._settingsService.ClientId = resp.ClientId; + this._settingsService.ClientName = resp.Name; - RefreshMembers(); + await RefreshMembers(); //Subscribe to events await SubscribeToEvents(); Queue.Clear(); - QueueResponse queueResponse = _client.RemotePartyClient.GetQueue(new Empty()); + ListMediaResponse mediaResponse = await _clientService.RemotePartyServiceClient.ListMediaAsync(new ListMediaRequest() + { + PageSize = 50, + Parent = "TODO" + }); //Convert received data to remote audio models - foreach (RemoteMediaData data in queueResponse.MediaList) + foreach (Media data in mediaResponse.Media) { //Assign received metadata (since this can't be aquired from a file) AudioMetadata meta = new AudioMetadata(); @@ -415,11 +424,10 @@ namespace Aurora.Design.Views.Party meta.Artist = data.Artist; meta.Duration = data.Duration; - RemoteAudio remote = new RemoteAudio(data.Id, + RemoteAudio remote = new RemoteAudio(data.Name, asHost, meta, - _client.RemotePlaybackClient, - _client.RemoteSyncClient); + _clientService.RemotePartyServiceClient); Queue.Add(remote); OnPropertyChanged("Queue"); @@ -434,47 +442,52 @@ namespace Aurora.Design.Views.Party private async Task LeaveParty() { //Stop receiving events - _client.StopEvents(); + // _client.StopEvents(); //Unsubscribe await UnsubscribeFromEvents(); //Leave party - LeavePartyRequest leaveReq = new LeavePartyRequest(); - await _client.RemotePartyClient.LeavePartyAsync(leaveReq); + DeleteMemberRequest req = new DeleteMemberRequest() + { + Name = _settingsService.ClientName + }; + + await _clientService.RemotePartyServiceClient.DeleteMemberAsync(req); } private async Task SubscribeToEvents() { - SubscribeRequest req = new SubscribeRequest(); - req.EventTypes.Add(EventType.PartyMemberJoined); - req.EventTypes.Add(EventType.PartyMemberLeft); - req.EventTypes.Add(EventType.MediaPlaying); - req.EventTypes.Add(EventType.MediaStopped); - if (!string.IsNullOrWhiteSpace(this._settingsService.ClientId)) - { - req.ClientId = this._settingsService.ClientId; - } + CreateEventSubscriptionListRequest req = new CreateEventSubscriptionListRequest(); + req.Parent = this._settingsService.ClientName; + req.EventSubscriptions.Add(new EventSubscription() { Type = EventType.MemberCreated }); + req.EventSubscriptions.Add(new EventSubscription() { Type = EventType.MemberDeleted }); + req.EventSubscriptions.Add(new EventSubscription() { Type = EventType.MediaPlaying }); + req.EventSubscriptions.Add(new EventSubscription() { Type = EventType.MediaStopped }); - - Console.WriteLine(string.Format("CLIENT {0} - SubscribeToEvents called from client with id", this._settingsService.ClientId)); - await _client.RemoteEventClient.SubscribeToEventsAsync(req); + Console.WriteLine(string.Format("CLIENT {0} - SubscribeToEvents called from client with id", this._settingsService.ClientName)); + await _clientService.RemotePartyServiceClient.CreateEventSubscriptionListAsync(req); } private async Task UnsubscribeFromEvents() { - UnsubscribeAllRequest unsubscribeReq = new UnsubscribeAllRequest(); - await _client.RemoteEventClient.UnsubscribeFromAllAsync(unsubscribeReq); + DeleteAllEventSubscriptionsRequest unsubscribeReq = new DeleteAllEventSubscriptionsRequest(); + await _clientService.RemotePartyServiceClient.DeleteAllEventSubscriptionsAsync(unsubscribeReq); } /// /// Refresh members list. /// - private void RefreshMembers() + private async Task RefreshMembers() { Members.Clear(); - MembersResponse response = _client.RemotePartyClient.GetPartyMembers(new Empty()); + ListMembersResponse response = await _clientService.RemotePartyServiceClient.ListMembersAsync( + new ListMembersRequest() + { + Parent = "TODO", + PageSize = 50, + }); //Add members - foreach (PartyMember member in response.Members) + foreach (Member member in response.Members) { Members.Add(member); } @@ -514,6 +527,47 @@ namespace Aurora.Design.Views.Party base.ChangePlayerState(null, Main.PlayAction.Pause); } + /// + /// Asynchronous function for processing events off of the event stream. + /// + /// + public async Task GetEvents() + { + _eventCancellationTokenSource = new CancellationTokenSource(); + string clientName = this._settingsService.ClientName; + Console.WriteLine(string.Format("CLIENT {0} - GetEvents called from client with id", clientName)); + using (AsyncServerStreamingCall eventStream = _clientService.RemotePartyServiceClient + .GetEvents(new GetEventsRequest { Parent = this._settingsService.ClientName })) + { + try + { + while (!_eventCancellationTokenSource.Token.IsCancellationRequested && + await eventStream.ResponseStream.MoveNext(_eventCancellationTokenSource.Token)) + { + try + { + BaseEvent e = new BaseEvent(eventStream.ResponseStream.Current); + + _eventHandlers.TryGetValue(e.DerivedEventCase, out EventHandler handler); + + if (handler != null && handler != null) + { + handler.Invoke(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)); + } + } + } #endregion Private Methods } } \ No newline at end of file diff --git a/Aurora/Models/Media/RemoteAudio.cs b/Aurora/Models/Media/RemoteAudio.cs index 679b57e..5620722 100644 --- a/Aurora/Models/Media/RemoteAudio.cs +++ b/Aurora/Models/Media/RemoteAudio.cs @@ -3,27 +3,23 @@ using System.IO; using System.Threading; using System.Threading.Tasks; using Aurora.Proto.General; -using Aurora.Proto.Playback; -using Aurora.Proto.Sync; +using Aurora.Proto.Party; namespace Aurora.Models.Media { public class RemoteAudio : BaseMedia { - private RemotePlaybackService.RemotePlaybackServiceClient _remotePlaybackClient; - private RemoteSyncService.RemoteSyncServiceClient _remoteSyncClient; + private RemotePartyService.RemotePartyServiceClient _remotePartyService; private CancellationTokenSource _cancellationTokenSource; #region Constructor public RemoteAudio(string id, bool fromHost, AudioMetadata metadata, - RemotePlaybackService.RemotePlaybackServiceClient playbackClient, - RemoteSyncService.RemoteSyncServiceClient syncClient) + RemotePartyService.RemotePartyServiceClient partyClient) { this.Id = id; - this._remotePlaybackClient = playbackClient; - this._remoteSyncClient = syncClient; + this._remotePartyService = partyClient; this.Metadata = metadata; this.FromHost = fromHost; @@ -41,11 +37,11 @@ namespace Aurora.Models.Media get { return MediaTypeEnum.Audio; } } - public RemoteSyncService.RemoteSyncServiceClient RemoteSyncClient + public RemotePartyService.RemotePartyServiceClient RemotePartyServiceClient { get { - return _remoteSyncClient; + return _remotePartyService; } } @@ -58,7 +54,7 @@ namespace Aurora.Models.Media public override async Task Load() { this.DataStream = new MemoryStream(); - using (var call = _remotePlaybackClient.GetSongStream(new SongRequest() { Id = this.Id })) + using (var call = _remotePartyService.StreamMedia(new StreamMediaRequest { Name = this.Id })) { try { diff --git a/Aurora/Models/PartyMember.cs b/Aurora/Models/PartyMember.cs index 920b6a6..b3eec9b 100644 --- a/Aurora/Models/PartyMember.cs +++ b/Aurora/Models/PartyMember.cs @@ -5,17 +5,17 @@ namespace Aurora.Proto.Party /// /// Partial PartyMember class with a constructor that generates a new id /// - public partial class PartyMember + public partial class Member { - public PartyMember(string id) + public Member(string id) { if (!string.IsNullOrWhiteSpace(id)) { - Id = id; + Name = id; } else { - Id = Guid.NewGuid().ToString(); + Name = Guid.NewGuid().ToString(); } } } diff --git a/Aurora/Proto/events.proto b/Aurora/Proto/events.proto deleted file mode 100644 index 08a0bf1..0000000 --- a/Aurora/Proto/events.proto +++ /dev/null @@ -1,77 +0,0 @@ -syntax = "proto3"; - -package Aurora.Proto.Events; - -import "Proto/general.proto"; -import "Proto/party.proto"; - -service RemoteEventService { - //Party Service - rpc GetEvents(EventsRequest) returns (stream BaseEvent) {}; - rpc SubscribeToEvents(SubscribeRequest) returns(SubscriptionResponse); - rpc UnsubscribeFromEvents(UnsubscribeRequest) returns (SubscriptionResponse); - rpc UnsubscribeFromAll(UnsubscribeAllRequest) returns (SubscriptionResponse); -} - -message EventsRequest { - string clientId = 1; -} - -/* Subscription messages */ -message SubscribeRequest { - repeated EventType eventTypes = 1; - string clientId = 2; -} - -message UnsubscribeRequest { - repeated EventType eventTypes = 1; - string clientId = 2; -} - -message UnsubscribeAllRequest { - string clientId = 1; -} - -message SubscriptionResponse { - bool successful = 1; -} - -/* Event Types */ -enum EventType { - PartyMemberJoined = 0; - PartyMemberLeft = 1; - MediaPlaying = 2; - MediaStopped = 3; -} -message BaseEvent { - EventType eventType = 1; - string clientKey = 2; - - oneof derivedEvent { - PartyMemberJoinedEvent partyMemberJoinedEvent = 3; - PartyMemberLeftEvent partyMemberLeftEvent = 4; - NewMediaPlayingEvent newMediaPlayingEvent = 5; - MediaPausedEvent mediaPausedEvent = 6; - MediaResumedEvent mediaResumedEvent = 7; - } -} - -message NewMediaPlayingEvent { - Aurora.Proto.Party.RemoteMediaData media = 1; -} - -message MediaResumedEvent { - Aurora.Proto.General.Empty empty = 1; -} - -message MediaPausedEvent { - Aurora.Proto.General.Empty empty = 1; -} - -message PartyMemberJoinedEvent { - Aurora.Proto.Party.PartyMember member = 1; -} - -message PartyMemberLeftEvent { - Aurora.Proto.Party.PartyMember member = 1; -} \ No newline at end of file diff --git a/Aurora/Proto/party.proto b/Aurora/Proto/party.proto index d5e54a4..d520b3a 100644 --- a/Aurora/Proto/party.proto +++ b/Aurora/Proto/party.proto @@ -3,55 +3,287 @@ syntax = "proto3"; package Aurora.Proto.Party; import "Proto/general.proto"; +import "google/protobuf/timestamp.proto"; +import "google/protobuf/field_mask.proto"; + +//Party + //Members + //EventSubscriptions + //Events + //Media service RemotePartyService { - //Party Service - rpc JoinParty(JoinPartyRequest) returns (JoinPartyResponse); - rpc LeaveParty(LeavePartyRequest) returns (LeavePartyResponse); - rpc GetPartyMembers(Aurora.Proto.General.Empty) returns (MembersResponse); - rpc GetQueue(Aurora.Proto.General.Empty) returns (QueueResponse); + //************** + //Party Resource + //************** + //Get Party + rpc GetParty(Aurora.Proto.General.Empty) returns (Party); + + //*************** + //Member Resource + //*************** + //List + rpc ListMembers(ListMembersRequest) returns (ListMembersResponse); + + //Get + rpc GetMember(GetMemberRequest) returns (Member); + + //Update + rpc UpdateMember(UpdateMemberRequest) returns (Member); + + //Create + rpc CreateMember(CreateMemberRequest) returns (Member); + + //Delete + rpc DeleteMember(DeleteMemberRequest) returns (Aurora.Proto.General.Empty) {}; + + //************** + //Media Resource + //************** + //List + rpc ListMedia(ListMediaRequest) returns (ListMediaResponse); + + //Get + rpc GetMedia(GetMediaRequest) returns (Media); + + //Create + rpc CreateMedia(CreateMediaRequest) returns (Media); + + //Delete + rpc DeleteMedia(DeleteMediaRequest) returns (Aurora.Proto.General.Empty) {}; + + //CUSTOM: Stream + rpc StreamMedia(StreamMediaRequest) returns (stream Aurora.Proto.General.Chunk) {}; + + //CUSTOM: Sync + rpc SyncMedia(SyncMediaRequest) returns (stream Sync) {}; + + //*************************** + //EventSubscriptions Resource + //*************************** + //List + rpc ListEventSubscriptions(ListEventSubscriptionsRequest) returns (ListEventSubscriptionsResponse); + + //Create + rpc CreateEventSubscription(CreateEventSubscriptionRequest) returns (EventSubscription); + + //Delete + rpc DeleteEventSubscription(DeleteEventSubscriptionRequest) returns (Aurora.Proto.General.Empty); + + //CUSTOM: Create EventSubscription List + rpc CreateEventSubscriptionList(CreateEventSubscriptionListRequest) returns (CreateEventSubscriptionListResponse); + + //CUSTOM: Delete all + rpc DeleteAllEventSubscriptions(DeleteAllEventSubscriptionsRequest) returns (Aurora.Proto.General.Empty); + + //***** + //Event + //***** + //Get + rpc GetEvents(GetEventsRequest) returns (stream BaseEvent) {}; } -message JoinPartyRequest { - string userName = 2; +message Party { + //The resource name of the party + string name = 1; + string displayName = 2; + string description = 3; + string hostIp = 4; + Member hostMember = 5; + google.protobuf.Timestamp createdOn = 6; } -message JoinPartyResponse { - PartyJoinedStatusEnum status = 1; - string clientId = 2; -} - -message LeavePartyRequest { - string clientId = 1; +enum PartyJoinedStatusEnum { + InParty = 0; + NotInParty = 1; } message LeavePartyResponse { PartyJoinedStatusEnum status = 1; } -message PartyMember { - string userName = 1; - string id = 2; +message Member { + //Resource name of the party member to be returned (Added by server) + string name = 1; + string userName = 2; + + //Added by server string ipAddress = 3; - int32 port = 4; -} -message MembersResponse { - repeated PartyMember members = 1; + + //Added by server + google.protobuf.Timestamp addedOn = 4; } -enum PartyJoinedStatusEnum { - Connected = 0; - Disconnected = 1; +message ListMembersRequest { + //Resource name of the parent of the members collection to be returned (The party) + string parent = 1; + int32 pageSize = 2; + string pageToken = 3; } -message QueueResponse{ - repeated RemoteMediaData mediaList = 1; +message ListMembersResponse { + repeated Member members = 1; + string nextPageToken = 2; +} +message GetMemberRequest { + //Resource name of the member to be returned + string name = 1; } -message RemoteMediaData { - string id = 1; +message CreateMemberRequest { + //Resource name of the parent collection of the member to be created (The party) + string parent = 1; + Member member = 2; +} + +message UpdateMemberRequest { + Member member = 1; + google.protobuf.FieldMask updateMask = 2; +} + +message DeleteMemberRequest { + //Resource name of the member to be deleted + string name = 1; +} + +message Media { + //Resource name of the remote media object + string name = 1; string title = 2; string artist = 3; string album = 4; string duration = 5; } + +message ListMediaRequest { + //Resource name of the parent of the media collection to be listed (The party) + string parent = 1; + int32 pageSize = 2; + string pageToken = 3; +} + +message ListMediaResponse { + repeated Media media = 1; + string nextPageToken = 3; +} + +message GetMediaRequest { + //Resource name of the media requested + string name = 1; +} + +message CreateMediaRequest { + //Resource name of the parent collection of the member to be created (The party) + string parent = 1; + Media media = 2; +} + +message DeleteMediaRequest { + //Resource name of the member to be deleted + string name = 1; +} + +message StreamMediaRequest { + //Resource name of the media requested + string name = 1; +} + +message SyncMediaRequest { + //Resource name of the media to sync with + string name = 1; +} + +message Sync { + int64 serverTimeTicks = 1; + float trackPosition= 2; +} + +/* Event Types */ +enum EventType { + MemberCreated = 0; + MemberDeleted = 1; + MediaPlaying = 2; + MediaStopped = 3; +} + +message BaseEvent { + //Resource name of the event ? + string name = 1; + EventType eventType = 2; + string clientKey = 3; + + oneof derivedEvent { + MemberCreatedEvent memberCreatedEvent = 4; + MemberDeletedEvent memberDeletedEvent = 5; + NewMediaPlayingEvent newMediaPlayingEvent = 6; + MediaPausedEvent mediaPausedEvent = 7; + MediaResumedEvent mediaResumedEvent = 8; + } +} + +message NewMediaPlayingEvent { + Media media = 1; +} + +message MediaResumedEvent { + Aurora.Proto.General.Empty empty = 1; +} + +message MediaPausedEvent { + Aurora.Proto.General.Empty empty = 1; +} + +message MemberCreatedEvent { + Member member = 1; +} + +message MemberDeletedEvent { + string memberName = 1; +} + +message EventSubscription { + EventType type = 2; +} + +message ListEventSubscriptionsRequest { + //Resource name of parent to the subscription list (The member) + string parent = 1; + int32 pageSize = 2; + string pageToken = 3; +} + +message ListEventSubscriptionsResponse { + repeated EventSubscription subscriptions = 1; +} + +message CreateEventSubscriptionRequest { + //Resource name of the parent to the subscription list (The member) + string parent = 1; + EventSubscription eventSubscription = 2; +} + +message DeleteEventSubscriptionRequest { + //Resource name of the subscription to delete + string parent = 1; + EventType type = 2; +} + +message CreateEventSubscriptionListRequest { + //Resource name of the parent to the subscription list (The member) + string parent = 1; + repeated EventSubscription eventSubscriptions = 2; +} + +message CreateEventSubscriptionListResponse { + repeated EventSubscription eventSubscriptions = 1; +} + +message DeleteAllEventSubscriptionsRequest { + //Resource name of the parent to the subscription list (the member) + string parent = 1; +} + +message GetEventsRequest { + //Resource name of the parent to the event stream (the member) + string parent = 1; +} diff --git a/Aurora/Proto/party.v2.proto b/Aurora/Proto/party.v2.proto deleted file mode 100644 index 175a219..0000000 --- a/Aurora/Proto/party.v2.proto +++ /dev/null @@ -1,276 +0,0 @@ -syntax = "proto3"; - -package Aurora.Proto.PartyV2; - -import "Proto/general.proto"; -import "google/protobuf/timestamp.proto"; -import "google/protobuf/field_mask.proto"; - -//Party - //Members - //EventSubscriptions - //Events - //Media - -service RemotePartyService { - //************** - //Party Resource - //************** - //Get Party - rpc GetParty(Aurora.Proto.General.Empty) returns (Party); - - //*************** - //Member Resource - //*************** - //List - rpc ListMembers(ListMembersRequest) returns (ListMembersResponse); - - //Get - rpc GetMember(GetMemberRequest) returns (Member); - - //Update - rpc UpdateMember(UpdateMemberRequest) returns (Member); - - //Create - rpc CreateMember(CreateMemberRequest) returns (Member); - - //Delete - rpc DeleteMember(DeleteMemberRequest) returns (Aurora.Proto.General.Empty) {}; - - //************** - //Media Resource - //************** - //List - rpc ListMedia(ListMediaRequest) returns (ListMediaResponse); - - //Get - rpc GetMedia(GetMediaRequest) returns (Media); - - //Create - rpc CreateMedia(CreateMediaRequest) returns (Media); - - //Delete - rpc DeleteMedia(DeleteMediaRequest) returns (Aurora.Proto.General.Empty) {}; - - //CUSTOM: Stream - rpc StreamMedia(StreamMediaRequest) returns (stream Aurora.Proto.General.Chunk) {}; - - //CUSTOM: Sync - rpc SyncMedia(SyncMediaRequest) returns (stream Sync) {}; - - //*************************** - //EventSubscriptions Resource - //*************************** - //List - rpc ListEventSubscriptions(ListEventSubscriptionsRequest) returns (ListEventSubscriptionsResponse); - - //Create - rpc CreateEventSubscription(CreateEventSubscriptionRequest) returns (EventSubscription); - - //Delete - rpc DeleteEventSubscription(DeleteEventSubscriptionRequest) returns (Aurora.Proto.General.Empty); - - //CUSTOM: Delete all - rpc DeleteAllEventSubscriptions(DeleteAllEventSubscriptionsRequest) returns (Aurora.Proto.General.Empty); - - //***** - //Event - //***** - //Get - rpc GetEvents(GetEventsRequest) returns (stream BaseEvent) {}; -} - -message Party { - //The resource name of the party - string name = 1; - string displayName = 2; - string description = 3; - string hostIp = 4; - Member hostMember = 5; - google.protobuf.Timestamp createdOn = 6; -} - -enum PartyJoinedStatusEnum { - InParty = 0; - NotInParty = 1; -} - -message LeavePartyResponse { - PartyJoinedStatusEnum status = 1; -} - -message Member { - //Resource name of the party member to be returned (Added by server) - string name = 1; - string userName = 2; - - //Added by server - string ipAddress = 3; - - //Added by server - google.protobuf.Timestamp addedOn = 4; -} - -message ListMembersRequest { - //Resource name of the parent of the members collection to be returned (The party) - string parent = 1; - int32 pageSize = 2; - string pageToken = 3; -} - -message ListMembersResponse { - repeated Member members = 1; - string nextPageToken = 2; -} -message GetMemberRequest { - //Resource name of the member to be returned - string name = 1; -} - -message CreateMemberRequest { - //Resource name of the parent collection of the member to be created (The party) - string parent = 1; - Member member = 2; -} - -message UpdateMemberRequest { - Member member = 1; - google.protobuf.FieldMask updateMask = 2; -} - -message DeleteMemberRequest { - //Resource name of the member to be deleted - string name = 1; -} - -message Media { - //Resource name of the remote media object - string name = 1; - string title = 2; - string artist = 3; - string album = 4; - string duration = 5; -} - -message ListMediaRequest { - //Resource name of the parent of the media collection to be listed (The party) - string parent = 1; - int32 pageSize = 2; - string pageToken = 3; -} - -message ListMediaResponse { - repeated Media media = 1; - string nextPageToken = 3; -} - -message GetMediaRequest { - //Resource name of the media requested - string name = 1; -} - -message CreateMediaRequest { - //Resource name of the parent collection of the member to be created (The party) - string parent = 1; - Media media = 2; -} - -message DeleteMediaRequest { - //Resource name of the member to be deleted - string name = 1; -} - -message StreamMediaRequest { - //Resource name of the media requested - string name = 1; -} - -message SyncMediaRequest { - //Resource name of the media to sync with - string name = 1; -} - -message Sync { - int64 serverTimeTicks = 1; - float trackPosition= 2; -} - -/* Event Types */ -enum EventType { - MemberCreated = 0; - MemberDeleted = 1; - MediaPlaying = 2; - MediaStopped = 3; -} - -message BaseEvent { - //Resource name of the event ? - string name = 1; - EventType eventType = 2; - string clientKey = 3; - - oneof derivedEvent { - MemberCreatedEvent memberCreatedEvent = 4; - MemberDeletedEvent memberDeletedEvent = 5; - NewMediaPlayingEvent newMediaPlayingEvent = 6; - MediaPausedEvent mediaPausedEvent = 7; - MediaResumedEvent mediaResumedEvent = 8; - } -} - -message NewMediaPlayingEvent { - Media media = 1; -} - -message MediaResumedEvent { - Aurora.Proto.General.Empty empty = 1; -} - -message MediaPausedEvent { - Aurora.Proto.General.Empty empty = 1; -} - -message MemberCreatedEvent { - Member member = 1; -} - -message MemberDeletedEvent { - string memberName = 1; -} - -message EventSubscription { - EventType type = 2; -} - -message ListEventSubscriptionsRequest { - //Resource name of parent to the subscription list (The member) - string parent = 1; - int32 pageSize = 2; - string pageToken = 3; -} - -message ListEventSubscriptionsResponse { - repeated EventSubscription subscriptions = 1; -} - -message CreateEventSubscriptionRequest { - //Resource name of the parent to the subscription list (The member) - string parent = 1; - EventSubscription eventSubscription = 2; -} - -message DeleteEventSubscriptionRequest { - //Resource name of the subscription to delete - string parent = 1; - EventType type = 2; -} - -message DeleteAllEventSubscriptionsRequest { - //Resource name of the parent to the subscription list (the member) - string parent = 1; -} - -message GetEventsRequest { - //Resource name of the parent to the event stream (the member) - string parent = 1; -} diff --git a/Aurora/Proto/playback.proto b/Aurora/Proto/playback.proto deleted file mode 100644 index 0f6832a..0000000 --- a/Aurora/Proto/playback.proto +++ /dev/null @@ -1,13 +0,0 @@ -syntax = "proto3"; - -package Aurora.Proto.Playback; - -import "Proto/general.proto"; - -service RemotePlaybackService { - rpc GetSongStream(SongRequest) returns (stream Aurora.Proto.General.Chunk) {}; -} - -message SongRequest { - string id = 1; -} \ No newline at end of file diff --git a/Aurora/Proto/sync.proto b/Aurora/Proto/sync.proto deleted file mode 100644 index 7235163..0000000 --- a/Aurora/Proto/sync.proto +++ /dev/null @@ -1,15 +0,0 @@ -syntax = "proto3"; - -package Aurora.Proto.Sync; - -import "Proto/general.proto"; -import "google/protobuf/timestamp.proto"; - -service RemoteSyncService { - rpc GetMediaSync(Aurora.Proto.General.Empty) returns (stream Sync) {}; -} - -message Sync { - int64 serverTimeTicks = 1; - float trackPosition= 2; -} \ No newline at end of file diff --git a/Aurora/RemoteImpl/RemoteEventImpl.cs b/Aurora/RemoteImpl/RemoteEventImpl.cs deleted file mode 100644 index 7a50b8a..0000000 --- a/Aurora/RemoteImpl/RemoteEventImpl.cs +++ /dev/null @@ -1,97 +0,0 @@ -using System; -using System.Threading.Tasks; -using System.Threading; -using System.Collections.Generic; -using Aurora.Services; -using Aurora.Utils; -using System.Linq; -using Aurora.Services.EventManager; -using Aurora.Proto.Events; - -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 override Task GetEvents(EventsRequest request, - Grpc.Core.IServerStreamWriter responseStream, - Grpc.Core.ServerCallContext context) - { - string peerId = Misc.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 callback = (BaseEvent bEvent) => - { - Console.WriteLine(string.Format("SERVER - Event fired for peer: {0}", peerId)); - //TODO need to remove callback if stream no longer exists IE. Client crashed or stopped - responseStream.WriteAsync(bEvent); - - }; - - Action cancelled = () => - { - are.Set(); - }; - - EventManager.Instance.AddEventHandler(callback, cancelled, Misc.Combine(new string[] { context.Peer, request.ClientId })); - are.WaitOne(); - return Task.FromResult(null); - } - - /// - /// RPC for subscribing to remote events - /// - /// SubscribeRequest - /// gRPC client context - /// - public override Task SubscribeToEvents(SubscribeRequest request, Grpc.Core.ServerCallContext context) - { - Console.WriteLine(string.Format("SERVER - Subscription from client with id: {0}", request.ClientId)); - EventManager.Instance.AddSubscriptionList(Misc.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(Misc.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(Misc.Combine(new string[] { context.Peer, request.ClientId })); - - return Task.FromResult(new SubscriptionResponse { Successful = true }); - } - } - -} \ No newline at end of file diff --git a/Aurora/RemoteImpl/RemotePartyImpl.cs b/Aurora/RemoteImpl/RemotePartyImpl.cs deleted file mode 100644 index afb15b4..0000000 --- a/Aurora/RemoteImpl/RemotePartyImpl.cs +++ /dev/null @@ -1,148 +0,0 @@ -using System; -using System.Threading.Tasks; -using System.Collections.ObjectModel; -using System.Linq; -using Aurora.Utils; -using Aurora.Proto.Party; -using Aurora.Proto.Events; -using Aurora.Services.EventManager; -using Aurora.Models.Media; -using Aurora.Services.Library; -using Autofac; - -namespace Aurora.RemoteImpl -{ - public class RemotePartyServiceImpl : RemotePartyService.RemotePartyServiceBase - { - /// - /// Dictionary of party members. Key -> ClientId - /// - private ObservableCollection _partyMembers; - - public RemotePartyServiceImpl() - { - _partyMembers = new ObservableCollection(); - } - - public ObservableCollection PartyMembers - { - get - { - return _partyMembers; - } - } - - public override Task JoinParty(JoinPartyRequest request, Grpc.Core.ServerCallContext context) - { - PartyMember partyMember = new PartyMember("") - { - UserName = request.UserName, - IpAddress = context.Host, - }; - - Console.WriteLine("SERVER - Client joined party: " + partyMember.Id); - - _partyMembers.Add(partyMember); - - BaseEvent e = new BaseEvent - { - EventType = EventType.PartyMemberJoined, - PartyMemberJoinedEvent = new PartyMemberJoinedEvent - { - Member = partyMember, - } - }; - - EventManager.Instance.FireEvent(e); - - JoinPartyResponse response = new JoinPartyResponse() { Status = PartyJoinedStatusEnum.Connected, ClientId = partyMember.Id }; - return Task.FromResult(response); - } - - public override Task LeaveParty(LeavePartyRequest request, Grpc.Core.ServerCallContext context) - { - PartyMember partyMember = _partyMembers.Where(e => e.Id == request.ClientId).Single(); - - _partyMembers.Remove(partyMember); - - BaseEvent bv = new BaseEvent - { - EventType = EventType.PartyMemberJoined, - PartyMemberLeftEvent = new PartyMemberLeftEvent - { - Member = partyMember, - } - }; - - EventManager.Instance.FireEvent(bv); - EventManager.Instance.RemoveAllSubscriptions(Misc.Combine(new string[] { context.Peer, request.ClientId })); - EventManager.Instance.CancelEventStream(Misc.Combine(new string[] { context.Peer, request.ClientId })); - - LeavePartyResponse response = new LeavePartyResponse() { Status = PartyJoinedStatusEnum.Disconnected }; - return Task.FromResult(response); - } - - public override Task GetPartyMembers(Proto.General.Empty empty, Grpc.Core.ServerCallContext context) - { - MembersResponse response = new MembersResponse(); - response.Members.AddRange(_partyMembers); - return Task.FromResult(response); - } - - public override Task GetQueue(Proto.General.Empty empty, Grpc.Core.ServerCallContext context) - { - QueueResponse mediaList = new QueueResponse(); - - //This will change as queuing operation gets solidified - //Simply return the hosts library - using (var scope = App.Container.BeginLifetimeScope()) - { - ILibraryService library = scope.Resolve(); - - ObservableCollection queue = library.GetLibrary(); - - - foreach (BaseMedia media in queue) - { - AudioMetadata metadata = new AudioMetadata(); - try - { - if (media.Metadata is AudioMetadata) - { - metadata = media.Metadata as AudioMetadata; - - RemoteMediaData data = new RemoteMediaData(); - data.Id = media.Id; - if (metadata.Title != null) - { - data.Title = metadata.Title; - } - if (metadata.Artist != null) - { - data.Artist = metadata.Artist; - } - if (metadata.Album != null) - { - data.Album = metadata.Album; - } - if (metadata.Duration != null) - { - data.Duration = metadata.Duration; - } - - mediaList.MediaList.Add(data); - } - } - catch (Exception ex) - { - Console.WriteLine(string.Format("Error preparing queue: {0}", ex.Message)); - } - } - } - - - return Task.FromResult(mediaList); - - } - } -} \ No newline at end of file diff --git a/Aurora/RemoteImpl/RemotePlaybackImpl.cs b/Aurora/RemoteImpl/RemotePlaybackImpl.cs deleted file mode 100644 index 9d554e8..0000000 --- a/Aurora/RemoteImpl/RemotePlaybackImpl.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System; -using System.Threading.Tasks; -using Aurora.Proto.Playback; -using Aurora.Proto.General; -using Aurora.Models.Media; -using Aurora.Services.Library; -using Autofac; - -namespace Aurora.RemoteImpl -{ - public class RemotePlaybackServiceImpl : RemotePlaybackService.RemotePlaybackServiceBase - { - public override async Task GetSongStream(SongRequest request, - Grpc.Core.IServerStreamWriter responseStream, - Grpc.Core.ServerCallContext context) - { - using (var scope = App.Container.BeginLifetimeScope()) - { - ILibraryService library = scope.Resolve(); - - BaseMedia originalSong = library.GetSong(request.Id); - if (!(originalSong is LocalAudio)) - { - return; - } - - //Copy media object to not interfere with other threads - LocalAudio songCopy = new LocalAudio((LocalAudio)originalSong); - - try - { - //Load only if not already loaded. (Multiple clients may be requesting media) - if (!songCopy.IsLoaded) - { - await songCopy.Load(); - } - - //Send stream - Console.WriteLine("Begin sending file"); - byte[] buffer = new byte[2048]; // read in chunks of 2KB - int bytesRead; - while ((bytesRead = songCopy.DataStream.Read(buffer, 0, buffer.Length)) > 0) - { - Google.Protobuf.ByteString bufferByteString = Google.Protobuf.ByteString.CopyFrom(buffer); - await responseStream.WriteAsync(new Chunk { Content = bufferByteString }); - } - Console.WriteLine("Done sending file"); - } - catch (Exception ex) - { - Console.WriteLine("Exception caught while sending audio file: " + ex.Message); - } - } - - } - } -} \ No newline at end of file diff --git a/Aurora/RemoteImpl/RemoteSyncImpl.cs b/Aurora/RemoteImpl/RemoteSyncImpl.cs deleted file mode 100644 index 1b32902..0000000 --- a/Aurora/RemoteImpl/RemoteSyncImpl.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System; -using System.Threading.Tasks; -using Aurora.Proto.Sync; -using Aurora.Proto.General; -using Aurora.Services.Player; -using Aurora; -using Autofac; - -namespace Aurora.RemoteImpl -{ - public class RemoteSyncServiceImpl : RemoteSyncService.RemoteSyncServiceBase - { - /// - /// RPC for getting a stream of media syncs - /// - /// - /// - /// - /// - public override async Task GetMediaSync(Empty request, - Grpc.Core.IServerStreamWriter responseStream, - Grpc.Core.ServerCallContext context) - { - using (var scope = App.Container.BeginLifetimeScope()) - { - IPlayer player = scope.Resolve(); - bool continueSync = true; - string currentId = player.CurrentMedia.Id; - MediaChangedEventHandler mediaChanged = (sender, e) => - { - if (e.NewId != currentId) - { - continueSync = false; - } - }; - - player.MediaChanged += mediaChanged; - - while (continueSync) - { - float length = player.CurrentMediaLength; - - Sync sync = new Sync() - { - TrackPosition = player.CurrentMediaPosition, - ServerTimeTicks = Utils.TimeUtils.GetNetworkTime().DateTime.Ticks - }; - await responseStream.WriteAsync(sync); - Console.WriteLine("Sent Sync"); - await Task.Delay(5000); - } - } - - } - } -} \ No newline at end of file diff --git a/Aurora/Services/BaseService.cs b/Aurora/Services/BaseService.cs deleted file mode 100644 index db02456..0000000 --- a/Aurora/Services/BaseService.cs +++ /dev/null @@ -1,22 +0,0 @@ -using System; - -namespace Aurora.Services -{ - public abstract class BaseService where T : class - { - private static volatile Lazy _instance = new Lazy(() => CreateInstanceOfT()); - - public static T Instance - { - get { return _instance.Value; } - } - - private static T CreateInstanceOfT() - { - - return Activator.CreateInstance(typeof(T), true) as T; - - } - } -} - diff --git a/Aurora/Services/Client/ClientService.cs b/Aurora/Services/Client/ClientService.cs new file mode 100644 index 0000000..3ee1f38 --- /dev/null +++ b/Aurora/Services/Client/ClientService.cs @@ -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; + } + } +} diff --git a/Aurora/Services/Client/IClientService.cs b/Aurora/Services/Client/IClientService.cs new file mode 100644 index 0000000..5ebe8b5 --- /dev/null +++ b/Aurora/Services/Client/IClientService.cs @@ -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(); + } +} \ No newline at end of file diff --git a/Aurora/Services/ClientService/ClientService.cs b/Aurora/Services/ClientService/ClientService.cs deleted file mode 100644 index 3b69d25..0000000 --- a/Aurora/Services/ClientService/ClientService.cs +++ /dev/null @@ -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; - } - - /// - /// 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(); - } - } - } -} \ No newline at end of file diff --git a/Aurora/Services/ClientService/EventInfo.cs b/Aurora/Services/ClientService/EventInfo.cs deleted file mode 100644 index f071e62..0000000 --- a/Aurora/Services/ClientService/EventInfo.cs +++ /dev/null @@ -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; } - } -} \ No newline at end of file diff --git a/Aurora/Services/ClientService/Events/MediaPausedEvent.cs b/Aurora/Services/ClientService/Events/MediaPausedEvent.cs deleted file mode 100644 index 9401103..0000000 --- a/Aurora/Services/ClientService/Events/MediaPausedEvent.cs +++ /dev/null @@ -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; - } - } -} \ No newline at end of file diff --git a/Aurora/Services/ClientService/Events/MediaResumedEvent.cs b/Aurora/Services/ClientService/Events/MediaResumedEvent.cs deleted file mode 100644 index 5dbb87a..0000000 --- a/Aurora/Services/ClientService/Events/MediaResumedEvent.cs +++ /dev/null @@ -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; - } - } -} \ No newline at end of file diff --git a/Aurora/Services/ClientService/Events/NewMediaPlayingEvent.cs b/Aurora/Services/ClientService/Events/NewMediaPlayingEvent.cs deleted file mode 100644 index b47d669..0000000 --- a/Aurora/Services/ClientService/Events/NewMediaPlayingEvent.cs +++ /dev/null @@ -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; - } - } -} \ No newline at end of file diff --git a/Aurora/Services/ClientService/Events/PartyMemberJoinedEvent.cs b/Aurora/Services/ClientService/Events/PartyMemberJoinedEvent.cs deleted file mode 100644 index 64eb86b..0000000 --- a/Aurora/Services/ClientService/Events/PartyMemberJoinedEvent.cs +++ /dev/null @@ -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; - } - } -} \ No newline at end of file diff --git a/Aurora/Services/ClientService/Events/PartyMemberLeftEvent.cs b/Aurora/Services/ClientService/Events/PartyMemberLeftEvent.cs deleted file mode 100644 index 18eb179..0000000 --- a/Aurora/Services/ClientService/Events/PartyMemberLeftEvent.cs +++ /dev/null @@ -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; - } - } -} \ No newline at end of file diff --git a/Aurora/Services/ClientService/IClientService.cs b/Aurora/Services/ClientService/IClientService.cs deleted file mode 100644 index ec116c1..0000000 --- a/Aurora/Services/ClientService/IClientService.cs +++ /dev/null @@ -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(); - - /// - /// Asynchronous function for processing events off of the event stream. - /// - /// - Task GetEvents(); - - void StopEvents(); - } -} \ No newline at end of file diff --git a/Aurora/Services/Server/EventManager/EventAction.cs b/Aurora/Services/EventManager/EventAction.cs similarity index 84% rename from Aurora/Services/Server/EventManager/EventAction.cs rename to Aurora/Services/EventManager/EventAction.cs index eabd846..18bf2a9 100644 --- a/Aurora/Services/Server/EventManager/EventAction.cs +++ b/Aurora/Services/EventManager/EventAction.cs @@ -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 { diff --git a/Aurora/Services/EventManager/EventManager.cs b/Aurora/Services/EventManager/EventManager.cs index c9e4886..058e70c 100644 --- a/Aurora/Services/EventManager/EventManager.cs +++ b/Aurora/Services/EventManager/EventManager.cs @@ -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 callback, Action cancel) - { - Callback = callback; - Cancel = cancel; - } - public Action Callback { get; set; } - public Action Cancel { get; set; } - } - public class EventManager : BaseService + public class EventManager : IEventManager { #region Fields private Dictionary> _subscriptionList; @@ -38,32 +27,32 @@ namespace Aurora.Services.EventManager #region Public Methods /// - /// Get the list of event type subscriptions for a given session id. + /// Get the list of event type subscriptions for a given sessionIdentifier id. /// - /// Session Id + /// sessionIdentifier Id /// - public List GetSubscriptionList(string session) + public List GetSubscriptionList(string sessionIdentifier) { List eventList = new List(); - if (_subscriptionList.ContainsKey(session)) + if (_subscriptionList.ContainsKey(sessionIdentifier)) { - _subscriptionList.TryGetValue(session, out eventList); + _subscriptionList.TryGetValue(sessionIdentifier, out eventList); } return eventList; } /// - /// Get the number of event subscriptions for a given session + /// Get the number of event subscriptions for a given sessionIdentifier /// - /// Session Id + /// sessionIdentifier Id /// - public int GetSubscriptionCount(string session) + public int GetSubscriptionCount(string sessionIdentifier) { List eventList = new List(); - 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 /// /// Add a new subscription /// - /// + /// /// - 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 eventList = new List(); eventList.Add(type); - _subscriptionList.Add(session, eventList); + _subscriptionList.Add(sessionIdentifier, eventList); success = true; } else { - _subscriptionList.TryGetValue(session, out List eventList); + _subscriptionList.TryGetValue(sessionIdentifier, out List eventList); if (eventList != null) { eventList.Add(type); @@ -104,79 +93,82 @@ namespace Aurora.Services.EventManager /// /// Add a list of subscriptions. This unsubscribes from unused events. /// - /// The browser session id. + /// The browser sessionIdentifier id. /// The list of event types to subscribe to. - public void AddSubscriptionList(string session, List types) + public void AddSubscriptionList(string sessionIdentifier, List types) { - RemoveAllSubscriptions(session); + RemoveAllSubscriptions(sessionIdentifier); foreach (EventType e in types) { - AddSubscription(session, e); + AddSubscription(sessionIdentifier, e); } } /// /// Unsubscribe from a given event type. /// - /// Session Id + /// sessionIdentifier Id /// Event Type to be removed - 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 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 types) + public void RemoveSubscriptionList(string sessionIdentifier, List types) { foreach (EventType e in types) { - RemoveSubscription(session, e); + RemoveSubscription(sessionIdentifier, e); } } /// - /// Remove all subscriptons for a given session. + /// Remove all subscriptons for a given sessionIdentifier. /// - /// Session Id - public void RemoveAllSubscriptions(string session) + /// sessionIdentifier Id + public void RemoveAllSubscriptions(string sessionIdentifier) { - if (_subscriptionList.ContainsKey(session)) + if (_subscriptionList.ContainsKey(sessionIdentifier)) { - _subscriptionList.Remove(session); + _subscriptionList.Remove(sessionIdentifier); } } - public void AddEventHandler(Action action, Action cancel, string sessionId) + public void AddEventHandler(Action 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) diff --git a/Aurora/Services/Server/EventManager/IEventManager.cs b/Aurora/Services/EventManager/IEventManager.cs similarity index 96% rename from Aurora/Services/Server/EventManager/IEventManager.cs rename to Aurora/Services/EventManager/IEventManager.cs index 11bf8d1..922b710 100644 --- a/Aurora/Services/Server/EventManager/IEventManager.cs +++ b/Aurora/Services/EventManager/IEventManager.cs @@ -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 { diff --git a/Aurora/Services/Player/IPlayer.cs b/Aurora/Services/Player/IPlayer.cs index 8fde035..3698ccc 100644 --- a/Aurora/Services/Player/IPlayer.cs +++ b/Aurora/Services/Player/IPlayer.cs @@ -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 { diff --git a/Aurora/Services/Player/PlayerService.cs b/Aurora/Services/Player/PlayerService.cs index 032c377..83e19b9 100644 --- a/Aurora/Services/Player/PlayerService.cs +++ b/Aurora/Services/Player/PlayerService.cs @@ -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 syncStream = remoteSyncClient - .GetMediaSync(new Proto.General.Empty())) + using (AsyncServerStreamingCall syncStream = remotePartyServiceClient + .SyncMedia(new SyncMediaRequest() { })) { try { diff --git a/Aurora/Services/Server/Controllers/Constructor.cs b/Aurora/Services/Server/Controllers/Constructor.cs index a0c78bd..f22c6a8 100644 --- a/Aurora/Services/Server/Controllers/Constructor.cs +++ b/Aurora/Services/Server/Controllers/Constructor.cs @@ -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 diff --git a/Aurora/Services/Server/Controllers/EventController.cs b/Aurora/Services/Server/Controllers/EventController.cs index 29fec14..abed097 100644 --- a/Aurora/Services/Server/Controllers/EventController.cs +++ b/Aurora/Services/Server/Controllers/EventController.cs @@ -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 diff --git a/Aurora/Services/Server/Controllers/EventSubscriptionController.cs b/Aurora/Services/Server/Controllers/EventSubscriptionController.cs index d78dd7c..77c7ca7 100644 --- a/Aurora/Services/Server/Controllers/EventSubscriptionController.cs +++ b/Aurora/Services/Server/Controllers/EventSubscriptionController.cs @@ -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 ListEventSubscriptions(ListEventSubscriptionsRequest request, Grpc.Core.ServerCallContext context) + public override Task ListEventSubscriptions( + ListEventSubscriptionsRequest request, + Grpc.Core.ServerCallContext context) { throw new NotImplementedException(); } - public override Task CreateEventSubscription(CreateEventSubscriptionRequest request, Grpc.Core.ServerCallContext context) + public override Task 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 DeleteEventSubscription(DeleteEventSubscriptionRequest request, Grpc.Core.ServerCallContext context) + public override Task CreateEventSubscriptionList( + CreateEventSubscriptionListRequest request, + Grpc.Core.ServerCallContext context) + { + Console.WriteLine(string.Format("SERVER - Subscription from client with id: {0}", request.Parent)); + + List eventTypes = new List(); + 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 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 DeleteAllEventSubscriptions(DeleteAllEventSubscriptionsRequest request, Grpc.Core.ServerCallContext context) + public override Task DeleteAllEventSubscriptions( + DeleteAllEventSubscriptionsRequest request, + Grpc.Core.ServerCallContext context) { this._eventManager.RemoveAllSubscriptions(Misc.Combine(new string[] { context.Peer, request.Parent })); diff --git a/Aurora/Services/Server/Controllers/MediaController.cs b/Aurora/Services/Server/Controllers/MediaController.cs index 0e7d2e8..1a49bd7 100644 --- a/Aurora/Services/Server/Controllers/MediaController.cs +++ b/Aurora/Services/Server/Controllers/MediaController.cs @@ -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; diff --git a/Aurora/Services/Server/Controllers/MemberController.cs b/Aurora/Services/Server/Controllers/MemberController.cs index 02b4a65..f878101 100644 --- a/Aurora/Services/Server/Controllers/MemberController.cs +++ b/Aurora/Services/Server/Controllers/MemberController.cs @@ -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; diff --git a/Aurora/Services/Server/Controllers/PartyController.cs b/Aurora/Services/Server/Controllers/PartyController.cs index 1f43962..1ca6eeb 100644 --- a/Aurora/Services/Server/Controllers/PartyController.cs +++ b/Aurora/Services/Server/Controllers/PartyController.cs @@ -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; diff --git a/Aurora/Services/Server/EventManager/EventManager.cs b/Aurora/Services/Server/EventManager/EventManager.cs deleted file mode 100644 index a258428..0000000 --- a/Aurora/Services/Server/EventManager/EventManager.cs +++ /dev/null @@ -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> _subscriptionList; - private Dictionary _actionList; - - #endregion Fields - public EventManager() - { - _subscriptionList = new Dictionary>(); - _actionList = new Dictionary(); - - } - - #region Private Methods - - - #endregion Private Methods - - #region Public Methods - /// - /// Get the list of event type subscriptions for a given sessionIdentifier id. - /// - /// sessionIdentifier Id - /// - public List GetSubscriptionList(string sessionIdentifier) - { - List eventList = new List(); - if (_subscriptionList.ContainsKey(sessionIdentifier)) - { - _subscriptionList.TryGetValue(sessionIdentifier, out eventList); - } - - return eventList; - } - - /// - /// Get the number of event subscriptions for a given sessionIdentifier - /// - /// sessionIdentifier Id - /// - public int GetSubscriptionCount(string sessionIdentifier) - { - List eventList = new List(); - if (_subscriptionList.ContainsKey(sessionIdentifier)) - { - _subscriptionList.TryGetValue(sessionIdentifier, out eventList); - } - - return eventList.Count(); - } - - /// - /// Add a new subscription - /// - /// - /// - public bool AddSubscription(string sessionIdentifier, EventType type) - { - bool success = false; - lock (_subscriptionList) - { - if (!_subscriptionList.ContainsKey(sessionIdentifier)) - { - //Add sessionIdentifier to subscription list - List eventList = new List(); - eventList.Add(type); - _subscriptionList.Add(sessionIdentifier, eventList); - success = true; - } - else - { - _subscriptionList.TryGetValue(sessionIdentifier, out List eventList); - if (eventList != null) - { - eventList.Add(type); - success = true; - } - } - } - - return success; - } - - /// - /// Add a list of subscriptions. This unsubscribes from unused events. - /// - /// The browser sessionIdentifier id. - /// The list of event types to subscribe to. - public void AddSubscriptionList(string sessionIdentifier, List types) - { - RemoveAllSubscriptions(sessionIdentifier); - - foreach (EventType e in types) - { - AddSubscription(sessionIdentifier, e); - } - } - - /// - /// Unsubscribe from a given event type. - /// - /// sessionIdentifier Id - /// Event Type to be removed - public void RemoveSubscription(string sessionIdentifier, EventType type) - { - lock (_subscriptionList) - { - if (_subscriptionList.ContainsKey(sessionIdentifier)) - { - List 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 types) - { - foreach (EventType e in types) - { - RemoveSubscription(sessionIdentifier, e); - } - } - - /// - /// Remove all subscriptons for a given sessionIdentifier. - /// - /// sessionIdentifier Id - public void RemoveAllSubscriptions(string sessionIdentifier) - { - if (_subscriptionList.ContainsKey(sessionIdentifier)) - { - _subscriptionList.Remove(sessionIdentifier); - } - } - - public void AddEventHandler(Action 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 actionsCopy = new Dictionary(); - //Copy actions list - lock (_actionList) - { - foreach (KeyValuePair pair in _actionList) - { - actionsCopy.Add(pair.Key, pair.Value); - } - } - - lock (_subscriptionList) - { - foreach (KeyValuePair> 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 - - } -} \ No newline at end of file diff --git a/Aurora/Services/Server/Server/ServerService.cs b/Aurora/Services/Server/Server/ServerService.cs index 7052c7f..3921bc7 100644 --- a/Aurora/Services/Server/Server/ServerService.cs +++ b/Aurora/Services/Server/Server/ServerService.cs @@ -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 diff --git a/Aurora/Services/ServerService.cs b/Aurora/Services/ServerService.cs deleted file mode 100644 index 8d2c0ac..0000000 --- a/Aurora/Services/ServerService.cs +++ /dev/null @@ -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 - { - 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; - - /// - /// Constructor. Registers GRPC service implementations. - /// - public ServerService() - { - string host = GetLocalIPAddress(); - using (var scope = App.Container.BeginLifetimeScope()) - { - var service = scope.Resolve(); - 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); - } - } - - - /// - /// Start Server - /// - 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)); - } - } - - /// - /// Shutdown server async. - /// - /// Task - 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; - } - } -} \ No newline at end of file diff --git a/Aurora/Services/Settings/ISettingsService.cs b/Aurora/Services/Settings/ISettingsService.cs index d8b74f3..4036b63 100644 --- a/Aurora/Services/Settings/ISettingsService.cs +++ b/Aurora/Services/Settings/ISettingsService.cs @@ -22,7 +22,7 @@ namespace Aurora.Services.Settings /// The current sessions clientId. This is assigned by the server. This is not persisted. /// /// - string ClientId { get; set; } + string ClientName { get; set; } string LibraryLocation { get; set; } } diff --git a/Aurora/Services/Settings/SettingsService.cs b/Aurora/Services/Settings/SettingsService.cs index 48a3617..a7930c1 100644 --- a/Aurora/Services/Settings/SettingsService.cs +++ b/Aurora/Services/Settings/SettingsService.cs @@ -67,7 +67,7 @@ namespace Aurora.Services.Settings /// The current sessions clientId. This is assigned by the server. This is not persisted. /// /// - public string ClientId + public string ClientName { get; set; }