A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
wifi-clear-channel-cmu.cc
Go to the documentation of this file.
1 /* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 The Boeing Company
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: Guangyu Pei <guangyu.pei@boeing.com>
19  */
20 
21 #include "ns3/core-module.h"
22 #include "ns3/network-module.h"
23 #include "ns3/mobility-module.h"
24 #include "ns3/stats-module.h"
25 #include "ns3/wifi-module.h"
26 #include "ns3/internet-module.h"
27 
28 #include <iostream>
29 #include <fstream>
30 #include <vector>
31 #include <string>
32 
34 
35 using namespace ns3;
36 
37 class Experiment
38 {
39 public:
40  Experiment ();
41  Experiment (std::string name);
42  uint32_t Run (const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy,
43  const NqosWifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel);
44 private:
45  void ReceivePacket (Ptr<Socket> socket);
46  void SetPosition (Ptr<Node> node, Vector position);
49  void GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize,
50  uint32_t pktCount, Time pktInterval );
51 
52  uint32_t m_pktsTotal;
53  Gnuplot2dDataset m_output;
54 };
55 
57 {
58 }
59 
60 Experiment::Experiment (std::string name)
61  : m_output (name)
62 {
63  m_output.SetStyle (Gnuplot2dDataset::LINES);
64 }
65 
66 void
68 {
69  Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
70  mobility->SetPosition (position);
71 }
72 
73 Vector
75 {
76  Ptr<MobilityModel> mobility = node->GetObject<MobilityModel> ();
77  return mobility->GetPosition ();
78 }
79 
80 void
82 {
83  Ptr<Packet> packet;
84  while ((packet = socket->Recv ()))
85  {
86  m_pktsTotal++;
87  }
88 }
89 
92 {
93  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
94  Ptr<Socket> sink = Socket::CreateSocket (node, tid);
95  InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80);
96  sink->Bind (local);
98  return sink;
99 }
100 
101 void
102 Experiment::GenerateTraffic (Ptr<Socket> socket, uint32_t pktSize,
103  uint32_t pktCount, Time pktInterval )
104 {
105  if (pktCount > 0)
106  {
107  socket->Send (Create<Packet> (pktSize));
108  Simulator::Schedule (pktInterval, &Experiment::GenerateTraffic, this,
109  socket, pktSize,pktCount-1, pktInterval);
110  }
111  else
112  {
113  socket->Close ();
114  }
115 }
116 
117 uint32_t
118 Experiment::Run (const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy,
119  const NqosWifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel)
120 {
121  m_pktsTotal = 0;
122 
123  NodeContainer c;
124  c.Create (2);
125 
126  InternetStackHelper internet;
127  internet.Install (c);
128 
129  YansWifiPhyHelper phy = wifiPhy;
130  phy.SetChannel (wifiChannel.Create ());
131 
132  NqosWifiMacHelper mac = wifiMac;
133  NetDeviceContainer devices = wifi.Install (phy, mac, c);
134 
135  MobilityHelper mobility;
136  Ptr<ListPositionAllocator> positionAlloc = CreateObject<ListPositionAllocator> ();
137  positionAlloc->Add (Vector (0.0, 0.0, 0.0));
138  positionAlloc->Add (Vector (5.0, 0.0, 0.0));
139  mobility.SetPositionAllocator (positionAlloc);
140  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
141  mobility.Install (c);
142 
143  Ipv4AddressHelper ipv4;
144  NS_LOG_INFO ("Assign IP Addresses.");
145  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
146  Ipv4InterfaceContainer i = ipv4.Assign (devices);
147 
148  Ptr<Socket> recvSink = SetupPacketReceive (c.Get (0));
149 
150  TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
151  Ptr<Socket> source = Socket::CreateSocket (c.Get (1), tid);
152  InetSocketAddress remote = InetSocketAddress (Ipv4Address ("255.255.255.255"), 80);
153  source->SetAllowBroadcast (true);
154  source->Connect (remote);
155  uint32_t packetSize = 1014;
156  uint32_t maxPacketCount = 200;
157  Time interPacketInterval = Seconds (1.);
158  Simulator::Schedule (Seconds (1.0), &Experiment::GenerateTraffic,
159  this, source, packetSize, maxPacketCount,interPacketInterval);
160  Simulator::Run ();
161 
162  Simulator::Destroy ();
163 
164  return m_pktsTotal;
165 }
166 
167 int main (int argc, char *argv[])
168 {
169  std::ofstream outfile ("clear-channel.plt");
170  std::vector <std::string> modes;
171 
172  modes.push_back ("DsssRate1Mbps");
173  modes.push_back ("DsssRate2Mbps");
174  modes.push_back ("DsssRate5_5Mbps");
175  modes.push_back ("DsssRate11Mbps");
176  // disable fragmentation
177  Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
178  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200"));
179 
180  CommandLine cmd;
181  cmd.Parse (argc, argv);
182 
183  Gnuplot gnuplot = Gnuplot ("clear-channel.eps");
184 
185  for (uint32_t i = 0; i < modes.size (); i++)
186  {
187  std::cout << modes[i] << std::endl;
188  Gnuplot2dDataset dataset (modes[i]);
189 
190  for (double rss = -102.0; rss <= -80.0; rss += 0.5)
191  {
193  dataset.SetStyle (Gnuplot2dDataset::LINES);
194 
195  WifiHelper wifi;
197  NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
198  Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode",
199  StringValue (modes[i]));
200  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
201  "DataMode",StringValue (modes[i]),
202  "ControlMode",StringValue (modes[i]));
203  wifiMac.SetType ("ns3::AdhocWifiMac");
204 
205  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
206  YansWifiChannelHelper wifiChannel;
207  wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
208  wifiChannel.AddPropagationLoss ("ns3::FixedRssLossModel","Rss",DoubleValue (rss));
209 
210 
211  NS_LOG_DEBUG (modes[i]);
212  experiment = Experiment (modes[i]);
213  wifiPhy.Set ("EnergyDetectionThreshold", DoubleValue (-110.0) );
214  wifiPhy.Set ("CcaMode1Threshold", DoubleValue (-110.0) );
215  wifiPhy.Set ("TxPowerStart", DoubleValue (15.0) );
216  wifiPhy.Set ("TxPowerEnd", DoubleValue (15.0) );
217  wifiPhy.Set ("RxGain", DoubleValue (0) );
218  wifiPhy.Set ("RxNoiseFigure", DoubleValue (7) );
219  uint32_t pktsRecvd = experiment.Run (wifi, wifiPhy, wifiMac, wifiChannel);
220  dataset.Add (rss, pktsRecvd);
221  }
222 
223  gnuplot.AddDataset (dataset);
224  }
225  gnuplot.SetTerminal ("postscript eps color enh \"Times-BoldItalic\"");
226  gnuplot.SetLegend ("RSS(dBm)", "Number of packets received");
227  gnuplot.SetExtra ("set xrange [-102:-83]");
228  gnuplot.GenerateOutput (outfile);
229  outfile.close ();
230 
231  return 0;
232 }
void AddPropagationLoss(std::string name, 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())
Helper class for UAN CW MAC example.
Definition: multirate.cc:71
void experiment(bool enableCtsRts)
Run single 10 seconds experiment with enabled or disabled RTS/CTS mechanism.
void GenerateTraffic(Ptr< Socket > socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval)
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:79
an Inet address class
tuple devices
Definition: first.py:32
Vector GetPosition(Ptr< Node > node)
Definition: wifi-adhoc.cc:70
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
hold variables of type string
Definition: string.h:18
virtual bool SetAllowBroadcast(bool allowBroadcast)=0
Configure whether broadcast datagram transmissions are allowed.
Make it easy to create and manage PHY objects for the yans model.
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 GenerateTraffic(Ptr< Socket > socket, uint32_t pktSize, Ptr< Node > n, uint32_t pktCount, Time pktInterval)
void ReceivePacket(Ptr< Socket > socket)
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:170
aggregate IP/TCP/UDP functionality to existing Nodes.
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:223
void Set(std::string name, const AttributeValue &v)
uint32_t packetSize
Definition: multirate.cc:109
helps to create WifiNetDevice objects
Definition: wifi-helper.h:88
void ReceivePacket(Ptr< Socket > socket)
Definition: multirate.cc:165
a 3d vector
Definition: vector.h:31
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer c) const
Definition: wifi-helper.cc:102
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.
int main(int argc, char *argv[])
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:367
Gnuplot2dDataset Run(const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy, const NqosWifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel, const MobilityHelper &mobility)
Definition: multirate.cc:373
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:1242
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:762
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:127
create non QoS-enabled MAC layers for a ns3::WifiNetDevice.
Parse command-line arguments.
Definition: command-line.h:177
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition: gnuplot.cc:736
virtual int Connect(const Address &address)=0
Initiate a connection to a remote host.
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:667
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
keep track of a set of node pointers.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
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())
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
Gnuplot2dDataset m_output
Definition: multirate.cc:102
manage and create wifi channel objects for the yans model.
void SetStyle(enum Style style)
Definition: gnuplot.cc:342
void SetExtra(const std::string &extra)
Definition: gnuplot.cc:743
void SetPosition(const Vector &position)
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:38
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
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:213
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.
static void SetPosition(Ptr< Node > node, Vector position)
void SetPropagationDelay(std::string name, 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())
virtual int Send(Ptr< Packet > p, uint32_t flags)=0
Send data (or dummy data) to the remote host.
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
virtual int Close(void)=0
Close a socket.
void SetTerminal(const std::string &terminal)
Definition: gnuplot.cc:724
Hold a floating point type.
Definition: double.h:41
Ptr< T > GetObject(void) const
Definition: object.h:362
void SetPosition(Ptr< Node > node, Vector position)
Definition: wifi-adhoc.cc:63
a unique identifier for an interface.
Definition: type-id.h:49
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
Definition: multirate.cc:153
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.