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 
50 #include <sstream>
51 #include <fstream>
52 #include <math.h>
53 
54 #include "ns3/core-module.h"
55 #include "ns3/network-module.h"
56 #include "ns3/internet-module.h"
57 #include "ns3/mobility-module.h"
58 #include "ns3/wifi-module.h"
59 #include "ns3/applications-module.h"
60 #include "ns3/stats-module.h"
61 #include "ns3/flow-monitor-module.h"
62 
63 using namespace ns3;
64 using namespace std;
65 
66 NS_LOG_COMPONENT_DEFINE ("RateAdaptationDistance");
67 
68 class NodeStatistics
69 {
70 public:
72 
73  void CheckStatistics (double time);
74 
75  void RxCallback (std::string path, Ptr<const Packet> packet, const Address &from);
76  void SetPosition (Ptr<Node> node, Vector position);
77  void AdvancePosition (Ptr<Node> node, int stepsSize, int stepsTime);
78  Vector GetPosition (Ptr<Node> node);
79 
80  Gnuplot2dDataset GetDatafile ();
81 
82 private:
83  uint32_t m_bytesTotal;
84  Gnuplot2dDataset m_output;
85 };
86 
88 {
89  m_bytesTotal = 0;
90 }
91 
92 void
93 NodeStatistics::RxCallback (std::string path, Ptr<const Packet> packet, const Address &from)
94 {
95  m_bytesTotal += packet->GetSize ();
96 }
97 
98 void
100 {
101 
102 }
103 
104 void
105 NodeStatistics::SetPosition (Ptr<Node> node, Vector position)
106 {
107  Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
108  mobility->SetPosition (position);
109 }
110 
111 Vector
113 {
114  Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
115  return mobility->GetPosition ();
116 }
117 
118 void
119 NodeStatistics::AdvancePosition (Ptr<Node> node, int stepsSize, int stepsTime)
120 {
121  Vector pos = GetPosition (node);
122  double mbs = ((m_bytesTotal * 8.0) / (1000000 * stepsTime));
123  m_bytesTotal = 0;
124  m_output.Add (pos.x, mbs);
125  pos.x += stepsSize;
126  SetPosition (node, pos);
127  Simulator::Schedule (Seconds (stepsTime), &NodeStatistics::AdvancePosition, this, node, stepsSize, stepsTime);
128 }
129 
132 {
133  return m_output;
134 }
135 
136 
137 void RateCallback (std::string path, uint32_t rate, Mac48Address dest)
138 {
139  NS_LOG_INFO ((Simulator::Now ()).GetSeconds () << " " << dest << " Rate " << rate);
140 }
141 
142 int main (int argc, char *argv[])
143 {
144  uint32_t rtsThreshold = 2346;
145  std::string manager = "ns3::MinstrelWifiManager";
146  std::string outputFileName = "minstrel";
147  int ap1_x = 0;
148  int ap1_y = 0;
149  int sta1_x = 5;
150  int sta1_y = 0;
151  int steps = 200;
152  int stepsSize = 1;
153  int stepsTime = 1;
154 
155  CommandLine cmd;
156  cmd.AddValue ("manager", "PRC Manager", manager);
157  cmd.AddValue ("rtsThreshold", "RTS threshold", rtsThreshold);
158  cmd.AddValue ("outputFileName", "Output filename", outputFileName);
159  cmd.AddValue ("steps", "How many different distances to try", steps);
160  cmd.AddValue ("stepsTime", "Time on each step", stepsTime);
161  cmd.AddValue ("stepsSize", "Distance between steps", stepsSize);
162  cmd.AddValue ("AP1_x", "Position of AP1 in x coordinate", ap1_x);
163  cmd.AddValue ("AP1_y", "Position of AP1 in y coordinate", ap1_y);
164  cmd.AddValue ("STA1_x", "Position of STA1 in x coordinate", sta1_x);
165  cmd.AddValue ("STA1_y", "Position of STA1 in y coordinate", sta1_y);
166  cmd.Parse (argc, argv);
167 
168  int simuTime = steps * stepsTime;
169 
170  // Define the APs
171  NodeContainer wifiApNodes;
172  wifiApNodes.Create (1);
173 
174  //Define the STAs
175  NodeContainer wifiStaNodes;
176  wifiStaNodes.Create (1);
177 
183 
184  wifiPhy.SetChannel (wifiChannel.Create ());
185 
186  NetDeviceContainer wifiApDevices;
187  NetDeviceContainer wifiStaDevices;
188  NetDeviceContainer wifiDevices;
189 
190  //Configure the STA node
191  wifi.SetRemoteStationManager (manager, "RtsCtsThreshold", UintegerValue (rtsThreshold));
192 
193  Ssid ssid = Ssid ("AP");
194  wifiMac.SetType ("ns3::StaWifiMac",
195  "Ssid", SsidValue (ssid),
196  "ActiveProbing", BooleanValue (false));
197  wifiStaDevices.Add (wifi.Install (wifiPhy, wifiMac, wifiStaNodes.Get (0)));
198 
199  //Configure the AP node
200  wifi.SetRemoteStationManager (manager, "RtsCtsThreshold", UintegerValue (rtsThreshold));
201 
202  ssid = Ssid ("AP");
203  wifiMac.SetType ("ns3::ApWifiMac",
204  "Ssid", SsidValue (ssid));
205  wifiApDevices.Add (wifi.Install (wifiPhy, wifiMac, wifiApNodes.Get (0)));
206 
207  wifiDevices.Add (wifiStaDevices);
208  wifiDevices.Add (wifiApDevices);
209 
210  // Configure the mobility.
211  MobilityHelper mobility;
212  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
213  //Initial position of AP and STA
214  positionAlloc->Add (Vector (ap1_x, ap1_y, 0.0));
215  positionAlloc->Add (Vector (sta1_x, sta1_y, 0.0));
216  mobility.SetPositionAllocator (positionAlloc);
217  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
218  mobility.Install (wifiApNodes.Get (0));
219  mobility.Install (wifiStaNodes.Get (0));
220 
221  //Statistics counter
222  NodeStatistics atpCounter = NodeStatistics (wifiApDevices, wifiStaDevices);
223 
224  //Move the STA by stepsSize meters every stepsTime seconds
225  Simulator::Schedule (Seconds (0.5 + stepsTime), &NodeStatistics::AdvancePosition, &atpCounter, wifiStaNodes.Get (0), stepsSize, stepsTime);
226 
227  //Configure the IP stack
229  stack.Install (wifiApNodes);
230  stack.Install (wifiStaNodes);
232  address.SetBase ("10.1.1.0", "255.255.255.0");
233  Ipv4InterfaceContainer i = address.Assign (wifiDevices);
234  Ipv4Address sinkAddress = i.GetAddress (0);
235  uint16_t port = 9;
236 
237  //Configure the CBR generator
238  PacketSinkHelper sink ("ns3::UdpSocketFactory", InetSocketAddress (sinkAddress, port));
239  ApplicationContainer apps_sink = sink.Install (wifiStaNodes.Get (0));
240 
241  OnOffHelper onoff ("ns3::UdpSocketFactory", InetSocketAddress (sinkAddress, port));
242  onoff.SetConstantRate (DataRate ("54Mb/s"), 1420);
243  onoff.SetAttribute ("StartTime", TimeValue (Seconds (0.5)));
244  onoff.SetAttribute ("StopTime", TimeValue (Seconds (simuTime)));
245  ApplicationContainer apps_source = onoff.Install (wifiApNodes.Get (0));
246 
247  apps_sink.Start (Seconds (0.5));
248  apps_sink.Stop (Seconds (simuTime));
249 
250  //------------------------------------------------------------
251  //-- Setup stats and data collection
252  //--------------------------------------------
253 
254  //Register packet receptions to calculate throughput
255  Config::Connect ("/NodeList/1/ApplicationList/*/$ns3::PacketSink/Rx",
256  MakeCallback (&NodeStatistics::RxCallback, &atpCounter));
257 
258  //Callbacks to print every change of rate
259  Config::Connect ("/NodeList/0/DeviceList/*/$ns3::WifiNetDevice/RemoteStationManager/$" + manager + "/RateChange",
261 
262  Simulator::Stop (Seconds (simuTime));
263  Simulator::Run ();
264 
265  std::ofstream outfile (("throughput-" + outputFileName + ".plt").c_str ());
266  Gnuplot gnuplot = Gnuplot (("throughput-" + outputFileName + ".eps").c_str (), "Throughput");
267  gnuplot.SetTerminal ("post eps color enhanced");
268  gnuplot.SetLegend ("Time (seconds)", "Throughput (Mb/s)");
269  gnuplot.SetTitle ("Throughput (AP to STA) vs time");
270  gnuplot.AddDataset (atpCounter.GetDatafile ());
271  gnuplot.GenerateOutput (outfile);
272 
274 
275  return 0;
276 }
Custom version of log2() to deal with Bug 1467.
holds a vector of ns3::Application pointers.
an Inet address class
AttributeValue implementation for Boolean.
Definition: boolean.h:34
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:73
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.
static Vector GetPosition(Ptr< Node > node)
Definition: multirate.cc:315
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())
static void Run(void)
Run the simulation.
Definition: simulator.cc:200
#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:766
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...
static YansWifiPhyHelper Default(void)
Create a phy helper in a default working state.
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Schedule an event to expire at the relative time "time" is reached.
Definition: simulator.h:819
STL namespace.
helps to create WifiNetDevice objects
Definition: wifi-helper.h:92
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
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:102
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.
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:928
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:96
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1296
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 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:201
void Connect(std::string path, const CallbackBase &cb)
Definition: config.cc:744
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:164
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.
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
manage and create wifi channel objects for the yans model.
static Time Now(void)
Return the current simulation virtual time.
Definition: simulator.cc:223
void SetPosition(const Vector &position)
tuple stack
Definition: first.py:34
The IEEE 802.11 SSID Information Element.
Definition: ssid.h:37
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:491
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:208
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:866
AttributeValue implementation for Ssid.
Definition: ssid.h:93
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.
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
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:455
Vector GetPosition(Ptr< Node > node)
void RateCallback(std::string path, uint32_t rate, Mac48Address dest)
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
static WifiHelper Default(void)
Definition: wifi-helper.cc:65