-
Notifications
You must be signed in to change notification settings - Fork 0
/
flat.cc
386 lines (305 loc) · 14.6 KB
/
flat.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
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/mobility-module.h"
#include "ns3/config-store-module.h"
#include "ns3/wifi-module.h"
#include "ns3/applications-module.h"
#include "ns3/internet-module.h"
#include "ns3/netanim-module.h"
#include "ns3/global-route-manager.h"
#include "ns3/ipv4-static-routing-helper.h"
#include "ns3/random-variable-stream.h"
#include "ns3/flow-monitor-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/csma-module.h"
#include "ns3/olsr-helper.h"
#include "ns3/aodv-helper.h"
#include "ns3/dsdv-module.h"
#include "ns3/dsr-module.h"
#include "ns3/internet-module.h"
#include "ns3/trace-helper.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
NS_LOG_COMPONENT_DEFINE ("flat-architecture");
using namespace ns3;
using namespace std;
using namespace dsr;
int main (int argc, char *argv[])
{
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%
//% Basic simulation parameters
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
std::string phyMode ("DsssRate1Mbps");
std::string rtslimit = "2200";
double distance = 150; // distance between nodes
uint32_t numNodes = 30; // number of nodes in the network
double interval = 0.001; // seconds
uint32_t packetSize = 1024; // bytes
uint32_t flows=7; // number of data flows in the network
double totalTime = 50.0; // simulation time
uint32_t gridWidth = 5;
int mobilityModel = 0;
int routing = 0;
int seed = 1;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%
//% Command line arguments
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
CommandLine cmd;
cmd.AddValue ("gridWidth", "grid Width", gridWidth);
cmd.AddValue ("numNodes", "Number of nodes", numNodes);
cmd.AddValue ("phyMode", "Wifi Phy mode", phyMode);
cmd.AddValue ("phyMode", "Wifi Phy mode", phyMode);
cmd.AddValue ("distance", "distance (m)", distance);
cmd.AddValue ("PacketSize", "Packet size in bytes", packetSize);
cmd.AddValue ("mobilityModel", "Mobility model", mobilityModel);
cmd.AddValue ("routing", "Routing", routing);
cmd.AddValue ("seed", "Seed", seed);
cmd.Parse (argc, argv);
ns3::RngSeedManager::SetSeed(seed);
// Convert to time object
Time interPacketInterval = Seconds (interval);
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%
//% Nodes configuration
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
NodeContainer nodes;
nodes.Create (numNodes);
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%
//% Channel and network configuration
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
// turn off RTS/CTS for frames below 2200 bytes
Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue (rtslimit));
// Fix non-unicast data rate to be the same as that of unicast
Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue (phyMode));
// The below set of helpers will help us to put together the wifi NICs we want
WifiHelper wifi;
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
// set it to zero; otherwise, gain will be added
wifiPhy.Set ("RxGain", DoubleValue (-20) );
// ns-3 supports RadioTap and Prism tracing extensions for 802.11b
wifiPhy.SetPcapDataLinkType (YansWifiPhyHelper::DLT_IEEE802_11_RADIO);
YansWifiChannelHelper wifiChannel;
wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
wifiChannel.AddPropagationLoss ("ns3::FriisPropagationLossModel");
wifiPhy.SetChannel (wifiChannel.Create ());
// Add a non-QoS upper mac, and disable rate control
NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
wifi.SetStandard (WIFI_PHY_STANDARD_80211b);
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
"DataMode",StringValue (phyMode),
"ControlMode",StringValue (phyMode));
// Set it to adhoc mode
wifiMac.SetType ("ns3::AdhocWifiMac");
NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%
//% Mobility model
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/* MobilityHelper mobility;
mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
"MinX", DoubleValue (0.0),
"MinY", DoubleValue (0.0),
"DeltaX", DoubleValue (distance),
"DeltaY", DoubleValue (distance),
"GridWidth", UintegerValue (gridWidth),
"LayoutType", StringValue ("RowFirst"));
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
mobility.Install (nodes);*/
MobilityHelper mobility;
switch(mobilityModel) {
case 0:
std::cout<<"mov 0"<<std::endl;
mobility.SetPositionAllocator ("ns3::GridPositionAllocator", "MinX", DoubleValue (20.0),
"MinY", DoubleValue (20.0),
"DeltaX", DoubleValue (20.0),
"DeltaY", DoubleValue (20.0),
"GridWidth", UintegerValue (5),
"LayoutType",StringValue("RowFirst"));
mobility.SetMobilityModel ("ns3::RandomDirection2dMobilityModel",
"Bounds", RectangleValue (Rectangle (0, 100, 20, 80)),
"Speed", StringValue ("ns3::ConstantRandomVariable[Constant=20]"),
"Pause", StringValue ("ns3::ConstantRandomVariable[Constant=10]"));
mobility.Install (nodes);
break;
case 1:
std::cout<<"mov 1"<<std::endl;
mobility.SetPositionAllocator ("ns3::RandomRectanglePositionAllocator",
"X", StringValue ("ns3::UniformRandomVariable[Min=50.0|Max=120.0]"),
"Y", StringValue ("ns3::UniformRandomVariable[Min=50.0|Max=120.0]"));
mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
"Mode", StringValue ("Time"),
"Time", StringValue ("2s"),
"Speed", StringValue ("ns3::ExponentialRandomVariable[Mean=20]"),
"Bounds", StringValue ("30|120|30|120"));
mobility.Install (nodes);
break;
default:
std::cout<<"mov 2"<<std::endl;
ObjectFactory position;
position.SetTypeId ("ns3::RandomRectanglePositionAllocator");
position.Set ("X", StringValue ("ns3::UniformRandomVariable[Min=20|Max=70]"));
position.Set ("Y", StringValue ("ns3::UniformRandomVariable[Min=20|Max=70]"));
Ptr<PositionAllocator> PositionAlloc = position.Create ()->GetObject<PositionAllocator> ();
mobility.SetMobilityModel ("ns3::RandomWaypointMobilityModel",
"Speed", StringValue ("ns3::ExponentialRandomVariable[Mean=20]"),
"Pause",StringValue ("ns3::ExponentialRandomVariable[Mean=5]"),
"PositionAllocator", PointerValue (PositionAlloc));
mobility.SetPositionAllocator (PositionAlloc);
mobility.Install (nodes);
break;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%
//% Routing Algorith
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
InternetStackHelper internet;
Ipv4ListRoutingHelper list;
//Enable OLSR
NS_LOG_INFO ("OLSR protocol");
OlsrHelper olsr;
AodvHelper aodv;
DsdvHelper dsdv;
switch(routing) {
case 0:
list.Add (olsr, 100);
std::cout<<"OLSR"<<std::endl;
break;
case 1:
list.Add(aodv, 100);
std::cout<<"AODV"<<std::endl;
break;
default:
list.Add(dsdv, 100);
std::cout<<"DSDV"<<std::endl;
break;
}
internet.SetRoutingHelper (list); // has effect on the next Install ()
internet.Install (nodes);
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/*
//Enable AODV
AodvHelper aodv;
NS_LOG_INFO ("AODV protocol");
list.Add (aodv, 100);
internet.SetRoutingHelper (list); // has effect on the next Install ()
internet.Install (nodes);
*/
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
/*
// Enable DSDV
NS_LOG_INFO ("DSDV protocol");
DsdvHelper dsdv;
list.Add (dsdv, 100);
internet.SetRoutingHelper (list); // has effect on the next Install ()
internet.Install (nodes);
*/
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Ipv4AddressHelper ipv4;
NS_LOG_INFO ("Assign IP Addresses.");
ipv4.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer ifcont = ipv4.Assign (devices);
//Collect all interfaces in a single container
NodeContainer allNodes;
allNodes.Add(nodes);
Ipv4InterfaceContainer allInterfaces;
allInterfaces.Add(ifcont);
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%
//% Random Traffic- (Poisson Model)
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
std::ostringstream stringRate;
stringRate <<packetSize;
int min = 0;
int max = allNodes.GetN()-1;
Ptr<UniformRandomVariable> x = CreateObject<UniformRandomVariable> ();
x->SetAttribute ("Min", DoubleValue (min));
x->SetAttribute ("Max", DoubleValue (max));
// The values returned by a uniformly distributed random
// variable should always be within the range
//
// [min, max) .
//
//int value = x->GetValue ();
for ( uint32_t i = 0; i <flows ; i++)
{
//int source= UniformVariable().GetInteger(0,allNodes.GetN()-1);
int source = x->GetValue ();
int sink =source;
while(sink==source){
sink=x->GetValue ();
}
OnOffHelper onoff("ns3::UdpSocketFactory",InetSocketAddress (allInterfaces.GetAddress (source), i+9));
onoff.SetAttribute ("OnTime", StringValue ("ns3::ExponentialRandomVariable[Mean=1]"));
onoff.SetAttribute ("OffTime", StringValue ("ns3::ExponentialRandomVariable[Mean=1]"));
onoff.SetAttribute ("PacketSize", StringValue(stringRate.str()));
onoff.SetAttribute ("DataRate", StringValue("2048bps"));
AddressValue remoteAddress (InetSocketAddress (allInterfaces.GetAddress (sink), i+9));
onoff.SetAttribute ("Remote", remoteAddress);
Ptr<UniformRandomVariable> var = CreateObject<UniformRandomVariable> ();
ApplicationContainer temp = onoff.Install (allNodes.Get (source));
temp.Start (Seconds (5.1));
temp.Stop (Seconds (totalTime));
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Simulator::Stop(Seconds(250.0));
FlowMonitorHelper flowmon; // always install flow monitor before Simulator::run
Ptr<FlowMonitor> monitor = flowmon.InstallAll ();
Simulator::Run ();
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//
// Clase FlowMonitor
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
uint64_t bytesTotal = 0;
double lastRxTime=-1;
double firstRxTime=-1;
uint32_t allTx = 0;
uint32_t allRx = 0;
float allDelay = 0.;
uint32_t allLx = 0;
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
monitor->CheckForLostPackets ();
Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());
std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ();
for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i)
{
if (firstRxTime < 0)
firstRxTime = i->second.timeFirstRxPacket.GetSeconds(); // Find first packet arrival time
else if (firstRxTime > i->second.timeFirstRxPacket.GetSeconds() )
firstRxTime = i->second.timeFirstRxPacket.GetSeconds();
if (lastRxTime < i->second.timeLastRxPacket.GetSeconds() )
lastRxTime = i->second.timeLastRxPacket.GetSeconds(); // Find last packet arrival time
bytesTotal = bytesTotal + i->second.rxBytes;
bytesTotal += i->second.rxBytes * 8.0 / (lastRxTime-firstRxTime)/1024;
allTx += i->second.txPackets;
allRx += i->second.rxPackets;
allDelay += i->second.delaySum.GetSeconds()/i->second.rxPackets ;
allLx += i->second.lostPackets;
}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// network performance
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//std::cout << "Nodes = " << nodes.GetN() << " "<< "Avg throughput = "<< bytesTotal*8/(lastRxTime-firstRxTime)/1024<<" kbits/sec"<< " AllTx= "<<allTx <<" AllRx= "<< allRx <<" AllLx= "<< allLx << " TotalDelay= " <<allDelay <<std::endl;
std::cout <<numNodes << " "<< bytesTotal*8/(lastRxTime-firstRxTime)/1024<< " "<<allTx <<" "<<allRx <<" "<<allLx <<" "<<allDelay<<std::endl;
Simulator::Destroy ();
return 0;
}