examples/multi-rate-second.cc
author Duy Nguyen <duy@soe.ucsc.edu>
Thu Aug 13 09:06:17 2009 +0200 (2009-08-13)
changeset 4706 c1c7c44be568
child 4738 bfc0ffd9cf7c
permissions -rw-r--r--
add minstrel examples
     1 /* 
     2  * Instructions:
     3  * ./waf --run multi-rate-second
     4  * gnuplot multi-rate-second.plt 
     5  *
     6  * Output: multi-rate-second.eps 
     7  *
     8  */
     9 
    10 #include "ns3/core-module.h"
    11 #include "ns3/common-module.h"
    12 #include "ns3/node-module.h"
    13 #include "ns3/helper-module.h"
    14 #include "ns3/mobility-module.h"
    15 #include "ns3/contrib-module.h"
    16 
    17 #include <iostream>
    18 #include <fstream>
    19 #include <vector>
    20 #include <string>
    21 
    22 NS_LOG_COMPONENT_DEFINE ("Main");
    23 
    24 using namespace ns3;
    25 
    26 class Experiment
    27 {
    28 public:
    29   Experiment ();
    30   Experiment (std::string name);
    31   Gnuplot2dDataset Run (const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy,
    32                         const NqosWifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel);
    33 private:
    34   void ReceivePacket (Ptr<Socket> socket);
    35   void SetPosition (Ptr<Node> node, Vector position);
    36   Vector GetPosition (Ptr<Node> node);
    37   Ptr<Socket> SetupPacketReceive (Ptr<Node> node);
    38   void GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize, 
    39                         uint32_t pktCount, Time pktInterval , Ptr<Node> node);
    40 
    41   uint32_t m_pktsTotal;
    42   Gnuplot2dDataset m_output;
    43   bool advanceStep; 
    44 };
    45 
    46 Experiment::Experiment ()
    47 {
    48   advanceStep= true;
    49 }
    50 
    51 Experiment::Experiment (std::string name)
    52   : m_output (name)
    53 {
    54   m_output.SetStyle (Gnuplot2dDataset::LINES);
    55 }
    56 
    57 void
    58 Experiment::SetPosition (Ptr<Node> node, Vector position)
    59 {
    60   Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
    61   mobility->SetPosition (position);
    62 }
    63 
    64 Vector
    65 Experiment::GetPosition (Ptr<Node> node)
    66 {
    67   Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
    68   return mobility->GetPosition ();
    69 }
    70 
    71 void
    72 Experiment::ReceivePacket (Ptr<Socket> socket)
    73 {
    74   Ptr<Packet> packet;
    75   while (packet = socket->Recv ())
    76     {
    77       m_pktsTotal ++;
    78     }
    79 }
    80 
    81 Ptr<Socket>
    82 Experiment::SetupPacketReceive (Ptr<Node> node)
    83 {
    84   TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
    85   Ptr<Socket> sink = Socket::CreateSocket (node, tid);
    86   InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80);
    87   sink->Bind (local);
    88   sink->SetRecvCallback (MakeCallback (&Experiment::ReceivePacket, this));
    89   return sink;
    90 }
    91 
    92 void
    93 Experiment::GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize, 
    94                              uint32_t pktCount, Time pktInterval, Ptr<Node> node )
    95 {
    96   Vector pos = GetPosition(node);
    97 
    98   ///to offset the start time
    99   double offSetTime = 100;
   100 
   101   if (pktCount > 0)
   102     {
   103       ///To simulate nodes moving in and out of transmission constantly
   104       if(pos.x <= 305 && advanceStep)
   105         {
   106           ///keep moving away
   107           pos.x += .1;
   108           SetPosition(node, pos);
   109         }
   110       else
   111         {
   112           if(pos.x < 150)
   113 	    {
   114               advanceStep=true;
   115             }
   116           else
   117             {
   118               advanceStep = false;
   119             }			
   120 
   121           ///moving back in
   122           pos.x -= .1;
   123           SetPosition(node, pos);
   124         }
   125       socket->Send (Create<Packet> (pktSize));
   126       Simulator::Schedule (pktInterval, &Experiment::GenerateTraffic, this, 
   127                            socket, pktSize,pktCount-1, pktInterval, node);
   128     }
   129   else
   130     {
   131       m_output.Add((Simulator::Now()).GetSeconds() - offSetTime , m_pktsTotal); 
   132     }
   133 }
   134 
   135 Gnuplot2dDataset
   136 Experiment::Run (const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy,
   137                  const NqosWifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel)
   138 {
   139   m_pktsTotal = 0;
   140 
   141   NodeContainer c;
   142   c.Create (2);
   143 
   144   YansWifiPhyHelper phy = wifiPhy;
   145   phy.SetChannel (wifiChannel.Create ());
   146 
   147   NqosWifiMacHelper mac = wifiMac;
   148   NetDeviceContainer devices = wifi.Install (phy, mac, c);
   149 
   150   MobilityHelper mobility;
   151   Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
   152   positionAlloc->Add (Vector (0.0, 0.0, 0.0));
   153   positionAlloc->Add (Vector (5.0, 0.0, 0.0));
   154   mobility.SetPositionAllocator (positionAlloc);
   155   mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
   156   mobility.Install (c);
   157 
   158   InternetStackHelper internet;
   159   internet.Install (c);
   160 
   161   Ipv4AddressHelper ipv4;
   162   NS_LOG_INFO ("Assign IP Addresses.");
   163   ipv4.SetBase ("10.1.1.0", "255.255.255.0");
   164   Ipv4InterfaceContainer wifiNodesInterface = ipv4.Assign (devices);
   165 
   166 
   167   Ptr<Socket> recvSink = SetupPacketReceive (c.Get (0));
   168 
   169   TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
   170   Ptr<Socket> source = Socket::CreateSocket (c.Get (1), tid);
   171   InetSocketAddress remote = InetSocketAddress (Ipv4Address ("255.255.255.255"), 80);
   172   source->Connect (remote);
   173   uint32_t packetSize = 1014;
   174   uint32_t maxPacketCount = 1000;
   175   Time interPacketInterval = Seconds (.1);
   176 
   177   Ptr<Node> n1 = c.Get(0);
   178   Ptr<Ipv4> ipv41 = n1->GetObject<Ipv4> ();
   179 
   180 
   181 
   182   for (int i= 1; i <= 100; i++) 
   183     {
   184 
   185       Simulator::Schedule (Seconds (i), &Experiment::GenerateTraffic,
   186                            this, source, packetSize, maxPacketCount,interPacketInterval, c.Get(1));
   187 
   188       if( i % 5 == 0 )
   189         {
   190           ///bring a network interface down 
   191           Simulator::Schedule (Seconds (i+.5), &Ipv4::SetDown, ipv41, 1); 
   192           i++;
   193           Simulator::Schedule (Seconds (i), &Experiment::GenerateTraffic,
   194                                this, source, packetSize, maxPacketCount,interPacketInterval, c.Get(1));
   195 	
   196           ///bring a network interface up
   197           Simulator::Schedule (Seconds (i+.2), &Ipv4::SetUp, ipv41, 1); 
   198           i++;
   199           Simulator::Schedule (Seconds (i), &Experiment::GenerateTraffic,
   200                                this, source, packetSize, maxPacketCount,interPacketInterval, c.Get(1));
   201         }
   202     }
   203 
   204   Simulator::Run ();
   205   Simulator::Destroy ();
   206 
   207   return m_output;
   208 }
   209 
   210 int main (int argc, char *argv[])
   211 {
   212   std::vector <std::string> ratesControl;
   213   ratesControl.push_back ("Ideal");
   214   ratesControl.push_back ("Minstrel");
   215 
   216   std::vector <std::string> wifiManager;
   217   wifiManager.push_back("ns3::IdealWifiManager");
   218   wifiManager.push_back("ns3::MinstrelWifiManager");
   219 
   220   // disable fragmentation
   221   Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
   222   Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200"));
   223 
   224   CommandLine cmd;
   225   cmd.Parse (argc, argv);
   226 
   227   Gnuplot gnuplot = Gnuplot ("multi-rate-second.eps");
   228   
   229   for (uint32_t i = 0; i < ratesControl.size(); i++)
   230   {
   231     Gnuplot2dDataset dataset (ratesControl[i]);
   232     dataset.SetStyle (Gnuplot2dDataset::LINES);
   233     Experiment experiment;
   234 
   235 
   236     WifiHelper wifi = WifiHelper::Default ();
   237     NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
   238     YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
   239     YansWifiChannelHelper wifiChannel  = YansWifiChannelHelper::Default ();
   240 
   241     wifiMac.SetType ("ns3::AdhocWifiMac");
   242 
   243     NS_LOG_DEBUG (ratesControl[i]);
   244 
   245     experiment = Experiment (ratesControl[i]);
   246     dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
   247     gnuplot.AddDataset (dataset);
   248 
   249   }
   250   gnuplot.SetTerminal ("postscript eps color enh \"Times-BoldItalic\"");
   251   gnuplot.SetLegend ("Time (Seconds)", "Number of packets received");
   252   gnuplot.SetExtra  ("set xrange [0:100]");
   253   gnuplot.GenerateOutput (std::cout);
   254 
   255   return 0;
   256 }