forked from NimmLor/IoT-Audio-Visualization-Center
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Networkscanner.xaml.cs
317 lines (284 loc) · 11.1 KB
/
Networkscanner.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
using MahApps.Metro.Controls;
using MahApps.Metro.Controls.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace Analyzer
{
public partial class Networkscanner : MetroWindow
{
DispatcherTimer timer;
int ScanCount;
int btnState = 0;
List<ManualResetEvent> doneEvents = new List<ManualResetEvent>();
public Networkscanner()
{
InitializeComponent();
dgDevices.ItemsSource = MyUtils.NetworkDevices;
//ThreadPool.SetMaxThreads(2, 2);
}
public bool InputCheck(string a, string b)
{
bool success = true;
int frm = 0;
int end = 0;
try
{
if (a.Substring(0, a.LastIndexOf('.')) != b.Substring(0, a.LastIndexOf('.')))
success = false;
if (a.Count(f => f == '.') != b.Count(f => f == '.'))
success = false;
for (int i = 0; i <= 1; i++)
{
string input;
IPAddress address;
if (i == 0) input = a; else input = b;
if (IPAddress.TryParse(input, out address))
{
switch (address.AddressFamily)
{
case System.Net.Sockets.AddressFamily.InterNetwork:
break;
case System.Net.Sockets.AddressFamily.InterNetworkV6:
success = false;
MessageBox.Show("Unfortunatly IPv6 is not supported.");
break;
default:
success = false;
break;
}
}
}
frm = int.Parse(a.Substring(a.LastIndexOf('.') + 1));
end = int.Parse(b.Substring(b.LastIndexOf('.') + 1));
if (end < frm) success = false;
}
catch (Exception ex)
{
success = false;
}
return success;
}
public void StartTimer()
{
timer = new DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(20);
timer.Tick += timer_Tick;
timer.Start();
}
void timer_Tick(object sender, EventArgs e)
{
dgDevices.ItemsSource = null;
dgDevices.ItemsSource = MyUtils.NetworkDevices;
bool isDone = true;
int removedDevs = 0;
try
{
foreach (MyUtils.NetworkDevice test in MyUtils.NetworkDevices)
{
if (test.ping < 0) isDone = false;
else if (test.allResponse == null) isDone = false;
if (isDone == false) break;
}
removedDevs += RemoveEmptyDevices();
}
catch (Exception ex)
{
isDone = false;
}
if (ScanCount == null) ScanCount = 1000;
// button Text:
string dots = "";
for (int i = 0; i < btnState; i++) dots += '.';
btnState++; if (btnState > 4) btnState = 0;
btnScan.Dispatcher.Invoke(() =>
{
btnScan.Content = "Scanning" + dots;
btnScan.IsEnabled = false;
});
if (isDone && (MyUtils.NetworkDevices.Count + removedDevs) >= ScanCount)
{
timer.Stop();
btnScan.Dispatcher.Invoke(() =>
{
btnScan.Content = "Start Networkscan";
btnScan.IsEnabled = true;
});
btnCancel.Dispatcher.Invoke(() =>
{
btnCancel.IsEnabled = false;
});
}
}
public List<MyUtils.NetworkDevice> FindDevs(string ipFrom, string ipTo, DataGrid dg, Button btn) //
{
List<MyUtils.NetworkDevice> dev = new List<MyUtils.NetworkDevice>();
int lastF = ipFrom.LastIndexOf(".");
int lastT = ipTo.LastIndexOf(".");
string frm = ipFrom.Substring(lastF + 1);
string tto = ipTo.Substring(lastT + 1);
btn.Dispatcher.Invoke(() =>
{
btn.Content = "Scanning";
});
for (int i = int.Parse(frm); i <= int.Parse(tto); i++)
{
try
{
string address = ipTo.Substring(0, lastT + 1);
System.Diagnostics.Debug.WriteLine(ipTo.Substring(0, lastT + 1) + i);
doneEvents.Add(new ManualResetEvent(false));
MyUtils.NetworkDevice nd = new MyUtils.NetworkDevice(address + i, doneEvents[doneEvents.Count - 1]);
ThreadPool.QueueUserWorkItem(nd.ThreadPoolCallback);
dev.Add(nd);
}
catch (SocketException ex)
{
}
catch (Exception ex)
{
}
}
return dev;
}
public static int RemoveEmptyDevices(bool removeUnscanned = false)
{
List<MyUtils.NetworkDevice> toRemove = new List<MyUtils.NetworkDevice>();
foreach (MyUtils.NetworkDevice n in MyUtils.NetworkDevices)
{
if (n.ping == -1) toRemove.Add(n);
else if (removeUnscanned && n.ping < 0) toRemove.Add(n);
}
int ret = toRemove.Count;
foreach (MyUtils.NetworkDevice r in toRemove)
{
MyUtils.NetworkDevices.Remove(r);
}
return ret;
}
// Buttons
private void BtnScan_Click(object sender, RoutedEventArgs e)
{
if (!InputCheck(IPstart.Text, IPend.Text))
{
MessageBox.Show("Invalid input");
return;
}
dgDevices.ItemsSource = MyUtils.NetworkDevices;
btnScan.IsEnabled = false;
btnCancel.IsEnabled = true;
StartTimer();
try
{
MyUtils.NetworkDevices = FindDevs(IPstart.Text, IPend.Text, dgDevices, btnScan);
}
catch (Exception ex) { MessageBox.Show("An error occured while scanning!\n\n\nError message: " + ex.Message); }
}
private void BtnRef_Click(object sender, RoutedEventArgs e)
{
dgDevices.ItemsSource = null;
dgDevices.ItemsSource = MyUtils.NetworkDevices;
}
private void BtnCancel_Click(object sender, RoutedEventArgs e)
{
foreach (ManualResetEvent mre in doneEvents)
{
mre.Set();
}
RemoveEmptyDevices(true);
timer.Stop();
dgDevices.ItemsSource = null;
dgDevices.ItemsSource = MyUtils.NetworkDevices;
btnScan.IsEnabled = true;
btnScan.Content = "Start Networkscan";
btnCancel.IsEnabled = false;
}
private void BtnOpenWeb_Click(object sender, RoutedEventArgs e)
{
System.Diagnostics.Process.Start("http://" + (sender as Button).CommandParameter.ToString());
}
private void btnAddDevice_Click(object sender, RoutedEventArgs e)
{
AddDeviceDialogAsync((sender as Button).CommandParameter.ToString());
}
public async void AddDeviceDialogAsync(string ipaddr)
{
string devicename = "";
string ip = ipaddr;
int port = 0;
int lines = 32;
int smoothing = 0;
var x = new MetroDialogSettings();
x.AffirmativeButtonText = "Next";
x.NegativeButtonText = "Cancel";
devicename = await this.ShowInputAsync("New Device", "How should the device be called?", x);
if (String.IsNullOrEmpty(devicename)) return;
var exist = MyUtils.UdpDevices.Find(o => o.DeviceName == devicename);
if (exist != null)
{
await this.ShowMessageAsync("Error!", "A device with that name already exists!");
return;
}
string porttext = "";
x.DefaultText = "4210";
porttext = await this.ShowInputAsync("New Device", "On what UDP-Port should the data be sent?", x);
if (!int.TryParse(porttext, out port)) port = -1;
while (port <= 0)
{
porttext = await this.ShowInputAsync("Invalid Number!", "On what UDP-Port should the data be sent?", x);
if (String.IsNullOrEmpty(porttext)) return;
if (!int.TryParse(porttext, out port)) port = -1;
}
x.DefaultText = "2";
string smtext = "";
smtext = await this.ShowInputAsync("New Device", "How much should the spectrum be smoothed?", x);
if (!int.TryParse(smtext, out smoothing)) smoothing = -1;
while ((smoothing <= 0 || smoothing > 100))
{
smtext = await this.ShowInputAsync("Invalid Number!", "How much should the spectrum be smoothed?", x);
if (String.IsNullOrEmpty(smtext)) return;
if (!int.TryParse(smtext, out smoothing)) smoothing = -1;
}
x.AffirmativeButtonText = "Add Device";
x.NegativeButtonText = "Cancel Device Creation";
var res = await this.ShowMessageAsync("Confirm", "Name: " + devicename + "\nIP-Address: " + ip + "\nLines: " + lines, MessageDialogStyle.AffirmativeAndNegative, x);
if (res == MessageDialogResult.Negative) return;
try
{
UdpDevice newDev = new UdpDevice(devicename, ip, port, lines, smoothing);
newDev.Smooth = true;
MyUtils.UdpDevices.Add(newDev);
foreach (Window window in Application.Current.Windows)
{
if (window.GetType() == typeof(MainWindow))
{
(window as MainWindow).Save();
(window as MainWindow).RefreshDeviceList();
}
}
await this.ShowMessageAsync("Success!", "Device created successfully!");
}
catch
{
await this.ShowInputAsync("Error", "Something went wrong while creating the new device!");
}
}
}
}