Network Simulator-3 (NS-3) Practical Lab Manual
ISBN 9788119221820

Highlights

Notes

  

Practical 13:: Program to Simulate Wireless Wi-Fi Network.

/* -*- Mode:C++; c-file-style:”gnu”; indent-tabs-mode:nil; -*- */

/*

* This program is free software; you can redistribute it and/or modify

* it under the terms of the GNU General Public License version 2 as

* published by the Free Software Foundation;

*

* This program is distributed in the hope that it will be useful,

* but WITHOUT ANY WARRANTY; without even the implied warranty of

* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

* 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

*

* (Adapted from third.cc)

*/

#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/mobility-module.h”

#include “ns3/internet-module.h”

#include “ns3/netanim-module.h”

#include “ns3/basic-energy-source.h”

#include “ns3/simple-device-energy-model.h”

#include “ns3/yans-wifi-helper.h”

#include “ns3/ssid.h”

#include “ns3/wifi-radio-energy-model.h”

using namespace ns3;

NS_LOG_COMPONENT_DEFINE (“WirelessAnimationExample”);

int

main (int argc, char *argv[])

{

uint32_t nWifi = 20;CommandLine cmd ( FILE );

cmd.AddValue (“nWifi”, “Number of wifi STA devices”, nWifi);

cmd.Parse (argc,argv);NodeContainer allNodes;NodeContainer wifiStaNodes;wifiStaNodes.Create (nWifi);allNodes.Add (wifiStaNodes);NodeContainer wifiApNode;wifiApNode.Create (1);allNodes.Add (wifiApNode);

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 staDevices;

staDevices = wifi.Install (phy, mac, wifiStaNodes);

mac.SetType (“ns3::ApWifiMac”,

“Ssid”, SsidValue (ssid));

NetDeviceContainer apDevices;

apDevices = wifi.Install (phy, mac, wifiApNode);

NodeContainer p2pNodes;

p2pNodes.Add (wifiApNode);

p2pNodes.Create (1);

allNodes.Add (p2pNodes.Get (1));

PointToPointHelper pointToPoint;

pointToPoint.SetDeviceAttribute (“DataRate”, StringValue (“5Mbps”));

pointToPoint.SetChannelAttribute (“Delay”, StringValue (“2ms”));

NetDeviceContainer p2pDevices;

p2pDevices = pointToPoint.Install (p2pNodes);

NodeContainer csmaNodes;

csmaNodes.Add (p2pNodes.Get (1));

csmaNodes.Create (1);

allNodes.Add (csmaNodes.Get (1));

CsmaHelper csma;

csma.SetChannelAttribute (“DataRate”, StringValue (“100Mbps”));

csma.SetChannelAttribute (“Delay”, TimeValue (NanoSeconds (6560)));

NetDeviceContainer csmaDevices;

csmaDevices = csma.Install (csmaNodes);

// Mobility

MobilityHelper mobility;

mobility.SetPositionAllocator (“ns3::GridPositionAllocator”,

“MinX”, DoubleValue (10.0),

“MinY”, DoubleValue (10.0),

“DeltaX”, DoubleValue (5.0),

“DeltaY”, DoubleValue (2.0),

“GridWidth”, UintegerValue (5),“LayoutType”, StringValue (“RowFirst”));

mobility.SetMobilityModel (“ns3::RandomWalk2dMobilityModel”,“Bounds”, RectangleValue (Rectangle (-50, 50, -25, 50)));

mobility.Install (wifiStaNodes);

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

mobility.Install (wifiApNode);

AnimationInterface::SetConstantPosition (p2pNodes.Get (1), 10, 30);

AnimationInterface::SetConstantPosition (csmaNodes.Get (1), 10, 33);

Ptr<BasicEnergySource> energySource = CreateObject<BasicEnergySource>();

Ptr<WifiRadioEnergyModel> energyModel =

CreateObject<WifiRadioEnergyModel>();

energySource->SetInitialEnergy (300);

energyModel->SetEnergySource (energySource);

energySource->AppendDeviceEnergyModel (energyModel);

// aggregate energy source to node

wifiApNode.Get (0)->AggregateObject (energySource);

// Install internet stack

InternetStackHelper stack;

stack.Install (allNodes);

// Install Ipv4 addresses

Ipv4AddressHelper address;

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

Ipv4InterfaceContainer p2pInterfaces;

p2pInterfaces = address.Assign (p2pDevices);

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

Ipv4InterfaceContainer csmaInterfaces;

csmaInterfaces = address.Assign (csmaDevices);

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

Ipv4InterfaceContainer staInterfaces;

staInterfaces = address.Assign (staDevices);

Ipv4InterfaceContainer apInterface;

apInterface = address.Assign (apDevices);

// Install applications

UdpEchoServerHelper echoServer (9);

ApplicationContainer serverApps = echoServer.Install (csmaNodes.Get (1));

serverApps.Start (Seconds (1.0));

serverApps.Stop (Seconds (15.0));

UdpEchoClientHelper echoClient (csmaInterfaces.GetAddress (1), 9);

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

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

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

ApplicationContainer clientApps = echoClient.Install (wifiStaNodes);

clientApps.Start (Seconds (2.0));

clientApps.Stop (Seconds (15.0));

Ipv4GlobalRoutingHelper::PopulateRoutingTables ();

Simulator::Stop (Seconds (15.0));

AnimationInterface anim (“wireless-animation.xml”); // Mandatory

for (uint32_t i = 0; i < wifiStaNodes.GetN (); ++i)

{

anim.UpdateNodeDescription (wifiStaNodes.Get (i), “STA”); // Optionalanim.UpdateNodeColor (wifiStaNodes.Get (i), 255, 0, 0); // Optional

}

for (uint32_t i = 0; i < wifiApNode.GetN (); ++i)

{

anim.UpdateNodeDescription (wifiApNode.Get (i), “AP”); // Optionalanim.UpdateNodeColor (wifiApNode.Get (i), 0, 255, 0); // Optional

}

for (uint32_t i = 0; i < csmaNodes.GetN (); ++i)

{

anim.UpdateNodeDescription (csmaNodes.Get (i), “CSMA”); // Optional

anim.UpdateNodeColor (csmaNodes.Get (i), 0, 0, 255); // Optional

}

anim.EnablePacketMetadata (true); // Optional

anim.EnableIpv4RouteTracking (“routingtable-wireless.xml”, Seconds (0),

Seconds (5), Seconds (0.25)); //Optional

anim.EnableWifiMacCounters (Seconds (0), Seconds (10)); //Optional

anim.EnableWifiPhyCounters (Seconds (0), Seconds (10)); //OptionalpointToPoint.EnablePcapAll(“wireless”);

Simulator::Run ();Simulator::Destroy ();return 0;

}

Output: