samples/main-adhoc-wifi.cc
changeset 2524 db72c0e7743e
parent 2494 1c69ea12779c
child 2559 ab98bf436a17
--- a/samples/main-adhoc-wifi.cc	Sat Mar 01 20:41:08 2008 +0100
+++ b/samples/main-adhoc-wifi.cc	Sat Mar 01 21:21:53 2008 +0100
@@ -19,6 +19,9 @@
  */
 
 #include "ns3/wifi-net-device.h"
+#include "ns3/arf-wifi-manager.h"
+#include "ns3/adhoc-wifi-mac.h"
+#include "ns3/wifi-phy.h"
 #include "ns3/wifi-channel.h"
 #include "ns3/simulator.h"
 #include "ns3/callback.h"
@@ -34,50 +37,67 @@
 #include "ns3/command-line.h"
 #include "ns3/gnuplot.h"
 #include "ns3/uinteger.h"
+#include "ns3/string.h"
+#include "ns3/config.h"
+#include "ns3/wifi-helper.h"
+#include "ns3/mobility-helper.h"
+#include "ns3/log.h"
 
 
 #include <iostream>
 
-using namespace ns3;
-static uint32_t g_bytesTotal = 0;
-static GnuplotDataset *g_output = 0;
+NS_LOG_COMPONENT_DEFINE ("Main");
 
-static Ptr<Node>
-CreateAdhocNode (Ptr<WifiChannel> channel,
-                 Vector position, const char *address)
+using namespace ns3;
+
+class Experiment
 {
-  Ptr<Node> node = CreateObject<Node> ();  
-  Ptr<AdhocWifiNetDevice> device = CreateObject<AdhocWifiNetDevice> (node, Mac48Address (address));
-  node->AddDevice (device);
-  device->Attach (channel);
-  Ptr<MobilityModel> mobility = CreateObject<StaticMobilityModel> ();
-  mobility->SetPosition (position);
-  node->AggregateObject (mobility);
-  
-  return node;
+public:
+  Experiment ();
+  Experiment (std::string name);
+  GnuplotDataset Run (const WifiHelper &wifi);
+private:
+  void ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &address);
+  void SetPosition (Ptr<Node> node, Vector position);
+  Vector GetPosition (Ptr<Node> node);
+  void AdvancePosition (Ptr<Node> node);
+  Ptr<Socket> SetupPacketReceive (Ptr<Node> node);
+
+  uint32_t m_bytesTotal;
+  GnuplotDataset m_output;
+};
+
+Experiment::Experiment ()
+  : m_output ()
+{}
+
+Experiment::Experiment (std::string name)
+  : m_output (name)
+{
+  m_output.SetStyle (GnuplotDataset::LINES);
 }
 
-static void
-SetPosition (Ptr<Node> node, Vector position)
+void
+Experiment::SetPosition (Ptr<Node> node, Vector position)
 {
   Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
   mobility->SetPosition (position);
 }
 
-static Vector
-GetPosition (Ptr<Node> node)
+Vector
+Experiment::GetPosition (Ptr<Node> node)
 {
   Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
   return mobility->GetPosition ();
 }
 
-static void 
-AdvancePosition (Ptr<Node> node) 
+void 
+Experiment::AdvancePosition (Ptr<Node> node) 
 {
   Vector pos = GetPosition (node);
-  double mbs = ((g_bytesTotal * 8.0) / 1000000);
-  g_bytesTotal = 0;
-  g_output->Add (pos.x, mbs);
+  double mbs = ((m_bytesTotal * 8.0) / 1000000);
+  m_bytesTotal = 0;
+  m_output.Add (pos.x, mbs);
   pos.x += 1.0;
   if (pos.x >= 210.0) 
     {
@@ -85,63 +105,70 @@
     }
   SetPosition (node, pos);
   //std::cout << "x="<<pos.x << std::endl;
-  Simulator::Schedule (Seconds (1.0), &AdvancePosition, node);
+  Simulator::Schedule (Seconds (1.0), &Experiment::AdvancePosition, this, node);
 }
 
-static void
-ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &address)
+void
+Experiment::ReceivePacket (Ptr<Socket> socket, Ptr<Packet> packet, const Address &address)
 {
-  g_bytesTotal += packet->GetSize ();
+  m_bytesTotal += packet->GetSize ();
 }
 
-static Ptr<Socket>
-SetupPacketReceive (Ptr<Node> node, uint16_t port)
+Ptr<Socket>
+Experiment::SetupPacketReceive (Ptr<Node> node)
 {
   TypeId tid = TypeId::LookupByName ("Packet");
   Ptr<SocketFactory> socketFactory = node->GetObject<SocketFactory> (tid);
   Ptr<Socket> sink = socketFactory->CreateSocket ();
   sink->Bind ();
-  sink->SetRecvCallback (MakeCallback (&ReceivePacket));
+  sink->SetRecvCallback (MakeCallback (&Experiment::ReceivePacket, this));
   return sink;
 }
 
-static void
-RunOneExperiment (void)
+GnuplotDataset
+Experiment::Run (const WifiHelper &wifi)
 {
-  g_bytesTotal = 0;
+  m_bytesTotal = 0;
 
-  Ptr<WifiChannel> channel = CreateObject<WifiChannel> ();
+  NodeContainer c;
+  c.Create (2);
 
-  Ptr<Node> a = CreateAdhocNode (channel, 
-                                 Vector (5.0,0.0,0.0),
-                                 "00:00:00:00:00:01");
-  Ptr<Node> b = CreateAdhocNode (channel,
-                                 Vector (0.0, 0.0, 0.0),
-                                 "00:00:00:00:00:02");
+  NetDeviceContainer devices = wifi.Build (c);
+
+  MobilityHelper mobility;
+  Ptr<ListPositionAllocator> positionAlloc = CreateObjectWith<ListPositionAllocator> ();
+  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
+  positionAlloc->Add (Vector (5.0, 0.0, 0.0));
+  mobility.SetPositionAllocator (positionAlloc);
+  mobility.SetMobilityModel ("StaticMobilityModel");
+
+  mobility.Layout (c.Begin (), c.End ());
 
   PacketSocketAddress destination = PacketSocketAddress ();
   destination.SetProtocol (1);
   destination.SetSingleDevice (0);
-  destination.SetPhysicalAddress (Mac48Address ("00:00:00:00:00:02"));
+  destination.SetPhysicalAddress (devices.Get (1)->GetAddress ());
   Ptr<Application> app = 
-    CreateObjectWith<OnOffApplication> ("Node", a, 
+    CreateObjectWith<OnOffApplication> ("Node", c.Get (0), 
                                         "Remote", Address (destination),
                                         "Protocol", TypeId::LookupByName ("Packet"),
                                         "OnTime", ConstantVariable (250),
                                         "OffTime", ConstantVariable (0),
                                         "DataRate", DataRate (60000000),
                                         "PacketSize", Uinteger (2000));
-  a->AddApplication (app);
+  c.Get (0)->AddApplication (app);
 
   app->Start (Seconds (0.5));
   app->Stop (Seconds (250.0));
 
-  Simulator::Schedule (Seconds (1.5), &AdvancePosition, b);
-  Ptr<Socket> recvSink = SetupPacketReceive (b, 10);
+  Simulator::Schedule (Seconds (1.5), &Experiment::AdvancePosition, this, c.Get (1));
+  Ptr<Socket> recvSink = SetupPacketReceive (c.Get (1));
 
   Simulator::Run ();
 
   Simulator::Destroy ();
+
+  return m_output;
 }
 
 int main (int argc, char *argv[])
@@ -149,106 +176,101 @@
   Simulator::SetLinkedList ();
 
   // disable fragmentation
-  DefaultValue::Bind ("WifiFragmentationThreshold", "2200");
+  Config::SetDefault ("WifiRemoteStationManager::FragmentationThreshold", String ("2200"));
+  Config::SetDefault ("WifiRemoteStationManager::RtsCtsThreshold", String ("2200"));
+
   CommandLine::Parse (argc, argv);
 
   Gnuplot gnuplot = Gnuplot ("reference-rates.png");
 
-  DefaultValue::Bind ("WifiRtsCtsThreshold", "2200");
+  Experiment experiment;
+  WifiHelper wifi;
+  GnuplotDataset dataset;
+
+  wifi.SetMac ("AdhocWifiMac");
+  wifi.SetPhy ("WifiPhy");
 
-  g_output = new GnuplotDataset ("54mb");
-  g_output->SetStyle (GnuplotDataset::LINES);
-  DefaultValue::Bind ("WifiRateControlAlgorithm", "ConstantRate");
-  DefaultValue::Bind ("WifiConstantDataRate", "54mb");
-  RunOneExperiment ();
-  gnuplot.AddDataset (*g_output);
-  delete g_output;
+  NS_LOG_DEBUG ("54");
+  experiment = Experiment ("54mb");
+  wifi.SetRemoteStationManager ("ConstantRateWifiManager",
+                                "DataMode", String ("wifia-54mbs"));
+  dataset = experiment.Run (wifi);
+  gnuplot.AddDataset (dataset);
 
-  g_output = new GnuplotDataset ("48mb");
-  g_output->SetStyle (GnuplotDataset::LINES);
-  DefaultValue::Bind ("WifiRateControlAlgorithm", "ConstantRate");
-  DefaultValue::Bind ("WifiConstantDataRate", "48mb");
-  RunOneExperiment ();
-  gnuplot.AddDataset (*g_output);
-  delete g_output;
+  NS_LOG_DEBUG ("48");
+  experiment = Experiment ("48mb");
+  wifi.SetRemoteStationManager ("ConstantRateWifiManager",
+                                "DataMode", String ("wifia-48mbs"));
+  dataset = experiment.Run (wifi);
+  gnuplot.AddDataset (dataset);
 
-  g_output = new GnuplotDataset ("36mb");
-  g_output->SetStyle (GnuplotDataset::LINES);
-  DefaultValue::Bind ("WifiRateControlAlgorithm", "ConstantRate");
-  DefaultValue::Bind ("WifiConstantDataRate", "36mb");
-  RunOneExperiment ();
-  gnuplot.AddDataset (*g_output);
-  delete g_output;
+  NS_LOG_DEBUG ("36");
+  experiment = Experiment ("36mb");
+  wifi.SetRemoteStationManager ("ConstantRateWifiManager",
+                                "DataMode", String ("wifia-36mbs"));
+  dataset = experiment.Run (wifi);
+  gnuplot.AddDataset (dataset);
 
-  g_output = new GnuplotDataset ("24mb");
-  g_output->SetStyle (GnuplotDataset::LINES);
-  DefaultValue::Bind ("WifiRateControlAlgorithm", "ConstantRate");
-  DefaultValue::Bind ("WifiConstantDataRate", "24mb");
-  RunOneExperiment ();
-  gnuplot.AddDataset (*g_output);
-  delete g_output;
+  NS_LOG_DEBUG ("24");
+  experiment = Experiment ("24mb");
+  wifi.SetRemoteStationManager ("ConstantRateWifiManager",
+                                "DataMode", String ("wifia-24mbs"));
+  dataset = experiment.Run (wifi);
+  gnuplot.AddDataset (dataset);
 
-  g_output = new GnuplotDataset ("18mb");
-  g_output->SetStyle (GnuplotDataset::LINES);
-  DefaultValue::Bind ("WifiRateControlAlgorithm", "ConstantRate");
-  DefaultValue::Bind ("WifiConstantDataRate", "18mb");
-  RunOneExperiment ();
-  gnuplot.AddDataset (*g_output);
-  delete g_output;
+  NS_LOG_DEBUG ("18");
+  experiment = Experiment ("18mb");
+  wifi.SetRemoteStationManager ("ConstantRateWifiManager",
+                                "DataMode", String ("wifia-18mbs"));
+  dataset = experiment.Run (wifi);
+  gnuplot.AddDataset (dataset);
 
-  g_output = new GnuplotDataset ("12mb");
-  g_output->SetStyle (GnuplotDataset::LINES);
-  DefaultValue::Bind ("WifiRateControlAlgorithm", "ConstantRate");
-  DefaultValue::Bind ("WifiConstantDataRate", "12mb");
-  RunOneExperiment ();
-  gnuplot.AddDataset (*g_output);
-  delete g_output;
+  NS_LOG_DEBUG ("12");
+  experiment = Experiment ("12mb");
+  wifi.SetRemoteStationManager ("ConstantRateWifiManager",
+                                "DataMode", String ("wifia-12mbs"));
+  dataset = experiment.Run (wifi);
+  gnuplot.AddDataset (dataset);
 
-  g_output = new GnuplotDataset ("9mb");
-  g_output->SetStyle (GnuplotDataset::LINES);
-  DefaultValue::Bind ("WifiRateControlAlgorithm", "ConstantRate");
-  DefaultValue::Bind ("WifiConstantDataRate", "9mb");
-  RunOneExperiment ();
-  gnuplot.AddDataset (*g_output);
-  delete g_output;
+  NS_LOG_DEBUG ("9");
+  experiment = Experiment ("9mb");
+  wifi.SetRemoteStationManager ("ConstantRateWifiManager",
+                                "DataMode", String ("wifia-9mbs"));
+  dataset = experiment.Run (wifi);
+  gnuplot.AddDataset (dataset);
 
-  g_output = new GnuplotDataset ("6mb");
-  g_output->SetStyle (GnuplotDataset::LINES);
-  DefaultValue::Bind ("WifiRateControlAlgorithm", "ConstantRate");
-  DefaultValue::Bind ("WifiConstantDataRate", "6mb");
-  RunOneExperiment ();
-  gnuplot.AddDataset (*g_output);
-  delete g_output;
+  NS_LOG_DEBUG ("6");
+  experiment = Experiment ("6mb");
+  wifi.SetRemoteStationManager ("ConstantRateWifiManager",
+                                "DataMode", String ("wifia-6mbs"));
+  dataset = experiment.Run (wifi);
+  gnuplot.AddDataset (dataset);
 
   gnuplot.GenerateOutput (std::cout);
 
-  gnuplot = Gnuplot ("rate-control.png");
+
 
-  DefaultValue::Bind ("WifiPhyStandard", "holland");
+  gnuplot = Gnuplot ("rate-control.png");
+  Config::SetDefault ("WifiPhy::Standard", String ("holland"));
+
 
-  g_output = new GnuplotDataset ("arf");
-  g_output->SetStyle (GnuplotDataset::LINES);
-  DefaultValue::Bind ("WifiRtsCtsThreshold", "2200");
-  DefaultValue::Bind ("WifiRateControlAlgorithm", "Arf");
-  RunOneExperiment ();
-  gnuplot.AddDataset (*g_output);
-  delete g_output;
+  NS_LOG_DEBUG ("arf");
+  experiment = Experiment ("arf");
+  wifi.SetRemoteStationManager ("ArfWifiManager");
+  dataset = experiment.Run (wifi);
+  gnuplot.AddDataset (dataset);
 
-  g_output = new GnuplotDataset ("aarf");
-  g_output->SetStyle (GnuplotDataset::LINES);
-  DefaultValue::Bind ("WifiRtsCtsThreshold", "2200");
-  DefaultValue::Bind ("WifiRateControlAlgorithm", "Aarf");
-  RunOneExperiment ();
-  gnuplot.AddDataset (*g_output);
-  delete g_output;
+  NS_LOG_DEBUG ("aarf");
+  experiment = Experiment ("aarf");
+  wifi.SetRemoteStationManager ("AarfWifiManager");
+  dataset = experiment.Run (wifi);
+  gnuplot.AddDataset (dataset);
 
-  g_output = new GnuplotDataset ("ideal");
-  g_output->SetStyle (GnuplotDataset::LINES);
-  DefaultValue::Bind ("WifiRtsCtsThreshold", "2200");
-  DefaultValue::Bind ("WifiRateControlAlgorithm", "Ideal");
-  RunOneExperiment ();
-  gnuplot.AddDataset (*g_output);
-  delete g_output;
+  NS_LOG_DEBUG ("ideal");
+  experiment = Experiment ("ideal");
+  wifi.SetRemoteStationManager ("IdealWifiManager");
+  dataset = experiment.Run (wifi);
+  gnuplot.AddDataset (dataset);
 
   gnuplot.GenerateOutput (std::cout);