A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-adhoc.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2005,2006,2007 INRIA
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
7 */
8
9#include "ns3/command-line.h"
10#include "ns3/config.h"
11#include "ns3/gnuplot.h"
12#include "ns3/ipv4-address-helper.h"
13#include "ns3/log.h"
14#include "ns3/mobility-helper.h"
15#include "ns3/mobility-model.h"
16#include "ns3/on-off-helper.h"
17#include "ns3/packet-socket-address.h"
18#include "ns3/packet-socket-helper.h"
19#include "ns3/string.h"
20#include "ns3/uinteger.h"
21#include "ns3/yans-wifi-channel.h"
22#include "ns3/yans-wifi-helper.h"
23
24/**
25 * @file
26 * @ingroup wifi
27 * Adhoc example
28 */
29
30using namespace ns3;
31
32NS_LOG_COMPONENT_DEFINE("Wifi-Adhoc");
33
34/** Unnamed namespace, to disambiguate class Experiment. */
35namespace
36{
37
38/**
39 * WiFi adhoc experiment class.
40 *
41 * It handles the creation and run of an experiment.
42 */
44{
45 public:
46 Experiment();
47 /**
48 * Constructor.
49 * @param name The name of the experiment.
50 */
51 Experiment(std::string name);
52
53 /**
54 * Run an experiment.
55 * @param wifi //!< The WifiHelper class.
56 * @param wifiPhy //!< The YansWifiPhyHelper class.
57 * @param wifiMac //!< The WifiMacHelper class.
58 * @param wifiChannel //!< The YansWifiChannelHelper class.
59 * @return a 2D dataset of the experiment data.
60 */
62 const YansWifiPhyHelper& wifiPhy,
63 const WifiMacHelper& wifiMac,
64 const YansWifiChannelHelper& wifiChannel);
65
66 private:
67 /**
68 * Receive a packet.
69 * @param socket The receiving socket.
70 */
71 void ReceivePacket(Ptr<Socket> socket);
72 /**
73 * Set the position of a node.
74 * @param node The node.
75 * @param position The position of the node.
76 */
77 void SetPosition(Ptr<Node> node, Vector position);
78 /**
79 * Get the position of a node.
80 * @param node The node.
81 * @return the position of the node.
82 */
83 Vector GetPosition(Ptr<Node> node);
84 /**
85 * Move a node by 1m on the x axis, stops at 210m.
86 * @param node The node.
87 */
88 void AdvancePosition(Ptr<Node> node);
89 /**
90 * Setup the receiving socket.
91 * @param node The receiving node.
92 * @return the socket.
93 */
95
96 uint32_t m_bytesTotal; //!< The number of received bytes.
97 Gnuplot2dDataset m_output; //!< The output dataset.
98};
99
103
104Experiment::Experiment(std::string name)
105 : m_output(name)
106{
108}
109
110void
111Experiment::SetPosition(Ptr<Node> node, Vector position)
112{
113 Ptr<MobilityModel> mobility = node->GetObject<MobilityModel>();
114 mobility->SetPosition(position);
115}
116
117Vector
119{
120 Ptr<MobilityModel> mobility = node->GetObject<MobilityModel>();
121 return mobility->GetPosition();
122}
123
124void
126{
127 Vector pos = GetPosition(node);
128 double mbs = ((m_bytesTotal * 8.0) / 1000000);
129 m_bytesTotal = 0;
130 m_output.Add(pos.x, mbs);
131 pos.x += 1.0;
132 if (pos.x >= 210.0)
133 {
134 return;
135 }
136 SetPosition(node, pos);
138}
139
140void
142{
143 Ptr<Packet> packet;
144 while ((packet = socket->Recv()))
145 {
146 m_bytesTotal += packet->GetSize();
147 }
148}
149
152{
153 TypeId tid = TypeId::LookupByName("ns3::PacketSocketFactory");
155 sink->Bind();
156 sink->SetRecvCallback(MakeCallback(&Experiment::ReceivePacket, this));
157 return sink;
158}
159
162 const YansWifiPhyHelper& wifiPhy,
163 const WifiMacHelper& wifiMac,
164 const YansWifiChannelHelper& wifiChannel)
165{
166 m_bytesTotal = 0;
167
169 c.Create(2);
170
171 PacketSocketHelper packetSocket;
172 packetSocket.Install(c);
173
174 YansWifiPhyHelper phy = wifiPhy;
175 phy.SetChannel(wifiChannel.Create());
176
177 NetDeviceContainer devices = wifi.Install(phy, wifiMac, c);
178
179 MobilityHelper mobility;
181 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
182 positionAlloc->Add(Vector(5.0, 0.0, 0.0));
183 mobility.SetPositionAllocator(positionAlloc);
184 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
185
186 mobility.Install(c);
187
188 PacketSocketAddress socket;
189 socket.SetSingleDevice(devices.Get(0)->GetIfIndex());
190 socket.SetPhysicalAddress(devices.Get(1)->GetAddress());
191 socket.SetProtocol(1);
192
193 OnOffHelper onoff("ns3::PacketSocketFactory", Address(socket));
194 onoff.SetConstantRate(DataRate(60000000));
195 onoff.SetAttribute("PacketSize", UintegerValue(2000));
196
197 ApplicationContainer apps = onoff.Install(c.Get(0));
198 apps.Start(Seconds(0.5));
199 apps.Stop(Seconds(250));
200
202 Ptr<Socket> recvSink = SetupPacketReceive(c.Get(1));
203
205
207
208 return m_output;
209}
210
211} // unnamed namespace
212
213int
214main(int argc, char* argv[])
215{
216 CommandLine cmd(__FILE__);
217 cmd.Parse(argc, argv);
218
219 Gnuplot gnuplot = Gnuplot("reference-rates.png");
220
221 Experiment experiment;
222 WifiHelper wifi;
223 wifi.SetStandard(WIFI_STANDARD_80211a);
224 WifiMacHelper wifiMac;
225 YansWifiPhyHelper wifiPhy;
227 Gnuplot2dDataset dataset;
228
229 wifiMac.SetType("ns3::AdhocWifiMac");
230
231 NS_LOG_DEBUG("54");
232 experiment = Experiment("54mb");
233 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
234 "DataMode",
235 StringValue("OfdmRate54Mbps"));
236 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
237 gnuplot.AddDataset(dataset);
238
239 NS_LOG_DEBUG("48");
240 experiment = Experiment("48mb");
241 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
242 "DataMode",
243 StringValue("OfdmRate48Mbps"));
244 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
245 gnuplot.AddDataset(dataset);
246
247 NS_LOG_DEBUG("36");
248 experiment = Experiment("36mb");
249 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
250 "DataMode",
251 StringValue("OfdmRate36Mbps"));
252 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
253 gnuplot.AddDataset(dataset);
254
255 NS_LOG_DEBUG("24");
256 experiment = Experiment("24mb");
257 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
258 "DataMode",
259 StringValue("OfdmRate24Mbps"));
260 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
261 gnuplot.AddDataset(dataset);
262
263 NS_LOG_DEBUG("18");
264 experiment = Experiment("18mb");
265 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
266 "DataMode",
267 StringValue("OfdmRate18Mbps"));
268 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
269 gnuplot.AddDataset(dataset);
270
271 NS_LOG_DEBUG("12");
272 experiment = Experiment("12mb");
273 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
274 "DataMode",
275 StringValue("OfdmRate12Mbps"));
276 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
277 gnuplot.AddDataset(dataset);
278
279 NS_LOG_DEBUG("9");
280 experiment = Experiment("9mb");
281 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
282 "DataMode",
283 StringValue("OfdmRate9Mbps"));
284 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
285 gnuplot.AddDataset(dataset);
286
287 NS_LOG_DEBUG("6");
288 experiment = Experiment("6mb");
289 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
290 "DataMode",
291 StringValue("OfdmRate6Mbps"));
292 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
293 gnuplot.AddDataset(dataset);
294
295 gnuplot.GenerateOutput(std::cout);
296
297 gnuplot = Gnuplot("rate-control.png");
298
299 NS_LOG_DEBUG("arf");
300 experiment = Experiment("arf");
301 wifi.SetRemoteStationManager("ns3::ArfWifiManager");
302 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
303 gnuplot.AddDataset(dataset);
304
305 NS_LOG_DEBUG("aarf");
306 experiment = Experiment("aarf");
307 wifi.SetRemoteStationManager("ns3::AarfWifiManager");
308 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
309 gnuplot.AddDataset(dataset);
310
311 NS_LOG_DEBUG("aarf-cd");
312 experiment = Experiment("aarf-cd");
313 wifi.SetRemoteStationManager("ns3::AarfcdWifiManager");
314 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
315 gnuplot.AddDataset(dataset);
316
317 NS_LOG_DEBUG("cara");
318 experiment = Experiment("cara");
319 wifi.SetRemoteStationManager("ns3::CaraWifiManager");
320 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
321 gnuplot.AddDataset(dataset);
322
323 NS_LOG_DEBUG("rraa");
324 experiment = Experiment("rraa");
325 wifi.SetRemoteStationManager("ns3::RraaWifiManager");
326 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
327 gnuplot.AddDataset(dataset);
328
329 NS_LOG_DEBUG("ideal");
330 experiment = Experiment("ideal");
331 wifi.SetRemoteStationManager("ns3::IdealWifiManager");
332 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
333 gnuplot.AddDataset(dataset);
334
335 gnuplot.GenerateOutput(std::cout);
336
337 return 0;
338}
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
Create a socket and prepare it for packet reception.
void SetPosition(Ptr< Node > node, Vector position)
Set the position of a node.
uint32_t m_bytesTotal
The number of received bytes.
Definition wifi-adhoc.cc:96
Gnuplot2dDataset m_output
The output dataset.
Definition wifi-adhoc.cc:97
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
Setup the receiving socket.
Vector GetPosition(Ptr< Node > node)
Get the position of a node.
void AdvancePosition(Ptr< Node > node)
Move a node by 1m on the x axis, stops at 210m.
void ReceivePacket(Ptr< Socket > socket)
Receive a packet.
Gnuplot2dDataset Run(const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy, const WifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel)
Run an experiment.
a polymophic address class
Definition address.h:114
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.
Class for representing data rates.
Definition data-rate.h:78
Class to represent a 2D points plot.
Definition gnuplot.h:105
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition gnuplot.h:359
void AddDataset(const GnuplotDataset &dataset)
Definition gnuplot.cc:785
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition gnuplot.cc:791
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.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
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.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:580
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static void Run()
Run the simulation.
Definition simulator.cc:161
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition socket.cc:61
Hold variables of type string.
Definition string.h:45
a unique identifier for an interface.
Definition type-id.h:49
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:870
Hold an unsigned integer type.
Definition uinteger.h:34
helps to create WifiNetDevice objects
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
manage and create wifi channel objects for the YANS model.
static YansWifiChannelHelper Default()
Create a channel helper in a default working state.
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)
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:690
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:194
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:260
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1381
@ WIFI_STANDARD_80211a
Every class exported by the ns3 library is enclosed in the ns3 namespace.
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:142
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44