A Discrete-Event Network Simulator
API
wifi-adhoc.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005,2006,2007 INRIA
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #include "ns3/core-module.h"
22 #include "ns3/network-module.h"
23 #include "ns3/applications-module.h"
24 #include "ns3/mobility-module.h"
25 #include "ns3/stats-module.h"
26 #include "ns3/wifi-module.h"
27 
28 using namespace ns3;
29 
31 
32 class Experiment
33 {
34 public:
35  Experiment ();
36  Experiment (std::string name);
37  Gnuplot2dDataset Run (const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy,
38  const WifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel);
39 private:
40  void ReceivePacket (Ptr<Socket> socket);
41  void SetPosition (Ptr<Node> node, Vector position);
42  Vector GetPosition (Ptr<Node> node);
43  void AdvancePosition (Ptr<Node> node);
45 
46  uint32_t m_bytesTotal;
47  Gnuplot2dDataset m_output;
48 };
49 
51 {
52 }
53 
54 Experiment::Experiment (std::string name)
55  : m_output (name)
56 {
57  m_output.SetStyle (Gnuplot2dDataset::LINES);
58 }
59 
60 void
62 {
64  mobility->SetPosition (position);
65 }
66 
67 Vector
69 {
71  return mobility->GetPosition ();
72 }
73 
74 void
76 {
77  Vector pos = GetPosition (node);
78  double mbs = ((m_bytesTotal * 8.0) / 1000000);
79  m_bytesTotal = 0;
80  m_output.Add (pos.x, mbs);
81  pos.x += 1.0;
82  if (pos.x >= 210.0)
83  {
84  return;
85  }
86  SetPosition (node, pos);
87  Simulator::Schedule (Seconds (1.0), &Experiment::AdvancePosition, this, node);
88 }
89 
90 void
92 {
93  Ptr<Packet> packet;
94  while ((packet = socket->Recv ()))
95  {
96  m_bytesTotal += packet->GetSize ();
97  }
98 }
99 
102 {
103  TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory");
104  Ptr<Socket> sink = Socket::CreateSocket (node, tid);
105  sink->Bind ();
107  return sink;
108 }
109 
112  const WifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel)
113 {
114  m_bytesTotal = 0;
115 
116  NodeContainer c;
117  c.Create (2);
118 
119  PacketSocketHelper packetSocket;
120  packetSocket.Install (c);
121 
122  YansWifiPhyHelper phy = wifiPhy;
123  phy.SetChannel (wifiChannel.Create ());
124 
125  WifiMacHelper mac = wifiMac;
126  NetDeviceContainer devices = wifi.Install (phy, mac, c);
127 
129  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
130  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
131  positionAlloc->Add (Vector (5.0, 0.0, 0.0));
132  mobility.SetPositionAllocator (positionAlloc);
133  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
134 
135  mobility.Install (c);
136 
137  PacketSocketAddress socket;
138  socket.SetSingleDevice (devices.Get (0)->GetIfIndex ());
139  socket.SetPhysicalAddress (devices.Get (1)->GetAddress ());
140  socket.SetProtocol (1);
141 
142  OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
143  onoff.SetConstantRate (DataRate (60000000));
144  onoff.SetAttribute ("PacketSize", UintegerValue (2000));
145 
146  ApplicationContainer apps = onoff.Install (c.Get (0));
147  apps.Start (Seconds (0.5));
148  apps.Stop (Seconds (250.0));
149 
150  Simulator::Schedule (Seconds (1.5), &Experiment::AdvancePosition, this, c.Get (1));
151  Ptr<Socket> recvSink = SetupPacketReceive (c.Get (1));
152 
153  Simulator::Run ();
154 
155  Simulator::Destroy ();
156 
157  return m_output;
158 }
159 
160 int main (int argc, char *argv[])
161 {
162  // disable fragmentation
163  Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
164  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200"));
165 
167  cmd.Parse (argc, argv);
168 
169  Gnuplot gnuplot = Gnuplot ("reference-rates.png");
170 
174  WifiMacHelper wifiMac;
175  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
176  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
177  Gnuplot2dDataset dataset;
178 
179  wifiMac.SetType ("ns3::AdhocWifiMac");
180 
181  NS_LOG_DEBUG ("54");
182  experiment = Experiment ("54mb");
183  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
184  "DataMode", StringValue ("OfdmRate54Mbps"));
185  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
186  gnuplot.AddDataset (dataset);
187 
188  NS_LOG_DEBUG ("48");
189  experiment = Experiment ("48mb");
190  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
191  "DataMode", StringValue ("OfdmRate48Mbps"));
192  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
193  gnuplot.AddDataset (dataset);
194 
195  NS_LOG_DEBUG ("36");
196  experiment = Experiment ("36mb");
197  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
198  "DataMode", StringValue ("OfdmRate36Mbps"));
199  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
200  gnuplot.AddDataset (dataset);
201 
202  NS_LOG_DEBUG ("24");
203  experiment = Experiment ("24mb");
204  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
205  "DataMode", StringValue ("OfdmRate24Mbps"));
206  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
207  gnuplot.AddDataset (dataset);
208 
209  NS_LOG_DEBUG ("18");
210  experiment = Experiment ("18mb");
211  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
212  "DataMode", StringValue ("OfdmRate18Mbps"));
213  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
214  gnuplot.AddDataset (dataset);
215 
216  NS_LOG_DEBUG ("12");
217  experiment = Experiment ("12mb");
218  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
219  "DataMode", StringValue ("OfdmRate12Mbps"));
220  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
221  gnuplot.AddDataset (dataset);
222 
223  NS_LOG_DEBUG ("9");
224  experiment = Experiment ("9mb");
225  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
226  "DataMode", StringValue ("OfdmRate9Mbps"));
227  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
228  gnuplot.AddDataset (dataset);
229 
230  NS_LOG_DEBUG ("6");
231  experiment = Experiment ("6mb");
232  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
233  "DataMode", StringValue ("OfdmRate6Mbps"));
234  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
235  gnuplot.AddDataset (dataset);
236 
237  gnuplot.GenerateOutput (std::cout);
238 
239  gnuplot = Gnuplot ("rate-control.png");
241 
242  NS_LOG_DEBUG ("arf");
243  experiment = Experiment ("arf");
244  wifi.SetRemoteStationManager ("ns3::ArfWifiManager");
245  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
246  gnuplot.AddDataset (dataset);
247 
248  NS_LOG_DEBUG ("aarf");
249  experiment = Experiment ("aarf");
250  wifi.SetRemoteStationManager ("ns3::AarfWifiManager");
251  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
252  gnuplot.AddDataset (dataset);
253 
254  NS_LOG_DEBUG ("aarf-cd");
255  experiment = Experiment ("aarf-cd");
256  wifi.SetRemoteStationManager ("ns3::AarfcdWifiManager");
257  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
258  gnuplot.AddDataset (dataset);
259 
260  NS_LOG_DEBUG ("cara");
261  experiment = Experiment ("cara");
262  wifi.SetRemoteStationManager ("ns3::CaraWifiManager");
263  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
264  gnuplot.AddDataset (dataset);
265 
266  NS_LOG_DEBUG ("rraa");
267  experiment = Experiment ("rraa");
268  wifi.SetRemoteStationManager ("ns3::RraaWifiManager");
269  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
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);
276  gnuplot.AddDataset (dataset);
277 
278  gnuplot.GenerateOutput (std::cout);
279 
280  return 0;
281 }
Helper class for UAN CW MAC example.
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:45
holds a vector of ns3::Application pointers.
uint32_t m_bytesTotal
Total bytes received.
Definition: wifi-adhoc.cc:46
tuple devices
Definition: first.py:32
Vector GetPosition(Ptr< Node > node)
Definition: wifi-adhoc.cc:68
static void AdvancePosition(Ptr< Node > node)
Definition: wifi-ap.cc:96
Class to represent a 2D points plot.
Definition: gnuplot.h:117
Ptr< YansWifiChannel > Create(void) const
void SetRemoteStationManager(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
Definition: wifi-helper.cc:700
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
Hold variables of type string.
Definition: string.h:41
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Make it easy to create and manage PHY objects for the yans model.
static Vector GetPosition(Ptr< Node > node)
Definition: multirate.cc:329
an address for a packet socket
void ReceivePacket(Ptr< Socket > socket)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:821
Vector GetPosition(void) const
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:756
helps to create WifiNetDevice objects
Definition: wifi-helper.h:213
void ReceivePacket(Ptr< Socket > socket)
Definition: multirate.cc:177
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
Give ns3::PacketSocket powers to ns3::Node.
tuple cmd
Definition: second.py:35
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
static void SetPosition(Ptr< Node > node, Vector position)
Definition: wifi-ap.cc:82
a polymophic address class
Definition: address.h:90
Class for representing data rates.
Definition: data-rate.h:88
virtual void SetStandard(WifiPhyStandard standard)
Definition: wifi-helper.cc:723
Keep track of the current position and velocity of an object.
void SetChannel(Ptr< YansWifiChannel > channel)
double Run(Parameters params)
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
tuple mobility
Definition: third.py:101
tuple phy
Definition: third.py:86
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:371
Hold an unsigned integer type.
Definition: uinteger.h:44
Vector3D Vector
Definition: vector.h:217
holds a vector of ns3::NetDevice pointers
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer::Iterator first, NodeContainer::Iterator last) const
Definition: wifi-helper.cc:729
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:762
void Add(double x, double y)
Definition: gnuplot.cc:359
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:128
tuple mac
Definition: third.py:92
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:205
This is intended to be the configuration used in this paper: Gavin Holland, Nitin Vaidya and Paramvir...
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
OFDM PHY for the 5 GHz band (Clause 17)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SetConstantRate(DataRate dataRate, uint32_t packetSize=512)
Helper function to set a constant rate source.
void SetPhysicalAddress(const Address address)
Set the destination address.
keep track of a set of node pointers.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
void SetMobilityModel(std::string type, std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue())
Gnuplot2dDataset m_output
Definition: multirate.cc:111
manage and create wifi channel objects for the yans model.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
create MAC layers for a ns3::WifiNetDevice.
void SetStyle(enum Style style)
Definition: gnuplot.cc:342
void SetPosition(const Vector &position)
virtual void SetType(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue(), std::string n10="", const AttributeValue &v10=EmptyAttributeValue())
Helper class used to assign positions and mobility models to nodes.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:269
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1007
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:782
void SetProtocol(uint16_t protocol)
Set the protocol.
void Add(Vector v)
Add a position to the list of positions.
void Parse(int argc, char *argv[])
Parse the program arguments.
tuple wifi
Definition: third.py:89
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void AdvancePosition(Ptr< Node > node)
Definition: wifi-adhoc.cc:75
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
void SetPosition(Ptr< Node > node, Vector position)
Definition: wifi-adhoc.cc:61
a unique identifier for an interface.
Definition: type-id.h:58
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
Definition: multirate.cc:165
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
void experiment(bool enableCtsRts, std::string wifiManager)
Run single 10 seconds experiment.