examples/multi-rate-first.cc
author Duy Nguyen <duy@soe.ucsc.edu>
Thu Aug 13 09:06:17 2009 +0200 (2009-08-13)
changeset 4706 c1c7c44be568
child 4723 294b82356191
permissions -rw-r--r--
add minstrel examples
     1 /**
     2  *
     3  * Instructions:
     4  * ./waf --run multi-rate-first > m.data
     5  * gnuplot m.data
     6  * eog *.png
     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 
    19 NS_LOG_COMPONENT_DEFINE ("Main");
    20 
    21 using namespace ns3;
    22 
    23 class Experiment
    24 {
    25 public:
    26   Experiment ();
    27   Experiment (std::string name);
    28   Gnuplot2dDataset Run (const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy,
    29                         const NqosWifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel, const MobilityHelper &mobility, int positionStep);
    30 private:
    31   void ReceivePacket (Ptr<Socket> socket);
    32   void SetPosition (Ptr<Node> node, Vector position);
    33   Vector GetPosition (Ptr<Node> node);
    34   void AdvancePosition (Ptr<Node> node);
    35   void BackTrackPosition (Ptr<Node> node);
    36   void StationaryPosition (Ptr<Node> node);
    37   Ptr<Socket> SetupPacketReceive (Ptr<Node> node);
    38 
    39   uint32_t m_bytesTotal;
    40   Gnuplot2dDataset m_output;
    41 };
    42 
    43 Experiment::Experiment ()
    44 {}
    45 
    46 Experiment::Experiment (std::string name)
    47   : m_output (name)
    48 {
    49   m_output.SetStyle (Gnuplot2dDataset::LINES);
    50 }
    51 
    52 void
    53 Experiment::SetPosition (Ptr<Node> node, Vector position)
    54 {
    55   Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
    56   mobility->SetPosition (position);
    57 }
    58 
    59 Vector
    60 Experiment::GetPosition (Ptr<Node> node)
    61 {
    62   Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
    63   return mobility->GetPosition ();
    64 }
    65 
    66 void 
    67 Experiment::AdvancePosition (Ptr<Node> node) 
    68 {
    69   Vector pos = GetPosition (node);
    70   double mbs = ((m_bytesTotal * 8.0) / 1000000);
    71   m_bytesTotal = 0;
    72   m_output.Add ((Simulator::Now()).GetSeconds(), mbs);
    73   pos.x += 1.0;
    74 	
    75   if (pos.x >= 210.0) 
    76     {
    77       return;
    78     }
    79   SetPosition (node, pos);
    80 
    81   //std::cout << "x="<<pos.x << std::endl;
    82   Simulator::Schedule (Seconds (1.0), &Experiment::AdvancePosition, this, node);
    83 }
    84 void 
    85 Experiment::BackTrackPosition (Ptr<Node> node) 
    86 {
    87   Vector pos = GetPosition (node);
    88   double mbs = ((m_bytesTotal * 8.0) / 1000000);
    89   m_bytesTotal = 0;
    90   m_output.Add ((Simulator::Now()).GetSeconds(), mbs);
    91   pos.x -= 1.0;
    92 	
    93   if (pos.x < 0) 
    94     {
    95       return;
    96     }
    97   SetPosition (node, pos);
    98 
    99   //std::cout << "x="<<pos.x << std::endl;
   100   Simulator::Schedule (Seconds (1.0), &Experiment::BackTrackPosition, this, node);
   101 }
   102 
   103 void
   104 Experiment::StationaryPosition (Ptr<Node> node)
   105 {
   106   double mbs = ((m_bytesTotal * 8.0) / 1000000);
   107   m_bytesTotal = 0;
   108   m_output.Add ((Simulator::Now()).GetSeconds(), mbs);
   109 
   110 }
   111 
   112 void
   113 Experiment::ReceivePacket (Ptr<Socket> socket)
   114 {
   115   Ptr<Packet> packet;
   116   while (packet = socket->Recv ())
   117     {
   118       m_bytesTotal += packet->GetSize ();
   119     }
   120 }
   121 
   122 Ptr<Socket>
   123 Experiment::SetupPacketReceive (Ptr<Node> node)
   124 {
   125   TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory");
   126   Ptr<Socket> sink = Socket::CreateSocket (node, tid);
   127   sink->Bind ();
   128   sink->SetRecvCallback (MakeCallback (&Experiment::ReceivePacket, this));
   129 
   130   return sink;
   131 
   132 }
   133 
   134 Gnuplot2dDataset
   135 Experiment::Run (const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy,
   136                  const NqosWifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel, const MobilityHelper &mobility, int positionStep)
   137 {
   138   m_bytesTotal = 0;
   139 
   140   NodeContainer c;
   141   c.Create (2);
   142 
   143   PacketSocketHelper packetSocket;
   144   packetSocket.Install (c);
   145 
   146   YansWifiPhyHelper phy = wifiPhy;
   147   phy.SetChannel (wifiChannel.Create ());
   148 
   149   NqosWifiMacHelper mac = wifiMac;
   150   NetDeviceContainer devices = wifi.Install (phy, mac, c);
   151 
   152   mobility.Install (c);
   153 
   154   PacketSocketAddress socket;
   155   socket.SetSingleDevice(devices.Get (0)->GetIfIndex ());
   156   socket.SetPhysicalAddress (devices.Get (1)->GetAddress ());
   157   socket.SetProtocol (1);
   158 
   159   OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
   160   onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (250)));
   161   onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0)));
   162   onoff.SetAttribute ("DataRate", DataRateValue (DataRate (60000000)));
   163   onoff.SetAttribute ("PacketSize", UintegerValue (2000));
   164 
   165   ApplicationContainer apps = onoff.Install (c.Get (0));
   166   apps.Start (Seconds (0.5));
   167   apps.Stop (Seconds (250.0));
   168 
   169 
   170   Ptr<Socket> recvSink = SetupPacketReceive (c.Get (1));
   171 
   172   if(positionStep == 1)
   173     {
   174       Simulator::Schedule (Seconds (1.5), &Experiment::AdvancePosition, this, c.Get (1));
   175     }
   176   else if(positionStep == -1)
   177     {
   178       Simulator::Schedule (Seconds (1.5), &Experiment::BackTrackPosition, this, c.Get (1));
   179     }
   180   else if(positionStep == 0)
   181     {
   182       for(int i = 1; i <= 210; i++) 
   183         {
   184           Simulator::Schedule (Seconds (i), &Experiment::StationaryPosition, this, c.Get (1));
   185         }
   186     }
   187   Simulator::Run ();
   188   Simulator::Destroy ();
   189 
   190   return m_output;
   191 }
   192 
   193 int main (int argc, char *argv[])
   194 {
   195   // disable fragmentation
   196   Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
   197   Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200"));
   198 
   199   CommandLine cmd;
   200   cmd.Parse (argc, argv);
   201 
   202   Gnuplot gnuplot = Gnuplot ("multi-rate-first.png");
   203   Experiment experiment;
   204   WifiHelper wifi = WifiHelper::Default ();
   205   NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
   206   YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
   207   YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
   208   Gnuplot2dDataset dataset;
   209   int myPositionStep = 0; 
   210 
   211 /*
   212 
   213   // Scenario 1: moving away from one another
   214   // Initially set them 5 meters apart 
   215   // Set  positionStep parameter of Experiment::Run to 1  
   216   // Set RateErrorModel of Experiment::Run to 0
   217   myPositionStep = 1;
   218 
   219   MobilityHelper mobility;
   220   Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
   221   positionAlloc->Add (Vector (0.0, 0.0, 0.0));
   222   positionAlloc->Add (Vector (5.0, 0.0, 0.0));
   223   mobility.SetPositionAllocator (positionAlloc);
   224   mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
   225 
   226   wifiMac.SetType ("ns3::AdhocWifiMac");
   227 
   228   gnuplot = Gnuplot ("multi-rate-first.png");
   229   Config::SetDefault ("ns3::YansWifiPhy::Standard", StringValue ("holland"));
   230 
   231   NS_LOG_DEBUG ("minstrel");
   232   experiment = Experiment ("minstrel");
   233   wifi.SetRemoteStationManager ("ns3::MinstrelWifiManager");
   234   dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel, mobility, myPositionStep);
   235   gnuplot.AddDataset (dataset);
   236 
   237   NS_LOG_DEBUG ("ideal");
   238   experiment = Experiment ("ideal");
   239   wifi.SetRemoteStationManager ("ns3::IdealWifiManager");
   240   dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel, mobility, myPositionStep);
   241   gnuplot.AddDataset (dataset);
   242 
   243   gnuplot.GenerateOutput (std::cout);
   244   */
   245 
   246 
   247   // Scenario 2: two nodes out of range, moving into transmission range range
   248   // Initially set them 230 meters apart 
   249   // Set positionStep parameter of Experiment::Rung to -1
   250   // set RateErrorModel of Experiment::Run to 0
   251 
   252   myPositionStep = -1;
   253 
   254   MobilityHelper mobility;
   255   Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
   256   positionAlloc->Add (Vector (0.0, 0.0, 0.0));
   257   positionAlloc->Add (Vector (230.0, 0.0, 0.0));
   258   mobility.SetPositionAllocator (positionAlloc);
   259   mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
   260 
   261   wifiMac.SetType ("ns3::AdhocWifiMac");
   262 
   263   gnuplot = Gnuplot ("multi-rate-first.png");
   264   Config::SetDefault ("ns3::YansWifiPhy::Standard", StringValue ("holland"));
   265 
   266   NS_LOG_DEBUG ("minstrel");
   267   experiment = Experiment ("minstrel");
   268   wifi.SetRemoteStationManager ("ns3::MinstrelWifiManager");
   269   dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel, mobility, myPositionStep);
   270   gnuplot.AddDataset (dataset);
   271 
   272   NS_LOG_DEBUG ("ideal");
   273   experiment = Experiment ("ideal");
   274   wifi.SetRemoteStationManager ("ns3::IdealWifiManager");
   275   dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel, mobility, myPositionStep);
   276   gnuplot.AddDataset (dataset);
   277 
   278   gnuplot.GenerateOutput (std::cout);
   279 
   280 
   281 
   282 /*
   283   // Scenario 3:
   284   // Initially set them 25 meters apart, stationary
   285   // Set positionStep parameter of Experiment::Rung to 0 
   286   // This is a sanity check
   287 
   288   myPositionStep = 0;
   289   MobilityHelper mobility;
   290   Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
   291   positionAlloc->Add (Vector (0.0, 0.0, 0.0));
   292   positionAlloc->Add (Vector (25.0, 0.0, 0.0));
   293   mobility.SetPositionAllocator (positionAlloc);
   294   mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
   295 
   296   wifiMac.SetType ("ns3::AdhocWifiMac");
   297 
   298   gnuplot = Gnuplot ("multi-rate-first.png");
   299   Config::SetDefault ("ns3::YansWifiPhy::Standard", StringValue ("holland"));
   300 
   301   NS_LOG_DEBUG ("minstrel");
   302   experiment = Experiment ("minstrel");
   303   wifi.SetRemoteStationManager ("ns3::MinstrelWifiManager");
   304   dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel, mobility, myPositionStep);
   305   gnuplot.AddDataset (dataset);
   306 
   307   NS_LOG_DEBUG ("ideal");
   308   experiment = Experiment ("ideal");
   309   wifi.SetRemoteStationManager ("ns3::IdealWifiManager");
   310   dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel, mobility, myPositionStep);
   311   gnuplot.AddDataset (dataset);
   312 
   313   gnuplot.GenerateOutput (std::cout);
   314   */
   315 
   316   return 0;
   317 }