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
36using namespace ns3;
37
38NS_LOG_COMPONENT_DEFINE ("Wifi-Adhoc");
39
46{
47public:
48 Experiment ();
53 Experiment (std::string name);
54
64 const WifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel);
65private:
70 void ReceivePacket (Ptr<Socket> socket);
76 void SetPosition (Ptr<Node> node, Vector position);
82 Vector GetPosition (Ptr<Node> node);
87 void AdvancePosition (Ptr<Node> node);
94
97};
98
100{
101}
102
103Experiment::Experiment (std::string name)
104 : m_output (name)
105{
106 m_output.SetStyle (Gnuplot2dDataset::LINES);
107}
108
109void
111{
113 mobility->SetPosition (position);
114}
115
116Vector
118{
120 return mobility->GetPosition ();
121}
122
123void
125{
126 Vector pos = GetPosition (node);
127 double mbs = ((m_bytesTotal * 8.0) / 1000000);
128 m_bytesTotal = 0;
129 m_output.Add (pos.x, mbs);
130 pos.x += 1.0;
131 if (pos.x >= 210.0)
132 {
133 return;
134 }
135 SetPosition (node, pos);
136 Simulator::Schedule (Seconds (1.0), &Experiment::AdvancePosition, this, node);
137}
138
139void
141{
142 Ptr<Packet> packet;
143 while ((packet = socket->Recv ()))
144 {
145 m_bytesTotal += packet->GetSize ();
146 }
147}
148
151{
152 TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory");
153 Ptr<Socket> sink = Socket::CreateSocket (node, tid);
154 sink->Bind ();
155 sink->SetRecvCallback (MakeCallback (&Experiment::ReceivePacket, this));
156 return sink;
157}
158
161 const WifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel)
162{
163 m_bytesTotal = 0;
164
166 c.Create (2);
167
168 PacketSocketHelper packetSocket;
169 packetSocket.Install (c);
170
171 YansWifiPhyHelper phy = wifiPhy;
172 phy.SetChannel (wifiChannel.Create ());
173
174 WifiMacHelper mac = wifiMac;
175 NetDeviceContainer devices = wifi.Install (phy, mac, c);
176
178 Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
179 positionAlloc->Add (Vector (0.0, 0.0, 0.0));
180 positionAlloc->Add (Vector (5.0, 0.0, 0.0));
181 mobility.SetPositionAllocator (positionAlloc);
182 mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
183
184 mobility.Install (c);
185
186 PacketSocketAddress socket;
187 socket.SetSingleDevice (devices.Get (0)->GetIfIndex ());
188 socket.SetPhysicalAddress (devices.Get (1)->GetAddress ());
189 socket.SetProtocol (1);
190
191 OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
192 onoff.SetConstantRate (DataRate (60000000));
193 onoff.SetAttribute ("PacketSize", UintegerValue (2000));
194
195 ApplicationContainer apps = onoff.Install (c.Get (0));
196 apps.Start (Seconds (0.5));
197 apps.Stop (Seconds (250.0));
198
199 Simulator::Schedule (Seconds (1.5), &Experiment::AdvancePosition, this, c.Get (1));
200 Ptr<Socket> recvSink = SetupPacketReceive (c.Get (1));
201
202 Simulator::Run ();
203
204 Simulator::Destroy ();
205
206 return m_output;
207}
208
209int main (int argc, char *argv[])
210{
211 CommandLine cmd (__FILE__);
212 cmd.Parse (argc, argv);
213
214 Gnuplot gnuplot = Gnuplot ("reference-rates.png");
215
218 wifi.SetStandard (WIFI_STANDARD_80211a);
219 WifiMacHelper wifiMac;
220 YansWifiPhyHelper wifiPhy;
221 YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
222 Gnuplot2dDataset dataset;
223
224 wifiMac.SetType ("ns3::AdhocWifiMac");
225
226 NS_LOG_DEBUG ("54");
227 experiment = Experiment ("54mb");
228 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
229 "DataMode", StringValue ("OfdmRate54Mbps"));
230 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
231 gnuplot.AddDataset (dataset);
232
233 NS_LOG_DEBUG ("48");
234 experiment = Experiment ("48mb");
235 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
236 "DataMode", StringValue ("OfdmRate48Mbps"));
237 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
238 gnuplot.AddDataset (dataset);
239
240 NS_LOG_DEBUG ("36");
241 experiment = Experiment ("36mb");
242 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
243 "DataMode", StringValue ("OfdmRate36Mbps"));
244 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
245 gnuplot.AddDataset (dataset);
246
247 NS_LOG_DEBUG ("24");
248 experiment = Experiment ("24mb");
249 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
250 "DataMode", StringValue ("OfdmRate24Mbps"));
251 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
252 gnuplot.AddDataset (dataset);
253
254 NS_LOG_DEBUG ("18");
255 experiment = Experiment ("18mb");
256 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
257 "DataMode", StringValue ("OfdmRate18Mbps"));
258 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
259 gnuplot.AddDataset (dataset);
260
261 NS_LOG_DEBUG ("12");
262 experiment = Experiment ("12mb");
263 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
264 "DataMode", StringValue ("OfdmRate12Mbps"));
265 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
266 gnuplot.AddDataset (dataset);
267
268 NS_LOG_DEBUG ("9");
269 experiment = Experiment ("9mb");
270 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
271 "DataMode", StringValue ("OfdmRate9Mbps"));
272 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
273 gnuplot.AddDataset (dataset);
274
275 NS_LOG_DEBUG ("6");
276 experiment = Experiment ("6mb");
277 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
278 "DataMode", StringValue ("OfdmRate6Mbps"));
279 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
280 gnuplot.AddDataset (dataset);
281
282 gnuplot.GenerateOutput (std::cout);
283
284 gnuplot = Gnuplot ("rate-control.png");
285
286 NS_LOG_DEBUG ("arf");
287 experiment = Experiment ("arf");
288 wifi.SetRemoteStationManager ("ns3::ArfWifiManager");
289 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
290 gnuplot.AddDataset (dataset);
291
292 NS_LOG_DEBUG ("aarf");
293 experiment = Experiment ("aarf");
294 wifi.SetRemoteStationManager ("ns3::AarfWifiManager");
295 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
296 gnuplot.AddDataset (dataset);
297
298 NS_LOG_DEBUG ("aarf-cd");
299 experiment = Experiment ("aarf-cd");
300 wifi.SetRemoteStationManager ("ns3::AarfcdWifiManager");
301 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
302 gnuplot.AddDataset (dataset);
303
304 NS_LOG_DEBUG ("cara");
305 experiment = Experiment ("cara");
306 wifi.SetRemoteStationManager ("ns3::CaraWifiManager");
307 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
308 gnuplot.AddDataset (dataset);
309
310 NS_LOG_DEBUG ("rraa");
311 experiment = Experiment ("rraa");
312 wifi.SetRemoteStationManager ("ns3::RraaWifiManager");
313 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
314 gnuplot.AddDataset (dataset);
315
316 NS_LOG_DEBUG ("ideal");
317 experiment = Experiment ("ideal");
318 wifi.SetRemoteStationManager ("ns3::IdealWifiManager");
319 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
320 gnuplot.AddDataset (dataset);
321
322 gnuplot.GenerateOutput (std::cout);
323
324 return 0;
325}
WiFi adhoc experiment class.
Definition: wifi-adhoc.cc:46
Gnuplot2dDataset Run(const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy, const WifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel)
Run an experiment.
Definition: wifi-adhoc.cc:160
uint32_t m_bytesTotal
The number of received bytes.
Definition: wifi-adhoc.cc:95
void ReceivePacket(Ptr< Socket > socket)
Receive a packet.
Definition: wifi-adhoc.cc:140
void SetPosition(Ptr< Node > node, Vector position)
Set the position of a node.
Definition: wifi-adhoc.cc:110
void AdvancePosition(Ptr< Node > node)
Move a node by 1m on the x axis, stops at 210m.
Definition: wifi-adhoc.cc:124
Gnuplot2dDataset m_output
The output dataset.
Definition: wifi-adhoc.cc:96
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
Setup the receiving socket.
Definition: wifi-adhoc.cc:150
Vector GetPosition(Ptr< Node > node)
Get the position of a node.
Definition: wifi-adhoc.cc:117
a polymophic address class
Definition: address.h:91
holds a vector of ns3::Application pointers.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
void Stop(Time stop)
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:229
Class for representing data rates.
Definition: data-rate.h:89
Class to represent a 2D points plot.
Definition: gnuplot.h:118
void SetStyle(enum Style style)
Definition: gnuplot.cc:344
void Add(double x, double y)
Definition: gnuplot.cc:361
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:372
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:758
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:764
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(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:43
void SetConstantRate(DataRate dataRate, uint32_t packetSize=512)
Helper function to set a constant rate source.
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
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:41
a unique identifier for an interface.
Definition: type-id.h:59
Hold an unsigned integer type.
Definition: uinteger.h:44
Vector3D Vector
Vector alias typedef for compatibility with mobility models.
Definition: vector.h:324
helps to create WifiNetDevice objects
Definition: wifi-helper.h:274
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(void) const
Make it easy to create and manage PHY objects for the YANS model.
void experiment(std::string queue_disc_type)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:273
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
@ WIFI_STANDARD_80211a
devices
Definition: first.py:39
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Ts... > MakeCallback(R(T::*memPtr)(Ts...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition: callback.h:1648
cmd
Definition: second.py:35
mac
Definition: third.py:96
wifi
Definition: third.py:99
mobility
Definition: third.py:107
phy
Definition: third.py:93
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56