A Discrete-Event Network Simulator
API
wifi-adhoc.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005,2006,2007 INRIA
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
19 
20 #include "ns3/command-line.h"
21 #include "ns3/config.h"
22 #include "ns3/gnuplot.h"
23 #include "ns3/ipv4-address-helper.h"
24 #include "ns3/log.h"
25 #include "ns3/mobility-helper.h"
26 #include "ns3/mobility-model.h"
27 #include "ns3/on-off-helper.h"
28 #include "ns3/packet-socket-address.h"
29 #include "ns3/packet-socket-helper.h"
30 #include "ns3/string.h"
31 #include "ns3/uinteger.h"
32 #include "ns3/yans-wifi-channel.h"
33 #include "ns3/yans-wifi-helper.h"
34 
35 using namespace ns3;
36 
37 NS_LOG_COMPONENT_DEFINE("Wifi-Adhoc");
38 
45 {
46  public:
47  Experiment();
52  Experiment(std::string name);
53 
62  Gnuplot2dDataset Run(const WifiHelper& wifi,
63  const YansWifiPhyHelper& wifiPhy,
64  const WifiMacHelper& wifiMac,
65  const YansWifiChannelHelper& wifiChannel);
66 
67  private:
72  void ReceivePacket(Ptr<Socket> socket);
78  void SetPosition(Ptr<Node> node, Vector position);
84  Vector GetPosition(Ptr<Node> node);
89  void AdvancePosition(Ptr<Node> node);
96 
97  uint32_t m_bytesTotal;
99 };
100 
102 {
103 }
104 
105 Experiment::Experiment(std::string name)
106  : m_output(name)
107 {
108  m_output.SetStyle(Gnuplot2dDataset::LINES);
109 }
110 
111 void
113 {
115  mobility->SetPosition(position);
116 }
117 
118 Vector
120 {
122  return mobility->GetPosition();
123 }
124 
125 void
127 {
128  Vector pos = GetPosition(node);
129  double mbs = ((m_bytesTotal * 8.0) / 1000000);
130  m_bytesTotal = 0;
131  m_output.Add(pos.x, mbs);
132  pos.x += 1.0;
133  if (pos.x >= 210.0)
134  {
135  return;
136  }
137  SetPosition(node, pos);
138  Simulator::Schedule(Seconds(1.0), &Experiment::AdvancePosition, this, node);
139 }
140 
141 void
143 {
144  Ptr<Packet> packet;
145  while ((packet = socket->Recv()))
146  {
147  m_bytesTotal += packet->GetSize();
148  }
149 }
150 
153 {
154  TypeId tid = TypeId::LookupByName("ns3::PacketSocketFactory");
155  Ptr<Socket> sink = Socket::CreateSocket(node, tid);
156  sink->Bind();
157  sink->SetRecvCallback(MakeCallback(&Experiment::ReceivePacket, this));
158  return sink;
159 }
160 
163  const YansWifiPhyHelper& wifiPhy,
164  const WifiMacHelper& wifiMac,
165  const YansWifiChannelHelper& wifiChannel)
166 {
167  m_bytesTotal = 0;
168 
169  NodeContainer c;
170  c.Create(2);
171 
172  PacketSocketHelper packetSocket;
173  packetSocket.Install(c);
174 
175  YansWifiPhyHelper phy = wifiPhy;
176  phy.SetChannel(wifiChannel.Create());
177 
178  NetDeviceContainer devices = wifi.Install(phy, wifiMac, c);
179 
181  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
182  positionAlloc->Add(Vector(0.0, 0.0, 0.0));
183  positionAlloc->Add(Vector(5.0, 0.0, 0.0));
184  mobility.SetPositionAllocator(positionAlloc);
185  mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
186 
187  mobility.Install(c);
188 
189  PacketSocketAddress socket;
190  socket.SetSingleDevice(devices.Get(0)->GetIfIndex());
191  socket.SetPhysicalAddress(devices.Get(1)->GetAddress());
192  socket.SetProtocol(1);
193 
194  OnOffHelper onoff("ns3::PacketSocketFactory", Address(socket));
195  onoff.SetConstantRate(DataRate(60000000));
196  onoff.SetAttribute("PacketSize", UintegerValue(2000));
197 
198  ApplicationContainer apps = onoff.Install(c.Get(0));
199  apps.Start(Seconds(0.5));
200  apps.Stop(Seconds(250.0));
201 
202  Simulator::Schedule(Seconds(1.5), &Experiment::AdvancePosition, this, c.Get(1));
203  Ptr<Socket> recvSink = SetupPacketReceive(c.Get(1));
204 
205  Simulator::Run();
206 
207  Simulator::Destroy();
208 
209  return m_output;
210 }
211 
212 int
213 main(int argc, char* argv[])
214 {
215  CommandLine cmd(__FILE__);
216  cmd.Parse(argc, argv);
217 
218  Gnuplot gnuplot = Gnuplot("reference-rates.png");
219 
222  wifi.SetStandard(WIFI_STANDARD_80211a);
223  WifiMacHelper wifiMac;
224  YansWifiPhyHelper wifiPhy;
225  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default();
226  Gnuplot2dDataset dataset;
227 
228  wifiMac.SetType("ns3::AdhocWifiMac");
229 
230  NS_LOG_DEBUG("54");
231  experiment = Experiment("54mb");
232  wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
233  "DataMode",
234  StringValue("OfdmRate54Mbps"));
235  dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
236  gnuplot.AddDataset(dataset);
237 
238  NS_LOG_DEBUG("48");
239  experiment = Experiment("48mb");
240  wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
241  "DataMode",
242  StringValue("OfdmRate48Mbps"));
243  dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
244  gnuplot.AddDataset(dataset);
245 
246  NS_LOG_DEBUG("36");
247  experiment = Experiment("36mb");
248  wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
249  "DataMode",
250  StringValue("OfdmRate36Mbps"));
251  dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
252  gnuplot.AddDataset(dataset);
253 
254  NS_LOG_DEBUG("24");
255  experiment = Experiment("24mb");
256  wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
257  "DataMode",
258  StringValue("OfdmRate24Mbps"));
259  dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
260  gnuplot.AddDataset(dataset);
261 
262  NS_LOG_DEBUG("18");
263  experiment = Experiment("18mb");
264  wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
265  "DataMode",
266  StringValue("OfdmRate18Mbps"));
267  dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
268  gnuplot.AddDataset(dataset);
269 
270  NS_LOG_DEBUG("12");
271  experiment = Experiment("12mb");
272  wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
273  "DataMode",
274  StringValue("OfdmRate12Mbps"));
275  dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
276  gnuplot.AddDataset(dataset);
277 
278  NS_LOG_DEBUG("9");
279  experiment = Experiment("9mb");
280  wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
281  "DataMode",
282  StringValue("OfdmRate9Mbps"));
283  dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
284  gnuplot.AddDataset(dataset);
285 
286  NS_LOG_DEBUG("6");
287  experiment = Experiment("6mb");
288  wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
289  "DataMode",
290  StringValue("OfdmRate6Mbps"));
291  dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
292  gnuplot.AddDataset(dataset);
293 
294  gnuplot.GenerateOutput(std::cout);
295 
296  gnuplot = Gnuplot("rate-control.png");
297 
298  NS_LOG_DEBUG("arf");
299  experiment = Experiment("arf");
300  wifi.SetRemoteStationManager("ns3::ArfWifiManager");
301  dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
302  gnuplot.AddDataset(dataset);
303 
304  NS_LOG_DEBUG("aarf");
305  experiment = Experiment("aarf");
306  wifi.SetRemoteStationManager("ns3::AarfWifiManager");
307  dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
308  gnuplot.AddDataset(dataset);
309 
310  NS_LOG_DEBUG("aarf-cd");
311  experiment = Experiment("aarf-cd");
312  wifi.SetRemoteStationManager("ns3::AarfcdWifiManager");
313  dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
314  gnuplot.AddDataset(dataset);
315 
316  NS_LOG_DEBUG("cara");
317  experiment = Experiment("cara");
318  wifi.SetRemoteStationManager("ns3::CaraWifiManager");
319  dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
320  gnuplot.AddDataset(dataset);
321 
322  NS_LOG_DEBUG("rraa");
323  experiment = Experiment("rraa");
324  wifi.SetRemoteStationManager("ns3::RraaWifiManager");
325  dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
326  gnuplot.AddDataset(dataset);
327 
328  NS_LOG_DEBUG("ideal");
329  experiment = Experiment("ideal");
330  wifi.SetRemoteStationManager("ns3::IdealWifiManager");
331  dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
332  gnuplot.AddDataset(dataset);
333 
334  gnuplot.GenerateOutput(std::cout);
335 
336  return 0;
337 }
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
Create a socket and prepare it for packet reception.
WiFi adhoc experiment class.
Definition: wifi-adhoc.cc:45
Gnuplot2dDataset Run(const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy, const WifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel)
Run an experiment.
Definition: wifi-adhoc.cc:162
uint32_t m_bytesTotal
The number of received bytes.
Definition: wifi-adhoc.cc:97
void ReceivePacket(Ptr< Socket > socket)
Receive a packet.
Definition: wifi-adhoc.cc:142
void SetPosition(Ptr< Node > node, Vector position)
Set the position of a node.
Definition: wifi-adhoc.cc:112
void AdvancePosition(Ptr< Node > node)
Move a node by 1m on the x axis, stops at 210m.
Definition: wifi-adhoc.cc:126
Gnuplot2dDataset m_output
The output dataset.
Definition: wifi-adhoc.cc:98
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
Setup the receiving socket.
Definition: wifi-adhoc.cc:152
Vector GetPosition(Ptr< Node > node)
Get the position of a node.
Definition: wifi-adhoc.cc:119
a polymophic address class
Definition: address.h:101
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Parse command-line arguments.
Definition: command-line.h:232
Class to represent a 2D points plot.
Definition: gnuplot.h:118
void SetStyle(enum Style style)
Definition: gnuplot.cc:345
void Add(double x, double y)
Definition: gnuplot.cc:362
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:373
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:759
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:765
Helper class used to assign positions and mobility models to nodes.
Keep track of the current position and velocity of an object.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:44
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:861
an address for a packet socket
void SetProtocol(uint16_t protocol)
Set the protocol.
void SetPhysicalAddress(const Address address)
Set the destination address.
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
Give ns3::PacketSocket powers to ns3::Node.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
Hold variables of type string.
Definition: string.h:56
a unique identifier for an interface.
Definition: type-id.h:59
Hold an unsigned integer type.
Definition: uinteger.h:45
Vector3D Vector
Vector alias typedef for compatibility with mobility models.
Definition: vector.h:324
helps to create WifiNetDevice objects
Definition: wifi-helper.h:324
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
manage and create wifi channel objects for the YANS model.
Ptr< YansWifiChannel > Create() const
Make it easy to create and manage PHY objects for the YANS model.
void experiment(std::string queue_disc_type)
void ReceivePacket(Ptr< Socket > socket)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:327
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
@ WIFI_STANDARD_80211a
devices
Definition: first.py:42
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:704
cmd
Definition: second.py:40
wifi
Definition: third.py:95
mobility
Definition: third.py:105
phy
Definition: third.py:89
static void AdvancePosition(Ptr< Node > node)
Move a node position by 5m on the x axis every second, up to 210m.
Definition: wifi-ap.cc:153
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55