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  //std::cout << "x="<<pos.x << std::endl;
88  Simulator::Schedule (Seconds (1.0), &Experiment::AdvancePosition, this, node);
89 }
90 
91 void
93 {
94  Ptr<Packet> packet;
95  while ((packet = socket->Recv ()))
96  {
97  m_bytesTotal += packet->GetSize ();
98  }
99 }
100 
103 {
104  TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory");
105  Ptr<Socket> sink = Socket::CreateSocket (node, tid);
106  sink->Bind ();
108  return sink;
109 }
110 
113  const WifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel)
114 {
115  m_bytesTotal = 0;
116 
117  NodeContainer c;
118  c.Create (2);
119 
120  PacketSocketHelper packetSocket;
121  packetSocket.Install (c);
122 
123  YansWifiPhyHelper phy = wifiPhy;
124  phy.SetChannel (wifiChannel.Create ());
125 
126  WifiMacHelper mac = wifiMac;
127  NetDeviceContainer devices = wifi.Install (phy, mac, c);
128 
130  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
131  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
132  positionAlloc->Add (Vector (5.0, 0.0, 0.0));
133  mobility.SetPositionAllocator (positionAlloc);
134  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
135 
136  mobility.Install (c);
137 
138  PacketSocketAddress socket;
139  socket.SetSingleDevice (devices.Get (0)->GetIfIndex ());
140  socket.SetPhysicalAddress (devices.Get (1)->GetAddress ());
141  socket.SetProtocol (1);
142 
143  OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
144  onoff.SetConstantRate (DataRate (60000000));
145  onoff.SetAttribute ("PacketSize", UintegerValue (2000));
146 
147  ApplicationContainer apps = onoff.Install (c.Get (0));
148  apps.Start (Seconds (0.5));
149  apps.Stop (Seconds (250.0));
150 
151  Simulator::Schedule (Seconds (1.5), &Experiment::AdvancePosition, this, c.Get (1));
152  Ptr<Socket> recvSink = SetupPacketReceive (c.Get (1));
153 
154  Simulator::Run ();
155 
156  Simulator::Destroy ();
157 
158  return m_output;
159 }
160 
161 int main (int argc, char *argv[])
162 {
163  // disable fragmentation
164  Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
165  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200"));
166 
168  cmd.Parse (argc, argv);
169 
170  Gnuplot gnuplot = Gnuplot ("reference-rates.png");
171 
175  WifiMacHelper wifiMac;
176  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
177  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
178  Gnuplot2dDataset dataset;
179 
180  wifiMac.SetType ("ns3::AdhocWifiMac");
181 
182  NS_LOG_DEBUG ("54");
183  experiment = Experiment ("54mb");
184  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
185  "DataMode", StringValue ("OfdmRate54Mbps"));
186  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
187  gnuplot.AddDataset (dataset);
188 
189  NS_LOG_DEBUG ("48");
190  experiment = Experiment ("48mb");
191  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
192  "DataMode", StringValue ("OfdmRate48Mbps"));
193  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
194  gnuplot.AddDataset (dataset);
195 
196  NS_LOG_DEBUG ("36");
197  experiment = Experiment ("36mb");
198  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
199  "DataMode", StringValue ("OfdmRate36Mbps"));
200  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
201  gnuplot.AddDataset (dataset);
202 
203  NS_LOG_DEBUG ("24");
204  experiment = Experiment ("24mb");
205  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
206  "DataMode", StringValue ("OfdmRate24Mbps"));
207  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
208  gnuplot.AddDataset (dataset);
209 
210  NS_LOG_DEBUG ("18");
211  experiment = Experiment ("18mb");
212  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
213  "DataMode", StringValue ("OfdmRate18Mbps"));
214  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
215  gnuplot.AddDataset (dataset);
216 
217  NS_LOG_DEBUG ("12");
218  experiment = Experiment ("12mb");
219  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
220  "DataMode", StringValue ("OfdmRate12Mbps"));
221  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
222  gnuplot.AddDataset (dataset);
223 
224  NS_LOG_DEBUG ("9");
225  experiment = Experiment ("9mb");
226  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
227  "DataMode", StringValue ("OfdmRate9Mbps"));
228  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
229  gnuplot.AddDataset (dataset);
230 
231  NS_LOG_DEBUG ("6");
232  experiment = Experiment ("6mb");
233  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
234  "DataMode", StringValue ("OfdmRate6Mbps"));
235  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
236  gnuplot.AddDataset (dataset);
237 
238  gnuplot.GenerateOutput (std::cout);
239 
240  gnuplot = Gnuplot ("rate-control.png");
242 
243  NS_LOG_DEBUG ("arf");
244  experiment = Experiment ("arf");
245  wifi.SetRemoteStationManager ("ns3::ArfWifiManager");
246  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
247  gnuplot.AddDataset (dataset);
248 
249  NS_LOG_DEBUG ("aarf");
250  experiment = Experiment ("aarf");
251  wifi.SetRemoteStationManager ("ns3::AarfWifiManager");
252  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
253  gnuplot.AddDataset (dataset);
254 
255  NS_LOG_DEBUG ("aarf-cd");
256  experiment = Experiment ("aarf-cd");
257  wifi.SetRemoteStationManager ("ns3::AarfcdWifiManager");
258  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
259  gnuplot.AddDataset (dataset);
260 
261  NS_LOG_DEBUG ("cara");
262  experiment = Experiment ("cara");
263  wifi.SetRemoteStationManager ("ns3::CaraWifiManager");
264  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
265  gnuplot.AddDataset (dataset);
266 
267  NS_LOG_DEBUG ("rraa");
268  experiment = Experiment ("rraa");
269  wifi.SetRemoteStationManager ("ns3::RraaWifiManager");
270  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
271  gnuplot.AddDataset (dataset);
272 
273  NS_LOG_DEBUG ("ideal");
274  experiment = Experiment ("ideal");
275  wifi.SetRemoteStationManager ("ns3::IdealWifiManager");
276  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
277  gnuplot.AddDataset (dataset);
278 
279  gnuplot.GenerateOutput (std::cout);
280 
281  return 0;
282 }
Helper class for UAN CW MAC example.
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:45
holds a vector of ns3::Application pointers.
void experiment(bool enableCtsRts)
Run single 10 seconds experiment with enabled or disabled RTS/CTS mechanism.
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:719
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:796
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
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 void SetStandard(enum WifiPhyStandard standard)
Definition: wifi-helper.cc:742
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer::Iterator first, NodeContainer::Iterator last) const
Definition: wifi-helper.cc:748
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:993
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.