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 * 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
35using namespace ns3;
36
37NS_LOG_COMPONENT_DEFINE("Wifi-Adhoc");
38
45{
46 public:
47 Experiment();
52 Experiment(std::string name);
53
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
99};
100
102{
103}
104
105Experiment::Experiment(std::string name)
106 : m_output(name)
107{
109}
110
111void
112Experiment::SetPosition(Ptr<Node> node, Vector position)
113{
114 Ptr<MobilityModel> mobility = node->GetObject<MobilityModel>();
115 mobility->SetPosition(position);
116}
117
118Vector
120{
121 Ptr<MobilityModel> mobility = node->GetObject<MobilityModel>();
122 return mobility->GetPosition();
123}
124
125void
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);
139}
140
141void
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");
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
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
180 MobilityHelper mobility;
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
203 Ptr<Socket> recvSink = SetupPacketReceive(c.Get(1));
204
206
208
209 return m_output;
210}
211
212int
213main(int argc, char* argv[])
214{
215 CommandLine cmd(__FILE__);
216 cmd.Parse(argc, argv);
217
218 Gnuplot gnuplot = Gnuplot("reference-rates.png");
219
221 WifiHelper wifi;
222 wifi.SetStandard(WIFI_STANDARD_80211a);
223 WifiMacHelper wifiMac;
224 YansWifiPhyHelper wifiPhy;
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}
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 for representing data rates.
Definition: data-rate.h:89
Class to represent a 2D points plot.
Definition: gnuplot.h:116
void SetStyle(Style style)
Definition: gnuplot.cc:359
void Add(double x, double y)
Definition: gnuplot.cc:377
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:370
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:796
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:802
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.
Definition: on-off-helper.h:37
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:77
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
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:72
Hold variables of type string.
Definition: string.h:56
a unique identifier for an interface.
Definition: type-id.h:59
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:836
Hold an unsigned integer type.
Definition: uinteger.h:45
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.
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:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
@ 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:704
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55