Can now somewhat reliably join a party

This commit is contained in:
watsonb8 2019-07-06 15:52:28 -04:00
parent 0d64c0732e
commit 2a3290defc
15 changed files with 140 additions and 125 deletions

View File

@ -20,11 +20,6 @@
VerticalOptions="Center"/>
<Entry
x:Name="HostnameEntry"/>
<Label
Text="Port"
VerticalOptions="Center"/>
<Entry
x:Name="PortEntry"/>
<Button
HorizontalOptions="Center"
x:Name="buttonHost"

View File

@ -31,10 +31,6 @@ namespace Aurora.Design.Components.HostSelector
{
Hostname = e.NewTextValue;
};
PortEntry.TextChanged += (sender, e) =>
{
Port = e.NewTextValue;
};
}
@ -119,33 +115,6 @@ namespace Aurora.Design.Components.HostSelector
#endregion Hostname property
#region Port property
public static readonly BindableProperty PortProperty =
BindableProperty.Create(propertyName: "Port",
returnType: typeof(string),
declaringType: typeof(HostSelector),
defaultBindingMode: BindingMode.TwoWay,
propertyChanged: OnPortChanged);
public string Port
{
get { return (string)GetValue(PortProperty); }
set { SetValue(PortProperty, value); }
}
private static void OnPortChanged(BindableObject bindable, object oldValue, object newValue)
{
string newVal = newValue as string;
HostSelector instance = bindable as HostSelector;
if (instance.PortEntry.Text != newVal)
{
instance.PortEntry.Text = newVal;
}
}
#endregion Port property
}

View File

@ -1,13 +1,14 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using Xamarin.Forms;
using Aurora.Design.Components.HorizontalList;
using Aurora.Models;
using Aurora.Proto;
namespace Aurora.Design.Components.MemberList
{
public partial class MemberList : ContentView
{
private static ObservableCollection<PartyMember> _newSource;
public MemberList()
{
InitializeComponent();
@ -22,7 +23,7 @@ namespace Aurora.Design.Components.MemberList
/// <returns></returns>
public static readonly BindableProperty MembersProperty =
BindableProperty.Create(propertyName: "Members",
returnType: typeof(IEnumerable<PartyMember>),
returnType: typeof(ObservableCollection<PartyMember>),
declaringType: typeof(MemberList),
defaultBindingMode: BindingMode.Default,
propertyChanged: OnMembersChanged);
@ -31,11 +32,11 @@ namespace Aurora.Design.Components.MemberList
/// Backing property for MembersProperty
/// </summary>
/// <value></value>
public IEnumerable<PartyMember> Members
public ObservableCollection<PartyMember> Members
{
get
{
return (IEnumerable<PartyMember>)GetValue(MembersProperty);
return (ObservableCollection<PartyMember>)GetValue(MembersProperty);
}
set
{
@ -55,8 +56,10 @@ namespace Aurora.Design.Components.MemberList
var membersList = control.FindByName("MembersHorizontalList") as HorizontalList.HorizontalList;
if (membersList != null)
{
membersList.ItemsSource = newValue as IEnumerable<PartyMember>;
_newSource = newValue as ObservableCollection<PartyMember>;
membersList.ItemsSource = newValue as ObservableCollection<PartyMember>;
}
}
}
}

View File

@ -27,7 +27,6 @@
<hs:HostSelector
Grid.Row="0"
Hostname="{Binding Hostname}"
Port="{Binding Port}"
HostCommand="{Binding HostCommand}"
JoinCommand="{Binding JoinCommand}"
IsVisible="{Binding IsSelectingHost}"/>

View File

@ -1,9 +1,7 @@
using System;
using System.Collections.ObjectModel;
using Aurora.Executors;
using Aurora.Design.Components.HostSelector;
using Aurora.Services;
using Aurora.Models;
using Aurora.Proto;
using Xamarin.Forms;
namespace Aurora.Design.Views.Party
@ -23,8 +21,6 @@ namespace Aurora.Design.Views.Party
private string _hostname;
private string _port;
private ObservableCollection<PartyMember> _members;
@ -66,12 +62,6 @@ namespace Aurora.Design.Views.Party
set { SetProperty(ref _hostname, value); }
}
public string Port
{
get { return _port; }
set { SetProperty(ref _port, value); }
}
#endregion Properties
@ -86,8 +76,10 @@ namespace Aurora.Design.Views.Party
private void OnJoinExecute()
{
_executor = BaseExecutor.CreateExecutor<ClientExecutor>();
Int32.TryParse(this.Port, out int intPort);
_executor.Initialize();
_executor.Connect(this.Hostname);
SetUpMembers();
State(PartyState.Connecting);
}
@ -98,24 +90,12 @@ namespace Aurora.Design.Views.Party
private void OnHostExecute()
{
Int32.TryParse(this.Port, out int intPort);
//Init gRPC server
ServerService.Instance.Initialize(this.Hostname, intPort);
//Instantiate and initialize all executors
_executor = BaseExecutor.CreateExecutor<HostExecutor>();
_executor.Initialize();
_executor.Connect(this.Hostname);
//start gRPC server
ServerService.Instance.Start();
_members = _executor.PartyMembers;
OnPropertyChanged("Members");
_executor.PartyMembers.CollectionChanged += (sender, e) =>
{
OnPropertyChanged("Members");
};
SetUpMembers();
//Change state
State(PartyState.Connecting);
@ -129,5 +109,18 @@ namespace Aurora.Design.Views.Party
#endregion Commands
private void SetUpMembers()
{
_members = _executor.PartyMembers;
OnPropertyChanged("Members");
_executor.PartyMembers.CollectionChanged += (sender, e) =>
{
if (_executor != null)
{
_members = _executor.PartyMembers;
}
OnPropertyChanged("Members");
};
}
}
}

View File

@ -9,10 +9,19 @@
<StackLayout
Orientation="Horizontal">
<Label
VerticalOptions="Center"
Text="Username"/>
<Entry
Text="{Binding Username}"/>
</StackLayout>
<StackLayout
Orientation="Horizontal">
<Label
VerticalOptions="Center"
Text="Default Port"/>
<Entry
Text="{Binding Port}"/>
</StackLayout>
</StackLayout>
</ContentView.Content>
</ContentView>

View File

@ -19,5 +19,16 @@ namespace Aurora.Design.Views.Profile
OnPropertyChanged("Username");
}
}
public string Port
{
get { return SettingsService.Instance.DefaultPort.ToString(); }
set
{
Int32.TryParse(value, out int portNum);
SettingsService.Instance.DefaultPort = portNum;
OnPropertyChanged("Port");
}
}
}
}

View File

@ -2,7 +2,7 @@ using System;
using System.Reflection;
using System.Linq;
using System.Collections.ObjectModel;
using Aurora.Models;
using Aurora.Proto;
namespace Aurora.Executors
{
@ -38,9 +38,7 @@ namespace Aurora.Executors
return executor;
}
public abstract void Initialize();
public abstract void Run();
public abstract void Connect(string hostname);
public abstract void Close();

View File

@ -1,31 +1,48 @@
using System;
using System.Collections.ObjectModel;
using Grpc.Core;
using Aurora.Proto;
using Aurora.Models;
using Aurora.Executors;
using Aurora.Services;
namespace Aurora.Executors
{
public class ClientExecutor : BaseExecutor
{
private Channel _channel;
private RemotePartyService.RemotePartyServiceClient _remotePartyClient;
private RemotePlaybackService.RemotePlaybackServiceClient _remotePlaybackClient;
private ObservableCollection<PartyMember> _partyMembers;
public ClientExecutor()
{
_partyMembers = new ObservableCollection<PartyMember>();
}
#region Properties
public override ObservableCollection<PartyMember> PartyMembers
{
get { return null; }
get { return _partyMembers; }
}
#endregion Properties
public override void AddToQueue()
public override void Connect(string hostname)
{
throw new NotImplementedException();
_channel = new Channel(string.Format("{0}:{1}", hostname, SettingsService.Instance.DefaultPort), ChannelCredentials.Insecure);
_remotePartyClient = new RemotePartyService.RemotePartyServiceClient(_channel);
_remotePlaybackClient = new RemotePlaybackService.RemotePlaybackServiceClient(_channel);
JoinParty();
}
public override void Close()
public override async void Close()
{
await _channel.ShutdownAsync();
}
public override void AddToQueue()
{
throw new NotImplementedException();
}
@ -40,19 +57,33 @@ namespace Aurora.Executors
throw new NotImplementedException();
}
public override void Initialize()
{
throw new NotImplementedException();
}
public override void RemoveFromQueue()
{
throw new NotImplementedException();
}
public override void Run()
private async void JoinParty()
{
throw new NotImplementedException();
await _remotePartyClient.JoinPartyAsync(new JoinPartyRequest
{
UserName = SettingsService.Instance.Username,
});
MembersResponse resposne = _remotePartyClient.GetPartyMembers(new Empty());
//Add members
foreach (PartyMember member in resposne.Members)
{
_partyMembers.Add(member);
}
//Remove out of date members
foreach (PartyMember member in _partyMembers)
{
if (!resposne.Members.Contains(member))
{
_partyMembers.Remove(member);
}
}
}
}
}

View File

@ -19,11 +19,17 @@ namespace Aurora.Executors
_remotePlaybackImpl = new RemotePlaybackServiceImpl();
}
public override void Initialize()
public override void Connect(string hostname)
{
//Initialize gRPC server
ServerService.Instance.Initialize(hostname);
//Register grpc RemoteService with singleton server service
ServerService.Instance.RegisterService(RemotePartyService.BindService(_remotePartyServiceImpl));
ServerService.Instance.RegisterService(RemotePlaybackService.BindService(_remotePlaybackImpl));
//start gRPC server
ServerService.Instance.Start();
}
#region Properties
@ -58,10 +64,5 @@ namespace Aurora.Executors
{
throw new NotImplementedException();
}
public override void Run()
{
throw new NotImplementedException();
}
}
}

View File

@ -1,16 +0,0 @@
using System;
namespace Aurora.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

@ -8,13 +8,11 @@ service RemotePartyService {
//Party Service
rpc JoinParty(JoinPartyRequest) returns (JoinPartyResponse);
rpc LeaveParty(LeavePartyRequest) returns (LeavePartyResponse);
rpc GetPartyMembers(Empty) returns (MembersResponse);
}
message JoinPartyRequest {
string clientId = 1;
string userName = 2;
string ipAddress = 3;
string port = 4;
}
message JoinPartyResponse {
@ -29,6 +27,19 @@ message LeavePartyResponse {
PartyJoinedStatusEnum status = 1;
}
message PartyMember {
string userName = 1;
string id = 2;
string ipAddress = 3;
int32 port = 4;
}
message MembersResponse {
repeated PartyMember members = 1;
}
enum PartyJoinedStatusEnum {
Connected = 0;
Disconnected = 1;

View File

@ -22,10 +22,10 @@ namespace Aurora.RemoteImpl
//Add self to members list
_partyMembers.Add(new PartyMember
{
Username = SettingsService.Instance.Username,
UserName = SettingsService.Instance.Username,
Id = "asdf",
IpAddress = ServerService.Instance.Hostname,
Port = ServerService.Instance.Port.ToString()
Port = ServerService.Instance.Port
});
}
@ -41,10 +41,8 @@ namespace Aurora.RemoteImpl
{
_partyMembers.Add(new PartyMember()
{
Username = request.UserName,
Id = request.ClientId,
IpAddress = request.IpAddress,
Port = request.Port,
UserName = request.UserName,
IpAddress = context.Host,
});
JoinPartyResponse response = new JoinPartyResponse() { Status = PartyJoinedStatusEnum.Connected };
@ -57,5 +55,12 @@ namespace Aurora.RemoteImpl
LeavePartyResponse response = new LeavePartyResponse() { Status = PartyJoinedStatusEnum.Disconnected };
return Task.FromResult(response);
}
public override Task<MembersResponse> GetPartyMembers(Empty empty, Grpc.Core.ServerCallContext context)
{
MembersResponse response = new MembersResponse();
response.Members.AddRange(_partyMembers);
return Task.FromResult(response);
}
}
}

View File

@ -8,7 +8,7 @@ namespace Aurora.Services
public class ServerService : BaseService<ServerService>
{
private string _hostname = "127.0.0.1";
private int _port = 50001;
private int _port = SettingsService.Instance.DefaultPort;
private Grpc.Core.Server _server;
/// <summary>
@ -28,9 +28,8 @@ namespace Aurora.Services
get { return _hostname; }
}
public void Initialize(string hostname, int port)
public void Initialize(string hostname)
{
this._port = port;
this._hostname = hostname;
_server = new Grpc.Core.Server
{

View File

@ -8,6 +8,8 @@ namespace Aurora.Services
public class SettingsService : BaseService<SettingsService>
{
private Lazy<ISettings> _appSettings;
private string _usernameKey = "username";
private string _defaultPortKey = "port";
public SettingsService()
{
@ -30,8 +32,6 @@ namespace Aurora.Services
}
}
private string _usernameKey = "username";
public string Username
{
get { return AppSettings.GetValueOrDefault(_usernameKey, ""); }
@ -40,5 +40,12 @@ namespace Aurora.Services
AppSettings.AddOrUpdateValue(_usernameKey, value);
}
}
public int DefaultPort
{
get { return AppSettings.GetValueOrDefault(_defaultPortKey, 4005); }
set { AppSettings.AddOrUpdateValue(_defaultPortKey, value); }
}
}
}