This repository was archived by the owner on May 28, 2020. It is now read-only.
forked from darktohka/NetworkSimulation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetworkObject.cs
More file actions
636 lines (541 loc) · 19.5 KB
/
Copy pathNetworkObject.cs
File metadata and controls
636 lines (541 loc) · 19.5 KB
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
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Drawing;
namespace NetworkSimulation
{
public class NetworkObject : GridObject
{
//BILL MADE THESE
private float noise;
//THESE ARE DISYER'S
[JsonProperty("objectType")]
private ObjectType objectType;
[JsonProperty("objectId")]
private int objectId;
[JsonProperty("name")]
private string name;
[JsonProperty("uploadMbps")]//USER INPUT
public double uploadMbps;
[JsonProperty("downloadMbps")]//USER INPUT
public double downloadMbps;
[JsonProperty("throttledUploadMbps")]
public double throttledUploadMbps;
[JsonProperty("throttledDownloadMbps")]
public double throttledDownloadMbps;
[JsonProperty("avgPingRate")]
private int avgPingRate;
[JsonProperty("packetLossChance")]
private int packetLossChance;
[JsonProperty("maxConnections")]//USER INPUT OR SET
private int maxConnections;
// Computers
[JsonProperty("actions")]
private List<Action> actions;
[JsonProperty("computerType")]
private ComputerType computerType;
[JsonProperty("wifiEnabled")]
private bool wifiEnabled;
// Routers and wifi extenders
[JsonProperty("wifiRange")]
private double wifiRange;
// Temporary variables
public ConnectionState connectionState;
public double wifiDropoff = 1.0D;
public NetworkObject(ObjectType objectType, int objectId, string name, int floor, int x, int y, double uploadMbps, double downloadMbps, double throttledUploadMbps, double throttledDownloadMbps, int avgPingRate, int packetLossChance, int maxConnections, List<Action> actions, ComputerType computerType, bool wifiEnabled, double wifiRange) : base(floor , x, y)
{//MOST ARE USER INPUT THRU UI===WAKE UP DISYER
this.objectType = objectType;
this.objectId = objectId;
this.name = name;
this.uploadMbps = uploadMbps;
this.downloadMbps = downloadMbps;
this.throttledUploadMbps = throttledUploadMbps;
this.throttledDownloadMbps = throttledDownloadMbps;
this.avgPingRate = avgPingRate;
this.packetLossChance = packetLossChance;
this.maxConnections = maxConnections;
this.actions = actions;
this.computerType = computerType;
this.wifiEnabled = wifiEnabled;
this.wifiRange = wifiRange;
}
// WARNING!!! CALL THIS IF ANYTHING CHANGES!!
public void RecalculateTemporary()
{
wifiDropoff = 1.0D;
connectionState = RecalcConnectionState();
}
public NetworkObject GetAssociatedRouter()
{
if (!wifiEnabled || (objectType != ObjectType.COMPUTER && objectType != ObjectType.WIFI_EXTENDER))
{
// No WIFI capability and not connected to any ethernet.
return null;
}
foreach (NetworkObject obj in Settings.GetSingleton().GetObjects())
{
if (obj.GetObjectType() == ObjectType.ROUTER || obj.GetObjectType() == ObjectType.WIFI_EXTENDER)
{
if (DistanceTo(obj) <= obj.GetWifiRange())
{
wifiDropoff = 1.0D / DistanceTo(obj);
return obj;
}
}
}
return null;
}
public List<int> GetGraphTrace()
{
List<int> fWalk = new List<int>();
fWalk.Add(GetObjectId());
if (GetObjectType() == ObjectType.MODEM)
{
return fWalk;
}
Queue<GraphWalk> walks = new Queue<GraphWalk>();
walks.Enqueue(new GraphWalk(GetObjectId(), fWalk));
while (walks.Count > 0)
{
GraphWalk walk = walks.Dequeue();
int currentId = walk.currentId;
List<NetworkObject> objects = Settings.GetSingleton().GetConnectedObjects(currentId);
foreach (NetworkObject obj in objects)
{
ObjectType type = obj.GetObjectType();
if (type == ObjectType.MODEM)
{
List<int> newList = new List<int>(walk.history);
newList.Add(currentId);
return newList;
}
if (type == ObjectType.HUB || type == ObjectType.SWITCH || type == ObjectType.POWERLINE || type == ObjectType.ROUTER)
{
if (!walk.history.Contains(currentId))
{
List<int> newList = new List<int>(walk.history);
newList.Add(currentId);
walks.Enqueue(new GraphWalk(GetObjectId(), newList));
}
}
}
}
return fWalk;
}
public ConnectionState RecalcConnectionState()
{
// If connected to a modem, we have internet
if (objectType == ObjectType.MODEM)
{
connectionState = ConnectionState.CONNECTED_CABLE;
return connectionState;
}
Queue<int> visitIds = new Queue<int>();
visitIds.Enqueue(GetObjectId());
List<int> visited = new List<int>();
while (visitIds.Count > 0)
{
int currentId = visitIds.Dequeue();
List<NetworkObject> objects = Settings.GetSingleton().GetConnectedObjects(currentId);
foreach (NetworkObject obj in objects) {
ObjectType type = obj.GetObjectType();
if (type == ObjectType.MODEM)
{
connectionState = ConnectionState.CONNECTED_CABLE;
return connectionState;
}
if (type == ObjectType.HUB || type == ObjectType.SWITCH || type == ObjectType.POWERLINE || type == ObjectType.ROUTER)
{
if (type == ObjectType.POWERLINE)
{
// For powerlines, we must check if the powerline is connected to another powerline!
List<NetworkObject> powerlineObjs = Settings.GetSingleton().GetConnectedObjects(obj.GetObjectId());
bool hasPowerline = false;
foreach (NetworkObject powerlineObj in powerlineObjs)
{
if (powerlineObj.GetObjectType() == ObjectType.POWERLINE)
{
hasPowerline = true;
break;
}
}
if (!hasPowerline)
{
continue;
}
}
if (!visited.Contains(obj.GetObjectId()))
{
visitIds.Enqueue(obj.GetObjectId());
visited.Add(obj.GetObjectId());
}
}
}
}
// If there is an associated router, we are connected through WiFi
if (GetAssociatedRouter() != null)
{
connectionState = ConnectionState.CONNECTED_WIFI;
return connectionState;
}
// Disconnected
connectionState = ConnectionState.DISCONNECTED;
return connectionState;
}
public bool IsConnectedToInternet()
{
return connectionState != ConnectionState.DISCONNECTED;
}
public string GetConnectionStateString()
{
switch (connectionState)
{
case ConnectionState.DISCONNECTED:
return "Disconnected";
case ConnectionState.CONNECTED_WIFI:
return "Connected through Wi-Fi";
case ConnectionState.CONNECTED_CABLE:
return "Connected through Ethernet";
default:
return "Disconnected";
}
}
public double DistanceTo(NetworkObject other)//BILL MADE THIS
{
return Math.Sqrt(Math.Pow(other.GetX() - GetX(), 2) + Math.Pow(other.GetY() - GetY(), 2) + Math.Pow(other.GetFloor() - GetFloor(), 2));
}
public ObjectType GetObjectType()
{
return objectType;
}
public void SetObjectType(ObjectType objectType)
{
this.objectType = objectType;
}
public int GetObjectId()
{
return objectId;
}
private double GetTrueMbps(double realMbps, double throttledMbps)
{
double mbps = throttledMbps;
if (throttledMbps <= 0)
{
mbps = realMbps;
if (GetObjectType() == ObjectType.COMPUTER)
{
mbps *= 0.9;
}
}
if (connectionState == ConnectionState.CONNECTED_WIFI)
{
mbps *= wifiDropoff;
}
return mbps;
}
public double GetTrueUploadMbps()
{
return GetTrueMbps(uploadMbps, throttledUploadMbps);
}
public double GetTrueDownloadMbps()
{
return GetTrueMbps(downloadMbps, throttledDownloadMbps);
}
public void SetUploadMbps(double uploadMbps)
{
this.uploadMbps = uploadMbps;
}
public void SetDownloadMbps(double downloadMbps)
{
this.downloadMbps = downloadMbps;
}
public void SetThrottledUploadMbps(double throttledUploadMbps)
{
this.throttledUploadMbps = throttledUploadMbps;
}
public void SetThrottledDownloadMbps(double throttledDownloadMbps)
{
this.throttledDownloadMbps = throttledDownloadMbps;
}
public double GetThrottledUploadMbps()
{
return throttledUploadMbps;
}
public double GetThrottledDownloadMbps()
{
return throttledDownloadMbps;
}
public int GetAvgPingRate()
{
return avgPingRate;
}
public void SetAvgPingRate(int avgPingRate)
{
this.avgPingRate = avgPingRate;
}
public int GetFinalPingRate()
{
if (connectionState == ConnectionState.DISCONNECTED)
{
return GetAvgPingRate();
} else if (connectionState == ConnectionState.CONNECTED_WIFI)
{
return GetAvgPingRate() + GetAssociatedRouter().GetFinalPingRate();
}
int pingRate = 0;
List<int> trace = GetGraphTrace();
foreach (int objectId in trace)
{
NetworkObject obj = Settings.GetSingleton().GetObject(objectId);
if (obj != null)
{
pingRate += obj.GetAvgPingRate();
}
}
return pingRate;
}
public int GetPacketLossChance()
{
return packetLossChance;
}
public void SetPacketLossChance(int packetLossChance)
{
this.packetLossChance = packetLossChance;
}
public int GetFinalPacketLossChance()
{
if (connectionState == ConnectionState.DISCONNECTED)
{
return GetPacketLossChance();
} else if (connectionState == ConnectionState.CONNECTED_WIFI)
{
return (GetPacketLossChance() + GetAssociatedRouter().GetFinalPacketLossChance()) / 2;
}
int allPacketLossChance = 0;
int packetLossNum = 0;
List<int> trace = GetGraphTrace();
foreach (int objectId in trace)
{
NetworkObject obj = Settings.GetSingleton().GetObject(objectId);
if (obj != null)
{
if (obj.GetPacketLossChance() >= 100)
{
return 100;
}
packetLossChance += obj.GetPacketLossChance();
packetLossNum += 1;
}
}
if (packetLossNum == 0)
{
return 0;
}
else
{
return (int) Math.Floor((double) allPacketLossChance / packetLossNum);
}
}
public int GetMaxConnections()
{
return maxConnections;
}
public void SetMaxConnections(int maxConnections)
{
this.maxConnections = maxConnections;
}
public int GetConnectionCount()
{
return Settings.GetSingleton().GetConnectedObjects(GetObjectId()).Count;
}
public List<Action> GetActions()
{
return actions;
}
public void RemoveActionAt(int index)
{
if (index < actions.Count)
{
actions.RemoveAt(index);
}
}
public void AddAction(Action action)
{
if (!actions.Contains(action))
{
actions.Add(action);
}
}
public void RemoveAction(Action action)
{
if (actions.Contains(action))
{
actions.Remove(action);
}
}
public double GetUploadMbpsUsage()
{
double mbps = 0;
foreach (Action action in actions)
{
mbps += action.GetDeltaUp();
}
return mbps;
}
public double GetDownloadMbpsUsage()
{
double mbps = 0;
foreach (Action action in actions)
{
mbps += action.GetDeltaDown();
}
return mbps;
}
// This returns the total upload mbps usage for the ENTIRE subgraph
public MbpsUsage GetTotalUploadMbpsUsage()
{
if (connectionState == ConnectionState.DISCONNECTED)
{
return new MbpsUsage(GetUploadMbpsUsage(), 0.0);
} else if (connectionState == ConnectionState.CONNECTED_WIFI)
{
MbpsUsage usage = GetAssociatedRouter().GetTotalUploadMbpsUsage();
usage.currentUsage += GetUploadMbpsUsage();
return usage;
}
double totalMbps = 0.0;
List<int> trace = GetGraphTrace();
foreach (int objectId in trace)
{
NetworkObject obj = Settings.GetSingleton().GetObject(objectId);
if (obj != null)
{
totalMbps += obj.GetUploadMbpsUsage();
}
}
double maxMbps = Settings.GetSingleton().GetObject(trace[trace.Count - 1]).GetTrueUploadMbps();
return new MbpsUsage(totalMbps, maxMbps);
}
// This returns the total download mbps usage for the ENTIRE subgraph
public MbpsUsage GetTotalDownloadMbpsUsage()
{
if (connectionState == ConnectionState.DISCONNECTED)
{
return new MbpsUsage(GetDownloadMbpsUsage(), 0.0);
} else if (connectionState == ConnectionState.CONNECTED_WIFI)
{
MbpsUsage usage = GetAssociatedRouter().GetTotalDownloadMbpsUsage();
usage.currentUsage += GetDownloadMbpsUsage();
return usage;
}
double totalMbps = 0.0;
List<int> trace = GetGraphTrace();
foreach (int objectId in trace)
{
NetworkObject obj = Settings.GetSingleton().GetObject(objectId);
if (obj != null)
{
totalMbps += obj.GetDownloadMbpsUsage();
}
}
double maxMbps = Settings.GetSingleton().GetObject(trace[trace.Count - 1]).GetTrueDownloadMbps();
return new MbpsUsage(totalMbps, maxMbps);
}
public ComputerType GetComputerType()
{
return computerType;
}
public void SetComputerType(ComputerType computerType)
{
this.computerType = computerType;
}
public bool GetWifiEnabled()
{
return wifiEnabled;
}
public void SetWifiEnabled(bool wifiEnabled)
{
this.wifiEnabled = wifiEnabled;
}
public double GetWifiRange()
{
return wifiRange;
}
public void SetWifiRange(double wifiRange)
{
this.wifiRange = wifiRange;
}
public string GetName()
{
return name;
}
public void SetName(string name)
{
this.name = name;
}
public void RemoveObject()
{
Settings.GetSingleton().RemoveObject(this);
}
public Bitmap GetImage()
{
switch (GetObjectType())
{
case ObjectType.COMPUTER:
return Properties.Resources.computer;
case ObjectType.HUB:
return Properties.Resources.hub;
case ObjectType.MODEM:
return Properties.Resources.modem;
case ObjectType.POWERLINE:
return Properties.Resources.powerline;
case ObjectType.ROUTER:
return Properties.Resources.router;
case ObjectType.SWITCH:
return Properties.Resources._switch;
case ObjectType.WIFI_EXTENDER:
return Properties.Resources.wifi;
default:
return Properties.Resources.empty;
}
}
public string GetObjectTypeName()
{
switch (GetObjectType())
{
case ObjectType.COMPUTER:
return "Computer";
case ObjectType.HUB:
return "Hub";
case ObjectType.MODEM:
return "Modem";
case ObjectType.POWERLINE:
return "Powerline";
case ObjectType.ROUTER:
return "Router";
case ObjectType.SWITCH:
return "Switch";
case ObjectType.WIFI_EXTENDER:
return "WiFi Extender";
default:
return "Unknown";
}
}
public String GetComputerTypeName()
{
switch (GetComputerType())
{
case ComputerType.MEDIA_SERVER:
return "Media Server";
case ComputerType.RASPBERRY_PI:
return "Raspberry Pi";
case ComputerType.SERVER:
return "Server";
case ComputerType.WORKSTATION:
return "Workstation";
default:
return "Unknown";
}
}
}
}