From b0307cf7b3dac4f619873a0c9fae82012f138921 Mon Sep 17 00:00:00 2001 From: watsonb8 Date: Mon, 3 Jun 2019 10:57:05 -0400 Subject: [PATCH] Beginning stages for party executors --- .../Client/Party/ClientPartyExecutor.cs | 73 ++++++++++++++++++ Aurora/Backend/Executors/BaseExecutor.cs | 38 +++++++++ Aurora/Backend/Executors/BasePartyExecutor.cs | 58 ++++++++++++++ Aurora/Backend/Models/PartyMember.cs | 16 ++++ .../Backend/Server/Party/HostPartyExecutor.cs | 77 +++++++++++++++++++ .../Backend/Server/Party/PartyServiceImpl.cs | 50 ++++++++++++ .../Playback}/PlaybackServiceImpl.cs | 2 +- .../Services/Server/PartyServiceImpl.cs | 10 --- .../Backend/Services/Server/ServerService.cs | 28 ------- Aurora/Backend/Services/ServerService.cs | 57 ++++++++++++++ 10 files changed, 370 insertions(+), 39 deletions(-) create mode 100644 Aurora/Backend/Client/Party/ClientPartyExecutor.cs create mode 100644 Aurora/Backend/Executors/BaseExecutor.cs create mode 100644 Aurora/Backend/Executors/BasePartyExecutor.cs create mode 100644 Aurora/Backend/Models/PartyMember.cs create mode 100644 Aurora/Backend/Server/Party/HostPartyExecutor.cs create mode 100644 Aurora/Backend/Server/Party/PartyServiceImpl.cs rename Aurora/Backend/{Services/Server => Server/Playback}/PlaybackServiceImpl.cs (75%) delete mode 100644 Aurora/Backend/Services/Server/PartyServiceImpl.cs delete mode 100644 Aurora/Backend/Services/Server/ServerService.cs create mode 100644 Aurora/Backend/Services/ServerService.cs diff --git a/Aurora/Backend/Client/Party/ClientPartyExecutor.cs b/Aurora/Backend/Client/Party/ClientPartyExecutor.cs new file mode 100644 index 0000000..f7c2800 --- /dev/null +++ b/Aurora/Backend/Client/Party/ClientPartyExecutor.cs @@ -0,0 +1,73 @@ +using System; +using Aurora.Backend.Executors; + +namespace Aurora.Backend.Client.Party +{ + public class ClientPartyExecutor : BasePartyExecutor + { + public ClientPartyExecutor() + { + + } + + public override void AddToQueue() + { + throw new NotImplementedException(); + } + + public override void Close() + { + throw new NotImplementedException(); + } + + public override void GetMembers() + { + throw new NotImplementedException(); + } + + public override void GetQueue() + { + throw new NotImplementedException(); + } + + public override void Initialize() + { + throw new NotImplementedException(); + } + + public override void Next() + { + throw new NotImplementedException(); + } + + public override void Pause() + { + throw new NotImplementedException(); + } + + public override void Play() + { + throw new NotImplementedException(); + } + + public override void Previous() + { + throw new NotImplementedException(); + } + + public override void RemoveFromQueue() + { + throw new NotImplementedException(); + } + + public override void Run() + { + throw new NotImplementedException(); + } + + public override void Stop() + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/Aurora/Backend/Executors/BaseExecutor.cs b/Aurora/Backend/Executors/BaseExecutor.cs new file mode 100644 index 0000000..dd09168 --- /dev/null +++ b/Aurora/Backend/Executors/BaseExecutor.cs @@ -0,0 +1,38 @@ +using System; +using System.Reflection; +using System.Linq; + +namespace Aurora.Backend.Executors +{ + public abstract class BaseExecutor + { + public BaseExecutor() + { + } + + public static BaseExecutor CreateExecutor(ExecutorType executorType) where T : BaseExecutor + { + MethodInfo method = typeof(T).GetMethod("Create"); + if (method == null) + { + throw new InvalidOperationException("Executor must include a 'create' method."); + } + + return method.Invoke(null, new object[] { executorType }) as BaseExecutor; + + // var types = typeof(T).Assembly.GetTypes(); + + // foreach (Type type in types) + // { + // MethodInfo genericMethod = method.MakeGenericMethod(type); + // genericMethod.Invoke(null, null); // No target, no arguments + // } + } + } + + public enum ExecutorType + { + Server, + Client + } +} \ No newline at end of file diff --git a/Aurora/Backend/Executors/BasePartyExecutor.cs b/Aurora/Backend/Executors/BasePartyExecutor.cs new file mode 100644 index 0000000..0889194 --- /dev/null +++ b/Aurora/Backend/Executors/BasePartyExecutor.cs @@ -0,0 +1,58 @@ +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(); + break; + } + case ExecutorType.Server: + { + executor = new HostPartyExecutor(); + 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(); + + public abstract void Play(); + + public abstract void Pause(); + + public abstract void Stop(); + + public abstract void Next(); + + public abstract void Previous(); + } +} \ No newline at end of file diff --git a/Aurora/Backend/Models/PartyMember.cs b/Aurora/Backend/Models/PartyMember.cs new file mode 100644 index 0000000..7769b5d --- /dev/null +++ b/Aurora/Backend/Models/PartyMember.cs @@ -0,0 +1,16 @@ +using System; + +namespace Aurora.Backend.Models +{ + public class PartyMember + { + public PartyMember() + { + } + + public string Username { get; set; } + public string Id { get; set; } + public string IpAddress { get; set; } + public string Port { get; set; } + } +} \ No newline at end of file diff --git a/Aurora/Backend/Server/Party/HostPartyExecutor.cs b/Aurora/Backend/Server/Party/HostPartyExecutor.cs new file mode 100644 index 0000000..5fd6397 --- /dev/null +++ b/Aurora/Backend/Server/Party/HostPartyExecutor.cs @@ -0,0 +1,77 @@ +using System; +using System.Threading.Tasks; +using Aurora.Backend.Executors; +using Aurora.Backend.Services; +using Aurora.Backend.Proto; + +namespace Aurora.Backend.Server.Party +{ + public class HostPartyExecutor : BasePartyExecutor + { + PartyServiceImpl _partyServiceImpl; + public HostPartyExecutor() + { + _partyServiceImpl = new PartyServiceImpl(); + } + + public override void Initialize() + { + ServerService.Instance.RegisterService(PartyService.BindService(_partyServiceImpl)); + } + + public override async void Close() + { + await ServerService.Instance.Stop(); + } + + public override void AddToQueue() + { + throw new NotImplementedException(); + } + + public override void GetMembers() + { + throw new NotImplementedException(); + } + + public override void GetQueue() + { + throw new NotImplementedException(); + } + + public override void Next() + { + throw new NotImplementedException(); + } + + public override void Pause() + { + throw new NotImplementedException(); + } + + public override void Play() + { + throw new NotImplementedException(); + } + + public override void Previous() + { + throw new NotImplementedException(); + } + + public override void RemoveFromQueue() + { + throw new NotImplementedException(); + } + + public override void Run() + { + throw new NotImplementedException(); + } + + public override void Stop() + { + throw new NotImplementedException(); + } + } +} \ No newline at end of file diff --git a/Aurora/Backend/Server/Party/PartyServiceImpl.cs b/Aurora/Backend/Server/Party/PartyServiceImpl.cs new file mode 100644 index 0000000..e118751 --- /dev/null +++ b/Aurora/Backend/Server/Party/PartyServiceImpl.cs @@ -0,0 +1,50 @@ +using System; +using System.Threading.Tasks; +using System.Collections.Generic; +using Aurora.Backend.Proto; +using Aurora.Backend.Models; + +namespace Aurora.Backend.Server.Party +{ + class PartyServiceImpl : PartyService.PartyServiceBase + { + /// + /// Dictionary of party members. Key -> ClientId + /// + private Dictionary _partyMembers; + + public PartyServiceImpl() + { + _partyMembers = new Dictionary(); + } + + public Dictionary PartyMembers + { + get + { + return _partyMembers; + } + } + + public override Task JoinParty(JoinPartyRequest request, Grpc.Core.ServerCallContext context) + { + _partyMembers.Add(request.ClientId, new PartyMember() + { + Username = request.UserName, + Id = request.ClientId, + IpAddress = request.IpAddress, + Port = request.Port, + }); + + JoinPartyResponse response = new JoinPartyResponse() { Status = PartyJoinedStatusEnum.Connected }; + return Task.FromResult(response); + } + + public override Task LeaveParty(LeavePartyRequest request, Grpc.Core.ServerCallContext context) + { + _partyMembers.Remove(request.ClientId); + LeavePartyResponse response = new LeavePartyResponse() { Status = PartyJoinedStatusEnum.Disconnected }; + return Task.FromResult(response); + } + } +} \ No newline at end of file diff --git a/Aurora/Backend/Services/Server/PlaybackServiceImpl.cs b/Aurora/Backend/Server/Playback/PlaybackServiceImpl.cs similarity index 75% rename from Aurora/Backend/Services/Server/PlaybackServiceImpl.cs rename to Aurora/Backend/Server/Playback/PlaybackServiceImpl.cs index 33ba9f1..443c8f1 100644 --- a/Aurora/Backend/Services/Server/PlaybackServiceImpl.cs +++ b/Aurora/Backend/Server/Playback/PlaybackServiceImpl.cs @@ -1,7 +1,7 @@ using System; using Aurora.Backend.Proto; -namespace Aurora.Backend.Services.Server +namespace Aurora.Backend.Server { class PlaybackServiceImpl : PlaybackService.PlaybackServiceBase { diff --git a/Aurora/Backend/Services/Server/PartyServiceImpl.cs b/Aurora/Backend/Services/Server/PartyServiceImpl.cs deleted file mode 100644 index 9367ad8..0000000 --- a/Aurora/Backend/Services/Server/PartyServiceImpl.cs +++ /dev/null @@ -1,10 +0,0 @@ -using System; -using Aurora.Backend.Proto; - -namespace Aurora.Backend.Services.Server -{ - class PartyServiceImpl : PartyService.PartyServiceBase - { - - } -} \ No newline at end of file diff --git a/Aurora/Backend/Services/Server/ServerService.cs b/Aurora/Backend/Services/Server/ServerService.cs deleted file mode 100644 index 4863621..0000000 --- a/Aurora/Backend/Services/Server/ServerService.cs +++ /dev/null @@ -1,28 +0,0 @@ -using System; -using Grpc.Core; -using Aurora.Backend.Proto; - -namespace Aurora.Backend.Services.Server -{ - public class ServerService : BaseService - { - const int Port = 50051; - public ServerService() - { - Grpc.Core.Server server = new Grpc.Core.Server - { - Services = { - PartyService.BindService(new PartyServiceImpl()), - PlaybackService.BindService(new PlaybackServiceImpl()) }, - Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } - }; - server.Start(); - - Console.WriteLine("Aurora server listening on port " + Port); - Console.WriteLine("Press any key to stop the server..."); - Console.ReadKey(); - - server.ShutdownAsync().Wait(); - } - } -} \ No newline at end of file diff --git a/Aurora/Backend/Services/ServerService.cs b/Aurora/Backend/Services/ServerService.cs new file mode 100644 index 0000000..8c88c1c --- /dev/null +++ b/Aurora/Backend/Services/ServerService.cs @@ -0,0 +1,57 @@ +using System; +using System.Threading.Tasks; +using Grpc.Core; +using Aurora.Backend.Proto; + +namespace Aurora.Backend.Services +{ + public class ServerService : BaseService + { + private const int Port = 50051; + private Grpc.Core.Server _server; + + /// + /// Constructor. Registers GRPC service implementations. + /// + public ServerService() + { + _server = new Grpc.Core.Server + { + Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } + }; + + Start(); + } + + /// + /// Start Server + /// + public void Start() + { + _server.Start(); + } + + /// + /// 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) } + }; + } + + public void RegisterService(ServerServiceDefinition definition) + { + _server.Services.Add(definition); + } + } +} \ No newline at end of file