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 }