Beginning stages for party executors

This commit is contained in:
watsonb8 2019-06-03 10:57:05 -04:00
parent 6503d2c410
commit b0307cf7b3
10 changed files with 370 additions and 39 deletions

View 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();
}
}
}

View 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
}
}

View 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();
}
}

View 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; }
}
}

View 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();
}
}
}

View 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);
}
}
}

View File

@ -1,7 +1,7 @@
using System;
using Aurora.Backend.Proto;
namespace Aurora.Backend.Services.Server
namespace Aurora.Backend.Server
{
class PlaybackServiceImpl : PlaybackService.PlaybackServiceBase
{

View File

@ -1,10 +0,0 @@
using System;
using Aurora.Backend.Proto;
namespace Aurora.Backend.Services.Server
{
class PartyServiceImpl : PartyService.PartyServiceBase
{
}
}

View File

@ -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();
}
}
}

View 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);
}
}
}