A Discrete-Event Network Simulator
API
wifi-adhoc.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2005,2006,2007 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
19  */
20 
21 #include "ns3/core-module.h"
22 #include "ns3/network-module.h"
23 #include "ns3/applications-module.h"
24 #include "ns3/mobility-module.h"
25 #include "ns3/stats-module.h"
26 #include "ns3/wifi-module.h"
27 
28 #include <iostream>
29 
30 using namespace ns3;
31 
33 
34 class Experiment
35 {
36 public:
37  Experiment ();
38  Experiment (std::string name);
39  Gnuplot2dDataset Run (const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy,
40  const WifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel);
41 private:
42  void ReceivePacket (Ptr<Socket> socket);
43  void SetPosition (Ptr<Node> node, Vector position);
44  Vector GetPosition (Ptr<Node> node);
45  void AdvancePosition (Ptr<Node> node);
47 
48  uint32_t m_bytesTotal;
49  Gnuplot2dDataset m_output;
50 };
51 
53 {
54 }
55 
56 Experiment::Experiment (std::string name)
57  : m_output (name)
58 {
59  m_output.SetStyle (Gnuplot2dDataset::LINES);
60 }
61 
62 void
64 {
66  mobility->SetPosition (position);
67 }
68 
69 Vector
71 {
73  return mobility->GetPosition ();
74 }
75 
76 void
78 {
79  Vector pos = GetPosition (node);
80  double mbs = ((m_bytesTotal * 8.0) / 1000000);
81  m_bytesTotal = 0;
82  m_output.Add (pos.x, mbs);
83  pos.x += 1.0;
84  if (pos.x >= 210.0)
85  {
86  return;
87  }
88  SetPosition (node, pos);
89  //std::cout << "x="<<pos.x << std::endl;
90  Simulator::Schedule (Seconds (1.0), &Experiment::AdvancePosition, this, node);
91 }
92 
93 void
95 {
96  Ptr<Packet> packet;
97  while ((packet = socket->Recv ()))
98  {
99  m_bytesTotal += packet->GetSize ();
100  }
101 }
102 
105 {
106  TypeId tid = TypeId::LookupByName ("ns3::PacketSocketFactory");
107  Ptr<Socket> sink = Socket::CreateSocket (node, tid);
108  sink->Bind ();
110  return sink;
111 }
112 
115  const WifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel)
116 {
117  m_bytesTotal = 0;
118 
119  NodeContainer c;
120  c.Create (2);
121 
122  PacketSocketHelper packetSocket;
123  packetSocket.Install (c);
124 
125  YansWifiPhyHelper phy = wifiPhy;
126  phy.SetChannel (wifiChannel.Create ());
127 
128  WifiMacHelper mac = wifiMac;
129  NetDeviceContainer devices = wifi.Install (phy, mac, c);
130 
132  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
133  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
134  positionAlloc->Add (Vector (5.0, 0.0, 0.0));
135  mobility.SetPositionAllocator (positionAlloc);
136  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
137 
138  mobility.Install (c);
139 
140  PacketSocketAddress socket;
141  socket.SetSingleDevice (devices.Get (0)->GetIfIndex ());
142  socket.SetPhysicalAddress (devices.Get (1)->GetAddress ());
143  socket.SetProtocol (1);
144 
145  OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
146  onoff.SetConstantRate (DataRate (60000000));
147  onoff.SetAttribute ("PacketSize", UintegerValue (2000));
148 
149  ApplicationContainer apps = onoff.Install (c.Get (0));
150  apps.Start (Seconds (0.5));
151  apps.Stop (Seconds (250.0));
152 
153  Simulator::Schedule (Seconds (1.5), &Experiment::AdvancePosition, this, c.Get (1));
154  Ptr<Socket> recvSink = SetupPacketReceive (c.Get (1));
155 
156  Simulator::Run ();
157 
158  Simulator::Destroy ();
159 
160  return m_output;
161 }
162 
163 int main (int argc, char *argv[])
164 {
165  // disable fragmentation
166  Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
167  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200"));
168 
170  cmd.Parse (argc, argv);
171 
172  Gnuplot gnuplot = Gnuplot ("reference-rates.png");
173 
177  WifiMacHelper wifiMac;
178  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
179  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
180  Gnuplot2dDataset dataset;
181 
182  wifiMac.SetType ("ns3::AdhocWifiMac");
183 
184  NS_LOG_DEBUG ("54");
185  experiment = Experiment ("54mb");
186  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
187  "DataMode", StringValue ("OfdmRate54Mbps"));
188  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
189  gnuplot.AddDataset (dataset);
190 
191  NS_LOG_DEBUG ("48");
192  experiment = Experiment ("48mb");
193  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
194  "DataMode", StringValue ("OfdmRate48Mbps"));
195  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
196  gnuplot.AddDataset (dataset);
197 
198  NS_LOG_DEBUG ("36");
199  experiment = Experiment ("36mb");
200  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
201  "DataMode", StringValue ("OfdmRate36Mbps"));
202  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
203  gnuplot.AddDataset (dataset);
204 
205  NS_LOG_DEBUG ("24");
206  experiment = Experiment ("24mb");
207  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
208  "DataMode", StringValue ("OfdmRate24Mbps"));
209  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
210  gnuplot.AddDataset (dataset);
211 
212  NS_LOG_DEBUG ("18");
213  experiment = Experiment ("18mb");
214  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
215  "DataMode", StringValue ("OfdmRate18Mbps"));
216  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
217  gnuplot.AddDataset (dataset);
218 
219  NS_LOG_DEBUG ("12");
220  experiment = Experiment ("12mb");
221  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
222  "DataMode", StringValue ("OfdmRate12Mbps"));
223  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
224  gnuplot.AddDataset (dataset);
225 
226  NS_LOG_DEBUG ("9");
227  experiment = Experiment ("9mb");
228  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
229  "DataMode", StringValue ("OfdmRate9Mbps"));
230  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
231  gnuplot.AddDataset (dataset);
232 
233  NS_LOG_DEBUG ("6");
234  experiment = Experiment ("6mb");
235  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
236  "DataMode", StringValue ("OfdmRate6Mbps"));
237  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
238  gnuplot.AddDataset (dataset);
239 
240  gnuplot.GenerateOutput (std::cout);
241 
242 
243  gnuplot = Gnuplot ("rate-control.png");
245 
246 
247  NS_LOG_DEBUG ("arf");
248  experiment = Experiment ("arf");
249  wifi.SetRemoteStationManager ("ns3::ArfWifiManager");
250  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
251  gnuplot.AddDataset (dataset);
252 
253  NS_LOG_DEBUG ("aarf");
254  experiment = Experiment ("aarf");
255  wifi.SetRemoteStationManager ("ns3::AarfWifiManager");
256  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
257  gnuplot.AddDataset (dataset);
258 
259  NS_LOG_DEBUG ("aarf-cd");
260  experiment = Experiment ("aarf-cd");
261  wifi.SetRemoteStationManager ("ns3::AarfcdWifiManager");
262  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
263  gnuplot.AddDataset (dataset);
264 
265  NS_LOG_DEBUG ("cara");
266  experiment = Experiment ("cara");
267  wifi.SetRemoteStationManager ("ns3::CaraWifiManager");
268  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
269  gnuplot.AddDataset (dataset);
270 
271  NS_LOG_DEBUG ("rraa");
272  experiment = Experiment ("rraa");
273  wifi.SetRemoteStationManager ("ns3::RraaWifiManager");
274  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
275  gnuplot.AddDataset (dataset);
276 
277  NS_LOG_DEBUG ("ideal");
278  experiment = Experiment ("ideal");
279  wifi.SetRemoteStationManager ("ns3::IdealWifiManager");
280  dataset = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
281  gnuplot.AddDataset (dataset);
282 
283  gnuplot.GenerateOutput (std::cout);
284 
285  return 0;
286 }
Helper class for UAN CW MAC example.
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:47
holds a vector of ns3::Application pointers.
void experiment(bool enableCtsRts)
Run single 10 seconds experiment with enabled or disabled RTS/CTS mechanism.
uint32_t m_bytesTotal
Total bytes received.
Definition: wifi-adhoc.cc:48
tuple devices
Definition: first.py:32
Vector GetPosition(Ptr< Node > node)
Definition: wifi-adhoc.cc:70
static void AdvancePosition(Ptr< Node > node)
Definition: wifi-ap.cc:100
Class to represent a 2D points plot.
Definition: gnuplot.h:113
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
Hold variables of type string.
Definition: string.h:41
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Make it easy to create and manage PHY objects for the yans model.
static Vector GetPosition(Ptr< Node > node)
Definition: multirate.cc:315
an address for a packet socket
void ReceivePacket(Ptr< Socket > socket)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:201
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
helps to create WifiNetDevice objects
Definition: wifi-helper.h:231
void ReceivePacket(Ptr< Socket > socket)
Definition: multirate.cc:165
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
Give ns3::PacketSocket powers to ns3::Node.
tuple cmd
Definition: second.py:35
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
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
a polymophic address class
Definition: address.h:90
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
tuple phy
Definition: third.py:86
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:367
Hold an unsigned integer type.
Definition: uinteger.h:44
Vector3D Vector
Definition: vector.h:166
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
double Run(bool enableProtection, bool enableShortSlotTime, bool enableShortPlcpPreamble, bool isMixed, bool isUdp, uint32_t payloadSize, uint32_t simulationTime)
void Add(double x, double y)
Definition: gnuplot.cc:359
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:128
tuple mac
Definition: third.py:92
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
This is intended to be the configuration used in this paper: Gavin Holland, Nitin Vaidya and Paramvir...
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
OFDM PHY for the 5 GHz band (Clause 17)
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SetConstantRate(DataRate dataRate, uint32_t packetSize=512)
Helper function to set a constant rate source.
void SetPhysicalAddress(const Address address)
Set the destination address.
keep track of a set of node pointers.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
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())
Gnuplot2dDataset m_output
Definition: multirate.cc:102
manage and create wifi channel objects for the yans model.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
create MAC layers for a ns3::WifiNetDevice.
void SetStyle(enum Style style)
Definition: gnuplot.cc:342
void SetPosition(const Vector &position)
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.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:236
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:774
void SetProtocol(uint16_t protocol)
Set the protocol.
void Add(Vector v)
Add a position to the list of positions.
void Parse(int argc, char *argv[])
Parse the program arguments.
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.
void AdvancePosition(Ptr< Node > node)
Definition: wifi-adhoc.cc:77
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
void SetPosition(Ptr< Node > node, Vector position)
Definition: wifi-adhoc.cc:63
a unique identifier for an interface.
Definition: type-id.h:58
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
Definition: multirate.cc:153
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.