A Discrete-Event Network Simulator
API
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 <sstream>
54 #include <fstream>
55 #include <math.h>
56 
57 #include "ns3/core-module.h"
58 #include "ns3/network-module.h"
59 #include "ns3/internet-module.h"
60 #include "ns3/mobility-module.h"
61 #include "ns3/wifi-module.h"
62 #include "ns3/applications-module.h"
63 #include "ns3/stats-module.h"
64 #include "ns3/flow-monitor-module.h"
65 
66 using namespace ns3;
67 using namespace std;
68 
69 NS_LOG_COMPONENT_DEFINE ("RateAdaptationDistance");
70 
71 class NodeStatistics
72 {
73 public:
75 
76  void CheckStatistics (double time);
77 
78  void RxCallback (std::string path, Ptr<const Packet> packet, const Address &from);
79  void SetPosition (Ptr<Node> node, Vector position);
80  void AdvancePosition (Ptr<Node> node, int stepsSize, int stepsTime);
81  Vector GetPosition (Ptr<Node> node);
82 
83  Gnuplot2dDataset GetDatafile ();
84 
85 private:
86  uint32_t m_bytesTotal;
87  Gnuplot2dDataset m_output;
88 };
89 
91 {
92  m_bytesTotal = 0;
93 }
94 
95 void
96 NodeStatistics::RxCallback (std::string path, Ptr<const Packet> packet, const Address &from)
97 {
98  m_bytesTotal += packet->GetSize ();
99 }
100 
101 void
103 {
104 
105 }
106 
107 void
108 NodeStatistics::SetPosition (Ptr<Node> node, Vector position)
109 {
111  mobility->SetPosition (position);
112 }
113 
114 Vector
116 {
117  Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
118  return mobility->GetPosition ();
119 }
120 
121 void
122 NodeStatistics::AdvancePosition (Ptr<Node> node, int stepsSize, int stepsTime)
123 {
124  Vector pos = GetPosition (node);
125  double mbs = ((m_bytesTotal * 8.0) / (1000000 * stepsTime));
126  m_bytesTotal = 0;
127  m_output.Add (pos.x, mbs);
128  pos.x += stepsSize;
129  SetPosition (node, pos);
130  Simulator::Schedule (Seconds (stepsTime), &NodeStatistics::AdvancePosition, this, node, stepsSize, stepsTime);
131 }
132 
135 {
136  return m_output;
137 }
138 
139 
140 void RateCallback (std::string path, uint64_t rate, Mac48Address dest)
141 {
142  NS_LOG_INFO ((Simulator::Now ()).GetSeconds () << " " << dest << " Rate " << rate / 1000000.0);
143 }
144 
145 int main (int argc, char *argv[])
146 {
147  uint32_t rtsThreshold = 65535;
148  std::string staManager = "ns3::MinstrelHtWifiManager";
149  std::string apManager = "ns3::MinstrelHtWifiManager";
150  std::string standard = "802.11n-5GHz";
151  std::string outputFileName = "minstrelHT";
152  uint32_t BE_MaxAmpduSize = 65535;
153  bool shortGuardInterval = false;
154  uint32_t chWidth = 20;
155  int ap1_x = 0;
156  int ap1_y = 0;
157  int sta1_x = 5;
158  int sta1_y = 0;
159  int steps = 100;
160  int stepsSize = 1;
161  int stepsTime = 1;
162 
164  cmd.AddValue ("staManager", "PRC Manager of the STA", staManager);
165  cmd.AddValue ("apManager", "PRC Manager of the AP", apManager);
166  cmd.AddValue ("standard", "Wifi Phy Standard", standard);
167  cmd.AddValue ("shortGuardInterval", "Enable Short Guard Interval in all stations", shortGuardInterval);
168  cmd.AddValue ("channelWidth", "Channel width of all the stations", chWidth);
169  cmd.AddValue ("rtsThreshold", "RTS threshold", rtsThreshold);
170  cmd.AddValue ("BE_MaxAmpduSize", "BE Mac A-MPDU size", BE_MaxAmpduSize);
171  cmd.AddValue ("outputFileName", "Output filename", outputFileName);
172  cmd.AddValue ("steps", "How many different distances to try", steps);
173  cmd.AddValue ("stepsTime", "Time on each step", stepsTime);
174  cmd.AddValue ("stepsSize", "Distance between steps", stepsSize);
175  cmd.AddValue ("AP1_x", "Position of AP1 in x coordinate", ap1_x);
176  cmd.AddValue ("AP1_y", "Position of AP1 in y coordinate", ap1_y);
177  cmd.AddValue ("STA1_x", "Position of STA1 in x coordinate", sta1_x);
178  cmd.AddValue ("STA1_y", "Position of STA1 in y coordinate", sta1_y);
179  cmd.Parse (argc, argv);
180 
181  int simuTime = steps * stepsTime;
182 
183  // Define the APs
184  NodeContainer wifiApNodes;
185  wifiApNodes.Create (1);
186 
187  //Define the STAs
189  wifiStaNodes.Create (1);
190 
191 
194  wifiPhy.SetChannel (wifiChannel.Create ());
195 
196  wifiPhy.Set("ShortGuardEnabled", BooleanValue(shortGuardInterval));
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  {
208  }
209  else if (standard == "802.11b")
210  {
212  }
213  else if (standard == "802.11g")
214  {
216  }
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  {
240  }
241  else if (standard == "802.11n-5GHz")
242  {
244  }
245 
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  "BE_MaxAmpduSize", UintegerValue (BE_MaxAmpduSize));
255  wifiStaDevices.Add (wifi.Install (wifiPhy, wifiMac, wifiStaNodes.Get (0)));
256 
257  //Configure the AP node
258  wifi.SetRemoteStationManager (apManager, "RtsCtsThreshold", UintegerValue (rtsThreshold));
259 
260  ssid = Ssid ("AP");
261  wifiMac.SetType ("ns3::ApWifiMac",
262  "Ssid", SsidValue (ssid),
263  "BE_MaxAmpduSize", UintegerValue (BE_MaxAmpduSize));
264  wifiApDevices.Add (wifi.Install (wifiPhy, wifiMac, wifiApNodes.Get (0)));
265  }
266  else if (standard == "802.11ac")
267  {
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  "BE_MaxAmpduSize", UintegerValue (BE_MaxAmpduSize));
278  wifiStaDevices.Add (wifi.Install (wifiPhy, wifiMac, wifiStaNodes.Get (0)));
279 
280  //Configure the AP node
281  wifi.SetRemoteStationManager (apManager, "RtsCtsThreshold", UintegerValue (rtsThreshold));
282 
283  ssid = Ssid ("AP");
284  wifiMac.SetType ("ns3::ApWifiMac",
285  "Ssid", SsidValue (ssid),
286  "BE_MaxAmpduSize", UintegerValue (BE_MaxAmpduSize));
287  wifiApDevices.Add (wifi.Install (wifiPhy, wifiMac, wifiApNodes.Get (0)));
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  // Configure the mobility.
298  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
299  //Initial position of AP and STA
300  positionAlloc->Add (Vector (ap1_x, ap1_y, 0.0));
301  positionAlloc->Add (Vector (sta1_x, sta1_y, 0.0));
302  mobility.SetPositionAllocator (positionAlloc);
303  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
304  mobility.Install (wifiApNodes.Get (0));
305  mobility.Install (wifiStaNodes.Get (0));
306 
307  //Statistics counter
308  NodeStatistics atpCounter = NodeStatistics (wifiApDevices, wifiStaDevices);
309 
310  //Move the STA by stepsSize meters every stepsTime seconds
311  Simulator::Schedule (Seconds (0.5 + stepsTime), &NodeStatistics::AdvancePosition, &atpCounter, wifiStaNodes.Get (0), stepsSize, stepsTime);
312 
313  //Configure the IP stack
315  stack.Install (wifiApNodes);
316  stack.Install (wifiStaNodes);
318  address.SetBase ("10.1.1.0", "255.255.255.0");
319  Ipv4InterfaceContainer i = address.Assign (wifiDevices);
320  Ipv4Address sinkAddress = i.GetAddress (0);
321  uint16_t port = 9;
322 
323  //Configure the CBR generator
324  PacketSinkHelper sink ("ns3::UdpSocketFactory", InetSocketAddress (sinkAddress, port));
325  ApplicationContainer apps_sink = sink.Install (wifiStaNodes.Get (0));
326 
327  OnOffHelper onoff ("ns3::UdpSocketFactory", InetSocketAddress (sinkAddress, port));
328  onoff.SetConstantRate (DataRate ("200Mb/s"), 1420);
329  onoff.SetAttribute ("StartTime", TimeValue (Seconds (0.5)));
330  onoff.SetAttribute ("StopTime", TimeValue (Seconds (simuTime)));
331  ApplicationContainer apps_source = onoff.Install (wifiApNodes.Get (0));
332 
333  apps_sink.Start (Seconds (0.5));
334  apps_sink.Stop (Seconds (simuTime));
335 
336  //------------------------------------------------------------
337  //-- Setup stats and data collection
338  //--------------------------------------------
339 
340  //Register packet receptions to calculate throughput
341  Config::Connect ("/NodeList/1/ApplicationList/*/$ns3::PacketSink/Rx",
342  MakeCallback (&NodeStatistics::RxCallback, &atpCounter));
343 
344  //Callbacks to print every change of rate
345  Config::Connect ("/NodeList/0/DeviceList/*/$ns3::WifiNetDevice/RemoteStationManager/$" + apManager + "/RateChange",
347 
348  Simulator::Stop (Seconds (simuTime));
349  Simulator::Run ();
350 
351  std::ofstream outfile (("throughput-" + outputFileName + ".plt").c_str ());
352  Gnuplot gnuplot = Gnuplot (("throughput-" + outputFileName + ".eps").c_str (), "Throughput");
353  gnuplot.SetTerminal ("post eps color enhanced");
354  gnuplot.SetLegend ("Time (seconds)", "Throughput (Mb/s)");
355  gnuplot.SetTitle ("Throughput (AP to STA) vs time");
356  gnuplot.AddDataset (atpCounter.GetDatafile ());
357  gnuplot.GenerateOutput (outfile);
358 
360 
361  return 0;
362 }
ERP-OFDM PHY (Clause 19, Section 19.5)
Custom version of log2() to deal with Bug 1467.
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:47
void Set(std::string name, const AttributeValue &v)
Definition: wifi-helper.cc:112
holds a vector of ns3::Application pointers.
an Inet address class
create HT-enabled MAC layers for a ns3::WifiNetDevice.
AttributeValue implementation for Boolean.
Definition: boolean.h:34
HT OFDM PHY for the 5 GHz band (clause 20)
void SetPosition(Ptr< Node > node, Vector position)
static void AdvancePosition(Ptr< Node > node)
Definition: wifi-ap.cc:100
Class to represent a 2D points plot.
Definition: gnuplot.h:113
holds a vector of std::pair of Ptr and interface index.
Ptr< YansWifiChannel > Create(void) const
void SetRemoteStationManager(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())
Definition: wifi-helper.cc:683
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:462
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:769
static Vector GetPosition(Ptr< Node > node)
Definition: multirate.cc:315
static void Run(void)
Run the simulation.
Definition: simulator.cc:201
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
aggregate IP/TCP/UDP functionality to existing Nodes.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:792
Vector GetPosition(void) const
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:244
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
HT OFDM PHY for the 2.4 GHz band (clause 20)
static YansWifiPhyHelper Default(void)
Create a phy helper in a default working state.
STL namespace.
helps to create WifiNetDevice objects
Definition: wifi-helper.h:231
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
tuple cmd
Definition: second.py:35
void AdvancePosition(Ptr< Node > node, int stepsSize, int stepsTime)
static void SetPosition(Ptr< Node > node, Vector position)
Definition: wifi-ap.cc:86
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer c) const
Definition: wifi-helper.cc:712
uint16_t port
Definition: dsdv-manet.cc:44
a polymophic address class
Definition: address.h:90
Gnuplot2dDataset GetDatafile()
void RxCallback(std::string path, Ptr< const Packet > packet, const Address &from)
static HtWifiMacHelper Default(void)
Create a mac helper in a default working state.
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)
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
tuple mobility
Definition: third.py:101
static EventId Schedule(Time const &delay, MEM mem_ptr, OBJ obj)
Schedule an event to expire after delay.
Definition: simulator.h:1238
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:367
AttributeValue implementation for Time.
Definition: nstime.h:957
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
holds a vector of ns3::NetDevice pointers
virtual void SetStandard(enum WifiPhyStandard standard)
Definition: wifi-helper.cc:706
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:762
static NqosWifiMacHelper Default(void)
Create a mac helper in a default working state.
void RateCallback(std::string path, uint64_t rate, Mac48Address dest)
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
create non QoS-enabled MAC layers for a ns3::WifiNetDevice.
Parse command-line arguments.
Definition: command-line.h:205
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:835
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:165
static VhtWifiMacHelper Default(void)
Create a mac helper in a default working state.
OFDM PHY for the 5 GHz band (Clause 17)
create VHT-enabled MAC layers for a ns3::WifiNetDevice.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
VHT OFDM PHY (clause 22)
DSSS PHY (Clause 15) and HR/DSSS PHY (Clause 18)
void SetMobilityModel(std::string type, 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())
an EUI-48 address
Definition: mac48-address.h:43
tuple ssid
Definition: third.py:93
manage and create wifi channel objects for the yans model.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:224
void SetPosition(const Vector &position)
tuple stack
Definition: first.py:34
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:38
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())
Set the underlying type of the MAC and its attributes.
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:40
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:495
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:209
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
AttributeValue implementation for Ssid.
Definition: ssid.h:95
void Add(Vector v)
Add a position to the list of positions.
NodeStatistics(NetDeviceContainer aps, NetDeviceContainer stas)
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())
void CheckStatistics(double time)
void Parse(int argc, char *argv[])
Parse the program arguments.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
tuple wifi
Definition: third.py:89
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
tuple address
Definition: first.py:37
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
void SetTerminal(const std::string &terminal)
Definition: gnuplot.cc:724
Vector GetPosition(Ptr< Node > node)
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
tuple wifiStaNodes
Definition: third.py:81