Run the simulation with RTS/CTS turned on and turned off

423 views
Skip to first unread message

Maher Abulkader M Khan

unread,
Apr 24, 2014, 10:10:15 AM4/24/14
to ns-3-...@googlegroups.com
 Hi All

Would you please help me to run this this programme to simulate it with RTS/CTS turned on and turned off.

#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 <iostream>
#include <sstream>  //
#include <ctime>   //


using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("tdpWireless6Nds");

uint32_t totalBytesReceived(0), mbs (0);
std::ofstream rdTrace;

//---------------------------------------------------------------------------------------
//-- Callback function is called whenever a packet is received successfully.
//-- This function cumulatively add the size of data packet to totalBytesReceived counter.
//---------------------------------------------------------------------------------------
void
ReceivePacket(std::string context, Ptr <const Packet> p)
{
  totalBytesReceived += p->GetSize();
}

//---------------------------------------------------------------------------------------
//-- Throughput calculating function.
//-- This function calculate the throughput every 0.1s using totalBytesReceived counter
//-- and write the results into a throughput tracefile
//---------------------------------------------------------------------------------------
void
CalculateThroughput()
{
  double mbs = ((totalBytesReceived*8.0)/100000);
  totalBytesReceived =0;
  rdTrace << Simulator::Now ().GetSeconds() << "\t"<< mbs << "\n";
  Simulator::Schedule (Seconds (0.1), &CalculateThroughput);
}

//---------------------------------------------------------------------------------------
// -- main
//---------------------------------------------------------------------------------------
int
main (int argc, char *argv[])
{
  //LogComponentEnable ("PacketSink",LOG_LEVEL_INFO);
  //-----------------------------------------------------
  //-- Open throughput trace file
  //-----------------------------------------------------
  rdTrace.open("tcpWireless6Nds.dat", std::ios::out);                                             //
  rdTrace << "# Time \t Throughput \n";


     
  //-----------------------------------------------------
  //-- Creating nodes for simulation
  //-----------------------------------------------------
  NodeContainer nodes;
  nodes.Create (6);

  //-----------------------------------------------------
  //-- Creating Wifi channels for created nodes
  //-----------------------------------------------------
  WifiHelper wifi = WifiHelper::Default ();
  wifi.SetStandard (WIFI_PHY_STANDARD_80211b);
  wifi.SetRemoteStationManager ("ns3::AarfWifiManager");
 
  YansWifiChannelHelper channel = YansWifiChannelHelper::Default();
  channel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
  channel.AddPropagationLoss ("ns3::NakagamiPropagationLossModel");
  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default();
  wifiPhy.SetChannel (channel.Create());
 
  NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default();
  wifiMac.SetType ("ns3::AdhocWifiMac");
 
  NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes);
 
  //-----------------------------------------------------
  //-- Creating mobility models for created wireless nodes
  //-----------------------------------------------------
  MobilityHelper mobility;
  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
  positionAlloc -> Add (Vector (0.0, 0.0, 0.0));
  positionAlloc -> Add (Vector (-5.0, 0.0, 0.0));
  positionAlloc -> Add (Vector (0.0, 5.0, 0.0));                                       //
  positionAlloc -> Add (Vector (0.0, 5.0, 0.0));                                       //
  positionAlloc -> Add (Vector (0.0, 0.0, 5.0));                                        //
  positionAlloc -> Add (Vector (0.0, 5.0, 0.0));                                        //
 
  mobility.SetPositionAllocator (positionAlloc);
  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
  mobility.Install (nodes);
 
  //-----------------------------------------------------
  //-- Install TCP/IP stack and assign IP address for nodes to communicate
  //-----------------------------------------------------
  InternetStackHelper stack;
  stack.Install (nodes);

  Ipv4AddressHelper address;
  address.SetBase ("10.1.1.0", "255.255.255.0");

  Ipv4InterfaceContainer interfaces = address.Assign (devices);

  //-----------------------------------------------------
  //-- Create, setup and install ON/OFF traffic generator
  //---------------------------------------------------

//Using for loop to create a total number of nodes with application                 //begin for loop

  for(int i = 0; i <5 ; i++)
  {
  uint16_t port = 50000;
  OnOffHelper onoff ("ns3::TcpSocketFactory",
                     Address (InetSocketAddress (interfaces.GetAddress (i+1), port+i)));      //
  onoff.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"));
  onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0.0]"));
  onoff.SetAttribute ("PacketSize", StringValue ("1024"));
  onoff.SetAttribute ("DataRate", StringValue ("10000kb/s"));
 
  ApplicationContainer apps = onoff.Install (nodes.Get (0));
  apps.Start (Seconds (2.0));
  apps.Stop (Seconds (10.0));

  //-----------------------------------------------------
  //-- Creating a sink to receive the packets from the traffic generator
  //-----------------------------------------------------


  PacketSinkHelper sinkHelper ("ns3::TcpSocketFactory",
                               InetSocketAddress (Ipv4Address::GetAny (), port));       //
  ApplicationContainer sinkApp = sinkHelper.Install (nodes.Get(i+1));                     //
  sinkApp.Start (Seconds (2.0));
  sinkApp.Stop (Seconds (10.0));
 
 
  //-----------------------------------------------------
  //-- Connect MacRx event at MAC layer of sink node to ReceivePacket function
  //-- for throughput calculation.
  //-----------------------------------------------------
 
std::stringstream ST;
ST<<"/NodeList/"<< (i+1) <<"/DeviceList/*/$ns3::WifiNetDevice/Mac/MacRx";                 //
Config::Connect (ST.str(), MakeCallback(&ReceivePacket) );         

 } //end of for


  //-----------------------------------------------------
  //-- Enable ASCII and PCAP tracing
  //-----------------------------------------------------
  AsciiTraceHelper ascii;
  wifiPhy.EnableAsciiAll (ascii.CreateFileStream("tcpWireless6Nds.tr"));
  wifiPhy.EnablePcapAll("tcpWireless6Nds");
 
  //-----------------------------------------------------
  //-- Trigger CalculateThroughput function, it will schedule itself afterwards
  //-----------------------------------------------------
  Simulator::Schedule (Seconds(0.0),&CalculateThroughput);


 
  //-----------------------------------------------------
  //-- Schedule a stop time at 11s and start the simulation
  //-----------------------------------------------------
  Simulator::Stop (Seconds (11));
  Simulator::Run ();
  Simulator::Destroy ();
 
  return 0;
}

//------------------------------------------------------------------------------------------------------------------
  

waiting for your soon reply

Thanks a lot

Maher

Konstantinos

unread,
Apr 24, 2014, 10:42:52 AM4/24/14
to ns-3-...@googlegroups.com
Dear Maher,

Please do not post the code within the message, use attachments!

Have you read/worked with the examples? There is one in /examples/wireless/wifi-hidden-terminal.cc which does exactly that, evaluates RTS/CTS mechanism.
Study that, and you will be able to do the same in your scenario.

Regards,
K.


On Thursday, April 24, 2014 3:10:15 PM UTC+1, Maher Abulkader M Khan wrote:
 Hi All

Would you please help me to run this this programme to simulate it with RTS/CTS turned on and turned off.
 
waiting for your soon reply

Thanks a lot

Maher

Reply all
Reply to author
Forward
0 new messages