WingMan – Blame information for rev 5

Subversion Repositories:
Rev:
Rev Author Line No. Line
5 office 1 using System;
2 using System.Collections.Concurrent;
3 using System.Collections.Generic;
4 using System.IO;
5 using System.Linq;
6 using System.Text;
7 using System.Threading;
8 using System.Threading.Tasks;
9 using MQTTnet;
10 using MQTTnet.Extensions.ManagedClient;
11 using WingMan.Communication;
12  
13 namespace WingMan.MouseKey
14 {
15 public class MouseKeyBindingsSynchronizer
16 {
17 public delegate void MouseKeyBindingsSynchronized(object sender, MouseKeyBindingsSynchronizedEventArgs e);
18 public event MouseKeyBindingsSynchronized OnMouseKeyBindingsSynchronized;
19  
20 private MouseKeyBindings MouseKeyBindings { get; set; }
21  
22 private ConcurrentDictionary<string, List<MouseKeyBindingExchange>> SynchronizedMouseKeyBindings { get; set; }
23  
24 private MQTTCommunication MQTTCommunication { get; set; }
25  
26 private CancellationTokenSource SynchronizationCancellationTokenSource { get; set; }
27  
28 public MouseKeyBindingsSynchronizer(MouseKeyBindings mouseKeyBindings, MQTTCommunication MQTTCommunication)
29 {
30 MouseKeyBindings = mouseKeyBindings;
31 this.MQTTCommunication = MQTTCommunication;
32  
33 SynchronizedMouseKeyBindings = new ConcurrentDictionary<string, List<MouseKeyBindingExchange>>();
34  
35 MQTTCommunication.OnMessageReceived += MqttCommunicationOnOnMessageReceived;
36  
37 SynchronizationCancellationTokenSource = new CancellationTokenSource();
38  
39 Task.Run(Synchronize, SynchronizationCancellationTokenSource.Token);
40 }
41  
42 private void MqttCommunicationOnOnMessageReceived(object sender, MqttApplicationMessageReceivedEventArgs e)
43 {
44 if (e.ApplicationMessage.Topic != "exchange")
45 return;
46  
47 using (var memoryStream = new MemoryStream(e.ApplicationMessage.Payload))
48 {
49 memoryStream.Position = 0L;
50  
51 var mouseKeyBindingsExchange = (MouseKeyBindingsExchange)MouseKeyBindingsExchange.XmlSerializer.Deserialize(memoryStream);
52  
53 if (SynchronizedMouseKeyBindings.TryGetValue(mouseKeyBindingsExchange.Nick, out var mouseKeyBinding) &&
54 mouseKeyBinding.SequenceEqual(mouseKeyBindingsExchange.ExchangeBindings))
55 return;
56  
57 // Nick does not exist so the bindings will be added.
58 SynchronizedMouseKeyBindings.AddOrUpdate(mouseKeyBindingsExchange.Nick,
59 mouseKeyBindingsExchange.ExchangeBindings, (s, list) => mouseKeyBindingsExchange.ExchangeBindings);
60  
61 OnMouseKeyBindingsSynchronized?.Invoke(sender,
62 new MouseKeyBindingsSynchronizedEventArgs(
63 mouseKeyBindingsExchange.ExchangeBindings));
64 }
65 }
66  
67 private async Task Synchronize()
68 {
69 do
70 {
71 await Task.Delay(1000).ConfigureAwait(false);
72  
73 if (!MouseKeyBindings.Bindings.Any())
74 continue;
75  
76 using (var memoryStream = new MemoryStream())
77 {
78 MouseKeyBindingsExchange.XmlSerializer.Serialize(memoryStream, new MouseKeyBindingsExchange(MQTTCommunication.Nick, MouseKeyBindings));
79  
80 memoryStream.Position = 0L;
81  
82 await MQTTCommunication.Broadcast("exchange", memoryStream.ToArray()).ConfigureAwait(false);
83 }
84  
85 } while (!SynchronizationCancellationTokenSource.Token.IsCancellationRequested);
86 }
87 }
88 }