-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstaticnodeolsr.cc
638 lines (575 loc) · 25 KB
/
staticnodeolsr.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
//double RL_rx_pack,RL_tx_pack,RL_rx_bytes,RL_tx_bytes;
#include "ns3/ipv4.h"
#include "ns3/ipv4-route.h"
#include "ns3/core-module.h"
#include "ns3/simulator-module.h"
#include "ns3/node-module.h"
#include "ns3/helper-module.h"
#include "ns3/global-routing-module.h"
#include "ns3/wifi-module.h"
#include "ns3/mesh-module.h"
#include "ns3/mobility-module.h"
#include "ns3/mesh-helper.h"
#include "ns3/mesh-module.h"
#include "ns3/wifi-phy.h"
#include "ns3/yans-wifi-helper.h"
#include "ns3/wifi-phy.h"
#include "ns3/flow-monitor.h"
#include "ns3/flow-monitor-helper.h"
#include "ns3/ipv4-flow-classifier.h"
#include "ns3/random-variable.h"
#include "ns3/energy-module.h"
#include "ns3/ipv4-routing-table-entry.h"
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>
#include "ns3/olsr-header.h"
#include "ns3/olsr-helper.h"
#include "ns3/olsr-repositories.h"
#include "ns3/simulator-module.h"
#include "ns3/olsr-routing-protocol.h"
#include "ns3/olsr-state.h"
using namespace ns3;
double RL_rx_pack, RL_tx_pack, RL_rx_bytes, RL_tx_bytes;
void RemainingEnergy (double oldValue, double remainingEnergy);
void TotalEnergy (double oldValue, double totalEnergy);
int t=0;
EnergySourceContainer sources;
class EnergyOlsr
{
public:
// Init test
EnergyOlsr ();
// Configure test from command line arguments
void Configure (int argc, char ** argv);
// Run test
int Run ();
private:
int
m_xSize; //x size of the grid
int
m_ySize; //y size of the grid
double
m_step; //separation between nodes
bool printRoutes;
double
m_totalTime;
uint32_t m_packetSize;
uint32_t apnodes;
uint32_t mobnodes;
bool
pcap;
std::string m_txrate;
double
m_txrate_dob;
//to calculate the lenght of the simulation
float m_timeTotal, m_timeStart, m_timeEnd;
// List of network nodes
NodeContainer nodes;
// List of all wifi devices
NetDeviceContainer wifiDevices;
//Addresses of interfaces:
Ipv4InterfaceContainer interfaces;
// MeshHelper. Report is not static methods
WifiHelper wifi_olsr;
private:
// Create nodes and setup their mobility
void CreateNodes ();
// Install internet m_stack on nodes
void InstallInternetStack ();
// Install applications randomly
void InstallApplicationRandom ();
};
EnergyOlsr::EnergyOlsr() :
m_xSize (5),
m_ySize (5),
m_step (170),
printRoutes (true),
m_totalTime (240),
m_packetSize (256),
pcap (true),
m_txrate ("10kbps"),
m_txrate_dob (150) //needed in kbps for the trace file
{
}
void
EnergyOlsr::Configure (int argc, char *argv[])
{
CommandLine cmd;
cmd.AddValue ("pcap", "Write PCAP traces.", pcap);
cmd.AddValue ("m_xSize", "m_xSize", m_xSize);
cmd.AddValue ("m_ySize", "m_ySize", m_ySize);
cmd.AddValue ("m_txrate", "m_txrate", m_txrate);
cmd.AddValue ("m_txrate_dob", "m_txrate_dob", m_txrate_dob);
cmd.Parse (argc, argv);
}
// remaining energy calculation
void RemainingEnergy (double oldValue, double remainingEnergy)
{
double speed;
ofstream outfile1;
ofstream outfile3;
outfile1.open("flows_drain_speed7.txt", ios::app);
outfile3.open("nod_remain_energy7.txt", ios::app);
NS_LOG_UNCOND (Simulator::Now().GetSeconds ()<< "s Current remaining energy = " << remainingEnergy << "J");
if(remainingEnergy == 0.0546874)
{
NS_LOG_UNCOND("BATTERY IS LOW....,Change path");
outfile3<<"battery is low..... change node";
}
// Energy draining speed calculation
speed=(oldValue-remainingEnergy)/(Simulator::Now ().GetSeconds ()-(Simulator::Now ().GetSeconds ()-1));
outfile1 <<"nodes draining speed "<<speed<<"\n";
if(speed<8.0000e-06)
{
NS_LOG_UNCOND("draining speed is high....,Change node");
outfile1<<"draining speed is high....,Change node";
}
if(remainingEnergy>0.0543445)
outfile3 <<Simulator::Now().GetSeconds ()<< "s Current remaining energy = " << remainingEnergy << "J"<< "\n";
outfile1.close();
outfile3.close();
}
void TotalEnergy (double oldValue, double totalEnergy)
{
NS_LOG_UNCOND ( Simulator::Now().GetSeconds() <<"s Total energyconsumed by radio = " << totalEnergy << "J");
}
void EnergyOlsr::CreateNodes ()
{
int i, j;
double m_txpower = 18.0; // dbm
// Calculate m_ySize*m_xSize stations grid topology
double position_x = 0;
double position_y = 0;
ListPositionAllocator myListPositionAllocator;
for (i = 1; i <= m_xSize; i++){
for (j = 1; j <= m_ySize; j++){
std::cout << "Node at x = " << position_x << ", y = " << position_y << "\n";
Vector3D n_pos (position_x, position_y, 0.0);
myListPositionAllocator.Add(n_pos);
position_y += m_step;
}
position_y = 0;
position_x += m_step;
}
// Create the nodes
nodes.Create (m_xSize*m_ySize);
// Configure YansWifiChannel
YansWifiPhyHelper WifiPhy = YansWifiPhyHelper::Default ();
WifiPhy.Set("EnergyDetectionThreshold", DoubleValue (-89.0) );
WifiPhy.Set("CcaMode1Threshold", DoubleValue (-62.0) );
WifiPhy.Set("TxGain", DoubleValue (1.0) );
WifiPhy.Set("RxGain", DoubleValue (1.0) );
WifiPhy.Set("TxPowerLevels", UintegerValue (1) );
WifiPhy.Set("TxPowerEnd", DoubleValue (m_txpower) );
WifiPhy.Set("TxPowerStart", DoubleValue (m_txpower) );
WifiPhy.Set("RxNoiseFigure", DoubleValue (7.0) );
YansWifiChannelHelper WifiChannel;
WifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
WifiChannel.AddPropagationLoss ("ns3::LogDistancePropagationLossModel","Exponent",
StringValue ("2.7"));
WifiPhy.SetChannel (WifiChannel.Create ());
wifi_olsr.SetStandard (WIFI_PHY_STANDARD_80211a);
wifi_olsr.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode",
StringValue ("OfdmRate6Mbps"), "RtsCtsThreshold", UintegerValue (2500));
// Install protocols and return container
NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default();
wifiMac.SetType ("ns3::AdhocWifiMac");
wifiDevices = wifi_olsr.Install (WifiPhy, wifiMac, nodes);
if (pcap)
{
WifiPhy.EnablePcapAll (std::string ("energy-aodv"));
}
// Place the protocols in the positions calculated before
MobilityHelper mobility;
mobility.SetPositionAllocator(&myListPositionAllocator);
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
mobility.Install (nodes);
}
void EnergyOlsr::InstallInternetStack ()
{
//configure OLSR
OlsrHelper olsr;
// you can configure olsr attributes here using olsr.Set(name, value)
InternetStackHelper stack;
stack.SetRoutingHelper (olsr);
stack.Install(nodes);
// stack.Install(mobnodes);
Ipv4AddressHelper address;
address.SetBase ("192.168.1.0", "255.255.255.0");
interfaces = address.Assign (wifiDevices);
if (printRoutes)
{
Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ("energy-olsr.routes", std::ios::out);
olsr.PrintRoutingTableAllAt(Seconds(20),routingStream);
}
}
void EnergyOlsr::InstallApplicationRandom ()
{
// Create as many connections as nodes has the grid
int m_nconn = m_xSize * m_ySize;
int i=0;
int m_source, m_dest, m_dest_port;
char num [2];
char onoff [7];
char sink [6];
double start_time, stop_time, duration;
// Set the parameters of the onoff application
Config::SetDefault ("ns3::OnOffApplication::PacketSize",
UintegerValue (m_packetSize));
Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue (m_txrate));
ApplicationContainer apps [m_nconn];
UniformVariable rand_nodes (0,m_ySize*m_xSize-1);
UniformVariable rand_port (49000,49100);
// 50 seconds for transitori are left at the beginning.
UniformVariable a(50,m_totalTime-15);
for (i = 0; i < m_nconn; i++){
start_time = a.GetValue();
ExponentialVariable b(30);
duration = b.GetValue()+1;
//
//
//
//if
//If the exponential variable gives us a value that added to the start time
//is greater than the maximum permitted, this is changed for the maximum
//10 seconds are left at the end to calculate well the statistics of each flow
if( (start_time + duration) > (m_totalTime - 10)){
stop_time = m_totalTime-10;
}else{
stop_time = start_time + duration;
}
// Create different names for the connections
// (we can not use vectors for OnOffHelper)
strcpy(onoff,"onoff");
strcpy(sink,"sink");
sprintf(num,"%d",i);
strcat(onoff,num);
strcat(sink,num);
// Set random variables of the destination (server) and destination port.
m_dest = rand_nodes.GetInteger (0,m_ySize*m_xSize-1);
m_dest_port = rand_port.GetInteger (49000,49100);
// Set random variables of the source (client)
m_source = rand_nodes.GetInteger (0,m_ySize*m_xSize-1);
// Client and server can not be the same node.
while (m_source == m_dest){
m_source = rand_nodes.GetInteger (0,m_ySize*m_xSize-1);
}
// Plot the connection values
std::cout << "\n\t Node "<< m_source << " to " << m_dest;
std::cout << "\n Start_time: " << start_time << "s";
std::cout << "\n Stop_time: " << stop_time << "s\n";
// Define UDP traffic for the onoff application
OnOffHelper onoff ("ns3::UdpSocketFactory", Address (InetSocketAddress
(interfaces.GetAddress (m_dest), m_dest_port)));
onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1)));
onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0)));
apps[i] = onoff.Install (nodes.Get(m_source));
apps[i].Start (Seconds (start_time));
apps[i].Stop (Seconds (stop_time));
// Create a packet sink to receive the packets
PacketSinkHelper sink ("ns3::UdpSocketFactory",InetSocketAddress
(interfaces.GetAddress (m_dest), 49001));
apps[i] = sink.Install (nodes.Get (m_dest));
apps[i].Start (Seconds (1.0));
}
BasicEnergySourceHelper basicSourceHelper;
basicSourceHelper.Set("BasicEnergySourceInitialEnergyJ",
DoubleValue(0.1));
sources = basicSourceHelper.Install(nodes);
WifiRadioEnergyModelHelper radioEnergyHelper;
radioEnergyHelper.Set("TxCurrentA", DoubleValue(0.0174));
DeviceEnergyModelContainer deviceModels =
radioEnergyHelper.Install(wifiDevices, sources);
Ptr<BasicEnergySource> basicSourcePtr = DynamicCast<BasicEnergySource>
(sources.Get(0));
Ptr<BasicEnergySource> basicSourcePtr1 = DynamicCast<BasicEnergySource>
(sources.Get(1));
Ptr<BasicEnergySource> basicSourcePtr2 = DynamicCast<BasicEnergySource>
(sources.Get(2));
Ptr<BasicEnergySource> basicSourcePtr3 = DynamicCast<BasicEnergySource>
(sources.Get(3));
Ptr<BasicEnergySource> basicSourcePtr4 = DynamicCast<BasicEnergySource>
(sources.Get(4));
Ptr<BasicEnergySource> basicSourcePtr5 = DynamicCast<BasicEnergySource>
(sources.Get(5));
Ptr<BasicEnergySource> basicSourcePtr6 = DynamicCast<BasicEnergySource>
(sources.Get(6));
Ptr<BasicEnergySource> basicSourcePtr7 = DynamicCast<BasicEnergySource>
(sources.Get(7));
Ptr<BasicEnergySource> basicSourcePtr8 = DynamicCast<BasicEnergySource>
(sources.Get(8));
Ptr<BasicEnergySource> basicSourcePtr9 = DynamicCast<BasicEnergySource>
(sources.Get(9));
Ptr<BasicEnergySource> basicSourcePtr10 = DynamicCast<BasicEnergySource>
(sources.Get(10));
Ptr<BasicEnergySource> basicSourcePtr11 = DynamicCast<BasicEnergySource>
(sources.Get(11));
Ptr<BasicEnergySource> basicSourcePtr12 = DynamicCast<BasicEnergySource>
(sources.Get(12));
Ptr<BasicEnergySource> basicSourcePtr13 = DynamicCast<BasicEnergySource>
(sources.Get(13));
Ptr<BasicEnergySource> basicSourcePtr14 = DynamicCast<BasicEnergySource>
(sources.Get(14));
Ptr<BasicEnergySource> basicSourcePtr15 = DynamicCast<BasicEnergySource>
(sources.Get(15));
Ptr<BasicEnergySource> basicSourcePtr16 = DynamicCast<BasicEnergySource>
(sources.Get(16));
Ptr<BasicEnergySource> basicSourcePtr17 = DynamicCast<BasicEnergySource>
(sources.Get(17));
Ptr<BasicEnergySource> basicSourcePtr18 = DynamicCast<BasicEnergySource>
(sources.Get(18));
Ptr<BasicEnergySource> basicSourcePtr19 = DynamicCast<BasicEnergySource>
(sources.Get(19));
Ptr<BasicEnergySource> basicSourcePtr20 = DynamicCast<BasicEnergySource>
(sources.Get(20));
Ptr<BasicEnergySource> basicSourcePtr21 = DynamicCast<BasicEnergySource>
(sources.Get(21));
Ptr<BasicEnergySource> basicSourcePtr22 = DynamicCast<BasicEnergySource>
(sources.Get(22));
Ptr<BasicEnergySource> basicSourcePtr23 = DynamicCast<BasicEnergySource>
(sources.Get(23));
Ptr<BasicEnergySource> basicSourcePtr24 = DynamicCast<BasicEnergySource>
(sources.Get(24));
ofstream outfile3;
outfile3.open("nod_remain_energy.txt", ios::app);
outfile3<<"node 1";
basicSourcePtr -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
outfile3<<"node 2";
basicSourcePtr1 -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
outfile3<<"node 3";
basicSourcePtr2 -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
outfile3<<"node 4";
basicSourcePtr3 -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
outfile3<<"node 5";
basicSourcePtr4 -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
outfile3<<"node 6";
basicSourcePtr5 -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
outfile3<<"node 7";
basicSourcePtr6 -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
outfile3<<"node 8";
basicSourcePtr7 -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
outfile3<<"node 9";
basicSourcePtr8 -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
outfile3<<"node 10";
basicSourcePtr9 -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
outfile3<<"node 11";
basicSourcePtr10 -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
outfile3<<"node 12";
basicSourcePtr11 -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
outfile3<<"node 13";
basicSourcePtr12 -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
outfile3<<"node 14";
basicSourcePtr13 -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
outfile3<<"node 15";
basicSourcePtr14 -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
outfile3<<"node 16";
basicSourcePtr15 -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
outfile3<<"node 17";
basicSourcePtr16 -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
outfile3<<"node 18";
basicSourcePtr17 -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
outfile3<<"node 19";
basicSourcePtr18 -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
outfile3<<"node 20";
basicSourcePtr19 -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
outfile3<<"node 21";
basicSourcePtr20 -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
outfile3<<"node 22";
basicSourcePtr21 -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
outfile3<<"node 23";
basicSourcePtr22 -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
outfile3<<"node 24";
basicSourcePtr23 -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
outfile3<<"node 25";
basicSourcePtr24 -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
Ptr<DeviceEnergyModel> basicRadioModelPtr = basicSourcePtr ->FindDeviceEnergyModels ("ns3::WifiRadioEnergyModel").Get(0);
NS_ASSERT (basicRadioModelPtr != NULL);
basicRadioModelPtr ->TraceConnectWithoutContext("TotalEnergyConsumption",MakeCallback(&TotalEnergy));
Ptr<DeviceEnergyModel> basicRadioModelPtr1 = basicSourcePtr1 ->FindDeviceEnergyModels ("ns3::WifiRadioEnergyModel").Get(0);
NS_ASSERT (basicRadioModelPtr != NULL);
basicRadioModelPtr1 ->TraceConnectWithoutContext("TotalEnergyConsumption",MakeCallback(&TotalEnergy));
}
int EnergyOlsr::Run ()
{
CreateNodes ();
InstallInternetStack ();
InstallApplicationRandom ();
// Install FlowMonitor on all nodes
FlowMonitorHelper flowmon;
Ptr<FlowMonitor> monitor = flowmon.InstallAll();
m_timeStart=clock();
Simulator::Stop (Seconds (m_totalTime));
Simulator::Run ();
// Define variables to calculate the metrics
int k=0;
int totaltxPackets = 0;
int totalrxPackets = 0;
int totaltxPacketsR = 0;
int totalrxPacketsR = 0;
double totaltxbytes = 0;
double totalrxbytes = 0;
double totaltxbytesR = 0;
double totalrxbytesR = 0;
double totaldelay = 0;
double totalrxbitrate = 0;
double difftx, diffrx,DropPacketsum;
double pdf_value, rxbitrate_value, txbitrate_value, delay_value;
double pdf_total, rxbitrate_total, delay_total;
//Print per flow statistics
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)
{
Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first);
difftx = i->second.timeLastTxPacket.GetSeconds() -
i->second.timeFirstTxPacket.GetSeconds();
diffrx = i->second.timeLastRxPacket.GetSeconds() -
i->second.timeFirstRxPacket.GetSeconds();
pdf_value = (double) i->second.rxPackets / (double) i->second.txPackets * 100;
txbitrate_value = (double) i->second.txBytes * 8 / 1024 / difftx;
if (i->second.rxPackets != 0){
rxbitrate_value = (double) i->second.rxPackets * m_packetSize * 8
/ 1024 / diffrx;
delay_value = (double) i->second.delaySum.GetSeconds() /
(double) i->second.rxPackets;
DropPacketsum+=i->second.packetsDropped.size();
}
else{
rxbitrate_value = 0;
delay_value = 0;
}
std::map<Ipv4Address, olsr::RoutingTableEntry>m_ipv4AddressEntry;
for (std::map<Ipv4Address, olsr::RoutingTableEntry>::const_iterator iter = m_ipv4AddressEntry.begin (); iter != m_ipv4AddressEntry.end (); ++iter)
{
std::stringstream ipStream;
//iter->second.nextAddr().Print(ipStream);
std::string ip=ipStream.str();
std::stringstream s(ip);
int a,b,c,d;
char ch;
s >> a >> ch >> b >> ch >> c >> ch >>d;
std::cout<<a<<b<<c<<d;
Ptr<BasicEnergySource> basicSourcePtr = DynamicCast<BasicEnergySource>
(sources.Get(d));
basicSourcePtr -> TraceConnectWithoutContext("RemainingEnergy",MakeCallback(&RemainingEnergy));
}
ofstream outfile4;
outfile4.open("path.txt", ios::app);
// We are only interested in the metrics of the data flows. This OLSR
// implementation create other flows with routing information at low bitrates,
// so a margin is defined to ensure that only our data flows are filtered.
if ( (!t.destinationAddress.IsSubnetDirectedBroadcast("255.255.255.0")) &&
(txbitrate_value > m_txrate_dob/1.2) && (rxbitrate_value < m_txrate_dob*1.2))
{
k++;
std::cout <<k;
std::cout << "\nFlow " << k << " (" << t.sourceAddress << " -> "<< t.destinationAddress << ")\n";
outfile4 << "\nFlow " << k << " (" << t.sourceAddress << " -> "<< t.destinationAddress << ")\n";
outfile4.close();
std::cout<<"packets dropped: "<<DropPacketsum<<"\n";
//std::cout << "Tx Packets:" << i->second.txPackets << "\n";
//std::cout << "Rx Packets:" << i->second.rxPackets << "\n";
std::cout << "Lost Packets:" << i->second.lostPackets << "\n";
//std::cout << "Dropped Packets:" << i->second.packetsDropped.size() << "\n";
std::cout << "PDF: " << pdf_value << " %\n";
std::cout << "Average delay:" << delay_value << "s\n";
std::cout << "Rx bitrate: " << rxbitrate_value << " kbps\n";
std::cout << "Tx bitrate: " << txbitrate_value << " kbps\n\n";
std::cout<<"intermediate nodes: "<<(i->second.timesForwarded/i->second.rxPackets)<<"\n";
//std::cout<<"next hop: "<<t.sourceAddress.GetNextHop();
// Acumulate for average statistics
totaltxPackets += i->second.txPackets;
totaltxbytes += i->second.txBytes;
totalrxPackets += i->second.rxPackets;
totaldelay += i->second.delaySum.GetSeconds();
totalrxbitrate += rxbitrate_value;
totalrxbytes += i->second.rxBytes;
}
else{
totaltxbytesR += i->second.txBytes;
totalrxbytesR += i->second.rxBytes;
totaltxPacketsR += i->second.txPackets;
totalrxPacketsR += i->second.rxPackets;
}
}
//Average all nodes statistics
if (totaltxPackets != 0){
pdf_total = (double) totalrxPackets / (double) totaltxPackets * 100;
RL_tx_pack = (double) totaltxPacketsR / (double) totaltxPackets;
RL_tx_bytes = totaltxbytesR / totaltxbytes;
}
else{
pdf_total = 0;
RL_tx_pack = 0;
RL_tx_bytes = 0;
}
if (totalrxPackets != 0){
rxbitrate_total = totalrxbitrate;
delay_total = (double) totaldelay / (double) totalrxPackets;
RL_rx_pack = (double) totalrxPacketsR / (double) totalrxPackets;
RL_rx_bytes = totalrxbytesR / totalrxbytes;
}
else{
rxbitrate_total = 0;
delay_total = 0;
RL_rx_pack = 0;
RL_rx_bytes = 0;
}
// Print allnodes statistics
std::cout <<"\nTotal PDF: " << pdf_total << " %\n";
std::cout <<"Total Rx bitrate: " << rxbitrate_total << " kbps\n";
std::cout <<"Total Delay: " << delay_total << " s\n";
// Print all nodes statistics in files
std::ostringstream os;
os << "1_OLSR_PDF.txt";
std::ofstream of (os.str().c_str(), ios::out | ios::app);
of << pdf_total << "\n";
std::ostringstream os2;
os2 << "1_OLSR_Delay.txt";
std::ofstream of2 (os2.str().c_str(), ios::out | ios::app);
of2 << delay_total << "\n";
std::ostringstream os3;
os3 << "1_OLSR_Throu.txt";
std::ofstream of3 (os3.str().c_str(), ios::out | ios::app);
of3 << rxbitrate_total << "\n";
std::ostringstream os5;
os5 << "1_OLSR_RL_RxBytes.txt";
std::ofstream of5 (os5.str().c_str(), ios::out | ios::app);
of5 << RL_rx_bytes << "\n";
std::ostringstream os7;
os7 << "1_OLSR_RL_RxPack.txt";
std::ofstream of7 (os7.str().c_str(), ios::out | ios::app);
of7 << RL_rx_pack << "\n";
of.close (); of2.close (); of3.close (); of5.close (); of7.close ();
Simulator::Destroy ();
m_timeEnd=clock();
m_timeTotal=(m_timeEnd - m_timeStart)/(double) CLOCKS_PER_SEC;
std::cout << "\n*** Simulation time: " << m_timeTotal << "s\n\n";
return 0;
}
void
PrintRoutingTable (Ptr<Node> node)
{
Ptr<RoutingProtocol> routing = node->GetObject<RoutingProtocol>(); // Print routing table entries for OLSR routing
std::vector<RoutingTableEntry> entry = routing->GetRoutingTableEntries();
// std::cout << "Routing table for device: " << Names::FindName(node) << std::endl;
std::cout << "Routing table for device: " << node->GetId() << std::endl;
std::cout << "DestinyAddress\t\tNextAddress\t\tInterface\t\tDistance \n";
for (std::vector<RoutingTableEntry, std::allocator<RoutingTableEntry> >::iterator i=entry.begin(); i!=entry.end(); i++)
{
std::cout << i->destAddr << "\t\t"<< i->nextAddr << "\t\t"<< i->interface << "\t\t"<< i->distance << std::endl;
}
}
int main (int argc, char *argv[])
{
EnergyOlsr Eo;
Eo.Configure (argc, argv);
Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ("wifi-simple-adhoc.routes", std::ios::out);
//olsr.PrintRoutingTableAllat(Seconds(20),routingStream);
return Eo.Run();
}