forked from Narendra-Kamath/NS3-SIMPLE-ROUTING-AODV-DSR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
naren-aodv.cc
125 lines (103 loc) · 4.06 KB
/
naren-aodv.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
#include "ns3/aodv-module.h"
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/mobility-module.h"
#include "ns3/wifi-module.h"
#include "ns3/applications-module.h"
#include "ns3/netanim-module.h"
#include "ns3/flow-monitor-module.h"
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <cmath>
using namespace ns3;
int packetsSent = 0;
int packetsReceived = 0;
void ReceivePacket (Ptr<Socket> socket)
{
Ptr<Packet> packet;
while ((packet = socket->Recv ()))
{
packetsReceived++;
std::cout<<"Received packet - "<<packetsReceived<<" and Size is "<<packet->GetSize ()<<" Bytes."<<std::endl;
}
}
static void GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize,
uint32_t pktCount, Time pktInterval )
{
if (pktCount > 0)
{
socket->Send (Create<Packet> (pktSize));
packetsSent++;
std::cout<<"Packet sent - "<<packetsSent<<std::endl;
Simulator::Schedule (pktInterval, &GenerateTraffic,
socket, pktSize,pktCount-1, pktInterval);
}
else
{
socket->Close ();
}
}
int main(int argc, char **argv)
{
uint32_t size=50;
double step=100;
double totalTime=100;
int packetSize = 1024;
int totalPackets = totalTime-1;
double interval = 1.0;
Time interPacketInterval = Seconds (interval);
NodeContainer nodes;
NetDeviceContainer devices;
Ipv4InterfaceContainer interfaces;
std::cout << "Creating " << (unsigned)size << " nodes " << step << " m apart.\n";
nodes.Create (size);
MobilityHelper mobility;
mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel",
"Bounds", RectangleValue (Rectangle (-50, 50, -25, 50)));
mobility.Install (nodes);
NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
wifiMac.SetType ("ns3::AdhocWifiMac");
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
wifiPhy.SetChannel (wifiChannel.Create ());
WifiHelper wifi = WifiHelper::Default ();
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate6Mbps"), "RtsCtsThreshold", UintegerValue (0));
devices = wifi.Install (wifiPhy, wifiMac, nodes);
AodvHelper aodv;
InternetStackHelper stack;
stack.SetRoutingHelper (aodv);
stack.Install (nodes);
Ipv4AddressHelper address;
address.SetBase ("10.0.0.0", "255.0.0.0");
interfaces = address.Assign (devices);
Ipv4GlobalRoutingHelper::PopulateRoutingTables ();
TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
Ptr<Socket> recvSink = Socket::CreateSocket (nodes.Get (size-1), tid);
InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 8080);
recvSink->Bind (local);
recvSink->SetRecvCallback (MakeCallback (&ReceivePacket));
Ptr<Socket> source = Socket::CreateSocket (nodes.Get (0), tid);
InetSocketAddress remote = InetSocketAddress (interfaces.GetAddress (size-1,0), 8080);
source->Connect (remote);
Simulator::Schedule (Seconds (1), &GenerateTraffic, source, packetSize, totalPackets, interPacketInterval);
std::cout << "Starting simulation for " << totalTime << " s ...\n";
AnimationInterface anim ("scratch/aodv-output.xml");
Ptr<FlowMonitor> flowmon;
FlowMonitorHelper flowmonHelper;
flowmon = flowmonHelper.InstallAll ();
Simulator::Stop (Seconds (totalTime));
Simulator::Run ();
flowmon->SetAttribute("DelayBinWidth", DoubleValue(0.01));
flowmon->SetAttribute("JitterBinWidth", DoubleValue(0.01));
flowmon->SetAttribute("PacketSizeBinWidth", DoubleValue(1));
flowmon->CheckForLostPackets();
flowmon->SerializeToXmlFile("scratch/aodv-flow.xml", true, true);
Simulator::Destroy ();
std::cout<<"\n\n***** OUTPUT *****\n\n";
std::cout<<"Total Packets sent = "<<packetsSent<<std::endl;
std::cout<<"Total Packets received = "<<packetsReceived<<std::endl;
std::cout<<"Packet delivery ratio = "<<(float)(packetsReceived/packetsSent)*100<<" %"<<std::endl;
}