-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1 test.cs
615 lines (538 loc) · 24.6 KB
/
Form1 test.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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
using System;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Timers;
using System.Net.NetworkInformation;
namespace Network_Tool
{
public partial class Form1 : Form
{
/// <summary>
/// intial startup processes, includes clearing volatile databases and eneabling/disabling specific tools.
/// </summary>
int Click = 0;
int tick = 0;
static System.Timers.Timer Time;
static System.Timers.Timer Holder;
public Form1()
{
InitializeComponent();
Change.Enabled = false;
Start.Enabled = false;
Halt.Enabled = false;
DataTable dt = new DataTable();
SqlConnection con = new SqlConnection("Data Source=databaseconn.database.windows.net;Initial Catalog=database conn;Integrated Security=False;User ID=ADMIN!;Password=ABCDEFG1!;Connect Timeout=30;Encrypt=True;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
SqlCommand clearHop = new SqlCommand("delete from hops", con);
SqlCommand getTick = new SqlCommand("select * from tick");
getTick.Connection = con;
SqlDataAdapter adapt = new SqlDataAdapter(getTick);
con.Open();
clearHop.ExecuteNonQuery();
adapt.Fill(dt);
con.Close();
int Gtick = Convert.ToInt32(dt.Rows[0][0].ToString());
pastupdate();
tick = tick + Gtick;
}
public void pastupdate()
{
SqlConnection con = new SqlConnection("Data Source=databaseconn.database.windows.net;Initial Catalog=database conn;Integrated Security=False;User ID=ADMIN!;Password=ABCDEFG1!;Connect Timeout=30;Encrypt=True;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
DataTable build = new DataTable();
build.Columns.Add("Adress", typeof(string));
build.Columns.Add("Date", typeof(string));
build.Columns.Add("Time", typeof(string));
SqlCommand buildpast = new SqlCommand("Select * from previous order by code");
buildpast.Connection = con;
SqlDataAdapter adapt = new SqlDataAdapter(buildpast);
con.Open();
adapt.Fill(build);
con.Close();
PastData.DataSource = build;
PastData.Update();
}
private void TestConn_Click(object sender, EventArgs e)
{
///testing the users inputs for the tool are valid. includes use of component 2
Adress.ReadOnly = true;
Interval.ReadOnly = true;
SqlConnection con = new SqlConnection("Data Source=databaseconn.database.windows.net;Initial Catalog=database conn;Integrated Security=False;User ID=ADMIN!;Password=ABCDEFG1!;Connect Timeout=30;Encrypt=True;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
try
{
if (Convert.ToInt32(Interval.Text) > 0)
{
TestConn.Enabled = false;
Change.Enabled = true;
Start.Enabled = true;
int test = Component2.Testcon(Adress.Text, Interval.Text);
if (test == 0)
{
ConnStat.Text = "Connected to" + Adress.Text;
}
if (test == 1)
{
ConnStat.Text = "Failed to connect to" + Adress.Text;
Start.Enabled = false;
Interval.ReadOnly = false;
Adress.ReadOnly = false;
Change.Enabled = true;
TestConn.Enabled = true;
}
if (test == 2)
{
ConnStat.Text = "Invalid IP adress";
TestConn.Enabled = true;
Start.Enabled = false;
Interval.ReadOnly = false;
Adress.ReadOnly = false;
Change.Enabled = true;
}
}
else
{
MessageBox.Show("Invalid Interval Entered");
TestConn.Enabled = true;
Start.Enabled = false;
Interval.ReadOnly = false;
Adress.ReadOnly = false;
Change.Enabled = true;
}
}
catch
{
MessageBox.Show("Invalid interval");
TestConn.Enabled = false;
Change.Enabled = true;
}
}
private void label2_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
///function for selection of the halt button.
Time.Stop();
tick += 1;
Halt.Enabled = false;
TestStat.Text = "Not Running";
Holder = new System.Timers.Timer(Convert.ToDouble(5000));
Holder.Elapsed += new ElapsedEventHandler(Time_Passed);
Holder.Enabled = true;
SqlConnection con = new SqlConnection("Data Source=databaseconn.database.windows.net;Initial Catalog=database conn;Integrated Security=False;User ID=ADMIN!;Password=ABCDEFG1!;Connect Timeout=30;Encrypt=True;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
SqlCommand deltick = new SqlCommand("delete from tick");
SqlDataAdapter newtick = new SqlDataAdapter();
newtick.InsertCommand = new SqlCommand("Insert into tick(tick) values (@tick)",con);
newtick.InsertCommand.Parameters.Add("@tick", SqlDbType.Int).Value = tick;
deltick.Connection = con;
con.Open();
deltick.ExecuteNonQuery();
newtick.InsertCommand.ExecuteNonQuery();
Holder.Start();
}
private void Time_Passed(object sender, ElapsedEventArgs e)
{
///using a timer to run selected functions in timed intervals
if (Click < 1)
{
Start.Invoke(new Action(() => Start.Enabled = true));
Change.Invoke(new Action(() => Change.Enabled = true));
Sync.Invoke(new Action(() => Sync.Enabled = true));
Click += 1;
}
else
{
KILL();
}
}
public void KILL()
{
///function to halt the timer "holder"
Holder.Stop();
Click = 0;
}
private void Start_Click(object sender, EventArgs e)
{
///initiating the test: ensuring the graph is clear, other controls are disabled to the user and adding the required data to the database
ClearData();
string date = DateTime.Now.Date.ToString();
string time = DateTime.Now.Hour.ToString();
SqlConnection Conn = new SqlConnection("Data Source=databaseconn.database.windows.net;Initial Catalog=database conn;Integrated Security=False;User ID=ADMIN!;Password=ABCDEFG1!;Connect Timeout=30;Encrypt=True;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
SqlDataAdapter addPast = new SqlDataAdapter();
addPast.InsertCommand = new SqlCommand("insert into previous(Adress, date, time,code) values (@host, @date, @time, @code)",Conn);
addPast.InsertCommand.Parameters.Add("@host", SqlDbType.Text).Value = Adress.Text.ToString();
addPast.InsertCommand.Parameters.Add("@date", SqlDbType.Text).Value = date;
addPast.InsertCommand.Parameters.Add("@time", SqlDbType.Text).Value = time;
addPast.InsertCommand.Parameters.Add("@code", SqlDbType.Int).Value = tick;
Conn.Open();
addPast.InsertCommand.ExecuteNonQuery();
Conn.Close();
///function for clicking the start button for the tests and the events that follow
Sync.Enabled = false;
TestStat.Text = "Running";
Start.Enabled = false;
Change.Enabled = false;
Halt.Enabled = true;
Time = new System.Timers.Timer(Convert.ToDouble(Interval.Text));
Time.Elapsed += new ElapsedEventHandler(Time_Elapsed);
Time.Enabled = true;
Time.Start();
}
public void clear()
{
///clearing the graph, invokes used due to multiple threads in execution
if (this.LATENTCHART.InvokeRequired)
{
LATENTCHART.Invoke(new Action(() => LATENTCHART.Series["Final Latency(ms)"].Points.Clear()));
LATENTCHART.Invoke(new Action(() => LATENTCHART.Series["Min Latency(ms)"].Points.Clear()));
LATENTCHART.Invoke(new Action(() => LATENTCHART.Series["Max Latency(ms)"].Points.Clear()));
LATENTCHART.Invoke(new Action(() => LATENTCHART.Series["Average Latency(ms)"].Points.Clear()));
LATENTCHART.Invoke(new Action(() => LATENTCHART.Series["Jitter"].Points.Clear()));
LATENTCHART.Invoke(new Action(() => LATENTCHART.Series["Packet loss"].Points.Clear()));
}
else
{
LATENTCHART.Series["Final Latency(ms)"].Points.Clear();
LATENTCHART.Series["Min Latency(ms)"].Points.Clear();
LATENTCHART.Series["Max Latency(ms)"].Points.Clear();
LATENTCHART.Series["Average Latency(ms)"].Points.Clear();
LATENTCHART.Series["Jitter"].Points.Clear();
LATENTCHART.Series["Packet loss"].Points.Clear();
}
}
private void Time_Elapsed(object sender, ElapsedEventArgs e)
{
///Primary timer for providing interleaved function execution
///function for actualy running the network trace tool in component 3/4
string target = Adress.Text;
string INT = Interval.Text;
while (Halt.Enabled == true)
{
///running the trace tool in component 3, clearing the graph and redrawing the points.
Component3.Times(target, INT, tick);
clear();
string date = DateTime.Now.Date.ToString();
string adress = Adress.Text.ToString();
string hour = DateTime.Now.Hour.ToString();
drawTrace(date, adress, hour);
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void Sync_Click(object sender, EventArgs e)
{
bool Valid = false;
if (CODE.Text == "")
{
if (CODE.Text == "" && GraphAddress.Text == "" && GraphDate.Text == "" && GraphHour.Text == "")
{
MessageBox.Show("Please fill in required the fields");
}
if (GraphHour.Text != "" && GraphDate.Text != "" && GraphAddress.Text == "")
{
MessageBox.Show("Please specify the target adress");
}
if (CODE.Text != "")
{
Valid = true;
}
if (GraphAddress.Text == "")
{
MessageBox.Show("Please enter an adress");
}
if (GraphDate.Text == "")
{
MessageBox.Show("Please enter a date");
}
if (GraphHour.Text == "")
{
MessageBox.Show("Please enter an hour");
}
else
{
Valid = true;
}
if (Valid == true)
{
syncauthenticate();
}
}
else
{
syncauthenticate();
}
}
public void syncauthenticate()
{
GraphAddress.ReadOnly = true;
GraphDate.ReadOnly = true;
GraphHour.ReadOnly = true;
CODE.ReadOnly = true;
SqlConnection connection = new SqlConnection("Data Source=databaseconn.database.windows.net;Initial Catalog=database conn;Integrated Security=False;User ID=ADMIN!;Password=ABCDEFG1!;Connect Timeout=30;Encrypt=True;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
SqlCommand trim = new SqlCommand("Delete from data where convert(varchar,hop) = ''");
trim.Connection = connection;
connection.Open();
trim.ExecuteNonQuery();
connection.Close();
///function used to sync past data to graph
clear();
string date = GraphDate.Text.ToString();
string adress = GraphAddress.Text.ToString();
string hour = GraphHour.Text.ToString();
date = date + " 00:00:00";
TestConn.Enabled = false;
drawTrace(date, adress, hour);
}
private void Change_Click(object sender, EventArgs e)
{
//enabling and disabling specific buttons when the user opts to change the target adress or interval.
Adress.ReadOnly = false;
Interval.ReadOnly = false;
Start.Enabled = false;
Halt.Enabled = false;
TestConn.Enabled = true;
Change.Enabled = false;
}
private void label7_Click(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void TraceText_Click(object sender, EventArgs e)
{
}
private void label7_Click_1(object sender, EventArgs e)
{
}
private void TraceText_TextChanged(object sender, EventArgs e)
{
}
public void drawTrace(string date, string adress, string hour)
{
///custom written aggregation algorithm
///function used for real time tracing of a ping and displaying the points in a text box on the GUI
///establishing the connection the the database
SqlConnection Conn = new SqlConnection("Data Source=databaseconn.database.windows.net;Initial Catalog=database conn;Integrated Security=False;User ID=ADMIN!;Password=ABCDEFG1!;Connect Timeout=30;Encrypt=True;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
DataTable data = new DataTable();
SqlCommand graph = new SqlCommand();
graph.Connection = Conn;
string tickcode = CODE.Text.ToString();
graph.CommandText = "select * from data where CONVERT(varchar, host) like '%" + adress + "%' and CONVERT(varchar, date) like '%" + date + "%' and CONVERT(varchar, time) like '%" + hour + "%' and Convert(varchar,code) like '%" + tickcode + "%' order by seq";
graph.CommandType = CommandType.Text;
try
{
Conn.Open();
}
catch
{
MessageBox.Show("Database Error");
}
///collecting the required data from the database
SqlDataAdapter fill = new SqlDataAdapter(graph);
fill.Fill(data);
Conn.Close();
DataView view = new DataView(data);
DataTable distinct = view.ToTable(true, "hop");
DataRow[] selectunique;
selectunique = distinct.Select();
DataTable dt = new DataTable();
dt.Columns.Add("HopNum", typeof(int));
dt.Columns.Add("Adress", typeof(string));
dt.Columns.Add("Final latency(ms)", typeof(int));
dt.Columns.Add("Min latency(ms)", typeof(int));
dt.Columns.Add("Max latency(ms)", typeof(int));
dt.Columns.Add("average latency(ms)", typeof(int));
dt.Columns.Add("Packet Loss", typeof(int));
dt.Columns.Add("Jitter", typeof(int));
//the process for selecting the data in the data table, grouping the points together and generating important data points
int x = 0;
for (int i = 0; i < selectunique.Length; i++)
{
string unique = selectunique[i][0].ToString();
if (unique != "X")
{
x += 1;
DataRow[] selected;
selected = data.Select("hop = '" + unique + "'");
int mxlat = 0;
int mnlat = 2000;
int avelat = 0;
int count = 0;
int totlat = 0;
int jitter = 0;
int totPL = 0;
for (int tick = 0; tick < selected.Length; tick++)
{
int lat1 = Convert.ToInt32(selected[tick][1]);
int lat2 = Convert.ToInt32(selected[tick][2]);
int latency = lat2 - lat1;
count++;
if (latency < 0)
{
latency = 1000 + latency;
}
if (latency > mxlat)
{
mxlat = latency;
}
if (latency < mnlat)
{
mnlat = latency;
}
totPL = totPL + Convert.ToInt32(selected[tick][8]);
totlat = totlat + latency;
}
avelat = totlat / count;
int finalLat = Convert.ToInt32(selected[selected.Length - 1][2]) - Convert.ToInt32(selected[selected.Length - 1][1]);
if (finalLat < 0)
{
finalLat = 1000 + finalLat;
}
jitter = totPL / count;
int PL = Convert.ToInt32(selected[selected.Length - 1][8]);
dt.Rows.Add(x, unique, finalLat, mnlat, mxlat, avelat, PL, jitter);
}
}
//adding data to the text box, use of an invoke if the application is able to run over multiple threads
DataSet Dset = new DataSet();
Dset.Clear();
Dset.Tables.Add(dt);
TRACETEXT.DataSource = dt;
LATENTCHART.DataSource = dt;
LATENTCHART.Series["Final Latency(ms)"].XValueMember = "Adress";
LATENTCHART.Series["Final Latency(ms)"].YValueMembers = "Final latency(ms)";
LATENTCHART.Series["Min Latency(ms)"].XValueMember = "Adress";
LATENTCHART.Series["Min Latency(ms)"].YValueMembers = "Min latency(ms)";
LATENTCHART.Series["Max Latency(ms)"].XValueMember = "Adress";
LATENTCHART.Series["Max Latency(ms)"].YValueMembers = "Max latency(ms)";
LATENTCHART.Series["Average Latency(ms)"].XValueMember = "Adress";
LATENTCHART.Series["Average Latency(ms)"].YValueMembers = "average latency(ms)";
LATENTCHART.Series["Packet loss"].XValueMember = "Adress";
LATENTCHART.Series["Packet loss"].YValueMembers = "Packet Loss";
LATENTCHART.Series["Jitter"].XValueMember = "Adress";
LATENTCHART.Series["Jitter"].YValueMembers = "Jitter";
LATENTCHART.ChartAreas["ChartArea1"].AxisX.Interval = 1;
LATENTCHART.Update();
}
private void UpdatePastData_Click(object sender, EventArgs e)
{
///the command behind the GUI control/button to refresh the small grid containing information on past tests
pastupdate();
}
private void PastQuery_Click(object sender, EventArgs e)
{
///the control behind the query for the past test grid
///the control takes the users input to query the database to return the data matching its parameters
string host = PastAdress.Text.ToString();
string date = PastDate.Text.ToString();
string time = PastTime.Text.ToString();
SqlConnection con = new SqlConnection("Data Source=databaseconn.database.windows.net;Initial Catalog=database conn;Integrated Security=False;User ID=ADMIN!;Password=ABCDEFG1!;Connect Timeout=30;Encrypt=True;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False");
DataTable pastquery = new DataTable();
pastquery.Columns.Add("Adress", typeof(string));
pastquery.Columns.Add("Date", typeof(string));
pastquery.Columns.Add("Time", typeof(string));
string pastcode = PastCode.Text.ToString();
SqlCommand buildpast = new SqlCommand("Select* from previous where convert(varchar, adress) like '%" + host + "%' and convert(varchar, date) like '%" + date + "%' and convert(varchar, time) like '%" + time + "%' and convert(varchar,code) like '%"+pastcode+"%' order by code ");
buildpast.Connection = con;
SqlDataAdapter adapt = new SqlDataAdapter(buildpast);
con.Open();
adapt.Fill(pastquery);
con.Close();
PastData.DataSource = pastquery;
PastData.Update();
}
private void ClearData()
{
///clearing the data from both the graph and the data grid
LATENTCHART.Series["Final Latency(ms)"].Points.Clear();
LATENTCHART.Series["Min Latency(ms)"].Points.Clear();
LATENTCHART.Series["Max Latency(ms)"].Points.Clear();
LATENTCHART.Series["Average Latency(ms)"].Points.Clear();
LATENTCHART.Series["Jitter"].Points.Clear();
LATENTCHART.Series["Packet loss"].Points.Clear();
TRACETEXT.DataSource = null;
TRACETEXT.Update();
}
private void Cleandata_Click(object sender, EventArgs e)
{
///the command behind the control to clear the data for the GUI
///Aditionaly reenables other controls to allow the user acess again
ClearData();
GraphAddress.ReadOnly = false;
GraphDate.ReadOnly = false;
GraphHour.ReadOnly = false;
CODE.ReadOnly = false;
TestConn.Enabled = true;
}
/// All following commands are the controls on the right hand side of the graph used to control the graph display
/// each chunk of code represents one of the check boxes
private void CheckPloss_CheckedChanged(object sender, EventArgs e)
{
if(LATENTCHART.Series["Packet loss"].Enabled == true)
{
LATENTCHART.Series["Packet loss"].Enabled = false;
}
else
{
LATENTCHART.Series["Packet loss"].Enabled = true;
}
}
private void HideFinalLat_CheckedChanged(object sender, EventArgs e)
{
if (LATENTCHART.Series["Final Latency(ms)"].Enabled == true)
{
LATENTCHART.Series["Final Latency(ms)"].Enabled = false;
}
else
{
LATENTCHART.Series["Final Latency(ms)"].Enabled = true;
}
}
private void HideMinimum_CheckedChanged(object sender, EventArgs e)
{
if (LATENTCHART.Series["Min Latency(ms)"].Enabled == true)
{
LATENTCHART.Series["Min Latency(ms)"].Enabled = false;
}
else
{
LATENTCHART.Series["Min Latency(ms)"].Enabled = true;
}
}
private void HideMaximum_CheckedChanged(object sender, EventArgs e)
{
if (LATENTCHART.Series["Max Latency(ms)"].Enabled == true)
{
LATENTCHART.Series["Max Latency(ms)"].Enabled = false;
}
else
{
LATENTCHART.Series["Max Latency(ms)"].Enabled = true;
}
}
private void HideAverage_CheckedChanged(object sender, EventArgs e)
{
if (LATENTCHART.Series["Average Latency(ms)"].Enabled == true)
{
LATENTCHART.Series["Average Latency(ms)"].Enabled = false;
}
else
{
LATENTCHART.Series["Average Latency(ms)"].Enabled = true;
}
}
private void HideJitter_CheckedChanged(object sender, EventArgs e)
{
if (LATENTCHART.Series["Jitter"].Enabled == true)
{
LATENTCHART.Series["Jitter"].Enabled = false;
}
else
{
LATENTCHART.Series["Jitter"].Enabled = true;
}
}
}
}