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 
69 using namespace ns3;
70 using namespace std;
71 
72 NS_LOG_COMPONENT_DEFINE ("RateAdaptationDistance");
73 
74 class NodeStatistics
75 {
76 public:
78 
79  void CheckStatistics (double time);
80 
81  void RxCallback (std::string path, Ptr<const Packet> packet, const Address &from);
82  void SetPosition (Ptr<Node> node, Vector position);
83  void AdvancePosition (Ptr<Node> node, int stepsSize, int stepsTime);
84  Vector GetPosition (Ptr<Node> node);
85 
86  Gnuplot2dDataset GetDatafile ();
87 
88 private:
89  uint32_t m_bytesTotal;
90  Gnuplot2dDataset m_output;
91 };
92 
94 {
95  m_bytesTotal = 0;
96 }
97 
98 void
99 NodeStatistics::RxCallback (std::string path, Ptr<const Packet> packet, const Address &from)
100 {
101  m_bytesTotal += packet->GetSize ();
102 }
103 
104 void
106 {
107 
108 }
109 
110 void
111 NodeStatistics::SetPosition (Ptr<Node> node, Vector position)
112 {
114  mobility->SetPosition (position);
115 }
116 
117 Vector
119 {
121  return mobility->GetPosition ();
122 }
123 
124 void
125 NodeStatistics::AdvancePosition (Ptr<Node> node, int stepsSize, int stepsTime)
126 {
127  Vector pos = GetPosition (node);
128  double mbs = ((m_bytesTotal * 8.0) / (1000000 * stepsTime));
129  m_bytesTotal = 0;
130  m_output.Add (pos.x, mbs);
131  pos.x += stepsSize;
132  SetPosition (node, pos);
133  Simulator::Schedule (Seconds (stepsTime), &NodeStatistics::AdvancePosition, this, node, stepsSize, stepsTime);
134 }
135 
138 {
139  return m_output;
140 }
141 
142 
143 void RateCallback (std::string path, uint64_t rate, Mac48Address dest)
144 {
145  NS_LOG_INFO ((Simulator::Now ()).GetSeconds () << " " << dest << " Rate " << rate / 1000000.0);
146 }
147 
148 int main (int argc, char *argv[])
149 {
150  uint32_t rtsThreshold = 65535;
151  std::string staManager = "ns3::MinstrelHtWifiManager";
152  std::string apManager = "ns3::MinstrelHtWifiManager";
153  std::string standard = "802.11n-5GHz";
154  std::string outputFileName = "minstrelHT";
155  uint32_t BeMaxAmpduSize = 65535;
156  bool shortGuardInterval = false;
157  uint32_t chWidth = 20;
158  int ap1_x = 0;
159  int ap1_y = 0;
160  int sta1_x = 5;
161  int sta1_y = 0;
162  int steps = 100;
163  int stepsSize = 1;
164  int stepsTime = 1;
165 
166  CommandLine cmd (__FILE__);
167  cmd.AddValue ("staManager", "PRC Manager of the STA", staManager);
168  cmd.AddValue ("apManager", "PRC Manager of the AP", apManager);
169  cmd.AddValue ("standard", "Wifi Phy Standard", standard);
170  cmd.AddValue ("shortGuardInterval", "Enable Short Guard Interval in all stations", shortGuardInterval);
171  cmd.AddValue ("channelWidth", "Channel width of all the stations", chWidth);
172  cmd.AddValue ("rtsThreshold", "RTS threshold", rtsThreshold);
173  cmd.AddValue ("BeMaxAmpduSize", "BE Mac A-MPDU size", BeMaxAmpduSize);
174  cmd.AddValue ("outputFileName", "Output filename", outputFileName);
175  cmd.AddValue ("steps", "How many different distances to try", steps);
176  cmd.AddValue ("stepsTime", "Time on each step", stepsTime);
177  cmd.AddValue ("stepsSize", "Distance between steps", stepsSize);
178  cmd.AddValue ("AP1_x", "Position of AP1 in x coordinate", ap1_x);
179  cmd.AddValue ("AP1_y", "Position of AP1 in y coordinate", ap1_y);
180  cmd.AddValue ("STA1_x", "Position of STA1 in x coordinate", sta1_x);
181  cmd.AddValue ("STA1_y", "Position of STA1 in y coordinate", sta1_y);
182  cmd.Parse (argc, argv);
183 
184  int simuTime = steps * stepsTime;
185 
186  // Define the APs
187  NodeContainer wifiApNodes;
188  wifiApNodes.Create (1);
189 
190  //Define the STAs
192  wifiStaNodes.Create (1);
193 
196  wifiPhy.SetChannel (wifiChannel.Create ());
197 
198  NetDeviceContainer wifiApDevices;
199  NetDeviceContainer wifiStaDevices;
200  NetDeviceContainer wifiDevices;
201 
203  if (standard == "802.11a" || standard == "802.11b" || standard == "802.11g")
204  {
205  if (standard == "802.11a")
206  {
207  wifi.SetStandard (WIFI_STANDARD_80211a);
208  }
209  else if (standard == "802.11b")
210  {
211  wifi.SetStandard (WIFI_STANDARD_80211b);
212  }
213  else if (standard == "802.11g")
214  {
215  wifi.SetStandard (WIFI_STANDARD_80211g);
216  }
217  WifiMacHelper wifiMac;
218 
219  //Configure the STA node
220  wifi.SetRemoteStationManager (staManager, "RtsCtsThreshold", UintegerValue (rtsThreshold));
221 
222  Ssid ssid = Ssid ("AP");
223  wifiMac.SetType ("ns3::StaWifiMac",
224  "Ssid", SsidValue (ssid));
225  wifiStaDevices.Add (wifi.Install (wifiPhy, wifiMac, wifiStaNodes.Get (0)));
226 
227  //Configure the AP node
228  wifi.SetRemoteStationManager (apManager, "RtsCtsThreshold", UintegerValue (rtsThreshold));
229 
230  ssid = Ssid ("AP");
231  wifiMac.SetType ("ns3::ApWifiMac",
232  "Ssid", SsidValue (ssid));
233  wifiApDevices.Add (wifi.Install (wifiPhy, wifiMac, wifiApNodes.Get (0)));
234  }
235  else if (standard == "802.11n-2.4GHz" || standard == "802.11n-5GHz")
236  {
237  if (standard == "802.11n-2.4GHz")
238  {
239  wifi.SetStandard (WIFI_STANDARD_80211n_2_4GHZ);
240  }
241  else if (standard == "802.11n-5GHz")
242  {
243  wifi.SetStandard (WIFI_STANDARD_80211n_5GHZ);
244  }
245 
246  WifiMacHelper wifiMac;
247 
248  //Configure the STA node
249  wifi.SetRemoteStationManager (staManager, "RtsCtsThreshold", UintegerValue (rtsThreshold));
250 
251  Ssid ssid = Ssid ("AP");
252  wifiMac.SetType ("ns3::StaWifiMac",
253  "Ssid", SsidValue (ssid));
254  wifiStaDevices.Add (wifi.Install (wifiPhy, wifiMac, wifiStaNodes.Get (0)));
255 
256  //Configure the AP node
257  wifi.SetRemoteStationManager (apManager, "RtsCtsThreshold", UintegerValue (rtsThreshold));
258 
259  ssid = Ssid ("AP");
260  wifiMac.SetType ("ns3::ApWifiMac",
261  "Ssid", SsidValue (ssid));
262  wifiApDevices.Add (wifi.Install (wifiPhy, wifiMac, wifiApNodes.Get (0)));
263 
264  Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Mac/BE_MaxAmpduSize", UintegerValue (BeMaxAmpduSize));
265  }
266  else if (standard == "802.11ac")
267  {
268  wifi.SetStandard (WIFI_STANDARD_80211ac);
269  WifiMacHelper wifiMac;
270 
271  //Configure the STA node
272  wifi.SetRemoteStationManager (staManager, "RtsCtsThreshold", UintegerValue (rtsThreshold));
273 
274  Ssid ssid = Ssid ("AP");
275  wifiMac.SetType ("ns3::StaWifiMac",
276  "Ssid", SsidValue (ssid));
277  wifiStaDevices.Add (wifi.Install (wifiPhy, wifiMac, wifiStaNodes.Get (0)));
278 
279  //Configure the AP node
280  wifi.SetRemoteStationManager (apManager, "RtsCtsThreshold", UintegerValue (rtsThreshold));
281 
282  ssid = Ssid ("AP");
283  wifiMac.SetType ("ns3::ApWifiMac",
284  "Ssid", SsidValue (ssid));
285  wifiApDevices.Add (wifi.Install (wifiPhy, wifiMac, wifiApNodes.Get (0)));
286 
287  Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Mac/BE_MaxAmpduSize", UintegerValue (BeMaxAmpduSize));
288  }
289 
290  wifiDevices.Add (wifiStaDevices);
291  wifiDevices.Add (wifiApDevices);
292 
293  // Set channel width
294  Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/ChannelWidth", UintegerValue (chWidth));
295 
296  // Set guard interval
297  Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/HtConfiguration/ShortGuardIntervalSupported", BooleanValue (shortGuardInterval));
298 
299  // Configure the mobility.
301  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
302  //Initial position of AP and STA
303  positionAlloc->Add (Vector (ap1_x, ap1_y, 0.0));
304  positionAlloc->Add (Vector (sta1_x, sta1_y, 0.0));
305  mobility.SetPositionAllocator (positionAlloc);
306  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
307  mobility.Install (wifiApNodes.Get (0));
308  mobility.Install (wifiStaNodes.Get (0));
309 
310  //Statistics counter
311  NodeStatistics atpCounter = NodeStatistics (wifiApDevices, wifiStaDevices);
312 
313  //Move the STA by stepsSize meters every stepsTime seconds
314  Simulator::Schedule (Seconds (0.5 + stepsTime), &NodeStatistics::AdvancePosition, &atpCounter, wifiStaNodes.Get (0), stepsSize, stepsTime);
315 
316  //Configure the IP stack
318  stack.Install (wifiApNodes);
319  stack.Install (wifiStaNodes);
321  address.SetBase ("10.1.1.0", "255.255.255.0");
322  Ipv4InterfaceContainer i = address.Assign (wifiDevices);
323  Ipv4Address sinkAddress = i.GetAddress (0);
324  uint16_t port = 9;
325 
326  //Configure the CBR generator
327  PacketSinkHelper sink ("ns3::UdpSocketFactory", InetSocketAddress (sinkAddress, port));
328  ApplicationContainer apps_sink = sink.Install (wifiStaNodes.Get (0));
329 
330  OnOffHelper onoff ("ns3::UdpSocketFactory", InetSocketAddress (sinkAddress, port));
331  onoff.SetConstantRate (DataRate ("400Mb/s"), 1420);
332  onoff.SetAttribute ("StartTime", TimeValue (Seconds (0.5)));
333  onoff.SetAttribute ("StopTime", TimeValue (Seconds (simuTime)));
334  ApplicationContainer apps_source = onoff.Install (wifiApNodes.Get (0));
335 
336  apps_sink.Start (Seconds (0.5));
337  apps_sink.Stop (Seconds (simuTime));
338 
339  //------------------------------------------------------------
340  //-- Setup stats and data collection
341  //--------------------------------------------
342 
343  //Register packet receptions to calculate throughput
344  Config::Connect ("/NodeList/1/ApplicationList/*/$ns3::PacketSink/Rx",
345  MakeCallback (&NodeStatistics::RxCallback, &atpCounter));
346 
347  //Callbacks to print every change of rate
348  Config::ConnectFailSafe ("/NodeList/0/DeviceList/*/$ns3::WifiNetDevice/RemoteStationManager/$" + apManager + "/RateChange",
350 
351  Simulator::Stop (Seconds (simuTime));
352  Simulator::Run ();
353 
354  std::ofstream outfile (("throughput-" + outputFileName + ".plt").c_str ());
355  Gnuplot gnuplot = Gnuplot (("throughput-" + outputFileName + ".eps").c_str (), "Throughput");
356  gnuplot.SetTerminal ("post eps color enhanced");
357  gnuplot.SetLegend ("Time (seconds)", "Throughput (Mb/s)");
358  gnuplot.SetTitle ("Throughput (AP to STA) vs time");
359  gnuplot.AddDataset (atpCounter.GetDatafile ());
360  gnuplot.GenerateOutput (outfile);
361 
363 
364  return 0;
365 }
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56
holds a vector of ns3::Application pointers.
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:557
void RateCallback(std::string path, uint64_t rate, Mac48Address dest)
an Inet address class
AttributeValue implementation for Boolean.
Definition: boolean.h:36
void SetPosition(Ptr< Node > node, Vector position)
static void AdvancePosition(Ptr< Node > node)
Definition: wifi-ap.cc:103
Class to represent a 2D points plot.
Definition: gnuplot.h:117
holds a vector of std::pair of Ptr<Ipv4> and interface index.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:852
Make it easy to create and manage PHY objects for the YANS model.
static YansWifiChannelHelper Default(void)
Create a channel helper in a default working state.
void Set(std::string path, const AttributeValue &value)
Definition: config.cc:839
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
aggregate IP/TCP/UDP functionality to existing Nodes.
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:756
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
cmd
Definition: second.py:35
static YansWifiPhyHelper Default(void)
Create a PHY helper in a default working state.
STL namespace.
helps to create WifiNetDevice objects
Definition: wifi-helper.h:318
static Vector GetPosition(Ptr< Node > node)
Definition: wifi-ap.cc:96
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
stack
Definition: first.py:41
void AdvancePosition(Ptr< Node > node, int stepsSize, int stepsTime)
static void SetPosition(Ptr< Node > node, Vector position)
Definition: wifi-ap.cc:89
uint16_t port
Definition: dsdv-manet.cc:45
a polymophic address class
Definition: address.h:90
Ptr< YansWifiChannel > Create(void) const
mobility
Definition: third.py:108
void RxCallback(std::string path, Ptr< const Packet > packet, const Address &from)
Class for representing data rates.
Definition: data-rate.h:88
Keep track of the current position and velocity of an object.
void SetChannel(Ptr< YansWifiChannel > channel)
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:371
AttributeValue implementation for Time.
Definition: nstime.h:1342
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
void SetTitle(const std::string &title)
Definition: gnuplot.cc:730
void Add(NetDeviceContainer other)
Append the contents of another NetDeviceContainer to the end of this container.
Hold an unsigned integer type.
Definition: uinteger.h:44
ssid
Definition: third.py:100
holds a vector of ns3::NetDevice pointers
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:762
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:226
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:918
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition: gnuplot.cc:736
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
address
Definition: first.py:44
bool ConnectFailSafe(std::string path, const CallbackBase &cb)
This function will attempt to find all trace sources which match the input path and will then connect...
Definition: config.cc:927
an EUI-48 address
Definition: mac48-address.h:43
manage and create wifi channel objects for the YANS model.
create MAC layers for a ns3::WifiNetDevice.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:195
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:35
virtual void SetType(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue(), std::string n10="", const AttributeValue &v10=EmptyAttributeValue())
wifi
Definition: third.py:96
Helper class used to assign positions and mobility models to nodes.
Ipv4 addresses are stored in host order in this class.
Definition: ipv4-address.h:41
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1278
AttributeValue implementation for Ssid.
Definition: ssid.h:105
void Add(Vector v)
Add a position to the list of positions.
NodeStatistics(NetDeviceContainer aps, NetDeviceContainer stas)
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
wifiStaNodes
Definition: third.py:88
void SetTerminal(const std::string &terminal)
Definition: gnuplot.cc:724
Vector GetPosition(Ptr< Node > node)
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:1642