119CS0169 - Lab - Assignment - 8
119CS0169 - Lab - Assignment - 8
119CS0169
Network Simulation Lab Assignment – 8
In this, each node periodically floods status of its links. Each node re-broadcasts link state
information received from its neighbors. Each node keeps track of link state information received
from other nodes. Each node uses above information to determine next hope to each destination.
It is proactive and table-driven.
Route Discovery: This phase determines the most optimal path for the transmission of data
packets between the source and the destination mobile nodes.
Route Maintenance: This phase performs the maintenance work of the route as the topology in
the mobile ad-hoc network is dynamic in nature and hence, there are many cases of link breakage
resulting in the network failure between the mobile nodes.
Code:
/*
* This example program allows one to run ns-3 DSDV, AODV, or OLSR under * a typical random
waypoint mobility model.
*
* By default, the simulation runs for 200 simulated seconds, of which * the first 50 are used for
start-up time. The number of nodes is 50.
* Nodes move according to RandomWaypointMobilityModel with a speed of * 20 m/s and no
pause time within a 300x1500 m region. The WiFi is * in ad hoc mode with a 2 Mb/s rate
(802.11b) and a Friis loss model. * The transmit power is set to 7.5 dBm.
*
* It is possible to change the mobility and density of the network by
* directly modifying the speed and the number of nodes. It is also
* possible to change the characteristics of the network by changing * the transmit power (as
power increases, the impact of mobility * decreases and the effective density increases).
*
* By default, OLSR is used, but specifying a value of 2 for the protocol * will cause AODV to be
used, and specifying a value of 3 will cause * DSDV to be used.
*
* By default, there are 10 source/sink data pairs sending UDP data
* at an application rate of 2.048 Kb/s each. This is typically done
* at a rate of 4 64-byte packets per second. Application data is * started at a random time
between 50 and 51 seconds and continues * to the end of the simulation.
*
* The program outputs a few items:
* - packet receptions are notified to stdout such as:
* <timestamp> <node-id> received one packet from <src-address>
* - each second, the data reception statistics are tabulated and output * to a comma-separated
value (csv) file
* - some tracing and flow monitor configuration that used to work is
* left commented inline in the program */
#include <fstream>
#include <iostream>
#include "ns3/core-module.h"
#include "ns3/network-module.h"
#include "ns3/internet-module.h"
#include "ns3/mobility-module.h"
#include "ns3/aodv-module.h"
#include "ns3/olsr-module.h"
#include "ns3/dsdv-module.h"
#include "ns3/dsr-module.h"
#include "ns3/applications-module.h"
#include "ns3/yans-wifi-helper.h"
NS_LOG_COMPONENT_DEFINE ("manet-routing-compare");
class RoutingExperiment
{
public:
RoutingExperiment (); void Run (int nSinks, double txp,
std::string CSVfileName); //static void SetMACParam
(ns3::NetDeviceContainer & devices,
// int slotDistance);
std::string CommandSetup (int argc, char **argv);
private:
Ptr<Socket> SetupPacketReceive (Ipv4Address addr, Ptr<Node> node);
void ReceivePacket (Ptr<Socket> socket); void CheckThroughput ();
uint32_t port;
uint32_t bytesTotal;
uint32_t packetsReceived;
std::string m_CSVfileName;
int m_nSinks; std::string
m_protocolName; double
m_txp; bool
m_traceMobility; uint32_t
m_protocol; };
RoutingExperiment::RoutingExperiment ()
: port (9),
bytesTotal (0),
packetsReceived (0),
m_CSVfileName ("manet-routing.output.csv"),
m_traceMobility (false),
m_protocol (2) // AODV
{
}
if (InetSocketAddress::IsMatchingType (senderAddress))
{
InetSocketAddress addr = InetSocketAddress::ConvertFrom (senderAddress);
oss << " received one packet from " << addr.GetIpv4 ();
}
else
{
oss << " received one packet!";
}
return oss.str ();
}
void
RoutingExperiment::ReceivePacket (Ptr<Socket> socket)
{
Ptr<Packet> packet;
Address senderAddress;
while ((packet = socket->RecvFrom (senderAddress)))
{
bytesTotal += packet->GetSize ();
packetsReceived += 1;
NS_LOG_UNCOND (PrintReceivedPacket (socket, packet, senderAddress));
}
}
void
RoutingExperiment::CheckThroughput ()
{
double kbs = (bytesTotal * 8.0) / 1000; bytesTotal = 0;
std::ofstream out (m_CSVfileName.c_str (), std::ios::app);
out.close ();
packetsReceived = 0;
Simulator::Schedule (Seconds (1.0), &RoutingExperiment::CheckThroughput, this); }
Ptr<Socket>
RoutingExperiment::SetupPacketReceive (Ipv4Address addr, Ptr<Node> node) {
TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
Ptr<Socket> sink = Socket::CreateSocket (node, tid); InetSocketAddress local =
InetSocketAddress (addr, port); sink->Bind (local); sink->SetRecvCallback
(MakeCallback (&RoutingExperiment::ReceivePacket, this));
return sink;
}
std::string
RoutingExperiment::CommandSetup (int argc, char **argv)
{
CommandLine cmd;
cmd.AddValue ("CSVfileName", "The name of the CSV output file name", m_CSVfileName);
cmd.AddValue ("traceMobility", "Enable mobility tracing", m_traceMobility);
cmd.AddValue ("protocol", "1=OLSR;2=AODV;3=DSDV;4=DSR", m_protocol);
cmd.Parse (argc, argv); return m_CSVfileName;
}
int
main (int argc, char *argv[])
{
RoutingExperiment experiment; std::string CSVfileName =
experiment.CommandSetup (argc,argv);
//blank out the last output file and write the column headers
std::ofstream out (CSVfileName.c_str ());
out << "SimulationSecond," <<
"ReceiveRate," <<
"PacketsReceived," <<
"NumberOfSinks," <<
"RoutingProtocol," <<
"TransmissionPower" <<
std::endl; out.close ();
void
RoutingExperiment::Run (int nSinks, double txp, std::string CSVfileName)
{
Packet::EnablePrinting ();
m_nSinks = nSinks;
m_txp = txp; m_CSVfileName =
CSVfileName;
NodeContainer adhocNodes;
adhocNodes.Create (nWifis);
wifiMac.SetType ("ns3::AdhocWifiMac");
NetDeviceContainer adhocDevices = wifi.Install (wifiPhy, wifiMac, adhocNodes);
ObjectFactory pos;
pos.SetTypeId ("ns3::RandomRectanglePositionAllocator"); pos.Set ("X",
StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=300.0]")); pos.Set ("Y",
StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1500.0]"));
std::stringstream ssSpeed;
ssSpeed << "ns3::UniformRandomVariable[Min=0.0|Max=" << nodeSpeed << "]";
std::stringstream ssPause; ssPause << "ns3::ConstantRandomVariable[Constant="
<< nodePause << "]";
mobilityAdhoc.SetMobilityModel ("ns3::RandomWaypointMobilityModel",
"Speed", StringValue (ssSpeed.str ()),
"Pause", StringValue (ssPause.str ()),
"PositionAllocator", PointerValue (taPositionAlloc));
mobilityAdhoc.SetPositionAllocator (taPositionAlloc);
mobilityAdhoc.Install (adhocNodes); streamIndex +=
mobilityAdhoc.AssignStreams (adhocNodes, streamIndex); NS_UNUSED
(streamIndex); // From this point, streamIndex is unused
AodvHelper aodv;
OlsrHelper olsr;
DsdvHelper dsdv;
DsrHelper dsr;
DsrMainHelper dsrMain;
Ipv4ListRoutingHelper list;
InternetStackHelper internet;
switch (m_protocol)
{ case
1:
list.Add (olsr, 100);
m_protocolName = "OLSR";
break;
case 2: list.Add (aodv, 100);
m_protocolName = "AODV";
break; case 3: list.Add
(dsdv, 100);
m_protocolName = "DSDV";
break;
case 4:
m_protocolName = "DSR";
break;
default:
NS_FATAL_ERROR ("No such protocol:" << m_protocol); }
if (m_protocol < 4)
{
internet.SetRoutingHelper (list);
internet.Install (adhocNodes);
}
else if (m_protocol == 4)
{
internet.Install (adhocNodes);
dsrMain.Install (dsr, adhocNodes);
}
std::stringstream ss;
ss << nWifis;
std::string nodes =
ss.str ();
Ptr<FlowMonitor> flowmon;
FlowMonitorHelper flowmonHelper;
flowmon = flowmonHelper.InstallAll (); */
CheckThroughput ();
Simulator::Destroy ();
}
Performance Comparison:
Throughput VS Number of Nodes
6
OLSR
5.5 AODV
DSDR
5
DSR
4.5
4
throughput
3.5
3
2.5
2
1.5
1
20 25 30 35 40 45 50 55 60
Number of Nodes