Event manager improvements. Changed to console.writeline instead of diagnostics

This commit is contained in:
watsonb8 2020-01-13 19:57:02 -05:00
parent 43c6a5a2c6
commit e796bf2f35
17 changed files with 165 additions and 73 deletions

View File

@ -2,7 +2,6 @@
<ContentView
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:hl="clr-namespace:Aurora.Design.Components.HorizontalList"
xmlns:flv="clr-namespace:DLToolkit.Forms.Controls;assembly=DLToolkit.Forms.Controls.FlowListView"
x:Class="Aurora.Design.Components.MemberList.MemberList">
<ContentView.Resources>
@ -14,7 +13,6 @@
x:Name="MembersList"
FlowColumnMinWidth="150"
RowHeight="150"
VerticalOptions="FillAndExpand"
SeparatorVisibility="None"
BackgroundColor="#181818"
HasUnevenRows="false">

View File

@ -25,7 +25,7 @@ namespace Aurora.Design.Extensions
foreach (var res in gtkAsm.GetManifestResourceNames())
{
System.Diagnostics.Debug.WriteLine("found resource: " + res);
Console.WriteLine("found resource: " + res);
}
if (gtkAsm == null && gtkAsm is Assembly)

View File

@ -146,7 +146,6 @@ namespace Aurora.Design.Views.Main
vm.OnActive();
_viewContent.Content = view;
MasterPage.ListView.SelectedItem = firstNavItem;
}

View File

@ -5,4 +5,33 @@ Label {
#MembersList {
height: 100;
background-color: transparent;
}
}
#LeaveButton {
margin-top: 20;
margin-bottom: 20;
vertical-align: bottom;
align-self: center;
}
#MembersList {
background-color: #1e1e1e;
}
#MembersList Grid {
margin-left: 20;
margin-right: 20;
margin-top: 20;
margin-bottom: 20;
width: 150;
border-radius: 25;
background-color: #626363;
}
#MembersList Label {
text-align: center;
vertical-align: middle;
color: white;
font-size: 20;
}

View File

@ -22,13 +22,31 @@
HeaderBackgroundColor="#181818"
x:Name="TabView">
<tabView:TabViewControl.ItemSource>
<!-- Members Tab -->
<tabView:TabItem
HeaderText="Members">
<ml:MemberList
x:Name="MembersList"
VerticalOptions="FillAndExpand"
Members="{Binding Members}"/>
<Grid
HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand">
<Grid.RowDefinitions>
<RowDefinition
Height="*"/>
<RowDefinition
Height="30"/>
</Grid.RowDefinitions>
<ml:MemberList
x:Name="MembersList"
Grid.Row="0"
VerticalOptions="FillAndExpand"
Members="{Binding Members}"/>
<!-- Leave Party Button -->
<Button
Grid.Row="1"
Text="Leave Party"
Command="{Binding LeavePartyCommand}"/>
</Grid>
</tabView:TabItem>
<!-- Library Tab -->
<tabView:TabItem
HeaderText="Queue">
<library:Library

View File

@ -45,6 +45,8 @@ namespace Aurora.Design.Views.Party
PlayCommand = new Command(OnDoubleClickExecute, CanDoubleClickExecute);
LeavePartyCommand = new Command(OnLeavePartyExecute, CanLeaveParty);
_client = ClientService.Instance;
_client.OnMediaPaused += this.OnMediaPaused;
@ -52,6 +54,7 @@ namespace Aurora.Design.Views.Party
_client.OnNewMediaPlaying += this.OnNewMediaPlaying;
_client.OnPartyMemberJoined += this.OnPartyMemberJoined;
_client.OnPartyMemberLeft += this.OnPartyMemberLeft;
}
~PartyViewModel()
@ -118,6 +121,8 @@ namespace Aurora.Design.Views.Party
/// <value></value>
public Command PlayCommand { get; private set; }
public Command LeavePartyCommand { get; private set; }
#endregion Properties
#region Events
@ -251,7 +256,7 @@ namespace Aurora.Design.Views.Party
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Exception occurred while receiviing events: ", ex.Message);
Console.WriteLine("Exception occurred while receiviing events: ", ex.Message);
}
}
@ -279,7 +284,7 @@ namespace Aurora.Design.Views.Party
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Exception occurred while receiviing events: ", ex.Message);
Console.WriteLine("Exception occurred while receiviing events: ", ex.Message);
}
}
@ -288,6 +293,16 @@ namespace Aurora.Design.Views.Party
return true;
}
private async void OnLeavePartyExecute()
{
await _client.RemotePartyClient.LeavePartyAsync(new LeavePartyRequest());
}
private bool CanLeaveParty()
{
return (this._state == PartyState.InParty || this._state == PartyState.Hosting) ? true : false;
}
public override void OnPlayButtonExecute()
{
if (base.IsPlaying())
@ -408,7 +423,7 @@ namespace Aurora.Design.Views.Party
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Error subscribing to events: " + ex.Message);
Console.WriteLine("Error subscribing to events: " + ex.Message);
}
}
@ -438,7 +453,7 @@ namespace Aurora.Design.Views.Party
}
System.Diagnostics.Debug.WriteLine(string.Format("CLIENT {0} - SubscribeToEvents called from client with id", SettingsService.Instance.ClientId));
Console.WriteLine(string.Format("CLIENT {0} - SubscribeToEvents called from client with id", SettingsService.Instance.ClientId));
await _client.RemoteEventClient.SubscribeToEventsAsync(req);
}
private async Task UnsubscribeFromEvents()

View File

@ -69,11 +69,11 @@ namespace Aurora.Models.Media
await this.DataStream.WriteAsync(buffer, 0, buffer.Length);
}
System.Diagnostics.Debug.WriteLine("Done receiving stream");
Console.WriteLine("Done receiving stream");
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Exception caught while loading remote audio:" + ex.Message);
Console.WriteLine("Exception caught while loading remote audio:" + ex.Message);
}
}
await base.Load();

View File

@ -3,6 +3,7 @@ using System.Threading.Tasks;
using System.Threading;
using System.Collections.Generic;
using Aurora.Services;
using Aurora.Utils;
using System.Linq;
using Aurora.Services.EventManager;
using Aurora.Proto.Events;
@ -27,19 +28,24 @@ namespace Aurora.RemoteImpl
Grpc.Core.IServerStreamWriter<BaseEvent> responseStream,
Grpc.Core.ServerCallContext context)
{
string peerId = Combine(new string[] { context.Peer, request.ClientId });
System.Diagnostics.Debug.WriteLine(string.Format("SERVER - Events request received from peer: {0}", peerId));
string peerId = Misc.Combine(new string[] { context.Peer, request.ClientId });
Console.WriteLine(string.Format("SERVER - Events request received from peer: {0}", peerId));
AutoResetEvent are = new AutoResetEvent(false);
Action<BaseEvent> callback = (BaseEvent bEvent) =>
{
System.Diagnostics.Debug.WriteLine(string.Format("SERVER - Event fired for peer: {0}", peerId));
Console.WriteLine(string.Format("SERVER - Event fired for peer: {0}", peerId));
//TODO need to remove callback if stream no longer exists IE. Client crashed or stopped
responseStream.WriteAsync(bEvent);
};
EventManager.Instance.AddEventHandler(callback, Combine(new string[] { context.Peer, request.ClientId }));
Action cancelled = () =>
{
are.Set();
};
EventManager.Instance.AddEventHandler(callback, cancelled, Misc.Combine(new string[] { context.Peer, request.ClientId }));
are.WaitOne();
return Task.FromResult<object>(null);
}
@ -52,8 +58,8 @@ namespace Aurora.RemoteImpl
/// <returns></returns>
public override Task<SubscriptionResponse> SubscribeToEvents(SubscribeRequest request, Grpc.Core.ServerCallContext context)
{
System.Diagnostics.Debug.WriteLine(string.Format("SERVER - Subscription from client with id: {0}", request.ClientId));
EventManager.Instance.AddSubscriptionList(Combine(new string[] { context.Peer, request.ClientId }), request.EventTypes.ToList());
Console.WriteLine(string.Format("SERVER - Subscription from client with id: {0}", request.ClientId));
EventManager.Instance.AddSubscriptionList(Misc.Combine(new string[] { context.Peer, request.ClientId }), request.EventTypes.ToList());
return Task.FromResult(new SubscriptionResponse { Successful = true });
}
@ -69,7 +75,7 @@ namespace Aurora.RemoteImpl
EventType[] eventTypes = null;
request.EventTypes.CopyTo(eventTypes, 0);
EventManager.Instance.RemoveSubscriptionList(Combine(new string[] { context.Peer, request.ClientId }), eventTypes.ToList());
EventManager.Instance.RemoveSubscriptionList(Misc.Combine(new string[] { context.Peer, request.ClientId }), eventTypes.ToList());
return Task.FromResult(new SubscriptionResponse { Successful = true });
}
@ -82,28 +88,10 @@ namespace Aurora.RemoteImpl
/// <returns></returns>
public override Task<SubscriptionResponse> UnsubscribeFromAll(UnsubscribeAllRequest request, Grpc.Core.ServerCallContext context)
{
EventManager.Instance.RemoveAllSubscriptions(Combine(new string[] { context.Peer, request.ClientId }));
EventManager.Instance.RemoveAllSubscriptions(Misc.Combine(new string[] { context.Peer, request.ClientId }));
return Task.FromResult(new SubscriptionResponse { Successful = true });
}
private string Combine(string[] args)
{
string outString = "";
foreach (string arg in args)
{
if (arg == args.Last())
{
outString += arg;
}
else
{
outString += arg + ":";
}
}
return outString;
}
}
}

View File

@ -2,13 +2,12 @@ using System;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
using System.Linq;
using Aurora.Utils;
using Aurora.Proto.Party;
using Aurora.Proto.Events;
using Aurora.Proto.General;
using Aurora.Services.EventManager;
using Aurora.Services;
using Aurora.Models.Media;
using System.Collections;
namespace Aurora.RemoteImpl
{
@ -40,7 +39,7 @@ namespace Aurora.RemoteImpl
IpAddress = context.Host,
};
System.Diagnostics.Debug.WriteLine("SERVER - Client joined party: " + partyMember.Id);
Console.WriteLine("SERVER - Client joined party: " + partyMember.Id);
_partyMembers.Add(partyMember);
@ -75,6 +74,8 @@ namespace Aurora.RemoteImpl
};
EventManager.Instance.FireEvent(bv);
EventManager.Instance.RemoveAllSubscriptions(Misc.Combine(new string[] { context.Peer, request.ClientId }));
EventManager.Instance.CancelEventStream(Misc.Combine(new string[] { context.Peer, request.ClientId }));
LeavePartyResponse response = new LeavePartyResponse() { Status = PartyJoinedStatusEnum.Disconnected };
return Task.FromResult(response);
@ -128,7 +129,7 @@ namespace Aurora.RemoteImpl
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(string.Format("Error preparing queue: {0}", ex.Message));
Console.WriteLine(string.Format("Error preparing queue: {0}", ex.Message));
}
}

View File

@ -33,7 +33,7 @@ namespace Aurora.RemoteImpl
}
//Send stream
System.Diagnostics.Debug.WriteLine("Begin sending file");
Console.WriteLine("Begin sending file");
byte[] buffer = new byte[2048]; // read in chunks of 2KB
int bytesRead;
while ((bytesRead = songCopy.DataStream.Read(buffer, 0, buffer.Length)) > 0)
@ -41,11 +41,11 @@ namespace Aurora.RemoteImpl
Google.Protobuf.ByteString bufferByteString = Google.Protobuf.ByteString.CopyFrom(buffer);
await responseStream.WriteAsync(new Chunk { Content = bufferByteString });
}
System.Diagnostics.Debug.WriteLine("Done sending file");
Console.WriteLine("Done sending file");
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Exception caught while sending audio file: " + ex.Message);
Console.WriteLine("Exception caught while sending audio file: " + ex.Message);
}
}
}

View File

@ -41,7 +41,7 @@ namespace Aurora.RemoteImpl
ServerTimeTicks = Utils.TimeUtils.GetNetworkTime().DateTime.Ticks
};
await responseStream.WriteAsync(sync);
System.Diagnostics.Debug.WriteLine("Sent Sync");
Console.WriteLine("Sent Sync");
await Task.Delay(5000);
}
}

View File

@ -95,7 +95,7 @@ namespace Aurora.Services.ClientService
{
_eventCancellationTokenSource = new CancellationTokenSource();
string clientId = SettingsService.Instance.ClientId;
System.Diagnostics.Debug.WriteLine(string.Format("CLIENT {0} - GetEvents called from client with id", clientId));
Console.WriteLine(string.Format("CLIENT {0} - GetEvents called from client with id", clientId));
using (AsyncServerStreamingCall<BaseEvent> eventStream = _remoteEventsClient
.GetEvents(new EventsRequest { ClientId = SettingsService.Instance.ClientId }))
{
@ -126,14 +126,14 @@ namespace Aurora.Services.ClientService
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Exception while parsing event ---" + ex.Message);
Console.WriteLine("Exception while parsing event ---" + ex.Message);
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(string.Format("EXCEPTION while parsing events --- " + ex.Message));
Console.WriteLine(string.Format("EXCEPTION while parsing events --- " + ex.Message));
}
}
}

View File

@ -7,17 +7,28 @@ using Aurora.Proto.Events;
namespace Aurora.Services.EventManager
{
public class EventAction
{
public EventAction(Action<BaseEvent> callback, Action cancel)
{
Callback = callback;
Cancel = cancel;
}
public Action<BaseEvent> Callback { get; set; }
public Action Cancel { get; set; }
}
public class EventManager : BaseService<EventManager>
{
#region Fields
private Dictionary<string, List<EventType>> _subscriptionList;
private Dictionary<string, Action<BaseEvent>> _actionList;
private Dictionary<string, EventAction> _actionList;
#endregion Fields
public EventManager()
{
_subscriptionList = new Dictionary<string, List<EventType>>();
_actionList = new Dictionary<string, Action<BaseEvent>>();
_actionList = new Dictionary<string, EventAction>();
}
#region Private Methods
@ -147,11 +158,11 @@ namespace Aurora.Services.EventManager
}
}
public void AddEventHandler(Action<BaseEvent> action, string sessionId)
public void AddEventHandler(Action<BaseEvent> action, Action cancel, string sessionId)
{
lock (_actionList)
{
_actionList.Add(sessionId, action);
_actionList.Add(sessionId, new EventAction(action, cancel));
}
}
@ -160,13 +171,21 @@ namespace Aurora.Services.EventManager
_actionList.Remove(sessionId);
}
public void CancelEventStream(string sessionId)
{
_actionList.TryGetValue(sessionId, out EventAction value);
value.Cancel();
RemoveEventHandler(sessionId);
}
public void FireEvent(BaseEvent bEvent)
{
Dictionary<string, Action<BaseEvent>> actionsCopy = new Dictionary<string, Action<BaseEvent>>();
Dictionary<string, EventAction> actionsCopy = new Dictionary<string, EventAction>();
//Copy actions list
lock (_actionList)
{
foreach (KeyValuePair<string, Action<BaseEvent>> pair in _actionList)
foreach (KeyValuePair<string, EventAction> pair in _actionList)
{
actionsCopy.Add(pair.Key, pair.Value);
}
@ -180,14 +199,14 @@ namespace Aurora.Services.EventManager
//If action list contains an action for id, invoke
if (actionsCopy.ContainsKey(pair.Key))
{
actionsCopy.TryGetValue(pair.Key, out Action<BaseEvent> action);
Task executionTask = new Task(() => action(bEvent));
actionsCopy.TryGetValue(pair.Key, out EventAction action);
Task executionTask = new Task(() => action.Callback(bEvent));
//Execute task with exception handler
executionTask.ContinueWith((Task task) =>
{
var exception = executionTask.Exception;
System.Diagnostics.Debug.WriteLine(string.Format("SERVER --- Exception occurred firing event"));
Console.WriteLine(string.Format("SERVER --- Exception occurred firing event"));
this._actionList.Remove(pair.Key);
},
TaskContinuationOptions.OnlyOnFaulted);

View File

@ -145,21 +145,21 @@ namespace Aurora.Services.PlayerService
newPosition - oldPosition < -0.001)
{
_mediaPlayer.Position = newPosition;
System.Diagnostics.Debug.WriteLine(string.Format("**Audio synced**"));
// System.Diagnostics.Debug.WriteLine(string.Format("Remote Server Time {0}", new DateTime(sync.ServerTimeTicks).ToLongTimeString()));
// System.Diagnostics.Debug.WriteLine(string.Format("Remote Track Time: {0}", sync.TrackPosition));
// System.Diagnostics.Debug.WriteLine(string.Format("Local Server Time: {0}", time.DateTime.ToLongTimeString()));
// System.Diagnostics.Debug.WriteLine(string.Format("Local Track Time: {0}", _mediaPlayer.Position));
// System.Diagnostics.Debug.WriteLine(string.Format("Offset: {0}", offset));
// System.Diagnostics.Debug.WriteLine(string.Format("Old Position: {0}", oldPosition));
// System.Diagnostics.Debug.WriteLine(string.Format("New Position: {0}", newPosition));
Console.WriteLine(string.Format("**Audio synced**"));
// Console.WriteLine(string.Format("Remote Server Time {0}", new DateTime(sync.ServerTimeTicks).ToLongTimeString()));
// Console.WriteLine(string.Format("Remote Track Time: {0}", sync.TrackPosition));
// Console.WriteLine(string.Format("Local Server Time: {0}", time.DateTime.ToLongTimeString()));
// Console.WriteLine(string.Format("Local Track Time: {0}", _mediaPlayer.Position));
// Console.WriteLine(string.Format("Offset: {0}", offset));
// Console.WriteLine(string.Format("Old Position: {0}", oldPosition));
// Console.WriteLine(string.Format("New Position: {0}", newPosition));
}
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Exception caught while attempting to sync: " + ex.Message);
Console.WriteLine("Exception caught while attempting to sync: " + ex.Message);
}
}
});

View File

@ -73,7 +73,7 @@ namespace Aurora.Services
try
{
System.Diagnostics.Debug.WriteLine(string.Format("Starting gRPC server at hostname: {0}, port: {1}", _hostname, _port));
Console.WriteLine(string.Format("Starting gRPC server at hostname: {0}, port: {1}", _hostname, _port));
if (!Initialized)
{
@ -93,7 +93,7 @@ namespace Aurora.Services
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(string.Format("Error starting gRPC server: {0}", ex.Message));
Console.WriteLine(string.Format("Error starting gRPC server: {0}", ex.Message));
}
}

25
Aurora/Utils/Combine.cs Normal file
View File

@ -0,0 +1,25 @@
using System.Linq;
namespace Aurora.Utils
{
public static class Misc
{
public static string Combine(string[] args)
{
string outString = "";
foreach (string arg in args)
{
if (arg == args.Last())
{
outString += arg;
}
else
{
outString += arg + ":";
}
}
return outString;
}
}
}

View File

@ -73,7 +73,7 @@ namespace Aurora.Utils
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine($"{ex.GetType()} {ex.Message}\n{ex.StackTrace}");
Console.WriteLine($"{ex.GetType()} {ex.Message}\n{ex.StackTrace}");
ex = ex.InnerException;
}
}