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 "ns3/core-module.h"
54 #include "ns3/internet-module.h"
55 #include "ns3/mobility-module.h"
56 #include "ns3/wifi-module.h"
57 #include "ns3/applications-module.h"
58 #include "ns3/stats-module.h"
59 
60 using namespace ns3;
61 using namespace std;
62 
63 NS_LOG_COMPONENT_DEFINE ("RateAdaptationDistance");
64 
65 class NodeStatistics
66 {
67 public:
69 
70  void CheckStatistics (double time);
71 
72  void RxCallback (std::string path, Ptr<const Packet> packet, const Address &from);
73  void SetPosition (Ptr<Node> node, Vector position);
74  void AdvancePosition (Ptr<Node> node, int stepsSize, int stepsTime);
75  Vector GetPosition (Ptr<Node> node);
76 
77  Gnuplot2dDataset GetDatafile ();
78 
79 private:
80  uint32_t m_bytesTotal;
81  Gnuplot2dDataset m_output;
82 };
83 
85 {
86  m_bytesTotal = 0;
87 }
88 
89 void
90 NodeStatistics::RxCallback (std::string path, Ptr<const Packet> packet, const Address &from)
91 {
92  m_bytesTotal += packet->GetSize ();
93 }
94 
95 void
97 {
98 
99 }
100 
101 void
102 NodeStatistics::SetPosition (Ptr<Node> node, Vector position)
103 {
105  mobility->SetPosition (position);
106 }
107 
108 Vector
110 {
111  Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
112  return mobility->GetPosition ();
113 }
114 
115 void
116 NodeStatistics::AdvancePosition (Ptr<Node> node, int stepsSize, int stepsTime)
117 {
118  Vector pos = GetPosition (node);
119  double mbs = ((m_bytesTotal * 8.0) / (1000000 * stepsTime));
120  m_bytesTotal = 0;
121  m_output.Add (pos.x, mbs);
122  pos.x += stepsSize;
123  SetPosition (node, pos);
124  Simulator::Schedule (Seconds (stepsTime), &NodeStatistics::AdvancePosition, this, node, stepsSize, stepsTime);
125 }
126 
129 {
130  return m_output;
131 }
132 
133 
134 void RateCallback (std::string path, uint64_t rate, Mac48Address dest)
135 {
136  NS_LOG_INFO ((Simulator::Now ()).GetSeconds () << " " << dest << " Rate " << rate / 1000000.0);
137 }
138 
139 int main (int argc, char *argv[])
140 {
141  uint32_t rtsThreshold = 65535;
142  std::string staManager = "ns3::MinstrelHtWifiManager";
143  std::string apManager = "ns3::MinstrelHtWifiManager";
144  std::string standard = "802.11n-5GHz";
145  std::string outputFileName = "minstrelHT";
146  uint32_t BE_MaxAmpduSize = 65535;
147  bool shortGuardInterval = false;
148  uint32_t chWidth = 20;
149  int ap1_x = 0;
150  int ap1_y = 0;
151  int sta1_x = 5;
152  int sta1_y = 0;
153  int steps = 100;
154  int stepsSize = 1;
155  int stepsTime = 1;
156 
158  cmd.AddValue ("staManager", "PRC Manager of the STA", staManager);
159  cmd.AddValue ("apManager", "PRC Manager of the AP", apManager);
160  cmd.AddValue ("standard", "Wifi Phy Standard", standard);
161  cmd.AddValue ("shortGuardInterval", "Enable Short Guard Interval in all stations", shortGuardInterval);
162  cmd.AddValue ("channelWidth", "Channel width of all the stations", chWidth);
163  cmd.AddValue ("rtsThreshold", "RTS threshold", rtsThreshold);
164  cmd.AddValue ("BE_MaxAmpduSize", "BE Mac A-MPDU size", BE_MaxAmpduSize);
165  cmd.AddValue ("outputFileName", "Output filename", outputFileName);
166  cmd.AddValue ("steps", "How many different distances to try", steps);
167  cmd.AddValue ("stepsTime", "Time on each step", stepsTime);
168  cmd.AddValue ("stepsSize", "Distance between steps", stepsSize);
169  cmd.AddValue ("AP1_x", "Position of AP1 in x coordinate", ap1_x);
170  cmd.AddValue ("AP1_y", "Position of AP1 in y coordinate", ap1_y);
171  cmd.AddValue ("STA1_x", "Position of STA1 in x coordinate", sta1_x);
172  cmd.AddValue ("STA1_y", "Position of STA1 in y coordinate", sta1_y);
173  cmd.Parse (argc, argv);
174 
175  int simuTime = steps * stepsTime;
176 
177  // Define the APs
178  NodeContainer wifiApNodes;
179  wifiApNodes.Create (1);
180 
181  //Define the STAs
183  wifiStaNodes.Create (1);
184 
185 
188  wifiPhy.SetChannel (wifiChannel.Create ());
189 
190  wifiPhy.Set ("ShortGuardEnabled", BooleanValue (shortGuardInterval));
191 
192  NetDeviceContainer wifiApDevices;
193  NetDeviceContainer wifiStaDevices;
194  NetDeviceContainer wifiDevices;
195 
197  if (standard == "802.11a" || standard == "802.11b" || standard == "802.11g")
198  {
199  if (standard == "802.11a")
200  {
202  }
203  else if (standard == "802.11b")
204  {
206  }
207  else if (standard == "802.11g")
208  {
210  }
211  WifiMacHelper wifiMac;
212 
213  //Configure the STA node
214  wifi.SetRemoteStationManager (staManager, "RtsCtsThreshold", UintegerValue (rtsThreshold));
215 
216  Ssid ssid = Ssid ("AP");
217  wifiMac.SetType ("ns3::StaWifiMac",
218  "Ssid", SsidValue (ssid));
219  wifiStaDevices.Add (wifi.Install (wifiPhy, wifiMac, wifiStaNodes.Get (0)));
220 
221  //Configure the AP node
222  wifi.SetRemoteStationManager (apManager, "RtsCtsThreshold", UintegerValue (rtsThreshold));
223 
224  ssid = Ssid ("AP");
225  wifiMac.SetType ("ns3::ApWifiMac",
226  "Ssid", SsidValue (ssid));
227  wifiApDevices.Add (wifi.Install (wifiPhy, wifiMac, wifiApNodes.Get (0)));
228  }
229  else if (standard == "802.11n-2.4GHz" || standard == "802.11n-5GHz")
230  {
231  if (standard == "802.11n-2.4GHz")
232  {
234  }
235  else if (standard == "802.11n-5GHz")
236  {
238  }
239 
240  WifiMacHelper wifiMac;
241 
242  //Configure the STA node
243  wifi.SetRemoteStationManager (staManager, "RtsCtsThreshold", UintegerValue (rtsThreshold));
244 
245  Ssid ssid = Ssid ("AP");
246  wifiMac.SetType ("ns3::StaWifiMac",
247  "Ssid", SsidValue (ssid),
248  "BE_MaxAmpduSize", UintegerValue (BE_MaxAmpduSize));
249  wifiStaDevices.Add (wifi.Install (wifiPhy, wifiMac, wifiStaNodes.Get (0)));
250 
251  //Configure the AP node
252  wifi.SetRemoteStationManager (apManager, "RtsCtsThreshold", UintegerValue (rtsThreshold));
253 
254  ssid = Ssid ("AP");
255  wifiMac.SetType ("ns3::ApWifiMac",
256  "Ssid", SsidValue (ssid),
257  "BE_MaxAmpduSize", UintegerValue (BE_MaxAmpduSize));
258  wifiApDevices.Add (wifi.Install (wifiPhy, wifiMac, wifiApNodes.Get (0)));
259  }
260  else if (standard == "802.11ac")
261  {
263  WifiMacHelper wifiMac;
264 
265  //Configure the STA node
266  wifi.SetRemoteStationManager (staManager, "RtsCtsThreshold", UintegerValue (rtsThreshold));
267 
268  Ssid ssid = Ssid ("AP");
269  wifiMac.SetType ("ns3::StaWifiMac",
270  "Ssid", SsidValue (ssid),
271  "BE_MaxAmpduSize", UintegerValue (BE_MaxAmpduSize));
272  wifiStaDevices.Add (wifi.Install (wifiPhy, wifiMac, wifiStaNodes.Get (0)));
273 
274  //Configure the AP node
275  wifi.SetRemoteStationManager (apManager, "RtsCtsThreshold", UintegerValue (rtsThreshold));
276 
277  ssid = Ssid ("AP");
278  wifiMac.SetType ("ns3::ApWifiMac",
279  "Ssid", SsidValue (ssid),
280  "BE_MaxAmpduSize", UintegerValue (BE_MaxAmpduSize));
281  wifiApDevices.Add (wifi.Install (wifiPhy, wifiMac, wifiApNodes.Get (0)));
282  }
283 
284  wifiDevices.Add (wifiStaDevices);
285  wifiDevices.Add (wifiApDevices);
286 
287  // Set channel width
288  Config::Set ("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/ChannelWidth", UintegerValue (chWidth));
289 
290  // Configure the mobility.
292  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
293  //Initial position of AP and STA
294  positionAlloc->Add (Vector (ap1_x, ap1_y, 0.0));
295  positionAlloc->Add (Vector (sta1_x, sta1_y, 0.0));
296  mobility.SetPositionAllocator (positionAlloc);
297  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
298  mobility.Install (wifiApNodes.Get (0));
299  mobility.Install (wifiStaNodes.Get (0));
300 
301  //Statistics counter
302  NodeStatistics atpCounter = NodeStatistics (wifiApDevices, wifiStaDevices);
303 
304  //Move the STA by stepsSize meters every stepsTime seconds
305  Simulator::Schedule (Seconds (0.5 + stepsTime), &NodeStatistics::AdvancePosition, &atpCounter, wifiStaNodes.Get (0), stepsSize, stepsTime);
306 
307  //Configure the IP stack
309  stack.Install (wifiApNodes);
310  stack.Install (wifiStaNodes);
312  address.SetBase ("10.1.1.0", "255.255.255.0");
313  Ipv4InterfaceContainer i = address.Assign (wifiDevices);
314  Ipv4Address sinkAddress = i.GetAddress (0);
315  uint16_t port = 9;
316 
317  //Configure the CBR generator
318  PacketSinkHelper sink ("ns3::UdpSocketFactory", InetSocketAddress (sinkAddress, port));
319  ApplicationContainer apps_sink = sink.Install (wifiStaNodes.Get (0));
320 
321  OnOffHelper onoff ("ns3::UdpSocketFactory", InetSocketAddress (sinkAddress, port));
322  onoff.SetConstantRate (DataRate ("200Mb/s"), 1420);
323  onoff.SetAttribute ("StartTime", TimeValue (Seconds (0.5)));
324  onoff.SetAttribute ("StopTime", TimeValue (Seconds (simuTime)));
325  ApplicationContainer apps_source = onoff.Install (wifiApNodes.Get (0));
326 
327  apps_sink.Start (Seconds (0.5));
328  apps_sink.Stop (Seconds (simuTime));
329 
330  //------------------------------------------------------------
331  //-- Setup stats and data collection
332  //--------------------------------------------
333 
334  //Register packet receptions to calculate throughput
335  Config::Connect ("/NodeList/1/ApplicationList/*/$ns3::PacketSink/Rx",
336  MakeCallback (&NodeStatistics::RxCallback, &atpCounter));
337 
338  //Callbacks to print every change of rate
339  Config::Connect ("/NodeList/0/DeviceList/*/$ns3::WifiNetDevice/RemoteStationManager/$" + apManager + "/RateChange",
341 
342  Simulator::Stop (Seconds (simuTime));
343  Simulator::Run ();
344 
345  std::ofstream outfile (("throughput-" + outputFileName + ".plt").c_str ());
346  Gnuplot gnuplot = Gnuplot (("throughput-" + outputFileName + ".eps").c_str (), "Throughput");
347  gnuplot.SetTerminal ("post eps color enhanced");
348  gnuplot.SetLegend ("Time (seconds)", "Throughput (Mb/s)");
349  gnuplot.SetTitle ("Throughput (AP to STA) vs time");
350  gnuplot.AddDataset (atpCounter.GetDatafile ());
351  gnuplot.GenerateOutput (outfile);
352 
354 
355  return 0;
356 }
ERP-OFDM PHY (Clause 19, Section 19.5)
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:45
void Set(std::string name, const AttributeValue &v)
Definition: wifi-helper.cc:132
holds a vector of ns3::Application pointers.
an Inet address class
AttributeValue implementation for Boolean.
Definition: boolean.h:36
HT 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:96
Class to represent a 2D points plot.
Definition: gnuplot.h:117
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:719
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
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:777
static Vector GetPosition(Ptr< Node > node)
Definition: multirate.cc:329
static void Run(void)
Run the simulation.
Definition: simulator.cc:226
#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:796
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:277
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
HT 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:213
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:82
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)
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:1375
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:1055
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:742
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer::Iterator first, NodeContainer::Iterator last) const
Definition: wifi-helper.cc:748
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
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...
Parse command-line arguments.
Definition: command-line.h:205
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:843
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:190
OFDM PHY for the 5 GHz band (Clause 17)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
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.
create MAC layers for a ns3::WifiNetDevice.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:249
void SetPosition(const Vector &position)
tuple stack
Definition: first.py:34
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())
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:498
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:234
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:993
AttributeValue implementation for Ssid.
Definition: ssid.h:117
void Add(Vector v)
Add a position to the list of positions.
NodeStatistics(NetDeviceContainer aps, NetDeviceContainer stas)
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