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
41{
42public:
43 Experiment ();
44 Experiment (std::string name);
46 const WifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel);
47private:
48 void ReceivePacket (Ptr<Socket> socket);
49 void SetPosition (Ptr<Node> node, Vector position);
50 Vector GetPosition (Ptr<Node> node);
51 void AdvancePosition (Ptr<Node> node);
53
56};
57
59{
60}
61
62Experiment::Experiment (std::string name)
63 : m_output (name)
64{
65 m_output.SetStyle (Gnuplot2dDataset::LINES);
66}
67
68void
70{
72 mobility->SetPosition (position);
73}
74
77{
79 return mobility->GetPosition ();
80}
81
82void
84{
85 Vector pos = GetPosition (node);
86 double mbs = ((m_bytesTotal * 8.0) / 1000000);
87 m_bytesTotal = 0;
88 m_output.Add (pos.x, mbs);
89 pos.x += 1.0;
90 if (pos.x >= 210.0)
91 {
92 return;
93 }
94 SetPosition (node, pos);
95 Simulator::Schedule (Seconds (1.0), &Experiment::AdvancePosition, this, node);
96}
97
98void
100{
101 Ptr<Packet> packet;
102 while ((packet = socket->Recv ()))
103 {
104 m_bytesTotal += packet->GetSize ();
105 }
106}
107
110{
111 TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory");
112 Ptr<Socket> sink = Socket::CreateSocket (node, tid);
113 sink->Bind ();
114 sink->SetRecvCallback (MakeCallback (&Experiment::ReceivePacket, this));
115 return sink;
116}
117
120 const WifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel)
121{
122 m_bytesTotal = 0;
123
125 c.Create (2);
126
127 PacketSocketHelper packetSocket;
128 packetSocket.Install (c);
129
130 YansWifiPhyHelper phy = wifiPhy;
131 phy.SetChannel (wifiChannel.Create ());
132
133 WifiMacHelper mac = wifiMac;
134 NetDeviceContainer devices = wifi.Install (phy, mac, c);
135
137 Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
138 positionAlloc->Add (Vector (0.0, 0.0, 0.0));
139 positionAlloc->Add (Vector (5.0, 0.0, 0.0));
140 mobility.SetPositionAllocator (positionAlloc);
141 mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
142
143 mobility.Install (c);
144
145 PacketSocketAddress socket;
146 socket.SetSingleDevice (devices.Get (0)->GetIfIndex ());
147 socket.SetPhysicalAddress (devices.Get (1)->GetAddress ());
148 socket.SetProtocol (1);
149
150 OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
151 onoff.SetConstantRate (DataRate (60000000));
152 onoff.SetAttribute ("PacketSize", UintegerValue (2000));
153
154 ApplicationContainer apps = onoff.Install (c.Get (0));
155 apps.Start (Seconds (0.5));
156 apps.Stop (Seconds (250.0));
157
158 Simulator::Schedule (Seconds (1.5), &Experiment::AdvancePosition, this, c.Get (1));
159 Ptr<Socket> recvSink = SetupPacketReceive (c.Get (1));
160
161 Simulator::Run ();
162
163 Simulator::Destroy ();
164
165 return m_output;
166}
167
168int main (int argc, char *argv[])
169{
170 CommandLine cmd (__FILE__);
171 cmd.Parse (argc, argv);
172
173 Gnuplot gnuplot = Gnuplot ("reference-rates.png");
174
177 wifi.SetStandard (WIFI_STANDARD_80211a);
178 WifiMacHelper wifiMac;
179 YansWifiPhyHelper wifiPhy;
180 YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
181 Gnuplot2dDataset dataset;
182
183 wifiMac.SetType ("ns3::AdhocWifiMac");
184
185 NS_LOG_DEBUG ("54");
186 experiment = Experiment ("54mb");
187 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
188 "DataMode", StringValue ("OfdmRate54Mbps"));
189 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
190 gnuplot.AddDataset (dataset);
191
192 NS_LOG_DEBUG ("48");
193 experiment = Experiment ("48mb");
194 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
195 "DataMode", StringValue ("OfdmRate48Mbps"));
196 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
197 gnuplot.AddDataset (dataset);
198
199 NS_LOG_DEBUG ("36");
200 experiment = Experiment ("36mb");
201 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
202 "DataMode", StringValue ("OfdmRate36Mbps"));
203 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
204 gnuplot.AddDataset (dataset);
205
206 NS_LOG_DEBUG ("24");
207 experiment = Experiment ("24mb");
208 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
209 "DataMode", StringValue ("OfdmRate24Mbps"));
210 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
211 gnuplot.AddDataset (dataset);
212
213 NS_LOG_DEBUG ("18");
214 experiment = Experiment ("18mb");
215 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
216 "DataMode", StringValue ("OfdmRate18Mbps"));
217 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
218 gnuplot.AddDataset (dataset);
219
220 NS_LOG_DEBUG ("12");
221 experiment = Experiment ("12mb");
222 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
223 "DataMode", StringValue ("OfdmRate12Mbps"));
224 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
225 gnuplot.AddDataset (dataset);
226
227 NS_LOG_DEBUG ("9");
228 experiment = Experiment ("9mb");
229 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
230 "DataMode", StringValue ("OfdmRate9Mbps"));
231 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
232 gnuplot.AddDataset (dataset);
233
234 NS_LOG_DEBUG ("6");
235 experiment = Experiment ("6mb");
236 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
237 "DataMode", StringValue ("OfdmRate6Mbps"));
238 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
239 gnuplot.AddDataset (dataset);
240
241 gnuplot.GenerateOutput (std::cout);
242
243 gnuplot = Gnuplot ("rate-control.png");
244
245 NS_LOG_DEBUG ("arf");
246 experiment = Experiment ("arf");
247 wifi.SetRemoteStationManager ("ns3::ArfWifiManager");
248 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
249 gnuplot.AddDataset (dataset);
250
251 NS_LOG_DEBUG ("aarf");
252 experiment = Experiment ("aarf");
253 wifi.SetRemoteStationManager ("ns3::AarfWifiManager");
254 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
255 gnuplot.AddDataset (dataset);
256
257 NS_LOG_DEBUG ("aarf-cd");
258 experiment = Experiment ("aarf-cd");
259 wifi.SetRemoteStationManager ("ns3::AarfcdWifiManager");
260 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
261 gnuplot.AddDataset (dataset);
262
263 NS_LOG_DEBUG ("cara");
264 experiment = Experiment ("cara");
265 wifi.SetRemoteStationManager ("ns3::CaraWifiManager");
266 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
267 gnuplot.AddDataset (dataset);
268
269 NS_LOG_DEBUG ("rraa");
270 experiment = Experiment ("rraa");
271 wifi.SetRemoteStationManager ("ns3::RraaWifiManager");
272 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
273 gnuplot.AddDataset (dataset);
274
275 NS_LOG_DEBUG ("ideal");
276 experiment = Experiment ("ideal");
277 wifi.SetRemoteStationManager ("ns3::IdealWifiManager");
278 dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
279 gnuplot.AddDataset (dataset);
280
281 gnuplot.GenerateOutput (std::cout);
282
283 return 0;
284}
Helper class for UAN CW MAC example.
Definition: wifi-adhoc.cc:41
Gnuplot2dDataset Run(const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy, const WifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel)
Definition: wifi-adhoc.cc:119
uint32_t m_bytesTotal
Total bytes received.
Definition: wifi-adhoc.cc:54
void ReceivePacket(Ptr< Socket > socket)
Definition: wifi-adhoc.cc:99
void SetPosition(Ptr< Node > node, Vector position)
Definition: wifi-adhoc.cc:69
void AdvancePosition(Ptr< Node > node)
Definition: wifi-adhoc.cc:83
Gnuplot2dDataset m_output
Definition: wifi-adhoc.cc:55
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
Definition: wifi-adhoc.cc:109
Vector GetPosition(Ptr< Node > node)
Definition: wifi-adhoc.cc:76
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:852
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:323
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:99
wifi
Definition: third.py:96
mobility
Definition: third.py:108
phy
Definition: third.py:93
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56