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

@ -505,6 +505,7 @@
<Project>{17F51282-39BE-4FE3-8EC7-6D108F5DD0FD}</Project>
<Name>Aurora</Name>
</ProjectReference>
<ProjectReference Include="..\..\aurora-shared\aurora-shared.csproj" />
</ItemGroup>
<ItemGroup>
<Content Include="Resources\play.png">

View File

@ -21,6 +21,7 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Aurora\Aurora.csproj" />
<ProjectReference Include="..\..\aurora-shared\aurora-shared.csproj" />
</ItemGroup>
<ItemGroup>
<None Update="Resources\Jidenna\The Chief\04 Bambi.mp3">

View File

@ -7,8 +7,7 @@ using Aurora.Design.Views.Profile;
using Aurora.Design.Views.Songs;
using Aurora.Design.Views.Stations;
using Aurora.Services.EventManager;
using Aurora.Services.Server;
using Aurora.Services.Client;
using Aurora.Services;
using Autofac;
using LibVLCSharp.Shared;
using Xamarin.Forms;
@ -28,13 +27,12 @@ namespace Aurora
//Register DI
ContainerBuilder _builder = new ContainerBuilder();
// _builder.RegisterInstance<IPlayer>(new PlayerService()).SingleInstance();
_builder.RegisterInstance<IPlayer>(new PlayerService()).SingleInstance();
_builder.RegisterType<Global>().As<Global>().SingleInstance();
_builder.RegisterType<PlayerService>().As<IPlayer>().SingleInstance();
_builder.RegisterType<SettingsService>().As<ISettingsService>().SingleInstance();
_builder.RegisterType<LibraryService>().As<ILibraryService>().SingleInstance();
_builder.RegisterType<EventManager>().As<IEventManager>().SingleInstance();
_builder.RegisterType<ServerService>().As<IServerService>().SingleInstance();
_builder.RegisterType<ClientService>().As<IClientService>().SingleInstance();
_builder.RegisterType<MainView>().SingleInstance();
_builder.RegisterType<AlbumsViewModel>();
_builder.RegisterType<ArtistsViewModel>();

View File

@ -83,4 +83,7 @@
<None Remove="Resources\like.png" />
<None Remove="Resources\play.png" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\aurora-shared\aurora-shared.csproj" />
</ItemGroup>
</Project>

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

View File

@ -315,6 +315,7 @@ namespace Aurora.Design.Views.Main
}
var view = (View)Activator.CreateInstance(viewType);
vm.SetView = this.SetView;
//Assign viewModel
_lastViewModel = vm;

View File

@ -2,36 +2,32 @@ using System.Collections.ObjectModel;
using Aurora.Proto.Party;
using Aurora.Models.Media;
using Aurora.Services.Settings;
using Aurora.Services;
namespace Aurora.Design.Views.Party
{
public enum PartyStateV2
{
SelectingHost,
InParty,
Hosting,
Connecting,
}
public class BasePartyViewModel : BasePlayerViewModel
{
protected string _hostname;
private PartyStateV2 _state;
protected Global _global;
protected ISettingsService _settingsService;
private ObservableCollection<Member> _members;
private ObservableCollection<BaseMedia> _queue;
private BaseMedia _selectedMedia;
private ISettingsService _settingsService;
private int _selectedTabIndex;
public BasePartyViewModel(
ISettingsService settingsService
ISettingsService settingsService,
Global global
)
{
_members = new ObservableCollection<Member>();
_queue = new ObservableCollection<BaseMedia>();
this._settingsService = settingsService;
this._global = global;
}
public int SelectedTabIndex
@ -60,16 +56,10 @@ namespace Aurora.Design.Views.Party
/// Public property for the currently selected song.
/// </summary>
/// <value></value>
public BaseMedia SelectedSong
public BaseMedia SelectedMedia
{
get { return _selectedMedia; }
set { SetProperty(ref _selectedMedia, value); }
}
public PartyStateV2 State
{
get { return _state; }
set { _state = value;}
}
}
}

View File

@ -1,24 +1,59 @@
using System;
using System.Threading.Tasks;
using Aurora.Design.Views.Party.NewPartyDialog;
using System.Net;
using System.Net.Sockets;
using Grpc.Core;
using Xamarin.Forms;
using Aurora.Services.Server;
using Aurora.Services.EventManager;
using Aurora.Services.Library;
using Aurora.Services.Settings;
using Aurora.Services;
using Aurora.Proto.Party;
using Aurora.Models.Media;
using Aurora.Services.Controllers;
using Aurora.Utils;
namespace Aurora.Design.Views.Party
{
public class HostPartyViewModel : BasePartyViewModel
{
private IServerService _serverService;
private IEventManager _eventManager;
private ILibraryService _libraryService;
private Grpc.Core.Server _server;
private RemotePartyService.RemotePartyServiceClient _remotePartyClient;
private Channel _channel;
private int _port = 8080;
private bool _isServerStarted = false;
public HostPartyViewModel(
ISettingsService settingsService,
IServerService serverService,
IEventManager eventManager
): base(settingsService)
IEventManager eventManager,
ILibraryService libraryService,
Global global
): base(settingsService, global)
{
this._serverService = serverService;
this._eventManager = eventManager;
this._global.State = PartyState.Hosting;
this._libraryService = libraryService;
// Create and start grpc server
string host = IpUtil.GetLocalIPAddress();
if(string.IsNullOrWhiteSpace(host))
{
// TODO display error to screen
throw new System.Exception("This device does not have a valid IP address");
}
this._hostname = IpUtil.GetLocalIPAddress();
this.StartServer("test", "test description");
this.StartClient();
// Register commands
PlayCommand = new Command(OnDoubleClickCommandExecute, CanDoubleClickCommandExecute);
LeavePartyCommand = new Command(OnLeavePartyCommandExecute, CanLeavePartyCommandExecute);
}
#region Properties
@ -43,7 +78,14 @@ namespace Aurora.Design.Views.Party
/// <returns></returns>
public override Task OnActive()
{
// Start server if not already started
if(!this._isServerStarted)
{
this._hostname = IpUtil.GetLocalIPAddress();
this.StartServer("test", "test description");
this.StartClient();
}
return Task.FromResult<object>(null);
}
@ -55,20 +97,13 @@ namespace Aurora.Design.Views.Party
{
return Task.FromResult<object>(null);
}
private async void OnHostCommandExecute()
{
}
private bool CanHostCommandExecute()
{
return true;
}
private async void OnLeavePartyCommandExecute()
{
await this._channel.ShutdownAsync();
await this._server.ShutdownAsync();
this._global.State = PartyState.Idle;
this.SetView(typeof(PartyView), typeof(PartyViewModel));
}
private bool CanLeavePartyCommandExecute()
@ -78,22 +113,43 @@ namespace Aurora.Design.Views.Party
public override void OnPlayButtonCommandExecute()
{
if (base.IsPlaying())
{
//Fire play stopped event
AudioMetadata meta = SelectedMedia.Metadata as AudioMetadata;
MediaPausedEvent mediaPaused = new MediaPausedEvent();
_eventManager.FireEvent(new BaseEvent()
{
MediaPausedEvent = mediaPaused
});
}
else
{
//Fire play resume event
AudioMetadata meta = SelectedMedia.Metadata as AudioMetadata;
MediaResumedEvent mediaResumed = new MediaResumedEvent();
_eventManager.FireEvent(new BaseEvent()
{
MediaResumedEvent = mediaResumed
});
}
}
public override bool CanPlayButtonCommandExecute()
{
return this.State == PartyStateV2.Hosting;
return true;
}
public override bool CanNextButtonCommandExecute()
{
return this.State == PartyStateV2.Hosting;
return true;
}
public override bool CanPreviousButtonCommandExecute()
{
return this.State == PartyStateV2.Hosting;
return true;
}
/// <summary>
@ -101,12 +157,77 @@ namespace Aurora.Design.Views.Party
/// </summary>
public void OnDoubleClickCommandExecute()
{
//Fire Playing event
AudioMetadata meta = SelectedMedia.Metadata as AudioMetadata;
NewMediaPlayingEvent mediaPlaying = new NewMediaPlayingEvent()
{
Media = new Media()
{
//TODO need full resource name
Name = SelectedMedia.Id,
Title = meta.Title,
Artist = meta.Artist,
Album = meta.Album,
}
};
_eventManager.FireEvent(new BaseEvent()
{
NewMediaPlayingEvent = mediaPlaying
});
}
public bool CanDoubleClickCommandExecute()
{
return this.State == PartyStateV2.Hosting;
return true;
}
#region Private Methods
private void StartServer(string partyName, string description)
{
try
{
// TODO bmw replace with display in UI
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 remotePartyController = new RemotePartyController(
partyName,
description,
this._libraryService,
base._settingsService,
_eventManager);
// Register grpc RemoteService with singleton server service
RemotePartyService.BindService(remotePartyController);
_server.Start();
this._isServerStarted = true;
}
catch (Exception ex)
{
// TODO bmw replace with display in UI
Console.WriteLine(string.Format("Error starting gRPC server: {0}", ex.Message));
}
}
private void StartClient()
{
_channel = new Channel(string.Format("{0}:{1}", this._hostname, this._port), ChannelCredentials.Insecure);
_remotePartyClient = new RemotePartyService.RemotePartyServiceClient(_channel);
}
#endregion Private Methods
}
}

View File

@ -1,34 +1,16 @@
using System;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Threading;
using System.Linq;
using Xamarin.Forms;
using Aurora.Proto.Party;
using Aurora.Models.Media;
using Aurora.Services.Client;
using Aurora.Design.Views.Party.NewPartyDialog;
using Aurora.Services.Settings;
using Aurora.Services.Server;
using Aurora.Services.EventManager;
using Grpc.Core;
using Aurora.Services;
namespace Aurora.Design.Views.Party
{
//TODO refactor
enum PartyState
{
SelectingHost,
InParty,
Hosting,
Connecting,
}
delegate void EventHandler(BaseEvent e);
public class PartyViewModel : BasePartyViewModel
{
public PartyViewModel(ISettingsService settingsService): base(settingsService)
public PartyViewModel(ISettingsService settingsService, Global global): base(settingsService, global)
{
}
@ -39,14 +21,14 @@ namespace Aurora.Design.Views.Party
public override Task OnActive()
{
OnPropertyChanged("SelectedTabIndex");
switch(this.State)
switch(this._global.State)
{
case PartyStateV2.Hosting:
case PartyState.Hosting:
{
this.SetView.Invoke(typeof(PartyView), typeof(HostPartyViewModel));
break;
}
case PartyStateV2.InParty:
case PartyState.InParty:
{
// TODO
break;

View File

@ -0,0 +1,22 @@
using System.Net;
using System.Net.Sockets;
namespace Aurora.Utils
{
public static class IpUtil
{
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;
}
}
}