Beginning stages for party executors
This commit is contained in:
		
							
								
								
									
										73
									
								
								Aurora/Backend/Client/Party/ClientPartyExecutor.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										73
									
								
								Aurora/Backend/Client/Party/ClientPartyExecutor.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -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(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										38
									
								
								Aurora/Backend/Executors/BaseExecutor.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								Aurora/Backend/Executors/BaseExecutor.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -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<T>(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 | ||||
|     } | ||||
| } | ||||
							
								
								
									
										58
									
								
								Aurora/Backend/Executors/BasePartyExecutor.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								Aurora/Backend/Executors/BasePartyExecutor.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -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(); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										16
									
								
								Aurora/Backend/Models/PartyMember.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										16
									
								
								Aurora/Backend/Models/PartyMember.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -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; } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										77
									
								
								Aurora/Backend/Server/Party/HostPartyExecutor.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										77
									
								
								Aurora/Backend/Server/Party/HostPartyExecutor.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -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(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										50
									
								
								Aurora/Backend/Server/Party/PartyServiceImpl.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										50
									
								
								Aurora/Backend/Server/Party/PartyServiceImpl.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -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 | ||||
|     { | ||||
|         /// <summary> | ||||
|         /// Dictionary of party members. Key -> ClientId | ||||
|         /// </summary> | ||||
|         private Dictionary<string, PartyMember> _partyMembers; | ||||
|  | ||||
|         public PartyServiceImpl() | ||||
|         { | ||||
|             _partyMembers = new Dictionary<string, PartyMember>(); | ||||
|         } | ||||
|  | ||||
|         public Dictionary<string, PartyMember> PartyMembers | ||||
|         { | ||||
|             get | ||||
|             { | ||||
|                 return _partyMembers; | ||||
|             } | ||||
|         } | ||||
|  | ||||
|         public override Task<JoinPartyResponse> 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<LeavePartyResponse> LeaveParty(LeavePartyRequest request, Grpc.Core.ServerCallContext context) | ||||
|         { | ||||
|             _partyMembers.Remove(request.ClientId); | ||||
|             LeavePartyResponse response = new LeavePartyResponse() { Status = PartyJoinedStatusEnum.Disconnected }; | ||||
|             return Task.FromResult(response); | ||||
|         } | ||||
|     } | ||||
| } | ||||
| @@ -1,7 +1,7 @@ | ||||
| using System; | ||||
| using Aurora.Backend.Proto; | ||||
| 
 | ||||
| namespace Aurora.Backend.Services.Server | ||||
| namespace Aurora.Backend.Server | ||||
| { | ||||
|     class PlaybackServiceImpl : PlaybackService.PlaybackServiceBase | ||||
|     { | ||||
| @@ -1,10 +0,0 @@ | ||||
| using System; | ||||
| using Aurora.Backend.Proto; | ||||
|  | ||||
| namespace Aurora.Backend.Services.Server | ||||
| { | ||||
|     class PartyServiceImpl : PartyService.PartyServiceBase | ||||
|     { | ||||
|  | ||||
|     } | ||||
| } | ||||
| @@ -1,28 +0,0 @@ | ||||
| using System; | ||||
| using Grpc.Core; | ||||
| using Aurora.Backend.Proto; | ||||
|  | ||||
| namespace Aurora.Backend.Services.Server | ||||
| { | ||||
|     public class ServerService : BaseService<ServerService> | ||||
|     { | ||||
|         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(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										57
									
								
								Aurora/Backend/Services/ServerService.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								Aurora/Backend/Services/ServerService.cs
									
									
									
									
									
										Normal file
									
								
							| @@ -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<ServerService> | ||||
|     { | ||||
|         private const int Port = 50051; | ||||
|         private Grpc.Core.Server _server; | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Constructor. Registers GRPC service implementations. | ||||
|         /// </summary> | ||||
|         public ServerService() | ||||
|         { | ||||
|             _server = new Grpc.Core.Server | ||||
|             { | ||||
|                 Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } | ||||
|             }; | ||||
|  | ||||
|             Start(); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Start Server | ||||
|         /// </summary> | ||||
|         public void Start() | ||||
|         { | ||||
|             _server.Start(); | ||||
|         } | ||||
|  | ||||
|         /// <summary> | ||||
|         /// Shutdown server async. | ||||
|         /// </summary> | ||||
|         /// <returns>Task</returns> | ||||
|         public async Task Stop() | ||||
|         { | ||||
|             await _server.ShutdownAsync(); | ||||
|         } | ||||
|  | ||||
|         public async Task Reset() | ||||
|         { | ||||
|             await Stop(); | ||||
|             _server = new Grpc.Core.Server | ||||
|             { | ||||
|                 Ports = { new ServerPort("localhost", Port, ServerCredentials.Insecure) } | ||||
|             }; | ||||
|         } | ||||
|  | ||||
|         public void RegisterService(ServerServiceDefinition definition) | ||||
|         { | ||||
|             _server.Services.Add(definition); | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user