A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
wifi-clear-channel-cmu.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 The Boeing Company
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Guangyu Pei <guangyu.pei@boeing.com>
7 */
8
9#include "ns3/command-line.h"
10#include "ns3/config.h"
11#include "ns3/double.h"
12#include "ns3/gnuplot.h"
13#include "ns3/internet-stack-helper.h"
14#include "ns3/ipv4-address-helper.h"
15#include "ns3/log.h"
16#include "ns3/mobility-helper.h"
17#include "ns3/mobility-model.h"
18#include "ns3/string.h"
19#include "ns3/yans-wifi-channel.h"
20#include "ns3/yans-wifi-helper.h"
21
22using namespace ns3;
23
24NS_LOG_COMPONENT_DEFINE("ClearChannelCmu");
25
26/**
27 * WiFi clear channel cmu experiment class.
28 *
29 * It handles the creation and run of an experiment.
30 */
31class Experiment
32{
33 public:
35 /**
36 * Constructor.
37 * \param name The name of the experiment.
38 */
39 Experiment(std::string name);
40 /**
41 * Run an experiment.
42 * \param wifi //!< The WifiHelper class.
43 * \param wifiPhy //!< The YansWifiPhyHelper class.
44 * \param wifiMac //!< The WifiMacHelper class.
45 * \param wifiChannel //!< The YansWifiChannelHelper class.
46 * \return the number of received packets.
47 */
48 uint32_t Run(const WifiHelper& wifi,
49 const YansWifiPhyHelper& wifiPhy,
50 const WifiMacHelper& wifiMac,
51 const YansWifiChannelHelper& wifiChannel);
52
53 private:
54 /**
55 * Receive a packet.
56 * \param socket The receiving socket.
57 */
59 /**
60 * Set the position of a node.
61 * \param node The node.
62 * \param position The position of the node.
63 */
64 void SetPosition(Ptr<Node> node, Vector position);
65 /**
66 * Get the position of a node.
67 * \param node The node.
68 * \return the position of the node.
69 */
70 Vector GetPosition(Ptr<Node> node);
71 /**
72 * Setup the receiving socket.
73 * \param node The receiving node.
74 * \return the socket.
75 */
77 /**
78 * Generate the traffic.
79 * \param socket The sending socket.
80 * \param pktSize The packet size.
81 * \param pktCount The number of packets to send.
82 * \param pktInterval The time between packets.
83 */
84 void GenerateTraffic(Ptr<Socket> socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval);
85
86 uint32_t m_pktsTotal; //!< Total number of received packets
87 Gnuplot2dDataset m_output; //!< Output dataset.
88};
89
91{
92}
93
94Experiment::Experiment(std::string name)
95 : m_output(name)
96{
98}
99
100void
101Experiment::SetPosition(Ptr<Node> node, Vector position)
102{
103 Ptr<MobilityModel> mobility = node->GetObject<MobilityModel>();
104 mobility->SetPosition(position);
105}
106
107Vector
109{
110 Ptr<MobilityModel> mobility = node->GetObject<MobilityModel>();
111 return mobility->GetPosition();
112}
113
114void
116{
117 Ptr<Packet> packet;
118 while ((packet = socket->Recv()))
119 {
120 m_pktsTotal++;
121 }
122}
123
126{
127 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
130 sink->Bind(local);
131 sink->SetRecvCallback(MakeCallback(&Experiment::ReceivePacket, this));
132 return sink;
133}
134
135void
138 uint32_t pktCount,
139 Time pktInterval)
140{
141 if (pktCount > 0)
142 {
143 socket->Send(Create<Packet>(pktSize));
144 Simulator::Schedule(pktInterval,
146 this,
147 socket,
148 pktSize,
149 pktCount - 1,
150 pktInterval);
151 }
152 else
153 {
154 socket->Close();
155 }
156}
157
159Experiment::Run(const WifiHelper& wifi,
160 const YansWifiPhyHelper& wifiPhy,
161 const WifiMacHelper& wifiMac,
162 const YansWifiChannelHelper& wifiChannel)
163{
164 m_pktsTotal = 0;
165
167 c.Create(2);
168
169 InternetStackHelper internet;
170 internet.Install(c);
171
172 YansWifiPhyHelper phy = wifiPhy;
173 phy.SetChannel(wifiChannel.Create());
174
175 NetDeviceContainer devices = wifi.Install(phy, wifiMac, c);
176
177 MobilityHelper mobility;
179 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
180 positionAlloc->Add(Vector(5.0, 0.0, 0.0));
181 mobility.SetPositionAllocator(positionAlloc);
182 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
183 mobility.Install(c);
184
186 NS_LOG_INFO("Assign IP Addresses.");
187 ipv4.SetBase("10.1.1.0", "255.255.255.0");
188 Ipv4InterfaceContainer i = ipv4.Assign(devices);
189
190 Ptr<Socket> recvSink = SetupPacketReceive(c.Get(0));
191
192 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
193 Ptr<Socket> source = Socket::CreateSocket(c.Get(1), tid);
194 InetSocketAddress remote = InetSocketAddress(Ipv4Address("255.255.255.255"), 80);
195 source->SetAllowBroadcast(true);
196 source->Connect(remote);
197 uint32_t packetSize = 1014;
198 uint32_t maxPacketCount = 200;
199 Time interPacketInterval = Seconds(1.);
202 this,
203 source,
205 maxPacketCount,
206 interPacketInterval);
208
210
211 return m_pktsTotal;
212}
213
214int
215main(int argc, char* argv[])
216{
217 std::ofstream outfile("clear-channel.plt");
218
219 const std::vector<std::string> modes{
220 "DsssRate1Mbps",
221 "DsssRate2Mbps",
222 "DsssRate5_5Mbps",
223 "DsssRate11Mbps",
224 };
225
226 CommandLine cmd(__FILE__);
227 cmd.Parse(argc, argv);
228
229 Gnuplot gnuplot = Gnuplot("clear-channel.eps");
230
231 for (uint32_t i = 0; i < modes.size(); i++)
232 {
233 std::cout << modes[i] << std::endl;
234 Gnuplot2dDataset dataset(modes[i]);
235
236 for (double rss = -102.0; rss <= -80.0; rss += 0.5)
237 {
239 dataset.SetStyle(Gnuplot2dDataset::LINES);
240
242 wifi.SetStandard(WIFI_STANDARD_80211b);
243 WifiMacHelper wifiMac;
244 Config::SetDefault("ns3::WifiRemoteStationManager::NonUnicastMode",
245 StringValue(modes[i]));
246 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
247 "DataMode",
248 StringValue(modes[i]),
249 "ControlMode",
250 StringValue(modes[i]));
251 wifiMac.SetType("ns3::AdhocWifiMac");
252
253 YansWifiPhyHelper wifiPhy;
254 YansWifiChannelHelper wifiChannel;
255 wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
256 wifiChannel.AddPropagationLoss("ns3::FixedRssLossModel", "Rss", DoubleValue(rss));
257
258 NS_LOG_DEBUG(modes[i]);
259 experiment = Experiment(modes[i]);
260 wifiPhy.Set("TxPowerStart", DoubleValue(15.0));
261 wifiPhy.Set("TxPowerEnd", DoubleValue(15.0));
262 wifiPhy.Set("RxGain", DoubleValue(0));
263 wifiPhy.Set("RxNoiseFigure", DoubleValue(7));
264 uint32_t pktsRecvd = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
265 dataset.Add(rss, pktsRecvd);
266 }
267
268 gnuplot.AddDataset(dataset);
269 }
270 gnuplot.SetTerminal("postscript eps color enh \"Times-BoldItalic\"");
271 gnuplot.SetLegend("RSS(dBm)", "Number of packets received");
272 gnuplot.SetExtra("set xrange [-102:-83]");
273 gnuplot.GenerateOutput(outfile);
274 outfile.close();
275
276 return 0;
277}
WiFi adhoc experiment class.
Definition wifi-adhoc.cc:34
uint32_t Run(const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy, const WifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel)
Run an experiment.
void GenerateTraffic(Ptr< Socket > socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval)
Generate the traffic.
void ReceivePacket(Ptr< Socket > socket)
Receive a packet.
void SetPosition(Ptr< Node > node, Vector position)
Set the position of a node.
Gnuplot2dDataset m_output
The output dataset.
Definition wifi-adhoc.cc:87
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
Setup the receiving socket.
uint32_t m_pktsTotal
Total number of received packets.
Experiment(std::string name)
Constructor.
Vector GetPosition(Ptr< Node > node)
Get the position of a node.
Parse command-line arguments.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
Class to represent a 2D points plot.
Definition gnuplot.h:105
void SetStyle(Style style)
Definition gnuplot.cc:348
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition gnuplot.h:359
void AddDataset(const GnuplotDataset &dataset)
Definition gnuplot.cc:785
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition gnuplot.cc:765
void SetTerminal(const std::string &terminal)
Definition gnuplot.cc:753
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition gnuplot.cc:791
void SetExtra(const std::string &extra)
Definition gnuplot.cc:772
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
Ipv4 addresses are stored in host order in this class.
static Ipv4Address GetAny()
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Helper class used to assign positions and mobility models to nodes.
Keep track of the current position and velocity of an object.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Smart pointer class similar to boost::intrusive_ptr.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
This method wraps the creation of sockets that is performed on a given node by a SocketFactory specif...
Definition socket.cc:61
Hold variables of type string.
Definition string.h:45
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
a unique identifier for an interface.
Definition type-id.h:48
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:872
helps to create WifiNetDevice objects
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
void Set(std::string name, const AttributeValue &v)
manage and create wifi channel objects for the YANS model.
void SetPropagationDelay(std::string name, Ts &&... args)
void AddPropagationLoss(std::string name, Ts &&... args)
Ptr< YansWifiChannel > Create() const
Make it easy to create and manage PHY objects for the YANS model.
void experiment(std::string queue_disc_type)
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:883
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:264
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
@ WIFI_STANDARD_80211b
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Callback< R, Args... > MakeCallback(R(T::*memPtr)(Args...), OBJ objPtr)
Build Callbacks for class method members which take varying numbers of arguments and potentially retu...
Definition callback.h:684
wifi
Definition third.py:84
mobility
Definition third.py:92
uint32_t pktSize
packet size used for the simulation (in bytes)
static const uint32_t packetSize
Packet size generated at the AP.
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44