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