-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScenario.cs
355 lines (306 loc) · 14.9 KB
/
Scenario.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
using org.squ.md.gen.BusinessObjects;
using System;
using System.Collections.Generic;
using System.Data;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Xml;
namespace org.squ.md.gen
{
class Scenario
{
public int TrialNumber { get; set; }
public ScenarioType ScenarioType { get; set; }
public int NumberOfNodes { get; set; }
public string IpNetwork { get; set; }
public int LinkDampingFactor { get; set; }
public List<Node> Nodes;
public P2PNetwork P2PNetwork { get; set; }
public List<Link> Links;
public Dictionary<IPAddress, IPAddress> IPLinks { get; set; }
public ScenarioStatistics Statistics { get; set; }
public TextBox ConsoleScreen;
//public Scenario(TextBox consoleScreen, ScenarioType scenarioType, int trialNumber, int numberOfNodes, string ipNetwork, int linkDampingFactor)
//{
// ConsoleScreen = consoleScreen;
// TrialNumber = trialNumber;
// ScenarioType = scenarioType;
// NumberOfNodes = numberOfNodes;
// IpNetwork = ipNetwork;
// LinkDampingFactor = linkDampingFactor;
// Nodes = new List<Node>();
// Links = new List<Link>();
// IPLinks = new Dictionary<IPAddress, IPAddress>();
// Make();
//}
public Scenario(AppUIControls appSettings, int lastTrial)
{
ConsoleScreen = appSettings.ConsoleScreen;
TrialNumber = lastTrial;
ScenarioType = appSettings.ScenarioType;
IpNetwork = appSettings.SG_P2PIpNetwork.Text;
IPLinks = new Dictionary<IPAddress, IPAddress>();
P2PNetwork p2pNetwork = new P2PNetwork(IPAddress.Parse(IpNetwork));
P2PNetwork = p2pNetwork;
switch (ScenarioType)
{
case ScenarioType.Damped: case ScenarioType.Mesh:
NumberOfNodes = int.Parse(appSettings.SG_NumberOfNodes.Text);
LinkDampingFactor = int.Parse(appSettings.SG_LinkDampingFactor.Text);
Nodes = new List<Node>();
Links = new List<Link>();
Make();
break;
case ScenarioType.Waxman:
//double lambda = double.Parse(appSettings.SG_WaxMan_Lambda.Text);
//double alpha = double.Parse(appSettings.SG_WaxMan_Alpha.Text);
//double beta = double.Parse(appSettings.SG_WaxMan_Beta.Text);
//double[] domain = new double[] { double.Parse(appSettings.SG_WaxMan_XMin.Text),
// double.Parse(appSettings.SG_WaxMan_XMax.Text),
// double.Parse(appSettings.SG_WaxMan_YMin.Text),
// double.Parse(appSettings.SG_WaxMan_XMax.Text)};
Waxman waxman = new Waxman(appSettings);
NumberOfNodes = waxman.Nodes.Count;
Nodes = waxman.Nodes;
P2PNetwork = waxman.P2PNetwork;
Links = waxman.Links;
IPLinks = waxman.IPLinks;
Statistics = new ScenarioStatistics(this);
break;
default:
break;
}
}
internal void Make()
{
for (int i = 1; i <= NumberOfNodes; i++)
{
int asNumber = i;
Nodes.Add(new Node(asNumber));
}
CreateLinks();
Statistics = new ScenarioStatistics(this);
}
private void CreateLinks()
{
Random rnd = new Random();
int p2pNetworkInUse = 0;
double dfRemainder = (100d - LinkDampingFactor) / 100d;
switch (ScenarioType)
{
case ScenarioType.Damped:
foreach (Node n in Nodes)
{
int maxConnectionsDamped = (int)Math.Floor((NumberOfNodes - 1) * dfRemainder);
if (maxConnectionsDamped == 0) { maxConnectionsDamped = 1; }
int numberOfLinksToGenerate = rnd.Next(1, maxConnectionsDamped);
List<Node> nodesToBeConnectedTo = Utils.ShuffleArrayAndReturn(Nodes, numberOfLinksToGenerate, n);
foreach (Node p in nodesToBeConnectedTo)
{
ConntectNodeToNode(n, p, p2pNetworkInUse);
p2pNetworkInUse++;
}
}
break;
case ScenarioType.Mesh:
List<Node> srcNodes = Nodes;
List<Node> dstNodes = Nodes;
foreach (Node n in srcNodes)
{
foreach (Node p in dstNodes)
{
if (n != p && !isConntected(n, p))
{
ConntectNodeToNode(n, p, p2pNetworkInUse);
p2pNetworkInUse++;
}
}
}
// Apply damping by removing DF % of links
if (LinkDampingFactor > 0)
{
int maximumNumberOfPossibleLinks = ((Nodes.Count) * (Nodes.Count - 1)) / 2;
double numberOfLinksToRemove = maximumNumberOfPossibleLinks * (LinkDampingFactor / 100d);
List<Link> linksToBeRemoved = Utils.ShuffleArrayAndReturn(Links, Convert.ToInt32(numberOfLinksToRemove));
ConsoleScreen.Text += "Removing: " + linksToBeRemoved.Count + " links of total of: " + maximumNumberOfPossibleLinks + Environment.NewLine;
int numberOfSkippedLinks = DeleteLinks(linksToBeRemoved);
while (numberOfSkippedLinks != 0)
{
ConsoleScreen.Text += "Compensate skipped links ..." + Environment.NewLine;
linksToBeRemoved = Utils.ShuffleArrayAndReturn(Links, Convert.ToInt32(numberOfSkippedLinks));
ConsoleScreen.Text += "Removing: " + linksToBeRemoved.Count + " links of total of: " + maximumNumberOfPossibleLinks + Environment.NewLine;
numberOfSkippedLinks = DeleteLinks(linksToBeRemoved);
}
}
break;
}
}
private int DeleteLinks(List<Link> linksToBeRemoved)
{
// Note: Dont delete a link that will make a node disconnected or has one link only
int numberOfSkippedLinks = 0;
foreach (Link l in linksToBeRemoved)
{
Node srcNode = Nodes.Find(x => x.AsNumber == l.SourceASN);
Node dstNode = Nodes.Find(x => x.AsNumber == l.DestinationASN);
bool isLastTwoLinksForANode = (srcNode.Links.Count <= 2 || dstNode.Links.Count <= 2);
ConsoleScreen.Text += "Trying to delete a link between " + srcNode.AsNumber + " (" + srcNode.Links.Count + ") and " + dstNode.AsNumber + "(" + dstNode.Links.Count + ") ... ";
if (!isLastTwoLinksForANode)
{
// Remove from IP Links
IPLinks.Remove(l.SourceIP);
// Delete from Links
Links.Remove(l);
// Update Nodes
srcNode.Peers.Remove(dstNode);
srcNode.Links.Remove(l);
dstNode.Peers.Remove(srcNode);
Link dstLink = dstNode.Links.Find(x => x.SourceASN == l.DestinationASN && x.DestinationASN == l.SourceASN);
dstNode.Links.Remove(dstLink);
ConsoleScreen.Text += "deleted" + Environment.NewLine;
}
else
{
ConsoleScreen.Text += "skipped" + Environment.NewLine;
numberOfSkippedLinks++;
}
}
return numberOfSkippedLinks;
}
private void ConntectNodeToNode(Node n, Node p, int p2pNetworkInUse)
{
if (P2PNetwork.SubnetsFree <= 0) { P2PNetwork.Expand(); }
Link l = new Link(n.AsNumber, P2PNetwork.Subnets[p2pNetworkInUse].FirstIpAddress, p.AsNumber, P2PNetwork.Subnets[p2pNetworkInUse].SecondIpAddress);
Links.Add(l);
IPLinks.Add(P2PNetwork.Subnets[p2pNetworkInUse].FirstIpAddress, P2PNetwork.Subnets[p2pNetworkInUse].SecondIpAddress);
P2PNetwork.SubnetsFree--;
P2PNetwork.SubnetsUsed++;
n.Peers.Add(p);
n.Links.Add(l);
p.Peers.Add(n);
p.Links.Add(l.flip());
}
private bool isConntected(Node srcNode, Node dstNode)
{
bool connected = Links.Exists(x => x.SourceASN == srcNode.AsNumber && x.DestinationASN == dstNode.AsNumber) ||
Links.Exists(x => x.DestinationASN == srcNode.AsNumber && x.SourceASN == dstNode.AsNumber);
return connected;
}
internal void ExportScenarioToFiles(string outputFolder)
{
// Export Nodes Configurations
foreach (Node n in Nodes)
{
string fileName = "AS" + n.AsNumber + ".conf";
string confFile = System.IO.Path.Combine(outputFolder, fileName);
System.IO.File.WriteAllLines(confFile, n.ToQuaggaConf());
XmlDocument doc = n.ToNbgpConf();
string xmlFile = System.IO.Path.Combine(outputFolder, "AS" + n.AsNumber + ".xml");
doc.Save(xmlFile);
}
// Export Nodes to CSV
ExportNodesToCSV(outputFolder);
ExportNodeCoordinatessToCSV(outputFolder);
// Export IP Edges to CSV
ExportIPEdgesToCSV(outputFolder);
// Export Edges to CSV
ExportEdgesToCSV(outputFolder);
// Export Network to CSV
ExportNetworkToCSV(outputFolder);
}
private void ExportNodesToCSV(string outputFolder)
{
string fileName = "nodes.csv";
string csvFile = System.IO.Path.Combine(outputFolder, fileName);
StringBuilder sb = new StringBuilder();
foreach (Node n in Nodes)
{
sb.Append(n.AsNumber + "," );
sb.Append(n.GetRouterId().ToString() + Environment.NewLine);
}
string[] content = sb.ToString().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
System.IO.File.WriteAllLines(csvFile, content);
}
private void ExportNodeCoordinatessToCSV(string outputFolder)
{
string fileName = "coordinates.csv";
string csvFile = System.IO.Path.Combine(outputFolder, fileName);
StringBuilder sb = new StringBuilder();
foreach (Node n in Nodes)
{
if (ScenarioType == ScenarioType.Waxman)
{
sb.Append(n.Coordinate.X + ",");
sb.Append(n.Coordinate.Y + Environment.NewLine);
}
else
{
sb.Append("0.0, 0.0" + Environment.NewLine);
}
}
string[] content = sb.ToString().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
System.IO.File.WriteAllLines(csvFile, content);
}
private void ExportIPEdgesToCSV(string outputFolder)
{
string fileName = "ipedges.csv";
string csvFile = System.IO.Path.Combine(outputFolder, fileName);
StringBuilder sb = new StringBuilder();
foreach (var i in IPLinks)
{
sb.Append(i.Key.ToString() + ",");
sb.Append(i.Value.ToString() + Environment.NewLine);
}
string[] content = sb.ToString().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
System.IO.File.WriteAllLines(csvFile, content);
}
internal void ExportEdgesToCSV(string outputFolder)
{
string fileName = "edges.csv";
string csvFile = System.IO.Path.Combine(outputFolder, fileName);
StringBuilder sb = new StringBuilder();
//sb.Append("Speaker,Peer"+ Environment.NewLine);
foreach (Link l in Links)
{
//sb.Append("AS" + l.SourceASN + ",AS" + l.DestinationASN + Environment.NewLine);
sb.Append(l.SourceASN + "," + l.DestinationASN + Environment.NewLine);
}
string[] content = sb.ToString().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
System.IO.File.WriteAllLines(csvFile, content);
}
private void ExportNetworkToCSV(string outputFolder)
{
string fileName = "network.csv";
string csvFile = System.IO.Path.Combine(outputFolder, fileName);
StringBuilder sb = new StringBuilder();
foreach (Link l in Links)
{
sb.Append(l.SourceASN + "," + l.DestinationASN + ",");
sb.Append(l.SourceIP + "," + l.DestinationIP + Environment.NewLine);
}
string[] content = sb.ToString().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
System.IO.File.WriteAllLines(csvFile, content);
}
internal string[] GetStatistics()
{
StringBuilder sb = new StringBuilder();
if (Statistics != null)
{
sb.Append(Environment.NewLine + "==========" + Environment.NewLine);
sb.Append("Statistics" + Environment.NewLine);
sb.Append("==========" + Environment.NewLine);
sb.Append("Scenario Type: " + Statistics.ScenarioType + Environment.NewLine);
sb.Append("Total number of nodes: " + Statistics.NumberOfNodes + Environment.NewLine);
sb.Append("Total number of links: " + Statistics.NumberOfLinks + Environment.NewLine);
sb.Append(Environment.NewLine + "==========" + Environment.NewLine);
sb.Append("Analysis" + Environment.NewLine);
sb.Append("==========" + Environment.NewLine);
sb.Append("Average number of links per peer: " + Statistics.AverageLinksPerPeer + Environment.NewLine);
sb.Append("Potential network; maximum number of links: " + Statistics.MaxNumberOfLinks + Environment.NewLine);
sb.Append("Network density: " + Statistics.Density + Environment.NewLine);
}
return sb.ToString().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
}
}
}