A Discrete-Event Network Simulator
API
wifi-rate-adaptation-distance.cc
Go to the documentation of this file.
1/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/*
3 * Copyright (c) 2014 Universidad de la República - Uruguay
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: Matías Richart <mrichart@fing.edu.uy>
19 */
20
53#include "ns3/gnuplot.h"
54#include "ns3/command-line.h"
55#include "ns3/config.h"
56#include "ns3/uinteger.h"
57#include "ns3/boolean.h"
58#include "ns3/log.h"
59#include "ns3/yans-wifi-helper.h"
60#include "ns3/ssid.h"
61#include "ns3/mobility-helper.h"
62#include "ns3/internet-stack-helper.h"
63#include "ns3/ipv4-address-helper.h"
64#include "ns3/packet-sink-helper.h"
65#include "ns3/on-off-helper.h"
66#include "ns3/yans-wifi-channel.h"
67#include "ns3/mobility-model.h"
68
69using namespace ns3;
70using namespace std;
71
72NS_LOG_COMPONENT_DEFINE ("RateAdaptationDistance");
73
76{
77public:
84
91 void RxCallback (std::string path, Ptr<const Packet> packet, const Address &from);
97 void SetPosition (Ptr<Node> node, Vector position);
104 void AdvancePosition (Ptr<Node> node, int stepsSize, int stepsTime);
110 Vector GetPosition (Ptr<Node> node);
115
116private:
119};
120
122{
123 m_bytesTotal = 0;
124}
125
126void
127NodeStatistics::RxCallback (std::string path, Ptr<const Packet> packet, const Address &from)
128{
129 m_bytesTotal += packet->GetSize ();
130}
131
132void
134{
136 mobility->SetPosition (position);
137}
138
139Vector
141{
143 return mobility->GetPosition ();
144}
145
146void
147NodeStatistics::AdvancePosition (Ptr<Node> node, int stepsSize, int stepsTime)
148{
149 Vector pos = GetPosition (node);
150 double mbs = ((m_bytesTotal * 8.0) / (1000000 * stepsTime));
151 m_bytesTotal = 0;
152 m_output.Add (pos.x, mbs);
153 pos.x += stepsSize;
154 SetPosition (node, pos);
155 Simulator::Schedule (Seconds (stepsTime), &NodeStatistics::AdvancePosition, this, node, stepsSize, stepsTime);
156}
157
160{
161 return m_output;
162}
163
164
165void RateCallback (std::string path, uint64_t rate, Mac48Address dest)
166{
167 NS_LOG_INFO ((Simulator::Now ()).GetSeconds () << " " << dest << " Rate " << rate / 1000000.0);
168}
169
170int main (int argc, char *argv[])
171{
172 uint32_t rtsThreshold = 65535;
173 std::string staManager = "ns3::MinstrelHtWifiManager";
174 std::string apManager = "ns3::MinstrelHtWifiManager";
175 std::string standard = "802.11n-5GHz";
176 std::string outputFileName = "minstrelHT";
177 uint32_t BeMaxAmpduSize = 65535;
178 bool shortGuardInterval = false;
179 uint32_t chWidth = 20;
180 int ap1_x = 0;
181 int ap1_y = 0;
182 int sta1_x = 5;
183 int sta1_y = 0;
184 int steps = 100;
185 int stepsSize = 1;
186 int stepsTime = 1;
187
188 CommandLine cmd (__FILE__);
189 cmd.AddValue ("staManager", "PRC Manager of the STA", staManager);
190 cmd.AddValue ("apManager", "PRC Manager of the AP", apManager);
191 cmd.AddValue ("standard", "Wifi Phy Standard", standard);
192 cmd.AddValue ("shortGuardInterval", "Enable Short Guard Interval in all stations", shortGuardInterval);
193 cmd.AddValue ("channelWidth", "Channel width of all the stations", chWidth);
194 cmd.AddValue ("rtsThreshold", "RTS threshold", rtsThreshold);
195 cmd.AddValue ("BeMaxAmpduSize", "BE Mac A-MPDU size", BeMaxAmpduSize);
196 cmd.AddValue ("outputFileName", "Output filename", outputFileName);
197 cmd.AddValue ("steps", "How many different distances to try", steps);
198 cmd.AddValue ("stepsTime", "Time on each step", stepsTime);
199 cmd.AddValue ("stepsSize", "Distance between steps", stepsSize);
200 cmd.AddValue ("AP1_x", "Position of AP1 in x coordinate", ap1_x);
201 cmd.AddValue ("AP1_y", "Position of AP1 in y coordinate", ap1_y);
202 cmd.AddValue ("STA1_x", "Position of STA1 in x coordinate", sta1_x);
203 cmd.AddValue ("STA1_y", "Position of STA1 in y coordinate", sta1_y);
204 cmd.Parse (argc, argv);
205
206 int simuTime = steps * stepsTime;
207
208 // Define the APs
209 NodeContainer wifiApNodes;
210 wifiApNodes.Create (1);
211
212 //Define the STAs
214 wifiStaNodes.Create (1);
215
216 YansWifiPhyHelper wifiPhy;
217 YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
218 wifiPhy.SetChannel (wifiChannel.Create ());
219
220 NetDeviceContainer wifiApDevices;
221 NetDeviceContainer wifiStaDevices;
222 NetDeviceContainer wifiDevices;
223
225 if (standard == "802.11a" || standard == "802.11b" || standard == "802.11g")
226 {
227 if (standard == "802.11a")
228 {
229 wifi.SetStandard (WIFI_STANDARD_80211a);
230 }
231 else if (standard == "802.11b")
232 {
233 wifi.SetStandard (WIFI_STANDARD_80211b);
234 }
235 else if (standard == "802.11g")
236 {
237 wifi.SetStandard (WIFI_STANDARD_80211g);
238 }
239 WifiMacHelper wifiMac;
240
241 //Configure the STA node
242 wifi.SetRemoteStationManager (staManager, "RtsCtsThreshold", UintegerValue (rtsThreshold));
243
244 Ssid ssid = Ssid ("AP");
245 wifiMac.SetType ("ns3::StaWifiMac",
246 "Ssid", SsidValue (ssid));
247 wifiStaDevices.Add (wifi.Install (wifiPhy, wifiMac, wifiStaNodes.Get (0)));
248
249 //Configure the AP node
250 wifi.SetRemoteStationManager (apManager, "RtsCtsThreshold", UintegerValue (rtsThreshold));
251
252 ssid = Ssid ("AP");
253 wifiMac.SetType ("ns3::ApWifiMac",
254 "Ssid", SsidValue (ssid));
255 wifiApDevices.Add (wifi.Install (wifiPhy, wifiMac, wifiApNodes.Get (0)));
256 }
257 else if (standard == "802.11n-2.4GHz" || standard == "802.11n-5GHz")
258 {
259 if (standard == "802.11n-2.4GHz")
260 {
261 wifi.SetStandard (WIFI_STANDARD_80211n);
262 }
263 else if (standard == "802.11n-5GHz")
264 {
265 wifi.SetStandard (WIFI_STANDARD_80211n);
266 }
267
268 WifiMacHelper wifiMac;
269
270 //Configure the STA node
271 wifi.SetRemoteStationManager (staManager, "RtsCtsThreshold", UintegerValue (rtsThreshold));
272
273 Ssid ssid = Ssid ("AP");
274 wifiMac.SetType ("ns3::StaWifiMac",
275 "Ssid", SsidValue (ssid));
276 wifiStaDevices.Add (wifi.Install (wifiPhy, wifiMac, wifiStaNodes.Get (0)));
277
278 //Configure the AP node
279 wifi.SetRemoteStationManager (apManager, "RtsCtsThreshold", UintegerValue (rtsThreshold));
280
281 ssid = Ssid ("AP");
282 wifiMac.SetType ("ns3::ApWifiMac",
283 "Ssid", SsidValue (ssid));
284 wifiApDevices.Add (wifi.Install (wifiPhy, wifiMac, wifiApNodes.Get (0)));
285
286 Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Mac/BE_MaxAmpduSize", UintegerValue (BeMaxAmpduSize));
287 }
288 else if (standard == "802.11ac")
289 {
290 wifi.SetStandard (WIFI_STANDARD_80211ac);
291 WifiMacHelper wifiMac;
292
293 //Configure the STA node
294 wifi.SetRemoteStationManager (staManager, "RtsCtsThreshold", UintegerValue (rtsThreshold));
295
296 Ssid ssid = Ssid ("AP");
297 wifiMac.SetType ("ns3::StaWifiMac",
298 "Ssid", SsidValue (ssid));
299 wifiStaDevices.Add (wifi.Install (wifiPhy, wifiMac, wifiStaNodes.Get (0)));
300
301 //Configure the AP node
302 wifi.SetRemoteStationManager (apManager, "RtsCtsThreshold", UintegerValue (rtsThreshold));
303
304 ssid = Ssid ("AP");
305 wifiMac.SetType ("ns3::ApWifiMac",
306 "Ssid", SsidValue (ssid));
307 wifiApDevices.Add (wifi.Install (wifiPhy, wifiMac, wifiApNodes.Get (0)));
308
309 Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Mac/BE_MaxAmpduSize", UintegerValue (BeMaxAmpduSize));
310 }
311
312 wifiDevices.Add (wifiStaDevices);
313 wifiDevices.Add (wifiApDevices);
314
315 // Set channel width
316 Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/ChannelWidth", UintegerValue (chWidth));
317
318 // Set guard interval
319 Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/HtConfiguration/ShortGuardIntervalSupported", BooleanValue (shortGuardInterval));
320
321 // Configure the mobility.
323 Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
324 //Initial position of AP and STA
325 positionAlloc->Add (Vector (ap1_x, ap1_y, 0.0));
326 positionAlloc->Add (Vector (sta1_x, sta1_y, 0.0));
327 mobility.SetPositionAllocator (positionAlloc);
328 mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
329 mobility.Install (wifiApNodes.Get (0));
330 mobility.Install (wifiStaNodes.Get (0));
331
332 //Statistics counter
333 NodeStatistics atpCounter = NodeStatistics (wifiApDevices, wifiStaDevices);
334
335 //Move the STA by stepsSize meters every stepsTime seconds
336 Simulator::Schedule (Seconds (0.5 + stepsTime), &NodeStatistics::AdvancePosition, &atpCounter, wifiStaNodes.Get (0), stepsSize, stepsTime);
337
338 //Configure the IP stack
340 stack.Install (wifiApNodes);
341 stack.Install (wifiStaNodes);
343 address.SetBase ("10.1.1.0", "255.255.255.0");
344 Ipv4InterfaceContainer i = address.Assign (wifiDevices);
345 Ipv4Address sinkAddress = i.GetAddress (0);
346 uint16_t port = 9;
347
348 //Configure the CBR generator
349 PacketSinkHelper sink ("ns3::UdpSocketFactory", InetSocketAddress (sinkAddress, port));
350 ApplicationContainer apps_sink = sink.Install (wifiStaNodes.Get (0));
351
352 OnOffHelper onoff ("ns3::UdpSocketFactory", InetSocketAddress (sinkAddress, port));
353 onoff.SetConstantRate (DataRate ("400Mb/s"), 1420);
354 onoff.SetAttribute ("StartTime", TimeValue (Seconds (0.5)));
355 onoff.SetAttribute ("StopTime", TimeValue (Seconds (simuTime)));
356 ApplicationContainer apps_source = onoff.Install (wifiApNodes.Get (0));
357
358 apps_sink.Start (Seconds (0.5));
359 apps_sink.Stop (Seconds (simuTime));
360
361 //------------------------------------------------------------
362 //-- Setup stats and data collection
363 //--------------------------------------------
364
365 //Register packet receptions to calculate throughput
366 Config::Connect ("/NodeList/1/ApplicationList/*/$ns3::PacketSink/Rx",
368
369 //Callbacks to print every change of rate
370 Config::ConnectFailSafe ("/NodeList/0/DeviceList/*/$ns3::WifiNetDevice/RemoteStationManager/$" + apManager + "/RateChange",
372
373 Simulator::Stop (Seconds (simuTime));
374 Simulator::Run ();
375
376 std::ofstream outfile (("throughput-" + outputFileName + ".plt").c_str ());
377 Gnuplot gnuplot = Gnuplot (("throughput-" + outputFileName + ".eps").c_str (), "Throughput");
378 gnuplot.SetTerminal ("post eps color enhanced");
379 gnuplot.SetLegend ("Time (seconds)", "Throughput (Mb/s)");
380 gnuplot.SetTitle ("Throughput (AP to STA) vs time");
381 gnuplot.AddDataset (atpCounter.GetDatafile ());
382 gnuplot.GenerateOutput (outfile);
383
384 Simulator::Destroy ();
385
386 return 0;
387}
Class to collect node statistics.
void SetPosition(Ptr< Node > node, Vector position)
Set node position.
Gnuplot2dDataset GetDatafile()
void RxCallback(std::string path, Ptr< const Packet > packet, const Address &from)
RX callback.
NodeStatistics(NetDeviceContainer aps, NetDeviceContainer stas)
Constructor.
uint32_t m_bytesTotal
Number of received bytes on a given state.
Vector GetPosition(Ptr< Node > node)
Get node position.
Gnuplot2dDataset m_output
Throughput output data.
void AdvancePosition(Ptr< Node > node, int stepsSize, int stepsTime)
Advance node position.
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.
AttributeValue implementation for Boolean.
Definition: boolean.h:37
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 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 SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition: gnuplot.cc:738
void SetTerminal(const std::string &terminal)
Definition: gnuplot.cc:726
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:764
void SetTitle(const std::string &title)
Definition: gnuplot.cc:732
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
an EUI-48 address
Definition: mac48-address.h:44
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
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
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
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:856
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:36
AttributeValue implementation for Ssid.
AttributeValue implementation for Time.
Definition: nstime.h:1308
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 SetChannel(Ptr< YansWifiChannel > channel)
uint16_t port
Definition: dsdv-manet.cc:45
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:920
bool ConnectFailSafe(std::string path, const CallbackBase &cb)
Definition: config.cc:929
void Set(std::string path, const AttributeValue &value)
Definition: config.cc:839
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
@ WIFI_STANDARD_80211a
@ WIFI_STANDARD_80211n
@ WIFI_STANDARD_80211g
@ WIFI_STANDARD_80211ac
@ WIFI_STANDARD_80211b
address
Definition: first.py:44
stack
Definition: first.py:41
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
STL namespace.
ssid
Definition: third.py:97
wifi
Definition: third.py:99
mobility
Definition: third.py:107
wifiStaNodes
Definition: third.py:88
void RateCallback(std::string path, uint64_t rate, Mac48Address dest)
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56