WingMan – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Globalization;
7 using System.Linq;
8 using System.Net;
9 using System.Text;
10 using System.Threading;
11 using System.Threading.Tasks;
12 using System.Windows.Forms;
13 using WingMan.Communication;
14 using WingMan.Host;
15  
16 namespace WingMan
17 {
18 public partial class Form1 : Form
19 {
20 private MQTTServer MQTTServer { get; set; } = new MQTTServer();
21  
22 private MQTTClient MQTTClient { get; set; } = new MQTTClient();
23  
24 private SemaphoreSlim MQTTServerSemaphore {get; set;} = new SemaphoreSlim(1, 1);
25  
26 private SemaphoreSlim MQTTClientSemaphore { get; set; } = new SemaphoreSlim(1, 1);
27  
28 public Form1()
29 {
30 InitializeComponent();
31 }
32  
33 private void AddressTextBoxClick(object sender, EventArgs e)
34 {
35 Address.BackColor = Color.Empty;
36 }
37  
38 private void PortTextBoxClick(object sender, EventArgs e)
39 {
40 Port.BackColor = Color.Empty;
41 }
42  
43 private async void HostButtonClickAsync(object sender, EventArgs e)
44 {
45 // Stop the MQTT server if it is running.
46 if (MQTTServer.ServerRunning)
47 {
48 await StopServer();
49 toolStripStatusLabel.Text = Properties.Strings.Server_stopped;
50 HostButton.BackColor = Color.Empty;
51  
52 ConnectButton.Enabled = true;
53 return;
54 }
55  
56 if (!ValidateAddressPort(out var ipAddress, out var port))
57 return;
58  
59 // Start the MQTT server.
60 await StartServer(ipAddress, port);
61 toolStripStatusLabel.Text = Properties.Strings.Server_started;
62 HostButton.BackColor = Color.Aquamarine;
63  
64 ConnectButton.Enabled = false;
65 }
66  
67 private bool ValidateAddressPort(out IPAddress address, out int port)
68 {
69 address = IPAddress.Any;
70 port = 0;
71  
72 if (string.IsNullOrEmpty(Address.Text) &&
73 string.IsNullOrEmpty(Port.Text))
74 {
75 Address.BackColor = Color.LightPink;
76 Port.BackColor = Color.LightPink;
77 return false;
78 }
79  
80 if (!IPAddress.TryParse(Address.Text, out address))
81 {
82 Address.BackColor = Color.LightPink;
83 return false;
84 }
85  
86 if (!uint.TryParse(Port.Text, out var uPort))
87 {
88 Port.BackColor = Color.LightPink;
89 return false;
90 }
91  
92 port = (int) uPort;
93  
94 Address.BackColor = Color.Empty;
95 Port.BackColor = Color.Empty;
96  
97 return true;
98 }
99  
100 private async Task StartServer(IPAddress ipAddress, int port)
101 {
102 await MQTTServerSemaphore.WaitAsync();
103 try
104 {
105 await MQTTServer.Start(ipAddress, port);
106 }
107 finally
108 {
109 MQTTServerSemaphore.Release();
110 }
111 }
112  
113 private async Task StopServer()
114 {
115 await MQTTServerSemaphore.WaitAsync();
116 try
117 {
118 await MQTTServer.Stop();
119 }
120 finally
121 {
122 MQTTServerSemaphore.Release();
123 }
124 }
125  
126 private async void ConnectButtonClickAsync(object sender, EventArgs e)
127 {
128 if (MQTTClient.ClientRunning)
129 {
130 await StopClient();
131 ConnectButton.Text = Properties.Strings.Connect;
132 ConnectButton.BackColor = Color.Empty;
133  
134 HostButton.Enabled = true;
135 return;
136 }
137  
138 if (!ValidateAddressPort(out var ipAddress, out var port))
139 return;
140  
141 await StartClient(ipAddress, port);
142  
143 toolStripStatusLabel.Text = Properties.Strings.Client_started;
144 ConnectButton.Text = Properties.Strings.Disconnect;
145 ConnectButton.BackColor = Color.Aquamarine;
146  
147 HostButton.Enabled = false;
148 }
149  
150 private async Task StopClient()
151 {
152 await MQTTClientSemaphore.WaitAsync();
153 try
154 {
155 await MQTTClient.Stop();
156 }
157 finally
158 {
159 MQTTClientSemaphore.Release();
160 }
161 }
162  
163 private async Task StartClient(IPAddress ipAddress, int port)
164 {
165 await MQTTClientSemaphore.WaitAsync();
166 try
167 {
168 await MQTTClient.Start(ipAddress, port);
169 }
170 finally
171 {
172 MQTTClientSemaphore.Release();
173 }
174 }
175 }
176 }