Adding a shared library for common classes

This commit is contained in:
Brandon Watson
2021-04-12 20:58:44 -04:00
parent 6b14d1264a
commit 4acf091511
28 changed files with 587 additions and 325 deletions

View File

@ -1,46 +0,0 @@
using Grpc.Core;
using Aurora.Proto.Party;
using Aurora.Services.Settings;
namespace Aurora.Services.Client
{
public class ClientService : IClientService
{
private RemotePartyService.RemotePartyServiceClient _remotePartyClient;
private Channel _channel;
private ISettingsService _settingsService;
public ClientService(ISettingsService settingsService)
{
this._settingsService = settingsService;
}
public bool IsStarted
{
get
{
return _remotePartyClient != null;
}
}
public RemotePartyService.RemotePartyServiceClient RemotePartyServiceClient
{
get { return this._remotePartyClient; }
}
public void Start(string hostname, string port)
{
_channel = new Channel(string.Format("{0}:{1}", hostname, port), ChannelCredentials.Insecure);
_remotePartyClient = new RemotePartyService.RemotePartyServiceClient(_channel);
}
public async void Close()
{
await _channel.ShutdownAsync();
_remotePartyClient = null;
}
}
}

View File

@ -1,15 +0,0 @@
using Aurora.Proto.Party;
namespace Aurora.Services.Client
{
public interface IClientService
{
bool IsStarted { get; }
RemotePartyService.RemotePartyServiceClient RemotePartyServiceClient { get; }
void Start(string hostname, string port);
void Close();
}
}

View File

@ -6,9 +6,9 @@ using Aurora.Services.Library;
using Aurora.Services.Settings;
using Aurora.Models.Media;
using Aurora.Services.EventManager;
using Autofac;
using Aurora.Utils;
namespace Aurora.Services.Server.Controllers
namespace Aurora.Services.Controllers
{
public partial class RemotePartyController : RemotePartyService.RemotePartyServiceBase
{
@ -36,9 +36,9 @@ namespace Aurora.Services.Server.Controllers
this._hostMember = new Member()
{
Name = GetNewMemberResourceName(_partyResourceName, ServerService.GetLocalIPAddress(), userName),
Name = GetNewMemberResourceName(_partyResourceName, IpUtil.GetLocalIPAddress(), userName),
UserName = userName,
IpAddress = ServerService.GetLocalIPAddress(),
IpAddress = IpUtil.GetLocalIPAddress(),
};
//Add media from library

View File

@ -4,7 +4,7 @@ using System.Threading;
using Aurora.Proto.Party;
using Aurora.Utils;
namespace Aurora.Services.Server.Controllers
namespace Aurora.Services.Controllers
{
public partial class RemotePartyController : RemotePartyService.RemotePartyServiceBase
{

View File

@ -5,7 +5,7 @@ using Aurora.Proto.Party;
using Aurora.Proto.General;
using Aurora.Utils;
namespace Aurora.Services.Server.Controllers
namespace Aurora.Services.Controllers
{
public partial class RemotePartyController : RemotePartyService.RemotePartyServiceBase
{

View File

@ -8,7 +8,7 @@ using Aurora.Services.Library;
using Aurora.Services.Player;
using Autofac;
namespace Aurora.Services.Server.Controllers
namespace Aurora.Services.Controllers
{
public partial class RemotePartyController : RemotePartyService.RemotePartyServiceBase
{

View File

@ -7,7 +7,7 @@ using Aurora.Utils;
using Grpc.Core;
using Google.Protobuf.WellKnownTypes;
namespace Aurora.Services.Server.Controllers
namespace Aurora.Services.Controllers
{
public partial class RemotePartyController : RemotePartyService.RemotePartyServiceBase
{

View File

@ -1,11 +1,11 @@
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using Aurora.Utils;
using Aurora.Proto.Party;
using Google.Protobuf.WellKnownTypes;
namespace Aurora.Services.Server.Controllers
namespace Aurora.Services.Controllers
{
public partial class RemotePartyController : RemotePartyService.RemotePartyServiceBase
{
@ -22,7 +22,7 @@ namespace Aurora.Services.Server.Controllers
Name = _partyResourceName,
DisplayName = this._displayName,
Description = this._description,
HostIp = ServerService.GetLocalIPAddress(),
HostIp = IpUtil.GetLocalIPAddress(),
HostMember = this._hostMember,
CreatedOn = Timestamp.FromDateTime(_startDateTime)
};

View File

@ -0,0 +1,19 @@
namespace Aurora.Services
{
public enum PartyState
{
Idle,
SelectingHost,
InParty,
Hosting,
Connecting,
}
public class Global
{
public Global()
{
this.State = PartyState.Idle;
}
public PartyState State {get; set;}
}
}

View File

@ -1,27 +0,0 @@
using System.Threading.Tasks;
namespace Aurora.Services.Server
{
public interface IServerService
{
int Port { get; }
string Hostname { get; }
bool Initialized { get; }
/// <summary>
/// Start Server
/// </summary>
void Start(string partyName, string description);
/// <summary>
/// Shutdown server async.
/// </summary>
/// <returns>Task</returns>
Task Stop();
Task Reset();
}
}

View File

@ -1,144 +0,0 @@
using System;
using System.Threading.Tasks;
using System.Net;
using System.Net.Sockets;
using Grpc.Core;
using Aurora.Services.Server.Controllers;
using Aurora.Services.Settings;
using Aurora.Services.Library;
using Aurora.Services.EventManager;
using Aurora.Proto.Party;
namespace Aurora.Services.Server
{
public class ServerService : IServerService
{
private int _port = 8080;
private string _hostname;
private Grpc.Core.Server _server;
private ILibraryService _libraryService;
private ISettingsService _settingsService;
private IEventManager _eventManager;
//Implementation class declarations
private RemotePartyController _remotePartyController;
/// <summary>
/// Constructor. Registers GRPC service implementations.
/// </summary>
public ServerService(ILibraryService libraryService, ISettingsService settingsService, IEventManager eventManager)
{
string host = GetLocalIPAddress();
this._libraryService = libraryService;
this._settingsService = settingsService;
this._eventManager = eventManager;
if (string.IsNullOrWhiteSpace(host))
{
throw new Exception("This device must have a valid IP address");
}
_hostname = host;
}
public int Port
{
get { return _port; }
}
public string Hostname
{
get { return _hostname; }
}
public bool Initialized
{
get
{
return (_remotePartyController != null &&
_server != null);
}
}
/// <summary>
/// Start Server
/// </summary>
public void Start(string partyName, string description)
{
try
{
Console.WriteLine(string.Format("Starting gRPC server at hostname: {0}, port: {1}", _hostname, _port));
_server = new Grpc.Core.Server
{
Ports = { new ServerPort(_hostname, _port, ServerCredentials.Insecure) }
};
//Construct implementations
_remotePartyController = new RemotePartyController(
partyName,
description,
_libraryService,
_settingsService,
_eventManager);
// Register grpc RemoteService with singleton server service
RegisterService(RemotePartyService.BindService(_remotePartyController));
_server.Start();
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Error starting gRPC server: {0}", ex.Message));
}
}
/// <summary>
/// Shutdown server async.
/// </summary>
/// <returns>Task</returns>
public async Task Stop()
{
try
{
await _server.ShutdownAsync();
await _server.ShutdownTask;
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Error stopping gRPC server: {0}", ex.Message));
}
}
public async Task Reset()
{
await Stop();
_server = new Grpc.Core.Server
{
Ports = { new ServerPort("localhost", _port, ServerCredentials.Insecure) }
};
}
private void RegisterService(ServerServiceDefinition definition)
{
_server.Services.Add(definition);
}
public static string GetLocalIPAddress()
{
string returnIp = "";
var host = Dns.GetHostEntry(Dns.GetHostName());
foreach (var ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
returnIp = ip.ToString();
}
}
return returnIp;
}
}
}