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
22/**
23 * @file
24 * @ingroup wifi
25 * Clear channel example
26 */
27
28using namespace ns3;
29
30NS_LOG_COMPONENT_DEFINE("ClearChannelCmu");
31
32/** Unnamed namespace, to disambiguate class Experiment. */
33namespace
34{
35
36/**
37 * WiFi clear channel cmu experiment class.
38 *
39 * It handles the creation and run of an experiment.
40 */
42{
43 public:
44 Experiment();
45 /**
46 * Constructor.
47 * @param name The name of the experiment.
48 */
49 Experiment(std::string name);
50 /**
51 * Run an experiment.
52 * @param wifi //!< The WifiHelper class.
53 * @param wifiPhy //!< The YansWifiPhyHelper class.
54 * @param wifiMac //!< The WifiMacHelper class.
55 * @param wifiChannel //!< The YansWifiChannelHelper class.
56 * @return the number of received packets.
57 */
58 uint32_t Run(const WifiHelper& wifi,
59 const YansWifiPhyHelper& wifiPhy,
60 const WifiMacHelper& wifiMac,
61 const YansWifiChannelHelper& wifiChannel);
62
63 private:
64 /**
65 * Receive a packet.
66 * @param socket The receiving socket.
67 */
68 void ReceivePacket(Ptr<Socket> socket);
69 /**
70 * Set the position of a node.
71 * @param node The node.
72 * @param position The position of the node.
73 */
74 void SetPosition(Ptr<Node> node, Vector position);
75 /**
76 * Get the position of a node.
77 * @param node The node.
78 * @return the position of the node.
79 */
80 Vector GetPosition(Ptr<Node> node);
81 /**
82 * Setup the receiving socket.
83 * @param node The receiving node.
84 * @return the socket.
85 */
87 /**
88 * Generate the traffic.
89 * @param socket The sending socket.
90 * @param pktSize The packet size.
91 * @param pktCount The number of packets to send.
92 * @param pktInterval The time between packets.
93 */
94 void GenerateTraffic(Ptr<Socket> socket, uint32_t pktSize, uint32_t pktCount, Time pktInterval);
95
96 uint32_t m_pktsTotal; //!< Total number of received packets
97 Gnuplot2dDataset m_output; //!< Output dataset.
98};
99
103
104Experiment::Experiment(std::string name)
105 : m_output(name)
106{
108}
109
110[[maybe_unused]] void
111Experiment::SetPosition(Ptr<Node> node, Vector position)
112{
113 Ptr<MobilityModel> mobility = node->GetObject<MobilityModel>();
114 mobility->SetPosition(position);
115}
116
117[[maybe_unused]] Vector
119{
120 Ptr<MobilityModel> mobility = node->GetObject<MobilityModel>();
121 return mobility->GetPosition();
122}
123
124void
126{
127 Ptr<Packet> packet;
128 while ((packet = socket->Recv()))
129 {
130 m_pktsTotal++;
131 }
132}
133
136{
137 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
140 sink->Bind(local);
141 sink->SetRecvCallback(MakeCallback(&Experiment::ReceivePacket, this));
142 return sink;
143}
144
145void
147 uint32_t pktSize,
148 uint32_t pktCount,
149 Time pktInterval)
150{
151 if (pktCount > 0)
152 {
153 socket->Send(Create<Packet>(pktSize));
154 Simulator::Schedule(pktInterval,
156 this,
157 socket,
158 pktSize,
159 pktCount - 1,
160 pktInterval);
161 }
162 else
163 {
164 socket->Close();
165 }
166}
167
170 const YansWifiPhyHelper& wifiPhy,
171 const WifiMacHelper& wifiMac,
172 const YansWifiChannelHelper& wifiChannel)
173{
174 m_pktsTotal = 0;
175
177 c.Create(2);
178
179 InternetStackHelper internet;
180 internet.Install(c);
181
182 YansWifiPhyHelper phy = wifiPhy;
183 phy.SetChannel(wifiChannel.Create());
184
185 NetDeviceContainer devices = wifi.Install(phy, wifiMac, c);
186
187 MobilityHelper mobility;
189 positionAlloc->Add(Vector(0.0, 0.0, 0.0));
190 positionAlloc->Add(Vector(5.0, 0.0, 0.0));
191 mobility.SetPositionAllocator(positionAlloc);
192 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
193 mobility.Install(c);
194
196 NS_LOG_INFO("Assign IP Addresses.");
197 ipv4.SetBase("10.1.1.0", "255.255.255.0");
198 Ipv4InterfaceContainer i = ipv4.Assign(devices);
199
200 Ptr<Socket> recvSink = SetupPacketReceive(c.Get(0));
201
202 TypeId tid = TypeId::LookupByName("ns3::UdpSocketFactory");
203 Ptr<Socket> source = Socket::CreateSocket(c.Get(1), tid);
204 InetSocketAddress remote = InetSocketAddress(Ipv4Address("255.255.255.255"), 80);
205 source->SetAllowBroadcast(true);
206 source->Connect(remote);
207 uint32_t packetSize = 1014;
208 uint32_t maxPacketCount = 200;
209 Time interPacketInterval = Seconds(1.);
212 this,
213 source,
215 maxPacketCount,
216 interPacketInterval);
218
220
221 return m_pktsTotal;
222}
223
224} // unnamed namespace
225
226int
227main(int argc, char* argv[])
228{
229 std::ofstream outfile("clear-channel.plt");
230
231 const std::vector<std::string> modes{
232 "DsssRate1Mbps",
233 "DsssRate2Mbps",
234 "DsssRate5_5Mbps",
235 "DsssRate11Mbps",
236 };
237
238 CommandLine cmd(__FILE__);
239 cmd.Parse(argc, argv);
240
241 Gnuplot gnuplot = Gnuplot("clear-channel.eps");
242
243 for (uint32_t i = 0; i < modes.size(); i++)
244 {
245 std::cout << modes[i] << std::endl;
246 Gnuplot2dDataset dataset(modes[i]);
247
248 for (double rss = -102.0; rss <= -80.0; rss += 0.5)
249 {
251 dataset.SetStyle(Gnuplot2dDataset::LINES);
252
253 WifiHelper wifi;
254 wifi.SetStandard(WIFI_STANDARD_80211b);
255 WifiMacHelper wifiMac;
256 Config::SetDefault("ns3::WifiRemoteStationManager::NonUnicastMode",
257 StringValue(modes[i]));
258 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
259 "DataMode",
260 StringValue(modes[i]),
261 "ControlMode",
262 StringValue(modes[i]));
263 wifiMac.SetType("ns3::AdhocWifiMac");
264
265 YansWifiPhyHelper wifiPhy;
266 YansWifiChannelHelper wifiChannel;
267 wifiChannel.SetPropagationDelay("ns3::ConstantSpeedPropagationDelayModel");
268 wifiChannel.AddPropagationLoss("ns3::FixedRssLossModel", "Rss", DoubleValue(rss));
269
270 NS_LOG_DEBUG(modes[i]);
271 experiment = Experiment(modes[i]);
272 wifiPhy.Set("TxPowerStart", DoubleValue(15.0));
273 wifiPhy.Set("TxPowerEnd", DoubleValue(15.0));
274 wifiPhy.Set("RxGain", DoubleValue(0));
275 wifiPhy.Set("RxNoiseFigure", DoubleValue(7));
276 uint32_t pktsRecvd = experiment.Run(wifi, wifiPhy, wifiMac, wifiChannel);
277 dataset.Add(rss, pktsRecvd);
278 }
279
280 gnuplot.AddDataset(dataset);
281 }
282 gnuplot.SetTerminal("postscript eps color enh \"Times-BoldItalic\"");
283 gnuplot.SetLegend("RSS(dBm)", "Number of packets received");
284 gnuplot.SetExtra("set xrange [-102:-83]");
285 gnuplot.GenerateOutput(outfile);
286 outfile.close();
287
288 return 0;
289}
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
Create a socket and prepare it for packet reception.
void SetPosition(Ptr< Node > node, Vector position)
Set the position of a node.
Vector GetPosition(Ptr< Node > node)
Get the position of a node.
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.
Ptr< Socket > SetupPacketReceive(Ptr< Node > node)
Setup the receiving socket.
void ReceivePacket(Ptr< Socket > socket)
Receive a packet.
Class to represent a 2D points plot.
Definition gnuplot.h:105
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.
Definition ptr.h:70
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:580
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:125
static void Run()
Run the simulation.
Definition simulator.cc:161
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
Simulation virtual time values and global simulation resolution.
Definition nstime.h:96
a unique identifier for an interface.
Definition type-id.h:49
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition type-id.cc:870
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 ReceivePacket(Ptr< Socket > socket)
static void GenerateTraffic(Ptr< Socket > socket, uint32_t pktSize, Ptr< Node > n, uint32_t pktCount, Time pktInterval)
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:690
void SetDefault(std::string name, const AttributeValue &value)
Definition config.cc:886
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:194
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:260
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition log.h:267
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:454
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1381
@ WIFI_STANDARD_80211b
Every class exported by the ns3 library is enclosed in the ns3 namespace.
wifi
Definition third.py:84
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