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
24using namespace ns3;
25
26NS_LOG_COMPONENT_DEFINE("Wifi-Adhoc");
27
28/**
29 * WiFi adhoc experiment class.
30 *
31 * It handles the creation and run of an experiment.
32 */
34{
35 public:
36 Experiment();
37 /**
38 * Constructor.
39 * @param name The name of the experiment.
40 */
41 Experiment(std::string name);
42
43 /**
44 * Run an experiment.
45 * @param wifi //!< The WifiHelper class.
46 * @param wifiPhy //!< The YansWifiPhyHelper class.
47 * @param wifiMac //!< The WifiMacHelper class.
48 * @param wifiChannel //!< The YansWifiChannelHelper class.
49 * @return a 2D dataset of the experiment data.
50 */
52 const YansWifiPhyHelper& wifiPhy,
53 const WifiMacHelper& wifiMac,
54 const YansWifiChannelHelper& wifiChannel);
55
56 private:
57 /**
58 * Receive a packet.
59 * @param socket The receiving socket.
60 */
61 void ReceivePacket(Ptr<Socket> socket);
62 /**
63 * Set the position of a node.
64 * @param node The node.
65 * @param position The position of the node.
66 */
67 void SetPosition(Ptr<Node> node, Vector position);
68 /**
69 * Get the position of a node.
70 * @param node The node.
71 * @return the position of the node.
72 */
73 Vector GetPosition(Ptr<Node> node);
74 /**
75 * Move a node by 1m on the x axis, stops at 210m.
76 * @param node The node.
77 */
78 void AdvancePosition(Ptr<Node> node);
79 /**
80 * Setup the receiving socket.
81 * @param node The receiving node.
82 * @return the socket.
83 */
85
86 uint32_t m_bytesTotal; //!< The number of received bytes.
87 Gnuplot2dDataset m_output; //!< The output dataset.
88};
89
93
94Experiment::Experiment(std::string name)
95 : m_output(name)
96{
98}
99
100void
101Experiment::SetPosition(Ptr<Node> node, Vector position)
102{
103 Ptr<MobilityModel> mobility = node->GetObject<MobilityModel>();
104 mobility->SetPosition(position);
105}
106
107Vector
109{
110 Ptr<MobilityModel> mobility = node->GetObject<MobilityModel>();
111 return mobility->GetPosition();
112}
113
114void
116{
117 Vector pos = GetPosition(node);
118 double mbs = ((m_bytesTotal * 8.0) / 1000000);
119 m_bytesTotal = 0;
120 m_output.Add(pos.x, mbs);
121 pos.x += 1.0;
122 if (pos.x >= 210.0)
123 {
124 return;
125 }
126 SetPosition(node, pos);
128}
129
130void
132{
133 Ptr<Packet> packet;
134 while ((packet = socket->Recv()))
135 {
136 m_bytesTotal += packet->GetSize();
137 }
138}
139
142{
143 TypeId tid = TypeId::LookupByName("ns3::PacketSocketFactory");
145 sink->Bind();
146 sink->SetRecvCallback(MakeCallback(&Experiment::ReceivePacket, this));
147 return sink;
148}
149
152 const YansWifiPhyHelper& wifiPhy,
153 const WifiMacHelper& wifiMac,
154 const YansWifiChannelHelper& wifiChannel)
155{
156 m_bytesTotal = 0;
157
159 c.Create(2);
160
161 PacketSocketHelper packetSocket;
162 packetSocket.Install(c);
163
164 YansWifiPhyHelper phy = wifiPhy;
165 phy.SetChannel(wifiChannel.Create());
166
167 NetDeviceContainer devices = wifi.Install(phy, wifiMac, c);
168
169 MobilityHelper mobility;
171 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
172 positionAlloc->Add(Vector(5.0, 0.0, 0.0));
173 mobility.SetPositionAllocator(positionAlloc);
174 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
175
176 mobility.Install(c);
177
178 PacketSocketAddress socket;
179 socket.SetSingleDevice(devices.Get(0)->GetIfIndex());
180 socket.SetPhysicalAddress(devices.Get(1)->GetAddress());
181 socket.SetProtocol(1);
182
183 OnOffHelper onoff("ns3::PacketSocketFactory", Address(socket));
184 onoff.SetConstantRate(DataRate(60000000));
185 onoff.SetAttribute("PacketSize", UintegerValue(2000));
186
187 ApplicationContainer apps = onoff.Install(c.Get(0));
188 apps.Start(Seconds(0.5));
189 apps.Stop(Seconds(250));
190
192 Ptr<Socket> recvSink = SetupPacketReceive(c.Get(1));
193
195
197
198 return m_output;
199}
200
201int
202main(int argc, char* argv[])
203{
204 CommandLine cmd(__FILE__);
205 cmd.Parse(argc, argv);
206
207 Gnuplot gnuplot = Gnuplot("reference-rates.png");
208
210 WifiHelper wifi;
211 wifi.SetStandard(WIFI_STANDARD_80211a);
212 WifiMacHelper wifiMac;
213 YansWifiPhyHelper wifiPhy;
215 Gnuplot2dDataset dataset;
216
217 wifiMac.SetType("ns3::AdhocWifiMac");
218
219 NS_LOG_DEBUG("54");
220 experiment = Experiment("54mb");
221 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
222 "DataMode",
223 StringValue("OfdmRate54Mbps"));
224 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
225 gnuplot.AddDataset(dataset);
226
227 NS_LOG_DEBUG("48");
228 experiment = Experiment("48mb");
229 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
230 "DataMode",
231 StringValue("OfdmRate48Mbps"));
232 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
233 gnuplot.AddDataset(dataset);
234
235 NS_LOG_DEBUG("36");
236 experiment = Experiment("36mb");
237 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
238 "DataMode",
239 StringValue("OfdmRate36Mbps"));
240 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
241 gnuplot.AddDataset(dataset);
242
243 NS_LOG_DEBUG("24");
244 experiment = Experiment("24mb");
245 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
246 "DataMode",
247 StringValue("OfdmRate24Mbps"));
248 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
249 gnuplot.AddDataset(dataset);
250
251 NS_LOG_DEBUG("18");
252 experiment = Experiment("18mb");
253 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
254 "DataMode",
255 StringValue("OfdmRate18Mbps"));
256 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
257 gnuplot.AddDataset(dataset);
258
259 NS_LOG_DEBUG("12");
260 experiment = Experiment("12mb");
261 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
262 "DataMode",
263 StringValue("OfdmRate12Mbps"));
264 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
265 gnuplot.AddDataset(dataset);
266
267 NS_LOG_DEBUG("9");
268 experiment = Experiment("9mb");
269 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
270 "DataMode",
271 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",
279 StringValue("OfdmRate6Mbps"));
280 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
281 gnuplot.AddDataset(dataset);
282
283 gnuplot.GenerateOutput(std::cout);
284
285 gnuplot = Gnuplot("rate-control.png");
286
287 NS_LOG_DEBUG("arf");
288 experiment = Experiment("arf");
289 wifi.SetRemoteStationManager("ns3::ArfWifiManager");
290 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
291 gnuplot.AddDataset(dataset);
292
293 NS_LOG_DEBUG("aarf");
294 experiment = Experiment("aarf");
295 wifi.SetRemoteStationManager("ns3::AarfWifiManager");
296 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
297 gnuplot.AddDataset(dataset);
298
299 NS_LOG_DEBUG("aarf-cd");
300 experiment = Experiment("aarf-cd");
301 wifi.SetRemoteStationManager("ns3::AarfcdWifiManager");
302 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
303 gnuplot.AddDataset(dataset);
304
305 NS_LOG_DEBUG("cara");
306 experiment = Experiment("cara");
307 wifi.SetRemoteStationManager("ns3::CaraWifiManager");
308 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
309 gnuplot.AddDataset(dataset);
310
311 NS_LOG_DEBUG("rraa");
312 experiment = Experiment("rraa");
313 wifi.SetRemoteStationManager("ns3::RraaWifiManager");
314 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
315 gnuplot.AddDataset(dataset);
316
317 NS_LOG_DEBUG("ideal");
318 experiment = Experiment("ideal");
319 wifi.SetRemoteStationManager("ns3::IdealWifiManager");
320 dataset = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
321 gnuplot.AddDataset(dataset);
322
323 gnuplot.GenerateOutput(std::cout);
324
325 return 0;
326}
WiFi adhoc experiment class.
Definition wifi-adhoc.cc:34
Gnuplot2dDataset Run(const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy, const WifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel)
Run an experiment.
uint32_t m_bytesTotal
The number of received bytes.
Definition wifi-adhoc.cc:86
void ReceivePacket(Ptr< Socket > socket)
Receive a packet.
void SetPosition(Ptr< Node > node, Vector position)
Set the position of a node.
void AdvancePosition(Ptr< Node > node)
Move a node by 1m on the x axis, stops at 210m.
Gnuplot2dDataset m_output
The output dataset.
Definition wifi-adhoc.cc:87
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
Setup the receiving socket.
Vector GetPosition(Ptr< Node > node)
Get the position of a node.
a polymophic address class
Definition address.h:90
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
void SetStyle(Style style)
Definition gnuplot.cc:348
void Add(double x, double y)
Definition gnuplot.cc:366
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.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
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:48
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:872
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)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1344
@ WIFI_STANDARD_80211a
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:684
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44