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/gnuplot.h"
22 #include "ns3/command-line.h"
23 #include "ns3/config.h"
24 #include "ns3/uinteger.h"
25 #include "ns3/string.h"
26 #include "ns3/log.h"
27 #include "ns3/yans-wifi-helper.h"
28 #include "ns3/mobility-helper.h"
29 #include "ns3/ipv4-address-helper.h"
30 #include "ns3/on-off-helper.h"
31 #include "ns3/yans-wifi-channel.h"
32 #include "ns3/mobility-model.h"
33 #include "ns3/packet-socket-helper.h"
34 #include "ns3/packet-socket-address.h"
35 
36 using namespace ns3;
37 
38 NS_LOG_COMPONENT_DEFINE ("Wifi-Adhoc");
39 
40 class Experiment
41 {
42 public:
43  Experiment ();
44  Experiment (std::string name);
45  Gnuplot2dDataset Run (const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy,
46  const WifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel);
47 private:
48  void ReceivePacket (Ptr<Socket> socket);
49  void SetPosition (Ptr<Node> node, Vector position);
50  Vector GetPosition (Ptr<Node> node);
51  void AdvancePosition (Ptr<Node> node);
53 
54  uint32_t m_bytesTotal;
55  Gnuplot2dDataset m_output;
56 };
57 
59 {
60 }
61 
62 Experiment::Experiment (std::string name)
63  : m_output (name)
64 {
65  m_output.SetStyle (Gnuplot2dDataset::LINES);
66 }
67 
68 void
70 {
72  mobility->SetPosition (position);
73 }
74 
75 Vector
77 {
79  return mobility->GetPosition ();
80 }
81 
82 void
84 {
85  Vector pos = GetPosition (node);
86  double mbs = ((m_bytesTotal * 8.0) / 1000000);
87  m_bytesTotal = 0;
88  m_output.Add (pos.x, mbs);
89  pos.x += 1.0;
90  if (pos.x >= 210.0)
91  {
92  return;
93  }
94  SetPosition (node, pos);
95  Simulator::Schedule (Seconds (1.0), &Experiment::AdvancePosition, this, node);
96 }
97 
98 void
100 {
101  Ptr<Packet> packet;
102  while ((packet = socket->Recv ()))
103  {
104  m_bytesTotal += packet->GetSize ();
105  }
106 }
107 
110 {
111  TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory");
112  Ptr<Socket> sink = Socket::CreateSocket (node, tid);
113  sink->Bind ();
114  sink->SetRecvCallback (MakeCallback (&Experiment::ReceivePacket, this));
115  return sink;
116 }
117 
120  const WifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel)
121 {
122  m_bytesTotal = 0;
123 
124  NodeContainer c;
125  c.Create (2);
126 
127  PacketSocketHelper packetSocket;
128  packetSocket.Install (c);
129 
130  YansWifiPhyHelper phy = wifiPhy;
131  phy.SetChannel (wifiChannel.Create ());
132 
133  WifiMacHelper mac = wifiMac;
134  NetDeviceContainer devices = wifi.Install (phy, mac, c);
135 
137  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
138  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
139  positionAlloc->Add (Vector (5.0, 0.0, 0.0));
140  mobility.SetPositionAllocator (positionAlloc);
141  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
142 
143  mobility.Install (c);
144 
145  PacketSocketAddress socket;
146  socket.SetSingleDevice (devices.Get (0)->GetIfIndex ());
147  socket.SetPhysicalAddress (devices.Get (1)->GetAddress ());
148  socket.SetProtocol (1);
149 
150  OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
151  onoff.SetConstantRate (DataRate (60000000));
152  onoff.SetAttribute ("PacketSize", UintegerValue (2000));
153 
154  ApplicationContainer apps = onoff.Install (c.Get (0));
155  apps.Start (Seconds (0.5));
156  apps.Stop (Seconds (250.0));
157 
158  Simulator::Schedule (Seconds (1.5), &Experiment::AdvancePosition, this, c.Get (1));
159  Ptr<Socket> recvSink = SetupPacketReceive (c.Get (1));
160 
161  Simulator::Run ();
162 
163  Simulator::Destroy ();
164 
165  return m_output;
166 }
167 
168 int main (int argc, char *argv[])
169 {
171  cmd.Parse (argc, argv);
172 
173  Gnuplot gnuplot = Gnuplot ("reference-rates.png");
174 
177  wifi.SetStandard (WIFI_PHY_STANDARD_80211a);
178  WifiMacHelper wifiMac;
179  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
180  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
181  Gnuplot2dDataset dataset;
182 
183  wifiMac.SetType ("ns3::AdhocWifiMac");
184 
185  NS_LOG_DEBUG ("54");
186  experiment = Experiment ("54mb");
187  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
188  "DataMode", StringValue ("OfdmRate54Mbps"));
189  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
190  gnuplot.AddDataset (dataset);
191 
192  NS_LOG_DEBUG ("48");
193  experiment = Experiment ("48mb");
194  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
195  "DataMode", StringValue ("OfdmRate48Mbps"));
196  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
197  gnuplot.AddDataset (dataset);
198 
199  NS_LOG_DEBUG ("36");
200  experiment = Experiment ("36mb");
201  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
202  "DataMode", StringValue ("OfdmRate36Mbps"));
203  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
204  gnuplot.AddDataset (dataset);
205 
206  NS_LOG_DEBUG ("24");
207  experiment = Experiment ("24mb");
208  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
209  "DataMode", StringValue ("OfdmRate24Mbps"));
210  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
211  gnuplot.AddDataset (dataset);
212 
213  NS_LOG_DEBUG ("18");
214  experiment = Experiment ("18mb");
215  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
216  "DataMode", StringValue ("OfdmRate18Mbps"));
217  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
218  gnuplot.AddDataset (dataset);
219 
220  NS_LOG_DEBUG ("12");
221  experiment = Experiment ("12mb");
222  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
223  "DataMode", StringValue ("OfdmRate12Mbps"));
224  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
225  gnuplot.AddDataset (dataset);
226 
227  NS_LOG_DEBUG ("9");
228  experiment = Experiment ("9mb");
229  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
230  "DataMode", StringValue ("OfdmRate9Mbps"));
231  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
232  gnuplot.AddDataset (dataset);
233 
234  NS_LOG_DEBUG ("6");
235  experiment = Experiment ("6mb");
236  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
237  "DataMode", StringValue ("OfdmRate6Mbps"));
238  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
239  gnuplot.AddDataset (dataset);
240 
241  gnuplot.GenerateOutput (std::cout);
242 
243  gnuplot = Gnuplot ("rate-control.png");
244  wifi.SetStandard (WIFI_PHY_STANDARD_holland);
245 
246  NS_LOG_DEBUG ("arf");
247  experiment = Experiment ("arf");
248  wifi.SetRemoteStationManager ("ns3::ArfWifiManager");
249  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
250  gnuplot.AddDataset (dataset);
251 
252  NS_LOG_DEBUG ("aarf");
253  experiment = Experiment ("aarf");
254  wifi.SetRemoteStationManager ("ns3::AarfWifiManager");
255  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
256  gnuplot.AddDataset (dataset);
257 
258  NS_LOG_DEBUG ("aarf-cd");
259  experiment = Experiment ("aarf-cd");
260  wifi.SetRemoteStationManager ("ns3::AarfcdWifiManager");
261  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
262  gnuplot.AddDataset (dataset);
263 
264  NS_LOG_DEBUG ("cara");
265  experiment = Experiment ("cara");
266  wifi.SetRemoteStationManager ("ns3::CaraWifiManager");
267  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
268  gnuplot.AddDataset (dataset);
269 
270  NS_LOG_DEBUG ("rraa");
271  experiment = Experiment ("rraa");
272  wifi.SetRemoteStationManager ("ns3::RraaWifiManager");
273  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
274  gnuplot.AddDataset (dataset);
275 
276  NS_LOG_DEBUG ("ideal");
277  experiment = Experiment ("ideal");
278  wifi.SetRemoteStationManager ("ns3::IdealWifiManager");
279  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
280  gnuplot.AddDataset (dataset);
281 
282  gnuplot.GenerateOutput (std::cout);
283 
284  return 0;
285 }
Helper class for UAN CW MAC example.
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56
holds a vector of ns3::Application pointers.
uint32_t m_bytesTotal
Total bytes received.
Definition: wifi-adhoc.cc:54
Vector GetPosition(Ptr< Node > node)
Definition: wifi-adhoc.cc:76
static void AdvancePosition(Ptr< Node > node)
Definition: wifi-ap.cc:103
Class to represent a 2D points plot.
Definition: gnuplot.h:117
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:831
Hold variables of type string.
Definition: string.h:41
Make it easy to create and manage PHY objects for the yans model.
static Vector GetPosition(Ptr< Node > node)
Definition: multirate.cc:341
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:202
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:756
cmd
Definition: second.py:35
helps to create WifiNetDevice objects
Definition: wifi-helper.h:230
void ReceivePacket(Ptr< Socket > socket)
Definition: multirate.cc:189
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.
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:89
a polymophic address class
Definition: address.h:90
Ptr< YansWifiChannel > Create(void) const
mobility
Definition: third.py:101
phy
Definition: third.py:86
Class for representing data rates.
Definition: data-rate.h:88
Keep track of the current position and velocity of an object.
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
double Run(Parameters params)
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
mac
Definition: third.py:92
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 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:213
This is intended to be the configuration used in this paper: Gavin Holland, Nitin Vaidya and Paramvir...
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
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.
Gnuplot2dDataset m_output
Definition: multirate.cc:123
manage and create wifi channel objects for the yans model.
create MAC layers for a ns3::WifiNetDevice.
void SetStyle(enum Style style)
Definition: gnuplot.cc:342
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())
wifi
Definition: third.py:89
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...
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:270
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
void SetProtocol(uint16_t protocol)
Set the protocol.
void Add(Vector v)
Add a position to the list of positions.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
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:83
devices
Definition: first.py:32
void SetPosition(Ptr< Node > node, Vector position)
Definition: wifi-adhoc.cc:69
a unique identifier for an interface.
Definition: type-id.h:58
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
Definition: multirate.cc:177
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.