-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathwifi_static.cc
746 lines (682 loc) · 28.9 KB
/
wifi_static.cc
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
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
*
* Author: Matteo Nerini
* Email: [email protected]
* Date: June 2020
*
*
* Network Topology:
*
* (xA,yA) (xB,yB) (xC,yC)
* * * *
* | x nStaA | x nStaB | x nStaC
* STA A STA B STA C
*
* (0,0)
* *
* | <- 3 APs devices in 1 node
* AP
*
*
* Building Topology:
*
* ^ -----------------------------
* 1 | | |
* 0 | | | StaA: Random Walk
* | | o | StaB: Constant Position
* m | | AP | StaC: Random Walk
* | | |
* v -----------------------------
* <--------------------------->
* 20 m
*
*/
#include "ns3/command-line.h"
#include "ns3/config.h"
#include "ns3/uinteger.h"
#include "ns3/boolean.h"
#include "ns3/double.h"
#include "ns3/string.h"
#include "ns3/pointer.h"
#include "ns3/log.h"
#include "ns3/yans-wifi-helper.h"
#include "ns3/spectrum-wifi-helper.h"
#include "ns3/ssid.h"
#include "ns3/mobility-helper.h"
#include "ns3/internet-stack-helper.h"
#include "ns3/ipv4-address-helper.h"
#include "ns3/udp-client-server-helper.h"
#include "ns3/packet-sink-helper.h"
#include "ns3/ipv4-global-routing-helper.h"
#include "ns3/on-off-helper.h"
#include "ns3/packet-sink.h"
#include "ns3/yans-wifi-channel.h"
#include "ns3/multi-model-spectrum-channel.h"
#include "ns3/wifi-net-device.h"
#include "ns3/qos-txop.h"
#include "ns3/wifi-mac.h"
#include "ns3/rng-seed-manager.h"
#include "ns3/flow-monitor.h"
#include "ns3/flow-monitor-helper.h"
#include "ns3/netanim-module.h"
#include "ns3/buildings-module.h"
#include "ns3/ipv4-flow-classifier.h"
#define ENDC "\033[0m"
#define ERROR "\033[91m"
#define OKGREEN "\033[92m"
#define WARNING "\033[93m"
#define OKBLUE "\033[94m"
using namespace ns3;
NS_LOG_COMPONENT_DEFINE ("wifi_static");
// function to create a new C/S application
void new_application (uint16_t& index, uint32_t payloadSize, double simulationTime,
NodeContainer staNodes, NodeContainer apNode, std::string dataRate_str,
Ipv4InterfaceContainer& apInterface, ApplicationContainer& clientApp, ApplicationContainer& serverApp)
{
uint16_t port = 5000 + index;
UdpServerHelper server (port);
server.SetAttribute("Port", UintegerValue (port));
serverApp = server.Install (apNode.Get (0));
serverApp.Start (Seconds (0.0));
serverApp.Stop (Seconds (simulationTime + 2));
OnOffHelper client ("ns3::UdpSocketFactory", InetSocketAddress (apInterface.GetAddress (0), port));
client.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
client.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
client.SetAttribute ("DataRate", StringValue (dataRate_str));
client.SetAttribute ("PacketSize", UintegerValue (payloadSize));
clientApp = client.Install (staNodes.Get (index-1));
clientApp.Start (Seconds (1.0));
clientApp.Stop (Seconds (simulationTime + 1));
index ++;
}
// function to compute channel numbers and widths for each slice
void compute_channels (int dataRateSumA, int& channelNumberA, int& channelWidthA,
int dataRateSumB, int& channelNumberB, int& channelWidthB,
int dataRateSumC, int& channelNumberC, int& channelWidthC)
{
if (dataRateSumA < 65)
{
channelWidthA = 20;
channelNumberA = 36;
}
else if (dataRateSumA < 130)
{
channelWidthA = 40;
channelNumberA = 38;
}
else if (dataRateSumA < 272)
{
channelWidthA = 80;
channelNumberA = 42;
}
else
{
channelWidthA = 160;
channelNumberA = 50;
}
channelWidthB = 20;
channelNumberB = 100;
if (dataRateSumC < 65)
{
channelWidthC = 20;
channelNumberC = 161;
}
else if (dataRateSumC < 130)
{
channelWidthC = 40;
channelNumberC = 159;
}
else if (dataRateSumC < 272)
{
channelWidthC = 80;
channelNumberC = 155;
}
else
{
channelWidthC = 160;
channelNumberC = 144;
}
}
// main function
int main (int argc, char *argv[])
{
uint32_t payloadSize = 1472; // bytes (UDP)
double simulationTime = 15; // seconds
int seed = 1; // seed used in the simulation
std::string csvFileName = "test.csv"; // csv file name
std::string band = "AX_5"; // AC_5, AX_2.4 or AX_5
std::string phyModel = "spectrum"; // "spectrum" or "yans"
bool constantMcs = 1; // 0 Minstrel or 1 constant
bool enablePcap = 0; // 0 no Pcap or 1 Pcap
double x_max = 20.0; // meters
double y_max = 10.0; // meters
double z_max = 3.0; // meters
int nStaA = 1; // number of stations A
int nStaB = 1; // number of stations B
int nStaC = 1; // number of stations C
// Network A
int channelNumberA = 42; // channel number A
int channelWidthA = 20; // 20, 40, 80 or 160 MHz
int mcs = 5; // from 0 to 11 (-1 = unset value)
int giA = 1600; // 800, 1600 or 3200 ns
int txPowerA = 2; // dBm
std::string dataRateA_old = "10Mb/s";
// Network B
int channelNumberB = 114; // channel number B
int channelWidthB = 20; // 20, 40, 80 or 160 MHz
//int mcsB = 5; // from 0 to 11 (-1 = unset value)
int giB = 1600; // 800, 1600 or 3200 ns
int txPowerB = -4; // dBm
std::string dataRateB_old = "10Mb/s";
// Network C
int channelNumberC = 155; // channel number B
int channelWidthC = 40; // 20, 40, 80 or 160 MHz
//int mcsC = 5; // from 0 to 11 (-1 = unset value)
int giC = 1600; // 800, 1600 or 3200 ns
int txPowerC = 18; // dBm
std::string dataRateC_old = "10Mb/s";
CommandLine cmd;
cmd.AddValue ("payloadSize", "Payload size in bytes", payloadSize);
cmd.AddValue ("simulationTime", "Simulation time in seconds", simulationTime);
cmd.AddValue ("seed", "Seed", seed);
cmd.AddValue ("csvFileName", "Name of the .csv file", csvFileName);
cmd.AddValue ("band", "AC_5, AX_2.4 or AX_5", band);
cmd.AddValue ("phyModel", "PHY layer model", phyModel);
cmd.AddValue ("constantMcs", "0 Minstrel or 1 constant", constantMcs);
cmd.AddValue ("enablePcap", "Enable/disable pcap file generation", enablePcap);
cmd.AddValue ("nStaA", "Number Stations A", nStaA);
cmd.AddValue ("nStaB", "Number Stations B", nStaB);
cmd.AddValue ("nStaC", "Number Stations C", nStaC);
// Network A
cmd.AddValue ("channelNumberA", "Channel number A", channelNumberA);
cmd.AddValue ("channelWidthA", "Channel width A", channelWidthA);
cmd.AddValue ("mcs", "if set, limit testing to a specific MCS A", mcs);
cmd.AddValue ("giA", "Guard interval A", giA);
cmd.AddValue ("txPowerA", "Transmission power A", txPowerA);
cmd.AddValue ("dataRateA_old", "Data rate A", dataRateA_old);
// Network B
cmd.AddValue ("channelNumberB", "Channel number B", channelNumberB);
cmd.AddValue ("channelWidthB", "Channel width B", channelWidthB);
//cmd.AddValue ("mcsB", "if set, limit testing to a specific MCS B", mcsB);
cmd.AddValue ("giB", "Guard interval B", giB);
cmd.AddValue ("txPowerB", "Transmission power B", txPowerB);
cmd.AddValue ("dataRateB_old", "Data rate B", dataRateB_old);
// Network B
cmd.AddValue ("channelNumberC", "Channel number C", channelNumberC);
cmd.AddValue ("channelWidthC", "Channel width C", channelWidthC);
//cmd.AddValue ("mcsC", "if set, limit testing to a specific MCS C", mcsC);
cmd.AddValue ("giC", "Guard interval C", giC);
cmd.AddValue ("txPowerC", "Transmission power C", txPowerC);
cmd.AddValue ("dataRateC_old", "Data rate C", dataRateC_old);
cmd.Parse (argc, argv);
// Set the PRNG seed
RngSeedManager::SetSeed (seed);
// Set random throughput for each flow in the the 3 slices
Ptr<UniformRandomVariable> dataRateA_ptr = CreateObject<UniformRandomVariable> ();
dataRateA_ptr->SetAttribute ("Min", DoubleValue (80));
dataRateA_ptr->SetAttribute ("Max", DoubleValue (101));
std::vector<int> dataRateA(nStaA);
std::vector<std::string> dataRateA_str(nStaA);
for (int i = 0; i < nStaA; i++)
{
dataRateA[i] = (int) dataRateA_ptr->GetValue();
dataRateA_str[i] = std::to_string(dataRateA[i]) + "Mb/s";
}
Ptr<UniformRandomVariable> dataRateB_ptr = CreateObject<UniformRandomVariable> ();
dataRateB_ptr->SetAttribute ("Min", DoubleValue (30));
dataRateB_ptr->SetAttribute ("Max", DoubleValue (51));
std::vector<int> dataRateB(nStaB);
std::vector<std::string> dataRateB_str(nStaB);
for (int i = 0; i < nStaB; i++)
{
dataRateB[i] = (int) dataRateB_ptr->GetValue();
dataRateB_str[i] = std::to_string(dataRateB[i]) + "Kb/s";
}
Ptr<UniformRandomVariable> dataRateC_ptr = CreateObject<UniformRandomVariable> ();
dataRateC_ptr->SetAttribute ("Min", DoubleValue (20));
dataRateC_ptr->SetAttribute ("Max", DoubleValue (41));
std::vector<int> dataRateC(nStaC);
std::vector<std::string> dataRateC_str(nStaC);
for (int i = 0; i < nStaC; i++)
{
dataRateC[i] = (int) dataRateC_ptr->GetValue();
dataRateC_str[i] = std::to_string(dataRateC[i]) + "Mb/s";
}
// Set positions for STAs
Ptr<UniformRandomVariable> x_ptr = CreateObject<UniformRandomVariable> ();
x_ptr->SetAttribute ("Min", DoubleValue (0));
x_ptr->SetAttribute ("Max", DoubleValue (x_max));
std::vector<double> x(nStaA+nStaB+nStaC);
Ptr<UniformRandomVariable> y_ptr = CreateObject<UniformRandomVariable> ();
y_ptr->SetAttribute ("Min", DoubleValue (0));
y_ptr->SetAttribute ("Max", DoubleValue (y_max));
std::vector<double> y(nStaA+nStaB+nStaC);
for (int i = 0; i < nStaA+nStaB+nStaC; i++)
{
x[i] = x_ptr->GetValue ();
y[i] = y_ptr->GetValue ();
}
// Compute Channels according to the static algorithm
int dataRateSumA = 0;
for(std::vector<int>::iterator it = dataRateA.begin(); it != dataRateA.end(); ++it)
dataRateSumA += *it;
int dataRateSumB = 0;
for(std::vector<int>::iterator it = dataRateB.begin(); it != dataRateB.end(); ++it)
dataRateSumB += *it;
int dataRateSumC = 0;
for(std::vector<int>::iterator it = dataRateC.begin(); it != dataRateC.end(); ++it)
dataRateSumC += *it;
compute_channels (dataRateSumA, channelNumberA, channelWidthA,
dataRateSumB, channelNumberB, channelWidthB,
dataRateSumC, channelNumberC, channelWidthC);
std::cout << "Channel number and width A: " << channelNumberA << channelWidthA << "\n";
std::cout << "Channel number and width B: " << channelNumberB << channelWidthB << "\n";
std::cout << "Channel number and width C: " << channelNumberC << channelWidthC << "\n";
// Create nStaA + nStaB + nStaC STAs node objects and 1 AP node object
NodeContainer staNodes;
staNodes.Create (nStaA + nStaB + nStaC);
NodeContainer apNode;
apNode.Create (1);
// Create a phy helper
SpectrumWifiPhyHelper spectrumPhy = SpectrumWifiPhyHelper::Default ();
YansWifiPhyHelper yansPhy = YansWifiPhyHelper::Default ();
Ptr<HybridBuildingsPropagationLossModel> lossModel = CreateObject<HybridBuildingsPropagationLossModel> ();
if (phyModel == "spectrum")
{
// Create the channel
Ptr<MultiModelSpectrumChannel> channel = CreateObject<MultiModelSpectrumChannel> ();
channel->AddPropagationLossModel (lossModel);
Ptr<ConstantSpeedPropagationDelayModel> delayModel = CreateObject<ConstantSpeedPropagationDelayModel> ();
channel->SetPropagationDelayModel (delayModel);
spectrumPhy.SetErrorRateModel ("ns3::NistErrorRateModel");
spectrumPhy.SetChannel (channel);
//spectrumPhy.Set ("TxPowerStart", DoubleValue (txPower));
//spectrumPhy.Set ("TxPowerEnd", DoubleValue (txPower));
}
else if (phyModel == "yans")
{
// Create the channel
YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
yansPhy.SetChannel (channel.Create ());
//yansPhy.Set ("TxPowerStart", DoubleValue (txPower));
//yansPhy.Set ("TxPowerEnd", DoubleValue (txPower));
}
else
{
std::cout << "Wrong phyModel value!" << std::endl;
return 0;
}
//Create a WifiMacHelper and a WifiHelper
WifiMacHelper mac;
WifiHelper wifi;
std::ostringstream oss;
if (band == "AC_5")
{
wifi.SetStandard (WIFI_PHY_STANDARD_80211ac);
Config::SetDefault ("ns3::HybridBuildingsPropagationLossModel::Frequency", DoubleValue (5.51e+09));
if (constantMcs == 0)
{
wifi.SetRemoteStationManager ("ns3::MinstrelHtWifiManager");
}
else if (constantMcs == 1)
{
oss << "VhtMcs" << mcs;
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
"DataMode", StringValue (oss.str ()),
"ControlMode", StringValue (oss.str ()));
}
else
{
std::cout << "Wrong constantMcs value!" << std::endl;
return 0;
}
}
else if (band == "AX_5")
{
wifi.SetStandard (WIFI_PHY_STANDARD_80211ax_5GHZ);
Config::SetDefault ("ns3::HybridBuildingsPropagationLossModel::Frequency", DoubleValue (5.51e+09));
if (constantMcs == 1)
{
oss << "HeMcs" << mcs;
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
"DataMode", StringValue (oss.str ()),
"ControlMode", StringValue (oss.str ()));
}
else
{
std::cout << "With AX_5, constantMcs must be 1!" << std::endl;
return 0;
}
}
else if (band == "AX_2.4")
{
wifi.SetStandard (WIFI_PHY_STANDARD_80211ax_2_4GHZ);
Config::SetDefault ("ns3::HybridBuildingsPropagationLossModel::Frequency", DoubleValue (2.44e+09));
if (constantMcs == 1)
{
oss << "HeMcs" << mcs;
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
"DataMode", StringValue (oss.str ()),
"ControlMode", StringValue (oss.str ()));
}
else
{
std::cout << "With AX_2.4, constantMcs must be 1!" << std::endl;
return 0;
}
}
else
{
std::cout << "Wrong band value!" << std::endl;
return 0;
}
// Declare NetDeviceContainers to hold the container returned by the helper
std::vector<NetDeviceContainer> staDeviceA(nStaA);
std::vector<NetDeviceContainer> staDeviceB(nStaB);
std::vector<NetDeviceContainer> staDeviceC(nStaC);
NetDeviceContainer apDeviceA, apDeviceB, apDeviceC;
Ssid ssid;
if (phyModel == "spectrum")
{
// Network A
ssid = Ssid ("networkA");
spectrumPhy.Set ("ChannelNumber", UintegerValue (channelNumberA));
spectrumPhy.Set ("TxPowerStart", DoubleValue (txPowerA));
spectrumPhy.Set ("TxPowerEnd", DoubleValue (txPowerA));
mac.SetType ("ns3::StaWifiMac",
"Ssid", SsidValue (ssid));
for (int i = 0; i < nStaA; i++)
staDeviceA[i] = wifi.Install (spectrumPhy, mac, staNodes.Get (i));
mac.SetType ("ns3::ApWifiMac",
"Ssid", SsidValue (ssid));
apDeviceA = wifi.Install (spectrumPhy, mac, apNode.Get(0));
// Network B
ssid = Ssid ("networkB");
spectrumPhy.Set ("ChannelNumber", UintegerValue (channelNumberB));
spectrumPhy.Set ("TxPowerStart", DoubleValue (txPowerB));
spectrumPhy.Set ("TxPowerEnd", DoubleValue (txPowerB));
mac.SetType ("ns3::StaWifiMac",
"Ssid", SsidValue (ssid));
for (int i = 0; i < nStaB; i++)
staDeviceB[i] = wifi.Install (spectrumPhy, mac, staNodes.Get (i + nStaA));
mac.SetType ("ns3::ApWifiMac",
"Ssid", SsidValue (ssid));
apDeviceB = wifi.Install (spectrumPhy, mac, apNode.Get(0));
// Network C
ssid = Ssid ("networkC");
spectrumPhy.Set ("ChannelNumber", UintegerValue (channelNumberC));
spectrumPhy.Set ("TxPowerStart", DoubleValue (txPowerC));
spectrumPhy.Set ("TxPowerEnd", DoubleValue (txPowerC));
mac.SetType ("ns3::StaWifiMac",
"Ssid", SsidValue (ssid));
for (int i = 0; i < nStaC; i++)
staDeviceC[i] = wifi.Install (spectrumPhy, mac, staNodes.Get (i + nStaA + nStaB));
mac.SetType ("ns3::ApWifiMac",
"Ssid", SsidValue (ssid));
apDeviceC = wifi.Install (spectrumPhy, mac, apNode.Get(0));
}
else if (phyModel == "yans")
{
// Network A
ssid = Ssid ("networkA");
yansPhy.Set ("ChannelNumber", UintegerValue (channelNumberA));
yansPhy.Set ("TxPowerStart", DoubleValue (txPowerA));
yansPhy.Set ("TxPowerEnd", DoubleValue (txPowerA));
mac.SetType ("ns3::StaWifiMac",
"Ssid", SsidValue (ssid));
for (int i = 0; i < nStaA; i++)
staDeviceA[i] = wifi.Install (spectrumPhy, mac, staNodes.Get (i));
mac.SetType ("ns3::ApWifiMac",
"Ssid", SsidValue (ssid));
apDeviceA = wifi.Install (yansPhy, mac, apNode.Get(0));
// Network B
ssid = Ssid ("networkB");
yansPhy.Set ("ChannelNumber", UintegerValue (channelNumberB));
yansPhy.Set ("TxPowerStart", DoubleValue (txPowerB));
yansPhy.Set ("TxPowerEnd", DoubleValue (txPowerB));
mac.SetType ("ns3::StaWifiMac",
"Ssid", SsidValue (ssid));
for (int i = 0; i < nStaB; i++)
staDeviceB[i] = wifi.Install (spectrumPhy, mac, staNodes.Get (i + nStaA));
mac.SetType ("ns3::ApWifiMac",
"Ssid", SsidValue (ssid));
apDeviceB = wifi.Install (yansPhy, mac, apNode.Get(0));
// Network C
ssid = Ssid ("networkC");
yansPhy.Set ("ChannelNumber", UintegerValue (channelNumberC));
yansPhy.Set ("TxPowerStart", DoubleValue (txPowerC));
yansPhy.Set ("TxPowerEnd", DoubleValue (txPowerC));
mac.SetType ("ns3::StaWifiMac",
"Ssid", SsidValue (ssid));
for (int i = 0; i < nStaC; i++)
staDeviceC[i] = wifi.Install (spectrumPhy, mac, staNodes.Get (i + nStaA + nStaB));
mac.SetType ("ns3::ApWifiMac",
"Ssid", SsidValue (ssid));
apDeviceC = wifi.Install (yansPhy, mac, apNode.Get(0));
}
else
{
std::cout << "Wrong phyModel value!" << std::endl;
return 0;
}
// Set channel width
for (int i = 0; i < nStaA; i++)
Config::Set ("/NodeList/" + std::to_string(i) + "/DeviceList/0/$ns3::WifiNetDevice/Phy/ChannelWidth",
UintegerValue (channelWidthA));
Config::Set ("/NodeList/" + std::to_string(nStaA+nStaB+nStaC) + "/DeviceList/0/$ns3::WifiNetDevice/Phy/ChannelWidth",
UintegerValue (channelWidthA)); ///NodeList/3/DeviceList 3?
for (int i = 0; i < nStaB; i++)
Config::Set ("/NodeList/" + std::to_string(nStaA+i) + "/DeviceList/0/$ns3::WifiNetDevice/Phy/ChannelWidth",
UintegerValue (channelWidthB));
Config::Set ("/NodeList/" + std::to_string(nStaA+nStaB+nStaC) + "/DeviceList/1/$ns3::WifiNetDevice/Phy/ChannelWidth",
UintegerValue (channelWidthB));
for (int i = 0; i < nStaC; i++)
Config::Set ("/NodeList/" + std::to_string(nStaA+nStaB+i) + "/DeviceList/0/$ns3::WifiNetDevice/Phy/ChannelWidth",
UintegerValue (channelWidthC));
Config::Set ("/NodeList/" + std::to_string(nStaA+nStaB+nStaC) + "/DeviceList/2/$ns3::WifiNetDevice/Phy/ChannelWidth",
UintegerValue (channelWidthC));
// Set guard interval
for (int i = 0; i < nStaA; i++)
Config::Set ("/NodeList/" + std::to_string(i) + "/DeviceList/0/$ns3::WifiNetDevice/HeConfiguration/GuardInterval",
TimeValue (NanoSeconds (giA)));
Config::Set ("/NodeList/" + std::to_string(nStaA+nStaB+nStaC) + "/DeviceList/0/$ns3::WifiNetDevice/HeConfiguration/GuardInterval",
TimeValue (NanoSeconds (giA)));
for (int i = 0; i < nStaB; i++)
Config::Set ("/NodeList/" + std::to_string(nStaA+i) + "/DeviceList/0/$ns3::WifiNetDevice/HeConfiguration/GuardInterval",
TimeValue (NanoSeconds (giB)));
Config::Set ("/NodeList/" + std::to_string(nStaA+nStaB+nStaC) + "/DeviceList/1/$ns3::WifiNetDevice/HeConfiguration/GuardInterval",
TimeValue (NanoSeconds (giB)));
for (int i = 0; i < nStaC; i++)
Config::Set ("/NodeList/" + std::to_string(nStaA+nStaB+i) + "/DeviceList/0/$ns3::WifiNetDevice/HeConfiguration/GuardInterval",
TimeValue (NanoSeconds (giC)));
Config::Set ("/NodeList/" + std::to_string(nStaA+nStaB+nStaC) + "/DeviceList/2/$ns3::WifiNetDevice/HeConfiguration/GuardInterval",
TimeValue (NanoSeconds (giC)));
// Set rts cts
Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/RemoteStationManager/RtsCtsThreshold",
UintegerValue (100));
// Create building
Ptr<Building> b = CreateObject <Building> ();
b->SetBoundaries (Box (0.0, x_max, 0.0, y_max, 0.0, z_max));
b->SetBuildingType (Building::Residential);
b->SetExtWallsType (Building::ConcreteWithWindows);
b->SetNFloors (1);
b->SetNRoomsX (1);
b->SetNRoomsY (1);
// Setting mobility model
Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
// Set position for AP
positionAlloc->Add (Vector (10.0, 5.0, 2.9));
// Set position for STAs
for (int i = 0; i < nStaA+nStaB+nStaC; i++)
positionAlloc->Add (Vector (x[i], y[i], 1.5));
MobilityHelper mobility;
mobility.SetPositionAllocator (positionAlloc);
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
mobility.Install (apNode);
mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
"Bounds", RectangleValue (Rectangle (0.0, x_max, 0.0, y_max)));
for (int i = 0; i < nStaA; i++)
mobility.Install (staNodes.Get (i));
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
for (int i = 0; i < nStaB; i++)
mobility.Install (staNodes.Get (nStaA+i));
mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
"Bounds", RectangleValue (Rectangle (0.0, x_max, 0.0, y_max)));
for (int i = 0; i < nStaC; i++)
mobility.Install (staNodes.Get (nStaA+nStaB+i));
BuildingsHelper::Install (apNode);
BuildingsHelper::Install (staNodes);
BuildingsHelper::MakeMobilityModelConsistent ();
// Internet stack
InternetStackHelper stack;
stack.Install (apNode);
stack.Install (staNodes);
Ipv4AddressHelper address;
address.SetBase ("192.168.1.0", "255.255.255.0");
std::vector<Ipv4InterfaceContainer> staInterfaceA(nStaA);
for (int i = 0; i < nStaA; i++)
staInterfaceA[i] = address.Assign (staDeviceA[i]);
Ipv4InterfaceContainer apInterfaceA = address.Assign (apDeviceA);
address.SetBase ("192.168.2.0", "255.255.255.0");
std::vector<Ipv4InterfaceContainer> staInterfaceB(nStaB);
for (int i = 0; i < nStaB; i++)
staInterfaceB[i] = address.Assign (staDeviceB[i]);
Ipv4InterfaceContainer apInterfaceB = address.Assign (apDeviceB);
address.SetBase ("192.168.3.0", "255.255.255.0");
std::vector<Ipv4InterfaceContainer> staInterfaceC(nStaC);
for (int i = 0; i < nStaC; i++)
staInterfaceC[i] = address.Assign (staDeviceC[i]);
Ipv4InterfaceContainer apInterfaceC = address.Assign (apDeviceC);
// Flow monitor
Ptr<FlowMonitor> flowMonitor;
FlowMonitorHelper flowHelper;
flowMonitor = flowHelper.InstallAll();
// Setting applications
uint16_t index = 1;
std::vector<ApplicationContainer> clientAppA(nStaA), serverAppA(nStaA);
std::vector<ApplicationContainer> clientAppB(nStaB), serverAppB(nStaB);
std::vector<ApplicationContainer> clientAppC(nStaC), serverAppC(nStaC);
for (int i = 0; i < nStaA; i++)
new_application (index, payloadSize, simulationTime, staNodes, apNode,
dataRateA_str[i], apInterfaceA, clientAppA[i], serverAppA[i]);
for (int i = 0; i < nStaB; i++)
new_application (index, payloadSize, simulationTime, staNodes, apNode,
dataRateB_str[i], apInterfaceB, clientAppB[i], serverAppB[i]);
for (int i = 0; i < nStaC; i++)
new_application (index, payloadSize, simulationTime, staNodes, apNode,
dataRateC_str[i], apInterfaceC, clientAppC[i], serverAppC[i]);
if (enablePcap)
{
if (phyModel == "spectrum")
{
spectrumPhy.EnablePcap ("AP_A", apDeviceA.Get (0));
spectrumPhy.EnablePcap ("STA_A", staDeviceA[0].Get (0));
spectrumPhy.EnablePcap ("AP_B", apDeviceB.Get (0));
spectrumPhy.EnablePcap ("STA_B", staDeviceB[0].Get (0));
spectrumPhy.EnablePcap ("AP_C", apDeviceC.Get (0));
spectrumPhy.EnablePcap ("STA_C", staDeviceC[0].Get (0));
}
else if (phyModel == "yans")
{
yansPhy.EnablePcap ("AP_A", apDeviceA.Get (0));
yansPhy.EnablePcap ("STA_A", staDeviceA[0].Get (0));
yansPhy.EnablePcap ("AP_B", apDeviceB.Get (0));
yansPhy.EnablePcap ("STA_B", staDeviceB[0].Get (0));
yansPhy.EnablePcap ("AP_C", apDeviceC.Get (0));
yansPhy.EnablePcap ("STA_C", staDeviceC[0].Get (0));
}
else
{
std::cout << "Wrong phyModel value!" << std::endl;
return 0;
}
}
Simulator::Stop (Seconds (simulationTime + 2));
time_t timeNow = time(0);
char* ctimeNow =ctime(&timeNow);
std::cout << OKBLUE <<"Simulation started! Time: " << ctimeNow << ENDC;
Simulator::Run ();
// Activate/deactivate the histograms and the per-probe detailed stats
std::cout << OKBLUE <<"Writing to file: FlowMonitorFile.xml"<< ENDC << std::endl;
flowMonitor->SerializeToXmlFile("FlowMonitorFile.xml", false, false);
// Show results
std::vector<uint64_t> totalPacketsRxA(nStaA), totalPacketsLossA(nStaA);
std::vector<uint64_t> totalPacketsRxB(nStaB), totalPacketsLossB(nStaB);
std::vector<uint64_t> totalPacketsRxC(nStaC), totalPacketsLossC(nStaC);
for (int i = 0; i < nStaA; i++)
{
totalPacketsRxA[i] = DynamicCast<UdpServer> (serverAppA[i].Get (0))->GetReceived ();
totalPacketsLossA[i] = DynamicCast<UdpServer> (serverAppA[i].Get (0))->GetLost ();
}
for (int i = 0; i < nStaB; i++)
{
totalPacketsRxB[i] = DynamicCast<UdpServer> (serverAppB[i].Get (0))->GetReceived ();
totalPacketsLossB[i] = DynamicCast<UdpServer> (serverAppB[i].Get (0))->GetLost ();
}
for (int i = 0; i < nStaC; i++)
{
totalPacketsRxC[i] = DynamicCast<UdpServer> (serverAppC[i].Get (0))->GetReceived ();
totalPacketsLossC[i] = DynamicCast<UdpServer> (serverAppC[i].Get (0))->GetLost ();
}
std::vector<uint32_t> txPackets_unsort(nStaA+nStaB+nStaC);
std::vector<uint32_t> rxPackets_unsort(nStaA+nStaB+nStaC);
std::vector<double> latency_unsort(nStaA+nStaB+nStaC);
std::vector<uint32_t> txPackets(nStaA+nStaB+nStaC);
std::vector<uint32_t> rxPackets(nStaA+nStaB+nStaC);
std::vector<double> latency(nStaA+nStaB+nStaC);
std::vector<uint32_t> destPorts(nStaA+nStaB+nStaC);
Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowHelper.GetClassifier ());
FlowMonitor::FlowStatsContainer stats = flowMonitor->GetFlowStats ();
for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i)
{
txPackets_unsort[i->first-1] = i->second.txPackets;
rxPackets_unsort[i->first-1] = i->second.rxPackets;
latency_unsort[i->first-1] = i->second.delaySum.ToDouble(Time::MS) / i->second.rxPackets;
Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first);
destPorts[i->first-1] = t.destinationPort;
//std::cout << "Flow " << i->first << " (" << t.sourceAddress << " -> " << t.destinationAddress << "," << destPorts[i->first-1] << ")\n";
//std::cout << " Tx Packets: " << totTxPackets[i->first-1] << "\n";
//std::cout << " Rx Packets: " << totRxPackets[i->first-1] << "\n";
//std::cout << " Delay Sum: " << totDelay[i->first-1] << "\n";
//std::cout << " Delay Sum MS: " << i->second.delaySum.ToDouble(Time::MS) << "\n";
//std::cout << " Delay: " << i->second.delaySum / i->second.rxPackets << "\n";
//std::cout << " Delay MS: " << i->second.delaySum.ToDouble(Time::MS) / i->second.rxPackets << "\n";
}
for (int i = 0; i < nStaA+nStaB+nStaC; i++)
{
txPackets[destPorts[i]-5001] = txPackets_unsort[i];
rxPackets[destPorts[i]-5001] = rxPackets_unsort[i];
latency[destPorts[i]-5001] = latency_unsort[i];
}
timeNow = time(0);
ctimeNow =ctime(&timeNow);
std::cout << OKBLUE << "Simulation finished! Time: " << ctimeNow << ENDC;
Simulator::Destroy ();
// Writing to file csvFileName.csv
std::cout << OKBLUE <<"Writing to file: " << csvFileName << ENDC << std::endl;
// std::ofstream out (csvFileName.c_str ()); // Use it to overwrite the file
std::ofstream out (csvFileName.c_str (), std::ios::app);
out << "channelNumberA, channelWidthA, channelNumberB, channelWidthB, channelNumberC, channelWidthC" << std::endl;
out << channelNumberA << "," << channelWidthA << ","
<< channelNumberB << "," << channelWidthB << ","
<< channelNumberC << "," << channelWidthC << std::endl;
for (int i = 0; i < nStaA; i++)
{
out << dataRateA[i] << "," << x[i] << "," << y[i] << ","
<< txPackets[i] << "," << rxPackets[i] << "," << latency[i]
<< std::endl;
}
for (int i = 0; i < nStaB; i++)
{
out << dataRateB[i] << "," << x[nStaA+i] << "," << y[nStaA+i] << ","
<< txPackets[nStaA+i] << "," << rxPackets[nStaA+i] << "," << latency[nStaA+i] << std::endl;
}
for (int i = 0; i < nStaC; i++)
{
out << dataRateC[i] << "," << x[nStaA+nStaB+i] << "," << y[nStaA+nStaB+i] << ","
<< txPackets[nStaA+nStaB+i] << "," << rxPackets[nStaA+nStaB+i] << "," << latency[nStaA+nStaB+i] << std::endl;
}
out.close ();
return 0;
}