A Discrete-Event Network Simulator
API
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{
108 m_output.SetStyle(Gnuplot2dDataset::LINES);
109}
110
111void
113{
115 mobility->SetPosition(position);
116}
117
118Vector
120{
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);
138 Simulator::Schedule(Seconds(1.0), &Experiment::AdvancePosition, this, node);
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");
155 Ptr<Socket> sink = Socket::CreateSocket(node, tid);
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 WifiMacHelper mac = wifiMac;
179 NetDeviceContainer devices = wifi.Install(phy, mac, c);
180
182 Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator>();
183 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
184 positionAlloc->Add(Vector(5.0, 0.0, 0.0));
185 mobility.SetPositionAllocator(positionAlloc);
186 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
187
188 mobility.Install(c);
189
190 PacketSocketAddress socket;
191 socket.SetSingleDevice(devices.Get(0)->GetIfIndex());
192 socket.SetPhysicalAddress(devices.Get(1)->GetAddress());
193 socket.SetProtocol(1);
194
195 OnOffHelper onoff("ns3::PacketSocketFactory", Address(socket));
196 onoff.SetConstantRate(DataRate(60000000));
197 onoff.SetAttribute("PacketSize", UintegerValue(2000));
198
199 ApplicationContainer apps = onoff.Install(c.Get(0));
200 apps.Start(Seconds(0.5));
201 apps.Stop(Seconds(250.0));
202
203 Simulator::Schedule(Seconds(1.5), &Experiment::AdvancePosition, this, c.Get(1));
204 Ptr<Socket> recvSink = SetupPacketReceive(c.Get(1));
205
206 Simulator::Run();
207
208 Simulator::Destroy();
209
210 return m_output;
211}
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
223 wifi.SetStandard(WIFI_STANDARD_80211a);
224 WifiMacHelper wifiMac;
225 YansWifiPhyHelper wifiPhy;
226 YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default();
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}
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:100
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 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.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:44
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() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:862
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:56
a unique identifier for an interface.
Definition: type-id.h:59
Hold an unsigned integer type.
Definition: uinteger.h:45
Vector3D Vector
Vector alias typedef for compatibility with mobility models.
Definition: vector.h:324
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.
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
void(* DataRate)(DataRate oldValue, DataRate newValue)
TracedValue callback signature for DataRate.
Definition: data-rate.h:327
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
@ WIFI_STANDARD_80211a
devices
Definition: first.py:35
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:707
cmd
Definition: second.py:33
mac
Definition: third.py:85
wifi
Definition: third.py:88
mobility
Definition: third.py:96
phy
Definition: third.py:82
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55