Ns3 Projects for B.E/B.Tech M.E/M.Tech PhD Scholars.  Phone-Number:9790238391   E-mail: ns3simulation@gmail.com

How to Implement on Demand Protocol in ns3

To implement the on demand protocol in ns3 needs to make the routes when needed and the sample of on-demand routing protocols are Ad hoc On-Demand Distance Vector (AODV) and Dynamic source routing (DSR). An AODV and DSR supports by ns3 environment. Now I am going to show how to implement and run the AODV in ns3. The process is same as DSR with light variation.

Implementing AODV in ns-3

  1. Set Up Your Environment

Make sure ns3 is installed. If have not installed then download it from official website.

  1. Create a New ns-3 Script

Create a new simulation script in the scratch directory of your ns-3 installation. For example, create a file named aodv-simulation.cc.

cd ns-3.xx

cd scratch

touch aodv-simulation.cc

3.      Include Necessary Headers

In your aodv-simulation.cc file, include the necessary headers for your simulation.

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/point-to-point-module.h”

#include “ns3/applications-module.h”

#include “ns3/mobility-module.h”

#include “ns3/wifi-module.h”

#include “ns3/aodv-module.h”

using namespace ns3;

4.      Set Up the Network Topology

Define the network topology. Let’s create a basic topology with a few nodes and use AODV as the routing protocol.

int main (int argc, char *argv[])

{

  CommandLine cmd;

  cmd.Parse (argc, argv);

  NodeContainer nodes;

  nodes.Create (10);

  // Set up mobility model

  MobilityHelper mobility;

  mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,

                                 “MinX”, DoubleValue (0.0),

                                 “MinY”, DoubleValue (0.0),

                                 “DeltaX”, DoubleValue (5.0),

                                 “DeltaY”, DoubleValue (5.0),

                                 “GridWidth”, UintegerValue (5),

                                 “LayoutType”, StringValue (“RowFirst”));

  mobility.SetMobilityModel (“ns3::ConstantPositionMobilityModel”);

  mobility.Install (nodes);

  // Set up WiFi

  WifiHelper wifi;

  wifi.SetStandard (WIFI_PHY_STANDARD_80211b);

  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();

  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();

  wifiPhy.SetChannel (wifiChannel.Create ());

  WifiMacHelper wifiMac;

  wifiMac.SetType (“ns3::AdhocWifiMac”);

  NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);

  InternetStackHelper stack;

  AodvHelper aodv;

  stack.SetRoutingHelper (aodv);

  stack.Install (nodes);

  Ipv4AddressHelper address;

  address.SetBase (“10.1.1.0”, “255.255.255.0”);

  Ipv4InterfaceContainer interfaces = address.Assign (devices);

 

  // Set up applications (e.g., a UDP echo server and client)

  UdpEchoServerHelper echoServer (9);

  ApplicationContainer serverApps = echoServer.Install (nodes.Get (9));

  serverApps.Start (Seconds (1.0));

  serverApps.Stop (Seconds (10.0));

  UdpEchoClientHelper echoClient (interfaces.GetAddress (9), 9);

  echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));

  echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));

  echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));

  ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));

  clientApps.Start (Seconds (2.0));

  clientApps.Stop (Seconds (10.0));

  // Enable tracing

  AsciiTraceHelper ascii;

  wifiPhy.EnableAsciiAll (ascii.CreateFileStream (“aodv-simulation.tr”));

  wifiPhy.EnablePcapAll (“aodv-simulation”);

  // Run the simulation

  Simulator::Run ();

  Simulator::Destroy ();

  return 0;

}

5.      Build and Run the Simulation

After writing your script, you need to build and run it.

./waf build

./waf –run scratch/aodv-simulation

6.      Analyze the Results

After running the simulation, you can analyze the results using the generated trace files (aodv-simulation.tr and aodv-simulation-0-0.pcap).

Complete Example Script for AODV

Here is a sample script to complete the references for AODV in ns3:

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/point-to-point-module.h”

#include “ns3/applications-module.h”

#include “ns3/mobility-module.h”

#include “ns3/wifi-module.h”

#include “ns3/aodv-module.h”

using namespace ns3;

int main (int argc, char *argv[])

{

  CommandLine cmd;

  cmd.Parse (argc, argv);

  NodeContainer nodes;

  nodes.Create (10);

  // Set up mobility model

  MobilityHelper mobility;

  mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,

                                 “MinX”, DoubleValue (0.0),

                                 “MinY”, DoubleValue (0.0),

                                 “DeltaX”, DoubleValue (5.0),

                                 “DeltaY”, DoubleValue (5.0),

                                 “GridWidth”, UintegerValue (5),

                                 “LayoutType”, StringValue (“RowFirst”));

  mobility.SetMobilityModel (“ns3::ConstantPositionMobilityModel”);

  mobility.Install (nodes);

  // Set up WiFi

  WifiHelper wifi;

  wifi.SetStandard (WIFI_PHY_STANDARD_80211b);

  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();

  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();

  wifiPhy.SetChannel (wifiChannel.Create ());

  WifiMacHelper wifiMac;

  wifiMac.SetType (“ns3::AdhocWifiMac”);

  NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);

  InternetStackHelper stack;

  AodvHelper aodv;

  stack.SetRoutingHelper (aodv);

  stack.Install (nodes);

  Ipv4AddressHelper address;

  address.SetBase (“10.1.1.0”, “255.255.255.0”);

  Ipv4InterfaceContainer interfaces = address.Assign (devices);

  // Set up applications (e.g., a UDP echo server and client)

  UdpEchoServerHelper echoServer (9);

  ApplicationContainer serverApps = echoServer.Install (nodes.Get (9));

  serverApps.Start (Seconds (1.0));

  serverApps.Stop (Seconds (10.0));

  UdpEchoClientHelper echoClient (interfaces.GetAddress (9), 9);

  echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));

  echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));

  echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));

  ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));

  clientApps.Start (Seconds (2.0));

  clientApps.Stop (Seconds (10.0));

  // Enable tracing

  AsciiTraceHelper ascii;

  wifiPhy.EnableAsciiAll (ascii.CreateFileStream (“aodv-simulation.tr”));

  wifiPhy.EnablePcapAll (“aodv-simulation”);

  // Run the simulation

  Simulator::Run ();

  Simulator::Destroy ();

  return 0;

}

Implementing DSR in ns-3

Now we want to implement the DSR (Dynamic Source Routing), the process is same. Here is sample setup to configure the DSR simulation:

#include “ns3/core-module.h”

#include “ns3/network-module.h”

#include “ns3/internet-module.h”

#include “ns3/point-to-point-module.h”

#include “ns3/applications-module.h”

#include “ns3/mobility-module.h”

#include “ns3/wifi-module.h”

#include “ns3/dsr-module.h”

using namespace ns3;

int main (int argc, char *argv[])

{

  CommandLine cmd;

  cmd.Parse (argc, argv);

  NodeContainer nodes;

  nodes.Create (10);

  // Set up mobility model

  MobilityHelper mobility;

  mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,

                                 “MinX”, DoubleValue (0.0),

                                 “MinY”, DoubleValue (0.0),

                                 “DeltaX”, DoubleValue (5.0),

                                 “DeltaY”, DoubleValue (5.0),

                                 “GridWidth”, UintegerValue (5),

                                 “LayoutType”, StringValue (“RowFirst”));

  mobility.SetMobilityModel (“ns3::ConstantPositionMobilityModel”);

  mobility.Install (nodes);

  // Set up WiFi

  WifiHelper wifi;

  wifi.SetStandard (WIFI_PHY_STANDARD_80211b);

  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();

  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();

  wifiPhy.SetChannel (wifiChannel.Create ());

  WifiMacHelper wifiMac;

  wifiMac.SetType (“ns3::AdhocWifiMac”);

  NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);

  InternetStackHelper stack;

  DsrMainHelper dsrMain;

  DsrHelper dsr;

  stack.Install (nodes);

  dsrMain.Install (dsr, nodes);

  Ipv4AddressHelper address;

  address.SetBase (“10.1.1.0”, “255.255.255.0”);

  Ipv4InterfaceContainer interfaces = address.Assign (devices);

  // Set up applications (e.g., a UDP echo server and client)

  UdpEchoServerHelper echoServer (9);

  ApplicationContainer serverApps = echoServer.Install (nodes.Get (9));

  serverApps.Start (Seconds (1.0));

  serverApps.Stop (Seconds (10.0));

  UdpEchoClientHelper echoClient (interfaces.GetAddress (9), 9);

  echoClient.SetAttribute (“MaxPackets”, UintegerValue (1));

  echoClient.SetAttribute (“Interval”, TimeValue (Seconds (1.0)));

  echoClient.SetAttribute (“PacketSize”, UintegerValue (1024));

  ApplicationContainer clientApps = echoClient.Install (nodes.Get (0));

  clientApps.Start (Seconds (2.0));

  clientApps.Stop (Seconds (10.0));

  // Enable tracing

  AsciiTraceHelper ascii;

  wifiPhy.EnableAsciiAll (ascii.CreateFileStream (“dsr-simulation.tr”));

  wifiPhy.EnablePcapAll (“dsr-simulation”);

  // Run the simulation

  Simulator::Run ();

  Simulator::Destroy ();

  return 0;

}

As we discussed earlier about how the AODV will perform in ns-3 environment and we help to provide further information about how the AODV will adapt in different environments. For Demand Protocol we follow the projects ideas and suggest best implementation plan for you.