WingMan

Subversion Repositories:
Compare Path: Rev
With Path: Rev
?path1? @ 6  →  ?path2? @ 7
/trunk/WingMan/MouseKey/MouseKeyBindingsSynchronizer.cs
@@ -1,13 +1,10 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using MQTTnet;
using MQTTnet.Extensions.ManagedClient;
using WingMan.Communication;
 
namespace WingMan.MouseKey
@@ -15,31 +12,34 @@
public class MouseKeyBindingsSynchronizer
{
public delegate void MouseKeyBindingsSynchronized(object sender, MouseKeyBindingsSynchronizedEventArgs e);
public event MouseKeyBindingsSynchronized OnMouseKeyBindingsSynchronized;
 
private MouseKeyBindings MouseKeyBindings { get; set; }
 
private ConcurrentDictionary<string, List<MouseKeyBindingExchange>> SynchronizedMouseKeyBindings { get; set; }
 
private MQTTCommunication MQTTCommunication { get; set; }
 
private CancellationTokenSource SynchronizationCancellationTokenSource { get; set; }
 
public MouseKeyBindingsSynchronizer(MouseKeyBindings mouseKeyBindings, MQTTCommunication MQTTCommunication)
public MouseKeyBindingsSynchronizer(MouseKeyBindings mouseKeyBindings, MQTTCommunication MQTTCommunication,
TaskScheduler taskScheduler, CancellationToken cancellationToken)
{
MouseKeyBindings = mouseKeyBindings;
this.MQTTCommunication = MQTTCommunication;
CancellationToken = cancellationToken;
TaskScheduler = taskScheduler;
 
SynchronizedMouseKeyBindings = new ConcurrentDictionary<string, List<MouseKeyBindingExchange>>();
SynchronizedMouseKeyBindings = new ConcurrentDictionary<string, List<MouseKeyBinding>>();
 
MQTTCommunication.OnMessageReceived += MqttCommunicationOnOnMessageReceived;
 
SynchronizationCancellationTokenSource = new CancellationTokenSource();
 
Task.Run(Synchronize, SynchronizationCancellationTokenSource.Token);
Task.Run(Synchronize, CancellationToken);
}
 
private void MqttCommunicationOnOnMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e)
private MouseKeyBindings MouseKeyBindings { get; }
 
private ConcurrentDictionary<string, List<MouseKeyBinding>> SynchronizedMouseKeyBindings { get; }
 
private MQTTCommunication MQTTCommunication { get; }
 
private CancellationToken CancellationToken { get; }
private TaskScheduler TaskScheduler { get; }
public event MouseKeyBindingsSynchronized OnMouseKeyBindingsSynchronized;
 
private async void MqttCommunicationOnOnMessageReceived(object sender,
MqttApplicationMessageReceivedEventArgs e)
{
if (e.ApplicationMessage.Topic != "exchange")
return;
@@ -48,19 +48,23 @@
{
memoryStream.Position = 0L;
 
var mouseKeyBindingsExchange = (MouseKeyBindingsExchange)MouseKeyBindingsExchange.XmlSerializer.Deserialize(memoryStream);
var mouseKeyBindingsExchange =
(MouseKeyBindingExchange) MouseKeyBindingExchange.XmlSerializer.Deserialize(memoryStream);
 
if (SynchronizedMouseKeyBindings.TryGetValue(mouseKeyBindingsExchange.Nick, out var mouseKeyBinding) &&
mouseKeyBinding.SequenceEqual(mouseKeyBindingsExchange.ExchangeBindings))
mouseKeyBinding.SequenceEqual(mouseKeyBindingsExchange.MouseKeyBindings))
return;
 
// Nick does not exist so the bindings will be added.
SynchronizedMouseKeyBindings.AddOrUpdate(mouseKeyBindingsExchange.Nick,
mouseKeyBindingsExchange.ExchangeBindings, (s, list) => mouseKeyBindingsExchange.ExchangeBindings);
mouseKeyBindingsExchange.MouseKeyBindings, (s, list) => mouseKeyBindingsExchange.MouseKeyBindings);
 
OnMouseKeyBindingsSynchronized?.Invoke(sender,
new MouseKeyBindingsSynchronizedEventArgs(
mouseKeyBindingsExchange.ExchangeBindings));
await Task.Delay(0)
.ContinueWith(
_ => OnMouseKeyBindingsSynchronized?.Invoke(sender,
new MouseKeyBindingsSynchronizedEventArgs(
mouseKeyBindingsExchange)),
CancellationToken, TaskContinuationOptions.None, TaskScheduler);
}
}
 
@@ -68,21 +72,21 @@
{
do
{
await Task.Delay(1000).ConfigureAwait(false);
await Task.Delay(1000, CancellationToken).ConfigureAwait(false);
 
if (!MouseKeyBindings.Bindings.Any())
if (!MQTTCommunication.Running)
continue;
 
using (var memoryStream = new MemoryStream())
{
MouseKeyBindingsExchange.XmlSerializer.Serialize(memoryStream, new MouseKeyBindingsExchange(MQTTCommunication.Nick, MouseKeyBindings));
MouseKeyBindingExchange.XmlSerializer.Serialize(memoryStream,
new MouseKeyBindingExchange(MQTTCommunication.Nick, MouseKeyBindings.Bindings));
 
memoryStream.Position = 0L;
 
await MQTTCommunication.Broadcast("exchange", memoryStream.ToArray()).ConfigureAwait(false);
}
 
} while (!SynchronizationCancellationTokenSource.Token.IsCancellationRequested);
} while (!CancellationToken.IsCancellationRequested);
}
}
}