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 "uan-rc-example.h"
44
45#include "ns3/applications-module.h"
46#include "ns3/callback.h"
47#include "ns3/config.h"
48#include "ns3/core-module.h"
49#include "ns3/log.h"
50#include "ns3/mobility-module.h"
51#include "ns3/network-module.h"
52#include "ns3/stats-module.h"
53
54#include <fstream>
55
56using namespace ns3;
57
58NS_LOG_COMPONENT_DEFINE("UanRcExample");
59
61 : m_simMin(1),
62 m_simMax(1),
63 m_simStep(1),
64 m_numRates(1023),
65 m_totalRate(4096),
66 m_maxRange(3000),
67 m_numNodes(15),
68 m_pktSize(1000),
69 m_doNode(true),
70 m_sifs(Seconds(0.05)),
71 m_simTime(Seconds(5000)),
72 m_gnuplotfile("uan-rc-example.gpl"),
73 m_bytesTotal(0)
74{
75}
76
77void
79{
80 Ptr<Packet> packet;
81 while ((packet = socket->Recv()))
82 {
83 m_bytesTotal += packet->GetSize();
84 }
85}
86
88Experiment::CreateMode(uint32_t kass, uint32_t fc, bool upperblock, std::string name) const
89{
90 std::ostringstream buf;
91 buf << name << " " << kass;
92
93 uint32_t rate = m_totalRate / (m_numRates + 1) * (kass);
94 uint32_t bw = kass * m_totalRate / (m_numRates + 1);
95 uint32_t fcmode;
96 if (upperblock)
97 {
98 fcmode = (m_totalRate - bw) / 2 + fc;
99 }
100 else
101 {
102 fcmode = (uint32_t)((-((double)m_totalRate) + (double)bw) / 2.0 + (double)fc);
103 }
104
105 uint32_t phyrate = m_totalRate;
106
107 UanTxMode mode;
108 mode = UanTxModeFactory::CreateMode(UanTxMode::OTHER, rate, phyrate, fcmode, bw, 2, buf.str());
109 return mode;
110}
111
112// Creates m_numRates different modes each dividing m_totalRate Hz (assumes 1 bit per hz)
113// centered at frequency fc
114void
116{
117 for (uint32_t i = 1; i < m_numRates + 1; i++)
118 {
119 m_controlModes.AppendMode(CreateMode(i, fc, false, "control "));
120 }
121 for (uint32_t i = m_numRates; i > 0; i--)
122 {
123 m_dataModes.AppendMode(CreateMode(i, fc, true, "data "));
124 }
125}
126
129{
130 UanHelper uan;
131
132 m_bytesTotal = 0;
133
134 uint32_t nNodes;
135 uint32_t a;
136 if (m_doNode)
137 {
138 a = 0;
139 nNodes = param;
140 }
141 else
142 {
143 nNodes = m_numNodes;
144 a = param;
145 }
146 Time pDelay = Seconds((double)m_maxRange / 1500.0);
147
148 uan.SetPhy("ns3::UanPhyDual",
149 "SupportedModesPhy1",
151 "SupportedModesPhy2",
153
154 uan.SetMac("ns3::UanMacRcGw",
155 "NumberOfRates",
157 "NumberOfNodes",
158 UintegerValue(nNodes),
159 "MaxReservations",
160 UintegerValue(a),
161 "SIFS",
163 "MaxPropDelay",
164 TimeValue(pDelay),
165 "FrameSize",
168
170 sink.Create(1);
171 NetDeviceContainer sinkDev = uan.Install(sink, chan);
172
173 uan.SetMac("ns3::UanMacRc",
174 "NumberOfRates",
176 "MaxPropDelay",
177 TimeValue(pDelay));
179 nodes.Create(nNodes);
180 NetDeviceContainer devices = uan.Install(nodes, chan);
181
182 MobilityHelper mobility;
183 uint32_t depth = 70;
185
188 pos->Add(Vector(m_maxRange, m_maxRange, depth));
189
190 for (uint32_t i = 0; i < nNodes; i++)
191 {
192 double theta = utheta->GetValue(0, 2.0 * M_PI);
193 double r = urv->GetValue(0, m_maxRange);
194
195 double x = m_maxRange + r * std::cos(theta);
196 double y = m_maxRange + r * std::sin(theta);
197
198 pos->Add(Vector(x, y, depth));
199 }
200
201 mobility.SetPositionAllocator(pos);
202 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
203 mobility.Install(sink);
204 mobility.Install(nodes);
205
206 PacketSocketHelper pktskth;
207 pktskth.Install(nodes);
208 pktskth.Install(sink);
209
210 PacketSocketAddress socket;
211 socket.SetSingleDevice(sinkDev.Get(0)->GetIfIndex());
212 socket.SetPhysicalAddress(sinkDev.Get(0)->GetAddress());
213 socket.SetProtocol(0);
214
215 OnOffHelper app("ns3::PacketSocketFactory", Address(socket));
216 app.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
217 app.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
218 app.SetAttribute("DataRate", DataRateValue(m_totalRate));
219 app.SetAttribute("PacketSize", UintegerValue(m_pktSize));
220
221 ApplicationContainer apps = app.Install(nodes);
222
223 apps.Start(Seconds(0.5));
224 apps.Stop(m_simTime + Seconds(0.5));
225
226 Ptr<Node> sinkNode = sink.Get(0);
227 TypeId psfid = TypeId::LookupByName("ns3::PacketSocketFactory");
228
229 Ptr<Socket> sinkSocket = Socket::CreateSocket(sinkNode, psfid);
230 sinkSocket->Bind(socket);
231 sinkSocket->SetRecvCallback(MakeCallback(&Experiment::ReceivePacket, this));
232
236
237 return m_bytesTotal;
238}
239
240int
241main(int argc, char* argv[])
242{
243 Experiment exp;
244 bool quiet = false;
245
246 CommandLine cmd(__FILE__);
247 cmd.AddValue("TotalRate", "Total channel capacity", exp.m_totalRate);
248 cmd.AddValue("NumberRates",
249 "Number of divided rates ( (NumberRates+1)%TotalRate should be 0)",
250 exp.m_numRates);
251 cmd.AddValue("MaxRange", "Maximum range between gateway and acoustic node", exp.m_maxRange);
252 cmd.AddValue("SimMin",
253 "Minimum parameter to test (nodes if DoNode=1, \"a\" param otherwise)",
254 exp.m_simMin);
255 cmd.AddValue("SimMax",
256 "Maximum parameter to test (nodes if DoNode=1, \"a\" param otherwise)",
257 exp.m_simMax);
258 cmd.AddValue("SimStep", "Amount to increment param per trial", exp.m_simStep);
259 cmd.AddValue("DataFile", "Filename for GnuPlot", exp.m_gnuplotfile);
260 cmd.AddValue("NumberNodes", "Number of nodes (invalid for doNode=1)", exp.m_numNodes);
261 cmd.AddValue("SIFS", "SIFS time duration", exp.m_sifs);
262 cmd.AddValue("PktSize", "Packet size in bytes", exp.m_pktSize);
263 cmd.AddValue("SimTime", "Simulation time per trial", exp.m_simTime);
264 cmd.AddValue("DoNode",
265 "1 for do max nodes simulation (invalidates AMin and AMax values)",
266 exp.m_doNode);
267 cmd.AddValue("Quiet", "Run in quiet mode (disable logging)", quiet);
268 cmd.Parse(argc, argv);
269
270 if (!quiet)
271 {
272 LogComponentEnable("UanRcExample", LOG_LEVEL_ALL);
273 }
274
275 exp.CreateDualModes(12000);
276
277 ;
278
280 for (uint32_t param = exp.m_simMin; param <= exp.m_simMax; param += exp.m_simStep)
281 {
282 uint32_t bytesRx = exp.Run(param);
283 NS_LOG_DEBUG("param=" << param << ": Received " << bytesRx << " bytes at sink");
284
285 double util = bytesRx * 8.0 / (exp.m_simTime.GetSeconds() * exp.m_totalRate);
286
287 ds.Add(param, util);
288
290 }
291
292 Gnuplot gp;
293 gp.AddDataset(ds);
294 std::ofstream of(exp.m_gnuplotfile);
295 if (!of.is_open())
296 {
297 NS_FATAL_ERROR("Can not open GNU Plot outfile: " << exp.m_gnuplotfile);
298 }
299 gp.GenerateOutput(of);
300
301 return 0;
302}
WiFi adhoc experiment class.
Definition wifi-adhoc.cc:34
Gnuplot2dDataset Run(const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy, const WifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel)
Run an experiment.
uint32_t m_simMin
Minimum parameter to test.
uint32_t m_bytesTotal
The number of received bytes.
Definition wifi-adhoc.cc:86
Time m_simTime
Simulation run time, default 1000 s.
uint32_t m_simStep
Amount to increment param per trial.
UanModesList m_dataModes
List of UanTxModes used for data channels.
Time m_sifs
SIFS time duration.
uint32_t m_totalRate
Total channel capacity.
UanTxMode CreateMode(uint32_t kass, uint32_t fc, bool upperblock, std::string name) const
Create a UanTxMode.
void ReceivePacket(Ptr< Socket > socket)
Receive a packet.
bool m_doNode
1 for do max nodes simulation (invalidates AMin and AMax values).
uint32_t m_simMax
Maximum parameter to test.
void CreateDualModes(uint32_t fc)
Create m_numRates matching control and data modes.
UanModesList m_controlModes
List of UanTxModes used for control channels.
uint32_t m_numRates
Number of divided rates ( (NumberRates+1)TotalRate should be 0).
uint32_t m_numNodes
Number of transmitting nodes.
uint32_t m_maxRange
Maximum range between gateway and acoustic node.
uint32_t m_pktSize
Packet size in bytes.
std::string m_gnuplotfile
Filename for GnuPlot.
a polymophic address class
Definition address.h:90
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
Class to represent a 2D points plot.
Definition gnuplot.h:105
void Add(double x, double y)
Definition gnuplot.cc:366
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 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.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
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.
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:94
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition nstime.h:392
AttributeValue implementation for Time.
Definition nstime.h:1395
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
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...
void AppendMode(UanTxMode mode)
Add mode to this list.
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
#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:191
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition log.h:257
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition object.h:619
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
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:291
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
@ LOG_LEVEL_ALL
Print everything.
Definition log.h:105
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition wifi-tcp.cc:44