Refactored to only having one executor per platform

This commit is contained in:
watsonb8
2019-07-05 11:37:44 -04:00
parent 0cb0546741
commit a01d399a1f
18 changed files with 226 additions and 170 deletions

View File

@ -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<T>(ExecutorType executorType) where T : BaseExecutor
public static BaseExecutor CreateExecutor<T>()
{
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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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
{
/// <summary>
/// Dictionary of party members. Key -> ClientId
/// </summary>
private Dictionary<string, PartyMember> _partyMembers;
public PartyServiceImpl()
public RemotePartyServiceImpl()
{
_partyMembers = new Dictionary<string, PartyMember>();
}

View File

@ -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<Chunk> 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");
// };
}
}
}

View File

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

View File

@ -7,7 +7,8 @@ namespace Aurora.Backend.Services
{
public class ServerService : BaseService<ServerService>
{
private const int Port = 50051;
private string _hostname = "127.0.0.1";
private int _port = 50001;
private Grpc.Core.Server _server;
/// <summary>
@ -15,12 +16,16 @@ namespace Aurora.Backend.Services
/// </summary>
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();
}
/// <summary>
@ -28,7 +33,15 @@ namespace Aurora.Backend.Services
/// </summary>
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));
}
}
/// <summary>
@ -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) }
};
}

View File

@ -0,0 +1,36 @@
using System;
using Xamarin.Forms;
namespace Aurora.Backend.Services
{
public class SettingsService : BaseService<SettingsService>
{
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);
}
}
}
}