From a01d399a1f589239514ead15f9e293559866fb43 Mon Sep 17 00:00:00 2001 From: watsonb8 Date: Fri, 5 Jul 2019 11:37:44 -0400 Subject: [PATCH] Refactored to only having one executor per platform --- Aurora/Aurora.csproj | 110 ++++++------------ Aurora/Backend/Executors/BaseExecutor.cs | 39 +++++-- Aurora/Backend/Executors/BasePartyExecutor.cs | 51 -------- .../ClientExecutor.cs} | 6 +- .../HostExecutor.cs} | 17 ++- .../Proto/{PartyService => }/party.proto | 5 +- .../{PlaybackService => }/playback.proto | 2 +- .../RemotePartyImpl.cs} | 6 +- .../Backend/RemoteImpl/RemotePlaybackImpl.cs | 32 +++++ .../Server/Playback/PlaybackServiceImpl.cs | 10 -- Aurora/Backend/Services/ServerService.cs | 25 +++- Aurora/Backend/Services/SettingsService.cs | 36 ++++++ .../HostSelector/HostSelector.xaml.cs | 2 +- .../Frontend/Views/MainView/MainViewModel.cs | 3 +- Aurora/Frontend/Views/Party/PartyViewModel.cs | 21 +++- .../Frontend/Views/Profile/ProfileView.xaml | 6 + .../Views/Profile/ProfileView.xaml.cs | 15 +++ .../Views/Profile/ProfileViewModel.cs | 10 ++ 18 files changed, 226 insertions(+), 170 deletions(-) delete mode 100644 Aurora/Backend/Executors/BasePartyExecutor.cs rename Aurora/Backend/{Client/Party/ClientPartyExecutor.cs => Executors/ClientExecutor.cs} (86%) rename Aurora/Backend/{Server/Party/HostPartyExecutor.cs => Executors/HostExecutor.cs} (60%) rename Aurora/Backend/Proto/{PartyService => }/party.proto (89%) rename Aurora/Backend/Proto/{PlaybackService => }/playback.proto (91%) rename Aurora/Backend/{Server/Party/PartyServiceImpl.cs => RemoteImpl/RemotePartyImpl.cs} (90%) create mode 100644 Aurora/Backend/RemoteImpl/RemotePlaybackImpl.cs delete mode 100644 Aurora/Backend/Server/Playback/PlaybackServiceImpl.cs create mode 100644 Aurora/Backend/Services/SettingsService.cs create mode 100644 Aurora/Frontend/Views/Profile/ProfileView.xaml create mode 100644 Aurora/Frontend/Views/Profile/ProfileView.xaml.cs create mode 100644 Aurora/Frontend/Views/Profile/ProfileViewModel.cs diff --git a/Aurora/Aurora.csproj b/Aurora/Aurora.csproj index c6ad3b5..d81916a 100644 --- a/Aurora/Aurora.csproj +++ b/Aurora/Aurora.csproj @@ -1,94 +1,52 @@ - + netstandard2.0 true - + pdbonly true - - - - - - - - - + + + + + + + + + - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + - + Player.xaml - - - + + + \ No newline at end of file diff --git a/Aurora/Backend/Executors/BaseExecutor.cs b/Aurora/Backend/Executors/BaseExecutor.cs index 30ede3c..05fa4d4 100644 --- a/Aurora/Backend/Executors/BaseExecutor.cs +++ b/Aurora/Backend/Executors/BaseExecutor.cs @@ -6,22 +6,47 @@ namespace Aurora.Backend.Executors { public abstract class BaseExecutor { - public BaseExecutor() + protected BaseExecutor() { } - public ExecutorType ExecutorType { get; protected set; } + public Type ExecutorType { get; protected set; } - public static BaseExecutor CreateExecutor(ExecutorType executorType) where T : BaseExecutor + public static BaseExecutor CreateExecutor() { - MethodInfo method = typeof(T).GetMethod("Create"); - if (method == null) + + BaseExecutor executor = null; + if (typeof(T) == typeof(HostExecutor)) { - throw new InvalidOperationException("Executor must include a 'create' method."); + executor = new HostExecutor(); + executor.ExecutorType = typeof(HostExecutor); + } + else if (typeof(T) == typeof(ClientExecutor)) + { + executor = new ClientExecutor(); + executor.ExecutorType = typeof(ClientExecutor); + } + else + { + throw new InvalidOperationException("Cannot create an executor of type: " + nameof(T)); } - return method.Invoke(null, new object[] { executorType }) as BaseExecutor; + return executor; } + + public abstract void Initialize(); + + public abstract void Run(); + + public abstract void Close(); + + public abstract void GetMembers(); + + public abstract void GetQueue(); + + public abstract void AddToQueue(); + + public abstract void RemoveFromQueue(); } public enum ExecutorType diff --git a/Aurora/Backend/Executors/BasePartyExecutor.cs b/Aurora/Backend/Executors/BasePartyExecutor.cs deleted file mode 100644 index 1c26b16..0000000 --- a/Aurora/Backend/Executors/BasePartyExecutor.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System; -using Aurora.Backend.Server.Party; -using Aurora.Backend.Client.Party; - -namespace Aurora.Backend.Executors -{ - public abstract class BasePartyExecutor : BaseExecutor - { - public BasePartyExecutor() - { - - } - - public static BasePartyExecutor Create(ExecutorType type) - { - BasePartyExecutor executor = null; - switch (type) - { - case ExecutorType.Client: - { - executor = new ClientPartyExecutor(); - executor.ExecutorType = type; - break; - } - case ExecutorType.Server: - { - executor = new HostPartyExecutor(); - executor.ExecutorType = type; - break; - } - } - - return executor; - } - - public abstract void Initialize(); - - public abstract void Run(); - - public abstract void Close(); - - public abstract void GetMembers(); - - public abstract void GetQueue(); - - public abstract void AddToQueue(); - - public abstract void RemoveFromQueue(); - - } -} \ No newline at end of file diff --git a/Aurora/Backend/Client/Party/ClientPartyExecutor.cs b/Aurora/Backend/Executors/ClientExecutor.cs similarity index 86% rename from Aurora/Backend/Client/Party/ClientPartyExecutor.cs rename to Aurora/Backend/Executors/ClientExecutor.cs index f7c0669..1583c65 100644 --- a/Aurora/Backend/Client/Party/ClientPartyExecutor.cs +++ b/Aurora/Backend/Executors/ClientExecutor.cs @@ -1,11 +1,11 @@ using System; using Aurora.Backend.Executors; -namespace Aurora.Backend.Client.Party +namespace Aurora.Backend.Executors { - public class ClientPartyExecutor : BasePartyExecutor + public class ClientExecutor : BaseExecutor { - public ClientPartyExecutor() + public ClientExecutor() { } diff --git a/Aurora/Backend/Server/Party/HostPartyExecutor.cs b/Aurora/Backend/Executors/HostExecutor.cs similarity index 60% rename from Aurora/Backend/Server/Party/HostPartyExecutor.cs rename to Aurora/Backend/Executors/HostExecutor.cs index 4d0eefc..c55ba56 100644 --- a/Aurora/Backend/Server/Party/HostPartyExecutor.cs +++ b/Aurora/Backend/Executors/HostExecutor.cs @@ -3,20 +3,25 @@ using System.Threading.Tasks; using Aurora.Backend.Executors; using Aurora.Backend.Services; using Aurora.Backend.Proto; +using Aurora.Backend.RemoteImpl; -namespace Aurora.Backend.Server.Party +namespace Aurora.Backend.Executors { - public class HostPartyExecutor : BasePartyExecutor + public class HostExecutor : BaseExecutor { - PartyServiceImpl _partyServiceImpl; - public HostPartyExecutor() + RemotePartyServiceImpl _remoteServiceImpl; + RemotePlaybackServiceImpl _remotePlaybackImpl; + public HostExecutor() { - _partyServiceImpl = new PartyServiceImpl(); + _remoteServiceImpl = new RemotePartyServiceImpl(); + } public override void Initialize() { - ServerService.Instance.RegisterService(PartyService.BindService(_partyServiceImpl)); + //Register grpc RemoteService with singleton server service + ServerService.Instance.RegisterService(RemotePartyService.BindService(_remoteServiceImpl)); + ServerService.Instance.RegisterService(RemotePlaybackService.BindService(_remotePlaybackImpl)); } public override async void Close() diff --git a/Aurora/Backend/Proto/PartyService/party.proto b/Aurora/Backend/Proto/party.proto similarity index 89% rename from Aurora/Backend/Proto/PartyService/party.proto rename to Aurora/Backend/Proto/party.proto index db844b7..5017088 100644 --- a/Aurora/Backend/Proto/PartyService/party.proto +++ b/Aurora/Backend/Proto/party.proto @@ -2,8 +2,9 @@ syntax = "proto3"; package Aurora.Backend.Proto; -// PartyServic definition -service PartyService{ +import "Backend/Proto/general.proto"; + +service RemotePartyService { //Party Service rpc JoinParty(JoinPartyRequest) returns (JoinPartyResponse); rpc LeaveParty(LeavePartyRequest) returns (LeavePartyResponse); diff --git a/Aurora/Backend/Proto/PlaybackService/playback.proto b/Aurora/Backend/Proto/playback.proto similarity index 91% rename from Aurora/Backend/Proto/PlaybackService/playback.proto rename to Aurora/Backend/Proto/playback.proto index 8ed189a..d9741e7 100644 --- a/Aurora/Backend/Proto/PlaybackService/playback.proto +++ b/Aurora/Backend/Proto/playback.proto @@ -4,7 +4,7 @@ package Aurora.Backend.Proto; import "Backend/Proto/general.proto"; -service PlaybackService { +service RemotePlaybackService { //Playback Service rpc GetPartyStream(Empty) returns (stream Chunk) {}; } diff --git a/Aurora/Backend/Server/Party/PartyServiceImpl.cs b/Aurora/Backend/RemoteImpl/RemotePartyImpl.cs similarity index 90% rename from Aurora/Backend/Server/Party/PartyServiceImpl.cs rename to Aurora/Backend/RemoteImpl/RemotePartyImpl.cs index e118751..84fa5cf 100644 --- a/Aurora/Backend/Server/Party/PartyServiceImpl.cs +++ b/Aurora/Backend/RemoteImpl/RemotePartyImpl.cs @@ -4,16 +4,16 @@ using System.Collections.Generic; using Aurora.Backend.Proto; using Aurora.Backend.Models; -namespace Aurora.Backend.Server.Party +namespace Aurora.Backend.RemoteImpl { - class PartyServiceImpl : PartyService.PartyServiceBase + public class RemotePartyServiceImpl : RemotePartyService.RemotePartyServiceBase { /// /// Dictionary of party members. Key -> ClientId /// private Dictionary _partyMembers; - public PartyServiceImpl() + public RemotePartyServiceImpl() { _partyMembers = new Dictionary(); } diff --git a/Aurora/Backend/RemoteImpl/RemotePlaybackImpl.cs b/Aurora/Backend/RemoteImpl/RemotePlaybackImpl.cs new file mode 100644 index 0000000..b91efe2 --- /dev/null +++ b/Aurora/Backend/RemoteImpl/RemotePlaybackImpl.cs @@ -0,0 +1,32 @@ +using System; +using System.Threading.Tasks; +using System.IO; +using Aurora.Backend.Proto; +using Aurora.Backend.Models; + +namespace Aurora.Backend.RemoteImpl +{ + public class RemotePlaybackServiceImpl : RemotePlaybackService.RemotePlaybackServiceBase + { + public override async Task GetPartyStream(Empty empty, + Grpc.Core.IServerStreamWriter responseStream, + Grpc.Core.ServerCallContext context) + { + throw new NotImplementedException("Working on it"); + // //Send stream + // string cwd = Directory.GetCurrentDirectory(); + // using (FileStream fs = System.IO.File.OpenRead(Path.Combine(cwd, request.FileName))) + // { + // Console.WriteLine("Begin sending file"); + // byte[] buffer = new byte[2048]; // read in chunks of 2KB + // int bytesRead; + // while ((bytesRead = fs.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"); + // }; + } + } +} \ No newline at end of file diff --git a/Aurora/Backend/Server/Playback/PlaybackServiceImpl.cs b/Aurora/Backend/Server/Playback/PlaybackServiceImpl.cs deleted file mode 100644 index 443c8f1..0000000 --- a/Aurora/Backend/Server/Playback/PlaybackServiceImpl.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; -using Aurora.Backend.Proto; - -namespace Aurora.Backend.Server -{ - class PlaybackServiceImpl : PlaybackService.PlaybackServiceBase - { - - } -} \ No newline at end of file diff --git a/Aurora/Backend/Services/ServerService.cs b/Aurora/Backend/Services/ServerService.cs index 8c88c1c..9d5a02c 100644 --- a/Aurora/Backend/Services/ServerService.cs +++ b/Aurora/Backend/Services/ServerService.cs @@ -7,7 +7,8 @@ namespace Aurora.Backend.Services { public class ServerService : BaseService { - private const int Port = 50051; + private string _hostname = "127.0.0.1"; + private int _port = 50001; private Grpc.Core.Server _server; /// @@ -15,12 +16,16 @@ namespace Aurora.Backend.Services /// public ServerService() { + } + + public void Initialize(string hostname, int port) + { + this._port = port; + this._hostname = hostname; _server = new Grpc.Core.Server { - Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } + Ports = { new ServerPort(_hostname, _port, ServerCredentials.Insecure) } }; - - Start(); } /// @@ -28,7 +33,15 @@ namespace Aurora.Backend.Services /// public void Start() { - _server.Start(); + try + { + Console.WriteLine(string.Format("Starting gRPC server at hostname: {0}, port: {1}", _hostname, _port)); + _server.Start(); + } + catch (Exception ex) + { + Console.WriteLine(string.Format("Error starting gRPC server: {0}", ex.Message)); + } } /// @@ -45,7 +58,7 @@ namespace Aurora.Backend.Services await Stop(); _server = new Grpc.Core.Server { - Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } + Ports = { new ServerPort("localhost", _port, ServerCredentials.Insecure) } }; } diff --git a/Aurora/Backend/Services/SettingsService.cs b/Aurora/Backend/Services/SettingsService.cs new file mode 100644 index 0000000..ba2e2bc --- /dev/null +++ b/Aurora/Backend/Services/SettingsService.cs @@ -0,0 +1,36 @@ +using System; +using Xamarin.Forms; + +namespace Aurora.Backend.Services +{ + public class SettingsService : BaseService + { + private string _usernameKey = "Username"; + + public SettingsService() + { + } + + public string Username + { + get + { + if (!Application.Current.Properties.ContainsKey(_usernameKey)) + { + return ""; + } + + Application.Current.Properties.TryGetValue(_usernameKey, out object val); + return val as string; + } + set + { + if (Application.Current.Properties.ContainsKey(_usernameKey)) + { + Application.Current.Properties.Remove(_usernameKey); + } + Application.Current.Properties.Add(_usernameKey, value); + } + } + } +} \ No newline at end of file diff --git a/Aurora/Frontend/Components/HostSelector/HostSelector.xaml.cs b/Aurora/Frontend/Components/HostSelector/HostSelector.xaml.cs index 463dfab..da725b2 100644 --- a/Aurora/Frontend/Components/HostSelector/HostSelector.xaml.cs +++ b/Aurora/Frontend/Components/HostSelector/HostSelector.xaml.cs @@ -110,7 +110,7 @@ namespace Aurora.Frontend.Components.HostSelector { string newVal = newValue as string; HostSelector instance = bindable as HostSelector; - if (instance.HostnameEntry.Text != newValue) + if (instance.HostnameEntry.Text != newVal) { instance.HostnameEntry.Text = newVal; } diff --git a/Aurora/Frontend/Views/MainView/MainViewModel.cs b/Aurora/Frontend/Views/MainView/MainViewModel.cs index c72f18f..04dafec 100644 --- a/Aurora/Frontend/Views/MainView/MainViewModel.cs +++ b/Aurora/Frontend/Views/MainView/MainViewModel.cs @@ -8,6 +8,7 @@ using Aurora.Frontend.Views.Artists; using Aurora.Frontend.Views.Songs; using Aurora.Frontend.Views.Stations; using Aurora.Frontend.Views.Party; +using Aurora.Frontend.Views.Profile; namespace Aurora.Frontend.Views.MainView { @@ -32,7 +33,7 @@ namespace Aurora.Frontend.Views.MainView _pages = new ObservableCollection(new[] { new NavigationItem { Id = 4, Title = "Party", Group="Social", TargetType = typeof(PartyView)}, - new NavigationItem { Id = 5, Title = "Profile", Group="Social", TargetType = typeof(ArtistsView)}, + new NavigationItem { Id = 5, Title = "Profile", Group="Social", TargetType = typeof(ProfileView)}, new NavigationItem { Id = 0, Title = "Songs", Group="Library", TargetType = typeof(SongsView) }, new NavigationItem { Id = 1, Title = "Artists", Group="Library", TargetType = typeof(ArtistsView)}, new NavigationItem { Id = 2, Title = "Albums", Group="Library", TargetType = typeof(AlbumsView)}, diff --git a/Aurora/Frontend/Views/Party/PartyViewModel.cs b/Aurora/Frontend/Views/Party/PartyViewModel.cs index 5d0d998..fe823dc 100644 --- a/Aurora/Frontend/Views/Party/PartyViewModel.cs +++ b/Aurora/Frontend/Views/Party/PartyViewModel.cs @@ -2,6 +2,7 @@ using System; using System.Collections.ObjectModel; using Aurora.Backend.Executors; using Aurora.Frontend.Components.HostSelector; +using Aurora.Backend.Services; using Xamarin.Forms; namespace Aurora.Frontend.Views.Party @@ -19,7 +20,7 @@ namespace Aurora.Frontend.Views.Party private PartyState _state; - private BasePartyExecutor _executor; + private BaseExecutor _executor; private string _hostname; @@ -91,7 +92,9 @@ namespace Aurora.Frontend.Views.Party #region Commands private void OnJoinExecute() { - _executor = BaseExecutor.CreateExecutor(ExecutorType.Client) as BasePartyExecutor; + _executor = BaseExecutor.CreateExecutor(); + Int32.TryParse(this.Port, out int intPort); + _executor.Initialize(); State(PartyState.Connecting); } @@ -102,7 +105,19 @@ namespace Aurora.Frontend.Views.Party private void OnHostExecute() { - _executor = BaseExecutor.CreateExecutor(ExecutorType.Server) as BasePartyExecutor; + Int32.TryParse(this.Port, out int intPort); + //Init gRPC server + ServerService.Instance.Initialize(this.Hostname, intPort); + + //Instantiate and initialize all executors + _executor = BaseExecutor.CreateExecutor(); + _executor.Initialize(); + + //start gRPC server + + ServerService.Instance.Start(); + + //Change state State(PartyState.Connecting); } diff --git a/Aurora/Frontend/Views/Profile/ProfileView.xaml b/Aurora/Frontend/Views/Profile/ProfileView.xaml new file mode 100644 index 0000000..7a1a6be --- /dev/null +++ b/Aurora/Frontend/Views/Profile/ProfileView.xaml @@ -0,0 +1,6 @@ + + + + + diff --git a/Aurora/Frontend/Views/Profile/ProfileView.xaml.cs b/Aurora/Frontend/Views/Profile/ProfileView.xaml.cs new file mode 100644 index 0000000..925c1fa --- /dev/null +++ b/Aurora/Frontend/Views/Profile/ProfileView.xaml.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using Xamarin.Forms; + +namespace Aurora.Frontend.Views.Profile +{ + public partial class ProfileView : ContentView + { + public ProfileView() + { + InitializeComponent(); + BindingContext = new ProfileViewModel(); + } + } +} diff --git a/Aurora/Frontend/Views/Profile/ProfileViewModel.cs b/Aurora/Frontend/Views/Profile/ProfileViewModel.cs new file mode 100644 index 0000000..3db90d9 --- /dev/null +++ b/Aurora/Frontend/Views/Profile/ProfileViewModel.cs @@ -0,0 +1,10 @@ +using System; +namespace Aurora.Frontend.Views.Profile +{ + public class ProfileViewModel + { + public ProfileViewModel() + { + } + } +}