WingMan – Diff between revs 9 and 10

Subversion Repositories:
Rev:
Only display areas with differencesIgnore whitespace
Rev 9 Rev 10
-   1 using System;
1 using System.Collections.Concurrent; 2 using System.Collections.Concurrent;
2 using System.Collections.Generic; 3 using System.Collections.Generic;
3 using System.IO; 4 using System.IO;
4 using System.Linq; 5 using System.Linq;
5 using System.Threading; 6 using System.Threading;
6 using System.Threading.Tasks; 7 using System.Threading.Tasks;
7 using MQTTnet; 8 using MQTTnet;
8 using WingMan.Communication; 9 using WingMan.Communication;
9   10  
10 namespace WingMan.MouseKey 11 namespace WingMan.MouseKey
11 { 12 {
12 public class MouseKeyBindingsSynchronizer 13 public class KeyBindingsSynchronizer : IDisposable
13 { 14 {
14 public delegate void MouseKeyBindingsSynchronized(object sender, MouseKeyBindingsSynchronizedEventArgs e); 15 public delegate void MouseKeyBindingsSynchronized(object sender, KeyBindingsSynchronizerEventArgs e);
15   16  
16 public MouseKeyBindingsSynchronizer(MouseKeyBindings mouseKeyBindings, MqttCommunication mqttCommunication, 17 public KeyBindingsSynchronizer(LocalKeyBindings localKeyBindings, MqttCommunication mqttCommunication,
17 TaskScheduler taskScheduler, CancellationToken cancellationToken) 18 TaskScheduler taskScheduler, CancellationToken cancellationToken)
18 { 19 {
19 MouseKeyBindings = mouseKeyBindings; 20 LocalKeyBindings = localKeyBindings;
20 MqttCommunication = mqttCommunication; 21 MqttCommunication = mqttCommunication;
21 CancellationToken = cancellationToken; 22 CancellationToken = cancellationToken;
22 TaskScheduler = taskScheduler; 23 TaskScheduler = taskScheduler;
23   24  
24 SynchronizedMouseKeyBindings = new ConcurrentDictionary<string, List<MouseKeyBinding>>(); 25 SynchronizedMouseKeyBindings = new ConcurrentDictionary<string, List<KeyBinding>>();
25   26  
26 mqttCommunication.OnMessageReceived += MqttCommunicationOnOnMessageReceived; 27 MqttCommunication.OnMessageReceived += MqttCommunicationOnMessageReceived;
27   28  
28 Task.Run(Synchronize, CancellationToken); 29 Task.Run(PeriodicSynchronize, CancellationToken);
29 } 30 }
30   31  
31 private MouseKeyBindings MouseKeyBindings { get; } 32 private LocalKeyBindings LocalKeyBindings { get; }
32   33  
33 private ConcurrentDictionary<string, List<MouseKeyBinding>> SynchronizedMouseKeyBindings { get; } 34 private ConcurrentDictionary<string, List<KeyBinding>> SynchronizedMouseKeyBindings { get; }
34   35  
35 private MqttCommunication MqttCommunication { get; } 36 private MqttCommunication MqttCommunication { get; }
36   37  
37 private CancellationToken CancellationToken { get; } 38 private CancellationToken CancellationToken { get; }
38 private TaskScheduler TaskScheduler { get; } 39 private TaskScheduler TaskScheduler { get; }
-   40  
-   41 public void Dispose()
-   42 {
-   43 MqttCommunication.OnMessageReceived -= MqttCommunicationOnMessageReceived;
-   44 }
-   45  
39 public event MouseKeyBindingsSynchronized OnMouseKeyBindingsSynchronized; 46 public event MouseKeyBindingsSynchronized OnMouseKeyBindingsSynchronized;
40   47  
41 private async void MqttCommunicationOnOnMessageReceived(object sender, 48 private async void MqttCommunicationOnMessageReceived(object sender,
42 MqttApplicationMessageReceivedEventArgs e) 49 MqttApplicationMessageReceivedEventArgs e)
43 { 50 {
44 if (e.ApplicationMessage.Topic != "exchange") 51 if (e.ApplicationMessage.Topic != "exchange")
45 return; 52 return;
46   53  
47 using (var memoryStream = new MemoryStream(e.ApplicationMessage.Payload)) 54 using (var memoryStream = new MemoryStream(e.ApplicationMessage.Payload))
48 { 55 {
49 memoryStream.Position = 0L; 56 memoryStream.Position = 0L;
50   57  
51 var mouseKeyBindingsExchange = 58 var mouseKeyBindingsExchange =
52 (MouseKeyBindingExchange) MouseKeyBindingExchange.XmlSerializer.Deserialize(memoryStream); 59 (KeyBindingExchange) KeyBindingExchange.XmlSerializer.Deserialize(memoryStream);
53   60  
54 if (SynchronizedMouseKeyBindings.TryGetValue(mouseKeyBindingsExchange.Nick, out var mouseKeyBinding) && 61 // Do not add own bindings.
55 mouseKeyBinding.SequenceEqual(mouseKeyBindingsExchange.MouseKeyBindings)) 62 if (string.Equals(mouseKeyBindingsExchange.Nick, MqttCommunication.Nick))
56 return; -  
57   63 return;
58 // Nick does not exist so the bindings will be added. 64  
-   65 if (SynchronizedMouseKeyBindings.TryGetValue(mouseKeyBindingsExchange.Nick, out var mouseKeyBinding) &&
59 SynchronizedMouseKeyBindings.AddOrUpdate(mouseKeyBindingsExchange.Nick, 66 mouseKeyBinding.SequenceEqual(mouseKeyBindingsExchange.KeyBindings))
60 mouseKeyBindingsExchange.MouseKeyBindings, (s, list) => mouseKeyBindingsExchange.MouseKeyBindings); 67 return;
61   68  
62 await Task.Delay(0) 69 await Task.Delay(0)
63 .ContinueWith( 70 .ContinueWith(
64 _ => OnMouseKeyBindingsSynchronized?.Invoke(sender, 71 _ => OnMouseKeyBindingsSynchronized?.Invoke(sender,
65 new MouseKeyBindingsSynchronizedEventArgs( 72 new KeyBindingsSynchronizerEventArgs(
66 mouseKeyBindingsExchange)), 73 mouseKeyBindingsExchange)),
67 CancellationToken, TaskContinuationOptions.None, TaskScheduler); 74 CancellationToken, TaskContinuationOptions.None, TaskScheduler);
-   75  
-   76 // Nick does not exist so the bindings will be added.
-   77 SynchronizedMouseKeyBindings.AddOrUpdate(mouseKeyBindingsExchange.Nick,
-   78 mouseKeyBindingsExchange.KeyBindings, (s, list) => mouseKeyBindingsExchange.KeyBindings);
68 } 79 }
69 } 80 }
70   81  
71 private async Task Synchronize() 82 private async Task PeriodicSynchronize()
72 { 83 {
73 do 84 do
74 { 85 {
75 await Task.Delay(1000, CancellationToken).ConfigureAwait(false); 86 await Task.Delay(1000, CancellationToken);
76   87  
77 if (!MqttCommunication.Running) 88 if (!MqttCommunication.Running)
78 continue; 89 continue;
79   90  
80 using (var memoryStream = new MemoryStream()) 91 using (var memoryStream = new MemoryStream())
81 { 92 {
82 MouseKeyBindingExchange.XmlSerializer.Serialize(memoryStream, 93 KeyBindingExchange.XmlSerializer.Serialize(memoryStream,
83 new MouseKeyBindingExchange(MqttCommunication.Nick, MouseKeyBindings.Bindings)); 94 new KeyBindingExchange(MqttCommunication.Nick, LocalKeyBindings.Bindings));
84   95  
85 memoryStream.Position = 0L; 96 memoryStream.Position = 0L;
86   97  
87 await MqttCommunication.Broadcast("exchange", memoryStream.ToArray()).ConfigureAwait(false); 98 await MqttCommunication.Broadcast("exchange", memoryStream.ToArray());
88 } 99 }
89 } while (!CancellationToken.IsCancellationRequested); 100 } while (!CancellationToken.IsCancellationRequested);
90 } 101 }
91 } 102 }
92 } 103 }
93   104