-
Notifications
You must be signed in to change notification settings - Fork 5
/
iot_MasterCopy.cc
407 lines (318 loc) · 14.2 KB
/
iot_MasterCopy.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
/* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: Soumya Banerjee (Adapted from wireless-animation.cc)
*/
#include <stdlib.h> /* srand, rand */
#include <math.h>
#include "ns3/core-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/csma-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/olsr-helper.h"
#include "ns3/wifi-module.h"
#include "ns3/mobility-module.h"
#include "ns3/internet-module.h"
#include "ns3/netanim-module.h"
#include "ns3/basic-energy-source.h"
#include "ns3/flow-monitor-helper.h"
//#include "ns3/simple-device-energy-model.h"
//#include "ns3/v4ping-helper.h"
//#include "ns3/v4ping.h"
using namespace ns3;
static bool verbose = 0;
char * stringbuilder( char* prefix, char* sufix){
char* buf = (char*)malloc(50);
snprintf(buf, 50, "%s%s", prefix, sufix);
return buf;
}
ApplicationContainer sendMessage(ApplicationContainer apps, double time, Ptr<Node>source,Ptr<Node>sink, uint32_t packetSize){
Ipv4Address remoteAddress = sink->GetObject<Ipv4> ()->GetAddress (1, 0).GetLocal ();
/*V4PingHelper ping4(remoteAddress); // remote address
//ping4.SetAttribute ("MaxPackets", UintegerValue (1));
ping4.SetAttribute ("Interval", TimeValue (Seconds (25.)));
ping4.SetAttribute ("Size", UintegerValue (packetSize));
ApplicationContainer apps = ping4.Install (source);
apps.Start (Seconds (3.0));
apps.Stop (Seconds (15.0));
*/
//std::cout <<"message at "<<time<<std::endl;
uint16_t port = 9; // well-known echo port number
//uint32_t packetSize = 1024;
uint32_t maxPacketCount = 1;
Time interPacketInterval = Seconds (20.);
UdpClientHelper client (remoteAddress, port);
client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
client.SetAttribute ("Interval", TimeValue (interPacketInterval));
client.SetAttribute ("PacketSize", UintegerValue (packetSize));
client.SetAttribute ("StartTime", TimeValue (Seconds (time)));
//std::cout<<time<<">>";
apps.Add(client.Install (source));
return apps;
}
ApplicationContainer authenticate(ApplicationContainer appContainer, double time, Ptr<Node> user, Ptr<Node> gateway , Ptr<Node> device ){
if (verbose){
std::cout<<"user : "<< user->GetObject<Ipv4> ()->GetAddress (1, 0).GetLocal ();
std::cout<<" gateway : "<< gateway->GetObject<Ipv4> ()->GetAddress (1, 0).GetLocal ();
std::cout<<" device : "<< device->GetObject<Ipv4> ()->GetAddress (1, 0).GetLocal ()<<std::endl;
}
uint32_t M1 = 68, M2=116, M3=136;
appContainer = sendMessage(appContainer, time, user, gateway , M1);
appContainer = sendMessage(appContainer, time, gateway, device, M2);
appContainer = sendMessage(appContainer, time, device, user, M3);
//appContainer = sendMessage(appContainer, time, user, device , M1);
//appContainer = sendMessage(appContainer, time, device, user, M2);
return appContainer;
}
int
main (int argc, char *argv[])
{
//
// First, we declare and initialize a few local variables that control some
// simulation parameters.
//
//uint32_t backboneNodes = 1;//0;
uint32_t mobileUserNodes = 3;
uint32_t smartDeviceNodes = 2;
uint32_t stopTime = 2400;
bool verbose = 0;
bool enablePcap = 0;
bool enableAnim = 0;
bool verifyResults = 0; //used for regression
char saveFilePrefix[50] ;
//
// Simulation defaults are typically set next, before command line
// arguments are parsed.
//
//
//
// For convenience, we add the local variables to the command line argument
// system so that they can be overridden with flags such as
// "--smartDeviceNodes=20"
//
CommandLine cmd;
//cmd.AddValue ("backboneNodes", "number of backbone nodes", backboneNodes);
cmd.AddValue ("MU", "number of leaf nodes", mobileUserNodes);
cmd.AddValue ("SD", "number of LAN nodes", smartDeviceNodes);
cmd.AddValue ("t", "simulation stop time (seconds)", stopTime);
cmd.AddValue ("p", "Enable/disable pcap file generation", enablePcap);
cmd.AddValue ("a", "Enable/disable xml gneration for netanim-module", enableAnim);
cmd.AddValue ("o", "Show output end of the simulation", verifyResults);
cmd.AddValue ("v", "Verbose mode.", verbose);
cmd.AddValue ("s", "Define the prefix for .pcap anf .xml files. Default: IOT ", saveFilePrefix);
//
// The system global variables and the local values added to the argument
// system can be overridden by command line arguments by using this call.
//
cmd.Parse (argc, argv);
if (stopTime < 2)
{
std::cout << "Use a simulation stop time >= 2 seconds" << std::endl;
exit (1);
}
if (verbose)
{
//LogComponentEnable("UdpClient", LOG_LEVEL_INFO);
//LogComponentEnable("UdpServer", LOG_LEVEL_INFO);
LogComponentEnable("Simulator", LOG_LEVEL_INFO);
}
//Since default reference loss is defined for 5 GHz, it needs to be changed when operating at 2.4 GHz
Config::SetDefault ("ns3::LogDistancePropagationLossModel::ReferenceLoss", DoubleValue (40.046));
// creating nodes
//NodeContainer allNodes;
NodeContainer wifiUserNodes;
wifiUserNodes.Create (mobileUserNodes);
//allNodes.Add (wifiUserNodes);
NodeContainer wifiDeviceNodes;
wifiDeviceNodes.Create (smartDeviceNodes);
//allNodes.Add (wifiDeviceNodes);
NodeContainer wifiGateway ;
wifiGateway.Create (1);
//allNodes.Add (wifiGateway);
// creating wireless channel
YansWifiChannelHelper channel = YansWifiChannelHelper::Default ();
YansWifiPhyHelper phy = YansWifiPhyHelper::Default ();
phy.SetChannel (channel.Create ());
WifiHelper wifi;
wifi.SetRemoteStationManager ("ns3::AarfWifiManager");
WifiMacHelper mac;
Ssid ssid = Ssid ("ns-3-ssid");
mac.SetType ("ns3::StaWifiMac",
"Ssid", SsidValue (ssid),
"ActiveProbing", BooleanValue (false));
NetDeviceContainer UserDevices;
UserDevices = wifi.Install (phy, mac, wifiUserNodes);
mac.SetType ("ns3::StaWifiMac",
"Ssid", SsidValue (ssid));
NetDeviceContainer SmartDevices;
SmartDevices = wifi.Install (phy, mac, wifiDeviceNodes);
mac.SetType ("ns3::ApWifiMac",
"Ssid", SsidValue (ssid));
NetDeviceContainer apDevices;
apDevices = wifi.Install (phy, mac, wifiGateway);
// defining Mobility
MobilityHelper mobility;
mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
"MinX", DoubleValue (-10.0),
"MinY", DoubleValue (-10.0),
"DeltaX", DoubleValue (5.0),
"DeltaY", DoubleValue (5.0),
"GridWidth", UintegerValue (5),
"LayoutType", StringValue ("RowFirst"));
//mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
// "Bounds", RectangleValue (Rectangle (-50, 50, -25, 50)));
mobility.SetMobilityModel ("ns3::RandomDirection2dMobilityModel",
"Bounds", RectangleValue (Rectangle (-150, 150, -150, 150)),
"Speed", StringValue ("ns3::ConstantRandomVariable[Constant=3]"),
"Pause", StringValue ("ns3::ConstantRandomVariable[Constant=0.4]"));
mobility.Install (wifiUserNodes);
/*MobilityModel mobilityModel = MobilityModel ();
mobilityModel.SetAttribute ("Bounds", RectangleValue (Rectangle (-150, 150, -150, 150)));
mobilityModel.SetAttribute ("Pause", StringValue ("ns3::ConstantRandomVariable[Constant=0.4]"));
for (uint32_t j = 0; j < wifiUserNodes.GetN (); ++j){
if(j==0)
mobilityModel.SetAttribute ("Speed", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
else if(j==1)
mobilityModel.SetAttribute ("Speed", StringValue ("ns3::ConstantRandomVariable[Constant=2]"));
else
mobilityModel.SetAttribute ("Speed", StringValue ("ns3::ConstantRandomVariable[Constant=15]"));
mobility.Install (wifiUserNodes.Get(j));
//std::cout<<RandomMobilityModel.GetAttribute("Speed", StringValue ("ns3::ConstantRandomVariable[Constant=15]"))<<std::endl;
}
*/
Ptr<ListPositionAllocator> subnetAlloc = CreateObject<ListPositionAllocator> ();
subnetAlloc->Add (Vector (0.0, 0.0, 0.0)); //for gateway
for (uint32_t j = 0; j < wifiDeviceNodes.GetN (); ++j){
double theta = (j)*360/wifiDeviceNodes.GetN();
uint32_t r =((double)rand() / (RAND_MAX))*80 +20;
subnetAlloc->Add (Vector (sin(theta)*r, cos(theta)*r, 0.0));
//std::cout <<"[ "<<sin(theta)*r<<","<< cos(theta)*r<<","<< theta<<"]"<<"r = "<<r<<std::endl;
}
mobility.SetPositionAllocator (subnetAlloc);
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
mobility.Install (wifiGateway);
mobility.Install (wifiDeviceNodes);
// Installing internet stack
InternetStackHelper stack;
OlsrHelper olsr;
stack.SetRoutingHelper (olsr); // has effect on the next Install ()??
stack.Install (wifiUserNodes);
stack.Install (wifiDeviceNodes);
stack.Install (wifiGateway);
// Install Ipv4 addresses
Ipv4AddressHelper address;
address.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer apInterface;
apInterface = address.Assign (apDevices);
//Ipv4InterfaceContainer userInterfaces;
apInterface = address.Assign (UserDevices);
//Ipv4InterfaceContainer deviceInterfaces;
apInterface = address.Assign (SmartDevices);
// crating applications
ApplicationContainer serverAppContainer, clientAppContainer;
uint16_t port = 9; // well-known echo port number
Ptr<Node> gateway = wifiGateway.Get (0);
UdpServerHelper server(port);
serverAppContainer.Add(server.Install (gateway));
double time = 1;
for (uint32_t i = 0; i < wifiUserNodes.GetN (); ++i){
Ptr<Node> user = wifiUserNodes.Get (i);
for (uint32_t j = 0; j < wifiDeviceNodes.GetN (); ++j){
Ptr<Node> device = wifiDeviceNodes.Get (j);
if(i==0){
serverAppContainer.Add(server.Install (device));
//std::cout <<"device "<<j<<std::endl;
}
clientAppContainer = authenticate(clientAppContainer, time , user, gateway , device );
//time = time +.2;
//std::cout <<time<<std::endl;
}
serverAppContainer.Add(server.Install (user));
//time = time +.1;
//std::cout <<"user "<<i<<std::endl;
}
serverAppContainer.Start (Seconds (0.0));
serverAppContainer.Stop (Seconds (stopTime+1));
//clientAppContainer.Start (Seconds (1.0)); //started induvugualy
clientAppContainer.Stop (Seconds (stopTime+1));
if (verbose){
std::cout <<"servers stops at "<<stopTime+1<<std::endl;
std::cout <<"final transmission scheduled at "<<(time-.33)<<std::endl;
std::cout << "server apps installed till now :"<<serverAppContainer.GetN ()<< std::endl;
std::cout << "client apps installed till now :"<<clientAppContainer.GetN ()<< std::endl;
}
snprintf(saveFilePrefix, 50, "IOT_%dx%d_", mobileUserNodes, smartDeviceNodes);
if (enablePcap){
//NS_LOG_INFO ("Configure Tracing.");
//
// Let's set up some ns-2-like ascii traces, using another helper class
//
//AsciiTraceHelper ascii;
//Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream (stringbuilder(saveFilePrefix,(char*)"_trace.tr"));
//phy.EnableAsciiAll (stream);
//stack.EnableAsciiIpv4All (stream);
phy.EnablePcap (stringbuilder(saveFilePrefix,(char*)"_users"), UserDevices, 0);
phy.EnablePcap (stringbuilder(saveFilePrefix,(char*)"_devices"), SmartDevices, 0);
phy.EnablePcap (stringbuilder(saveFilePrefix,(char*)"_gateway"), apDevices, 0);
}
if(enableAnim) {
AnimationInterface anim (stringbuilder(saveFilePrefix,(char*)"-animation.xml")); // Mandatory
for (uint32_t i = 0; i < wifiUserNodes.GetN (); ++i)
{
anim.UpdateNodeDescription (wifiUserNodes.Get (i), "MU"); // Optional
anim.UpdateNodeColor (wifiUserNodes.Get (i), 255, 0, 0); // Optional
}
for (uint32_t i = 0; i < wifiDeviceNodes.GetN (); ++i)
{
anim.UpdateNodeDescription (wifiDeviceNodes.Get (i), "SD"); // Optional
anim.UpdateNodeColor (wifiDeviceNodes.Get (i), 255, 255, 0); // Optional
}
for (uint32_t i = 0; i < wifiGateway.GetN (); ++i)
{
anim.UpdateNodeDescription (wifiGateway.Get (i), "Gateway"); // Optional
anim.UpdateNodeColor (wifiGateway.Get (i), 0, 255, 0); // Optional
}
//anim.EnablePacketMetadata (); // Optional/
anim.EnableWifiMacCounters (Seconds (0), Seconds (10)); //Optional
anim.EnableWifiPhyCounters (Seconds (0), Seconds (10)); //Optional
}
//Populate routing table
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
// setting up simulator
Ptr<FlowMonitor> flowMonitor;
FlowMonitorHelper flowHelper;
flowMonitor = flowHelper.InstallAll();
Simulator::Stop (Seconds (stopTime+1));
Simulator::Run ();
Simulator::Destroy ();
flowMonitor->SerializeToXmlFile(stringbuilder(saveFilePrefix,(char*)"_flowMonitor.xml"), false, false);
uint32_t bytes_received = 0;
for (uint32_t i = 0; i < serverAppContainer.GetN (); ++i){
char nodename[30+sizeof(serverAppContainer.GetN ())*8];
uint32_t expected=1;
uint32_t totalPacketsThrough = DynamicCast<UdpServer> (serverAppContainer.Get (i))->GetReceived ();;
if (i==0){
snprintf(nodename, sizeof(nodename), "gateway\t");
expected = smartDeviceNodes * mobileUserNodes;
bytes_received += expected*84;
}else if (i<smartDeviceNodes+1){
snprintf(nodename, sizeof(nodename), "smart device %d ", i);
expected = mobileUserNodes ;
bytes_received += expected*124;
}else{
snprintf(nodename, sizeof(nodename), "mobile user %d ", i-smartDeviceNodes);
expected = smartDeviceNodes;
bytes_received += expected*0;
}
std::cout <<"Number of packets received at "<<nodename<<"\t : " << totalPacketsThrough <<" / "<< expected << std::endl;
//uint32_t lost = DynamicCast<UdpServer> (serverAppContainer.Get (i))->GetLost ();
//uint32_t window = DynamicCast<UdpServer> (serverAppContainer.Get (i))->GetPacketWindowSize ();
//std::cout <<"\t. Packets lost: " << lost <<"( "<<window<<" ) "<< std::endl;
}
std::cout <<"Total bytes received ("<<mobileUserNodes<<" , "<<smartDeviceNodes<<") : "<< bytes_received << std::endl;
return 0;
}