A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
uan-rc-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 University of Washington
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Leonard Tracy <lentracy@gmail.com>
7 */
8
9/**
10 * @file uan-rc-example.cc
11 * @ingroup uan
12 *
13 * This example uses UanMacRc and UanMacRcGw which combined form a system
14 * using what is referred to as RC-MAC. Details of RC-MAC will be published
15 * soon. In brief terms, RC-MAC is a dual channel protocol wherein the
16 * available bandwidth is dynamically divided into two channels,
17 * a reservation channel and a data channel. The network is assumed
18 * to consist of a single gateway node which services several
19 * non-gateway nodes.
20 *
21 * Time is divided into cycles. The non-gateway nodes transmit RTS packets
22 * on the reservation channel in parallel to scheduled data transmissions
23 * (scheduled in the previous cycle), and the gateway stores these requests
24 * for the duration of the cycle. At the start of the next cycle
25 * the gateway node transmits a CTS which contains packet transmission times
26 * for reserved packets as well as bandwidth allocation information
27 *
28 * This script deploys a single gateway node (current UanMacRc only supports
29 * a single gateway) in the center of a region and then distributes
30 * non-gateway nodes around the gateway with a uniformly distributed range
31 * between each node and the gateway.
32 *
33 * The script supports two simulation types. By default the gateway
34 * dynamically determines the optimal parameter settings and
35 * simulations are run with varying number of nodes (SimMin to SimMax as
36 * set by the command line). If DoNode=0 is given as a command line option
37 * then the mac parameter "a" (approximate expected number of successful
38 * RTS arrivals per cycle) is varied as the simulation parameter.
39 *
40 * For questions about this MAC protocol email "lentracy@gmail.com"
41 */
42
43#include "ns3/applications-module.h"
44#include "ns3/callback.h"
45#include "ns3/config.h"
46#include "ns3/core-module.h"
47#include "ns3/log.h"
48#include "ns3/mobility-module.h"
49#include "ns3/network-module.h"
50#include "ns3/stats-module.h"
51#include "ns3/uan-module.h"
52
53#include <fstream>
54
55using namespace ns3;
56
57NS_LOG_COMPONENT_DEFINE("UanRcExample");
58
59/** Unnamed namespace, to disambiguate class Experiment. */
60namespace
61{
62
63/**
64 * @ingroup uan
65 *
66 * Container for the parameters describing a single experiment.
67 *
68 * An experiment samples a range of parameter values. The parameter
69 * controls the number of nodes (if m_doNode is true), or the
70 * UanMacRcGw MaxReservations attribute.
71 *
72 * @see uan-rc-example.cc
73 *
74 */
76{
77 public:
78 uint32_t m_simMin; //!< Minimum parameter to test.
79 uint32_t m_simMax; //!< Maximum parameter to test.
80 uint32_t m_simStep; //!< Amount to increment param per trial
81 uint32_t m_numRates; //!< Number of divided rates ( (NumberRates+1)%TotalRate should be 0).
82 uint32_t m_totalRate; //!< Total channel capacity.
83 uint32_t m_maxRange; //!< Maximum range between gateway and acoustic node.
84 uint32_t m_numNodes; //!< Number of nodes (invalid for m_doNode true).
85 uint32_t m_pktSize; //!< Packet size in bytes.
86 bool m_doNode; //!< 1 for do max nodes simulation (invalidates AMin and AMax values).
87 Time m_sifs; //!< SIFS time duration.
88 Time m_simTime; //!< Simulation time per trial
89
90 std::string m_gnuplotfile; //!< Filename for GnuPlot.
91
92 uint32_t m_bytesTotal; //!< Total number of bytes received in a simulation run.
93
94 UanModesList m_dataModes; //!< List of UanTxModes used for data channels.
95 UanModesList m_controlModes; //!< List of UanTxModes used for control channels.
96
97 /**
98 * Callback to receive a packet.
99 *
100 * @param socket The socket receiving packets.
101 */
102 void ReceivePacket(Ptr<Socket> socket);
103 /**
104 * Create a UanTxMode.
105 *
106 * The mode physical rate is set equal to m_totalRate.
107 * The data rate and bandwidth are set to
108 *
109 * \f[{\rm{rate, bandwidth}} = {\rm{kass}}\frac{{{\rm{m\_totalRate}}}}{{{\rm{m\_numRates}} +
110 * 1}}\f]
111 *
112 * The center frequency is set to
113 *
114 * \f[{f_{center}} = {\rm{fc}} + \frac{{ \pm {\rm{m\_totalRate}} \mp {\rm{rate}}}}{2}\f]
115 *
116 * where the upper sign is taken if upperblock is true.
117 *
118 * @param kass Fraction of total bandwidth assigned to mode.
119 * @param fc Mode center frequency offset.
120 * @param upperblock Sign choice in setting the center frequency.
121 * @param name Mode name.
122 * @return The new mode.
123 */
124 UanTxMode CreateMode(uint32_t kass, uint32_t fc, bool upperblock, std::string name) const;
125 /**
126 * Create m_numRates matching control and data modes.
127 *
128 * @param fc Mode center frequency offset.
129 */
130 void CreateDualModes(uint32_t fc);
131 /**
132 * Run a parametrized experiment.
133 *
134 * The parameter sets either the number of nodes (if m_doNode is true)
135 * or the \"a\" parameter, which controls the UanMacRcGw MaxReservations
136 * attribute.
137 *
138 * @param param The parameter value.
139 * @return The total number of bytes delivered.
140 */
141 uint32_t Run(uint32_t param);
142
143 /** Default constructor. */
144 Experiment();
145};
146
148 : m_simMin(1),
149 m_simMax(1),
150 m_simStep(1),
151 m_numRates(1023),
152 m_totalRate(4096),
153 m_maxRange(3000),
154 m_numNodes(15),
155 m_pktSize(1000),
156 m_doNode(true),
157 m_sifs(Seconds(0.05)),
158 m_simTime(Seconds(5000)),
159 m_gnuplotfile("uan-rc-example.gpl"),
160 m_bytesTotal(0)
161{
162}
163
164void
166{
167 Ptr<Packet> packet;
168 while ((packet = socket->Recv()))
169 {
170 m_bytesTotal += packet->GetSize();
171 }
172}
173
175Experiment::CreateMode(uint32_t kass, uint32_t fc, bool upperblock, std::string name) const
176{
177 std::ostringstream buf;
178 buf << name << " " << kass;
179
180 uint32_t rate = m_totalRate / (m_numRates + 1) * (kass);
181 uint32_t bw = kass * m_totalRate / (m_numRates + 1);
182 uint32_t fcmode;
183 if (upperblock)
184 {
185 fcmode = (m_totalRate - bw) / 2 + fc;
186 }
187 else
188 {
189 fcmode = (uint32_t)((-((double)m_totalRate) + (double)bw) / 2.0 + (double)fc);
190 }
191
192 uint32_t phyrate = m_totalRate;
193
194 UanTxMode mode;
195 mode = UanTxModeFactory::CreateMode(UanTxMode::OTHER, rate, phyrate, fcmode, bw, 2, buf.str());
196 return mode;
197}
198
199// Creates m_numRates different modes each dividing m_totalRate Hz (assumes 1 bit per hz)
200// centered at frequency fc
201void
203{
204 for (uint32_t i = 1; i < m_numRates + 1; i++)
205 {
206 m_controlModes.AppendMode(CreateMode(i, fc, false, "control "));
207 }
208 for (uint32_t i = m_numRates; i > 0; i--)
209 {
210 m_dataModes.AppendMode(CreateMode(i, fc, true, "data "));
211 }
212}
213
216{
217 UanHelper uan;
218
219 m_bytesTotal = 0;
220
221 uint32_t nNodes;
222 uint32_t a;
223 if (m_doNode)
224 {
225 a = 0;
226 nNodes = param;
227 }
228 else
229 {
230 nNodes = m_numNodes;
231 a = param;
232 }
233 Time pDelay = Seconds((double)m_maxRange / 1500.0);
234
235 uan.SetPhy("ns3::UanPhyDual",
236 "SupportedModesPhy1",
238 "SupportedModesPhy2",
240
241 uan.SetMac("ns3::UanMacRcGw",
242 "NumberOfRates",
244 "NumberOfNodes",
245 UintegerValue(nNodes),
246 "MaxReservations",
247 UintegerValue(a),
248 "SIFS",
250 "MaxPropDelay",
251 TimeValue(pDelay),
252 "FrameSize",
255
257 sink.Create(1);
258 NetDeviceContainer sinkDev = uan.Install(sink, chan);
259
260 uan.SetMac("ns3::UanMacRc",
261 "NumberOfRates",
263 "MaxPropDelay",
264 TimeValue(pDelay));
266 nodes.Create(nNodes);
267 NetDeviceContainer devices = uan.Install(nodes, chan);
268
269 MobilityHelper mobility;
270 uint32_t depth = 70;
272
275 pos->Add(Vector(m_maxRange, m_maxRange, depth));
276
277 for (uint32_t i = 0; i < nNodes; i++)
278 {
279 double theta = utheta->GetValue(0, 2.0 * M_PI);
280 double r = urv->GetValue(0, m_maxRange);
281
282 double x = m_maxRange + r * std::cos(theta);
283 double y = m_maxRange + r * std::sin(theta);
284
285 pos->Add(Vector(x, y, depth));
286 }
287
288 mobility.SetPositionAllocator(pos);
289 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
290 mobility.Install(sink);
291 mobility.Install(nodes);
292
293 PacketSocketHelper pktskth;
294 pktskth.Install(nodes);
295 pktskth.Install(sink);
296
297 PacketSocketAddress socket;
298 socket.SetSingleDevice(sinkDev.Get(0)->GetIfIndex());
299 socket.SetPhysicalAddress(sinkDev.Get(0)->GetAddress());
300 socket.SetProtocol(0);
301
302 OnOffHelper app("ns3::PacketSocketFactory", Address(socket));
303 app.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
304 app.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
305 app.SetAttribute("DataRate", DataRateValue(m_totalRate));
306 app.SetAttribute("PacketSize", UintegerValue(m_pktSize));
307
308 ApplicationContainer apps = app.Install(nodes);
309
310 apps.Start(Seconds(0.5));
311 apps.Stop(m_simTime + Seconds(0.5));
312
313 Ptr<Node> sinkNode = sink.Get(0);
314 TypeId psfid = TypeId::LookupByName("ns3::PacketSocketFactory");
315
316 Ptr<Socket> sinkSocket = Socket::CreateSocket(sinkNode, psfid);
317 sinkSocket->Bind(socket);
318 sinkSocket->SetRecvCallback(MakeCallback(&Experiment::ReceivePacket, this));
319
323
324 return m_bytesTotal;
325}
326
327} // unnamed namespace
328
329int
330main(int argc, char* argv[])
331{
332 Experiment exp;
333 bool quiet = false;
334
335 CommandLine cmd(__FILE__);
336 cmd.AddValue("TotalRate", "Total channel capacity", exp.m_totalRate);
337 cmd.AddValue("NumberRates",
338 "Number of divided rates ( (NumberRates+1)%TotalRate should be 0)",
339 exp.m_numRates);
340 cmd.AddValue("MaxRange", "Maximum range between gateway and acoustic node", exp.m_maxRange);
341 cmd.AddValue("SimMin",
342 "Minimum parameter to test (nodes if DoNode=1, \"a\" param otherwise)",
343 exp.m_simMin);
344 cmd.AddValue("SimMax",
345 "Maximum parameter to test (nodes if DoNode=1, \"a\" param otherwise)",
346 exp.m_simMax);
347 cmd.AddValue("SimStep", "Amount to increment param per trial", exp.m_simStep);
348 cmd.AddValue("DataFile", "Filename for GnuPlot", exp.m_gnuplotfile);
349 cmd.AddValue("NumberNodes", "Number of nodes (invalid for doNode=1)", exp.m_numNodes);
350 cmd.AddValue("SIFS", "SIFS time duration", exp.m_sifs);
351 cmd.AddValue("PktSize", "Packet size in bytes", exp.m_pktSize);
352 cmd.AddValue("SimTime", "Simulation time per trial", exp.m_simTime);
353 cmd.AddValue("DoNode",
354 "1 for do max nodes simulation (invalidates AMin and AMax values)",
355 exp.m_doNode);
356 cmd.AddValue("Quiet", "Run in quiet mode (disable logging)", quiet);
357 cmd.Parse(argc, argv);
358
359 if (!quiet)
360 {
361 LogComponentEnable("UanRcExample", LOG_LEVEL_ALL);
362 }
363
364 exp.CreateDualModes(12000);
365
366 ;
367
368 Gnuplot2dDataset ds;
369 for (uint32_t param = exp.m_simMin; param <= exp.m_simMax; param += exp.m_simStep)
370 {
371 uint32_t bytesRx = exp.Run(param);
372 NS_LOG_DEBUG("param=" << param << ": Received " << bytesRx << " bytes at sink");
373
374 double util = bytesRx * 8.0 / (exp.m_simTime.GetSeconds() * exp.m_totalRate);
375
376 ds.Add(param, util);
377
379 }
380
381 Gnuplot gp;
382 gp.AddDataset(ds);
383 std::ofstream of(exp.m_gnuplotfile);
384 if (!of.is_open())
385 {
386 NS_FATAL_ERROR("Can not open GNU Plot outfile: " << exp.m_gnuplotfile);
387 }
388 gp.GenerateOutput(of);
389
390 return 0;
391}
cairo_uint64_t x
_cairo_uint_96by64_32x64_divrem:
uint32_t r
uint32_t m_simStep
Amount to increment param per trial.
uint32_t m_numRates
Number of divided rates ( (NumberRates+1)TotalRate should be 0).
std::string m_gnuplotfile
Filename for GnuPlot.
uint32_t m_simMax
Maximum parameter to test.
bool m_doNode
1 for do max nodes simulation (invalidates AMin and AMax values).
UanTxMode CreateMode(uint32_t kass, uint32_t fc, bool upperblock, std::string name) const
Create a UanTxMode.
uint32_t m_bytesTotal
Total number of bytes received in a simulation run.
uint32_t Run(uint32_t param)
Run a parametrized experiment.
void ReceivePacket(Ptr< Socket > socket)
Callback to receive a packet.
uint32_t m_simMin
Minimum parameter to test.
uint32_t m_numNodes
Number of nodes (invalid for m_doNode true).
UanModesList m_controlModes
List of UanTxModes used for control channels.
void CreateDualModes(uint32_t fc)
Create m_numRates matching control and data modes.
UanModesList m_dataModes
List of UanTxModes used for data channels.
uint32_t m_maxRange
Maximum range between gateway and acoustic node.
a polymophic address class
Definition address.h:111
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Parse command-line arguments.
AttributeValue implementation for DataRate.
Definition data-rate.h:285
void Add(double x, double y)
Definition gnuplot.cc:366
void AddDataset(const GnuplotDataset &dataset)
Definition gnuplot.cc:785
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition gnuplot.cc:791
Helper class used to assign positions and mobility models to nodes.
holds a vector of ns3::NetDevice pointers
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
keep track of a set of node pointers.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
an address for a packet socket
void SetProtocol(uint16_t protocol)
Set the protocol.
void SetPhysicalAddress(const Address address)
Set the destination address.
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
Give ns3::PacketSocket powers to ns3::Node.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
Smart pointer class similar to boost::intrusive_ptr.
Definition ptr.h:70
static void SetRun(uint64_t run)
Set the run number of simulation.
static uint64_t GetRun()
Get the current run number.
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
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:96
AttributeValue implementation for Time.
Definition nstime.h:1483
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:872
UAN configuration helper.
Definition uan-helper.h:31
void SetPhy(std::string phyType, Ts &&... args)
Set PHY attributes.
Definition uan-helper.h:193
void SetMac(std::string type, Ts &&... args)
Set MAC attributes.
Definition uan-helper.h:186
NetDeviceContainer Install(NodeContainer c) const
This method creates a simple ns3::UanChannel (with a default ns3::UanNoiseModelDefault and ns3::UanPr...
Container for UanTxModes.
AttributeValue implementation for UanModesList.
static UanTxMode CreateMode(UanTxMode::ModulationType type, uint32_t dataRateBps, uint32_t phyRateSps, uint32_t cfHz, uint32_t bwHz, uint32_t constSize, std::string name)
Abstraction of packet modulation information.
Definition uan-tx-mode.h:32
@ OTHER
Unspecified/undefined.
Definition uan-tx-mode.h:45
Hold an unsigned integer type.
Definition uinteger.h:34
void ReceivePacket(Ptr< Socket > socket)
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
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
#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
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:627
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1381
NodeContainer nodes
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void LogComponentEnable(const std::string &name, LogLevel level)
Enable the logging output associated with that log component.
Definition log.cc:284
@ LOG_LEVEL_ALL
Print everything.
Definition log.h:108
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44