-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
507 lines (384 loc) · 15.5 KB
/
MainWindow.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
using System;
using System.Net.NetworkInformation;
using System.Text;
using System.Windows.Threading;
using System.Windows;
using System.Threading;
using System.ComponentModel;
using System.Speech.Synthesis;
using System.Collections.Generic;
using System.Windows.Controls.Primitives;
using System.Windows.Controls;
namespace DoPing
{
public class error
{
public string returnMessage { get; set; }
public int bg_status { get; set; }
public bool is_alarm_on { get; set; }
public bool user_stopped_the_alarm { get; set; }
public bool is_ttl_checked ;
public bool is_rtt_checked ;
public bool is_len_checked ;
public int network_gone;
public int max_packet_lost_count;
public DateTime ? last_timeout;
public List<string> packet_lost_error_list = new List<string>(30);
public error()
{
is_len_checked = false;
is_rtt_checked = true;
is_ttl_checked = false;
returnMessage = "";
bg_status = 0;
is_alarm_on = false;
user_stopped_the_alarm = false;
network_gone = 0;
max_packet_lost_count = 3;
packet_lost_error_list.Clear();
}
}
public partial class MainWindow : Window
{
string hostname = "192.168.3.10";
int timeout = 3000;
string data = "somedummydata";
bool has_ping_started = false;
bool is_reset = false;
SpeechSynthesizer synth = new SpeechSynthesizer();
PingOptions options = new PingOptions(64, true);
DispatcherTimer timer = new DispatcherTimer();
BackgroundWorker bg = new BackgroundWorker();
error r_obj = new error();
bool win_closed_clicked=false;
Dictionary<int, string> custom_error_sound = new Dictionary<int, string>();
public MainWindow()
{
InitializeComponent();
stop_alarm_btn.Visibility = Visibility.Hidden;
txt1.Text = hostname;
custom_error_sound.Add(1, "request is timed out");
custom_error_sound.Add(2, "ttl reached 0");
custom_error_sound.Add(3, "packet size is too long");
custom_error_sound.Add(4, "no resources");
custom_error_sound.Add(5, "ping is prohibated");
mtu_box.Text = "32";
timeout_box.Text ="3";
notify_box.Text = r_obj.max_packet_lost_count.ToString();
bg.DoWork += new DoWorkEventHandler(PingMe);
bg.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
bg.WorkerSupportsCancellation = true;
}
private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (list1.Items.Count == 21)
list1.Items.Clear();
list1.Items.Add(((error)e.Result).returnMessage);
list1.SelectedIndex = list1.Items.Count - 1;
list1.ScrollIntoView(list1.SelectedIndex);
r_obj.returnMessage = string.Empty;
bool is_alarm_on= ((error)e.Result).is_alarm_on;
if(is_reset)
{
list1.Items.Add("Thread cancelled");
return;
}
if(is_alarm_on && r_obj.network_gone>r_obj.max_packet_lost_count)
stop_alarm_btn.Visibility = Visibility.Visible;
else
stop_alarm_btn.Visibility = Visibility.Hidden;
if (r_obj.last_timeout!=null)
{
lst_time_out.Text = r_obj.last_timeout.ToString() +" "+r_obj.network_gone+" "+"times";
}
if (!win_closed_clicked)
bg.RunWorkerAsync(r_obj);
}
private void PingMe(object sender, DoWorkEventArgs e)
{
Thread.Sleep(1500);
byte[] buffer = Encoding.ASCII.GetBytes(data);
string returnMessage= string.Empty;
int status = 0;
bool user_stopped_alarm = r_obj.user_stopped_the_alarm;
Ping ping = new Ping();
try
{
PingReply reply = ping.Send(hostname, timeout,buffer,options);
if (reply.Status == IPStatus.Success)
{
returnMessage = "reply from" + " " + reply.Address;
if (r_obj.is_len_checked)
returnMessage= returnMessage + " "+"bytes=" + " " + reply.Buffer.Length;
if(r_obj.is_rtt_checked)
returnMessage = returnMessage +" "+ "time=" + " " + reply.RoundtripTime+"ms";
if (r_obj.is_ttl_checked)
returnMessage = returnMessage + " "+ "ttl=" + " " + reply.Options.Ttl;
//list1.Items.Add(returnMessage);
status = 0;
r_obj.is_alarm_on = false;
if (r_obj.last_timeout != null)
{
r_obj.packet_lost_error_list.Add("timed out at" + " " + r_obj.last_timeout.ToString() + " " + r_obj.network_gone + " " + "times".ToString());
r_obj.last_timeout = null;
}
r_obj.network_gone = 0;
}
else if(reply.Status == IPStatus.TimedOut)
{
returnMessage= "time out at"+" " + DateTime.Now.ToLocalTime().ToString() ;
status = 1;
r_obj.network_gone++;
r_obj.last_timeout = DateTime.Now.ToLocalTime();
// r_obj.packet_lost_error_list.Add("timed out at"+" "+ DateTime.Now.ToLongTimeString().ToString());
}
else if (reply.Status == IPStatus.TimeExceeded)
{
returnMessage="TTL reaches at 0" +" "+ DateTime.Now.ToLocalTime().ToString();
status = 2;
r_obj.network_gone++;
r_obj.last_timeout = DateTime.Now.ToLocalTime();
// r_obj.packet_lost_error_list.Add("timed out at" + " " + DateTime.Now.ToLongTimeString().ToString());
}
else if (reply.Status == IPStatus.PacketTooBig)
{
returnMessage="packet too big" + DateTime.Now.ToLocalTime().ToString();
status = 3;
r_obj.network_gone++;
r_obj.last_timeout = DateTime.Now.ToLocalTime();
// r_obj.packet_lost_error_list.Add("timed out at" + " " + DateTime.Now.ToLongTimeString().ToString());
}
else if (reply.Status == IPStatus.NoResources)
{
returnMessage="no resource....." + DateTime.Now.ToLocalTime().ToString();
status = 4;
r_obj.network_gone++;
r_obj.last_timeout = DateTime.Now.ToLocalTime();
// r_obj.packet_lost_error_list.Add("timed out at" + " " + DateTime.Now.ToLongTimeString().ToString());
}
else if (reply.Status == IPStatus.DestinationProhibited)
{
returnMessage="ping is prohibated" + DateTime.Now.ToLocalTime().ToString();
status = 5;
r_obj.network_gone++;
r_obj.last_timeout = DateTime.Now.ToLocalTime();
// r_obj.packet_lost_error_list.Add("timed out at" + " " + DateTime.Now.ToLongTimeString().ToString());
}
else
{
returnMessage = reply.Status.ToString();
status = 99;
r_obj.network_gone++;
r_obj.last_timeout = DateTime.Now.ToLocalTime();
// r_obj.packet_lost_error_list.Add("timed out at" + " " + DateTime.Now.ToLongTimeString().ToString());
}
r_obj.returnMessage = returnMessage;
r_obj.bg_status = status;
e.Result = r_obj;
if (status == 99 && !user_stopped_alarm && r_obj.network_gone>r_obj.max_packet_lost_count)
{
synth.Speak(returnMessage);
r_obj.is_alarm_on = true;
}
else if (status != 0 && !user_stopped_alarm && r_obj.network_gone > r_obj.max_packet_lost_count)
{
synth.Speak(custom_error_sound[status]);
r_obj.is_alarm_on = true;
}
}
catch (PingException ex)
{
returnMessage = ex.Message;
r_obj.returnMessage = returnMessage;
r_obj.bg_status = 100;
r_obj.is_alarm_on = true;
r_obj.network_gone++;
e.Result = r_obj;
r_obj.last_timeout = DateTime.Now.ToLocalTime();
// r_obj.packet_lost_error_list.Add("timed out at" + " " + DateTime.Now.ToLongTimeString().ToString());
if (!user_stopped_alarm && r_obj.network_gone > r_obj.max_packet_lost_count)
synth.Speak(r_obj.returnMessage);
}
}
private void Button_Click(object sender, RoutedEventArgs e)
{
int mtu = 0;
int timeout=0;
int max_p_lost_count = r_obj.max_packet_lost_count;
if (mtu_box.Text.Length > 0)
{
try
{
mtu = Convert.ToInt32(mtu_box.Text);
if (mtu < 0)
{
list1.Items.Add("packet size can not be negative");
return;
}
else if (mtu > 65535)
{
list1.Items.Add("packet size should bnot be greater than 65535 byte");
return;
}
else
{
char[] b = new char[mtu];
String buffer_msg = new string(b);
data = buffer_msg;
}
}
catch
{
list1.Items.Add("packet size shoud be an integer");
return;
}
//
}
if (timeout_box.Text.Length > 0)
{
try
{
timeout = Convert.ToInt32(timeout_box.Text);
if (timeout < 0)
{
list1.Items.Add("reply timeout period can not be negative");
return;
}
else if (timeout > 10)
{
list1.Items.Add("reply timeout period is too long. should be less than 10 sec.");
return;
}
else
{
this.timeout = timeout;
}
}
catch
{
list1.Items.Add("reply timeout period shoud be an integer");
return;
}
//
}
if (notify_box.Text.Length > 0)
{
try
{
max_p_lost_count = Convert.ToInt32(notify_box.Text);
if (max_p_lost_count < 0)
{
list1.Items.Add("packet lost notifying counter can not be negative");
return;
}
else
{
r_obj.max_packet_lost_count = max_p_lost_count;
}
}
catch
{
list1.Items.Add("packet lost notifying counter should be an integer");
return;
}
//
}
if (txt1.Text.Length>0)
{
hostname = txt1.Text;
}
if(!bg.IsBusy)
{
ping_btn.IsEnabled = false;
is_reset = false;
txt1.IsEnabled = false;
has_ping_started = true;
list1.Items.Add("pinging started with message size" + " " + data.Length + " " + "and timeout" + " " + timeout);
list1.Items.Add("we will notify you if continiously"+" "+r_obj.max_packet_lost_count+" "+" packet is lost");
bg.RunWorkerAsync();
}
else
{
synth.Speak("background thread is busy please open new instance");
}
}
private void win_Loaded(object sender, RoutedEventArgs e)
{
}
private void ttl_chkbox_Checked(object sender, RoutedEventArgs e)
{
r_obj.is_ttl_checked = true;
}
private void rtt_chkbox_Checked(object sender, RoutedEventArgs e)
{
r_obj.is_rtt_checked = true;
}
private void len_chkbox_Checked(object sender, RoutedEventArgs e)
{
r_obj.is_len_checked = true;
}
private void ttl_chkbox_Unchecked(object sender, RoutedEventArgs e)
{
r_obj.is_ttl_checked = false;
}
private void rtt_chkbox_Unchecked(object sender, RoutedEventArgs e)
{
r_obj.is_rtt_checked = false;
}
private void len_chkbox_Unchecked(object sender, RoutedEventArgs e)
{
r_obj.is_len_checked = false;
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
MainWindow win = new MainWindow();
win.Show();
}
private void stop_alarm_btn_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("alarm is stopped. now reset so that it will notify you again");
r_obj.user_stopped_the_alarm = true;
}
private void win_Closing(object sender, CancelEventArgs e)
{
bg.CancelAsync();
win_closed_clicked = true;
}
private void Button_Click_2(object sender, RoutedEventArgs e)
{//reset function
if (has_ping_started)
{
txt1.IsEnabled = true;
this.data = string.Empty;
is_reset = true;
this.timeout = 3000;
r_obj = new error();
r_obj.packet_lost_error_list.Clear();
ping_btn.IsEnabled = true;
list1.Items.Clear();
has_ping_started = false;
}
else
{
MessageBox.Show("pinging has not been started yet");
}
}
private void Button_Click_3(object sender, RoutedEventArgs e)
{
//history button
history win2 = new history();
if (r_obj.packet_lost_error_list.Count ==0)
{
win2.history_list.Items.Add("NO HISTORY FOUND");
}
else
{
foreach(string s in r_obj.packet_lost_error_list)
{
win2.history_list.Items.Add(s);
}
}
win2.Show();
}
}
}