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 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation;
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16 *
17 * Author: Leonard Tracy <lentracy@gmail.com>
18 */
19
20/**
21 * \file uan-rc-example.cc
22 * \ingroup uan
23 *
24 * This example uses UanMacRc and UanMacRcGw which combined form a system
25 * using what is referred to as RC-MAC. Details of RC-MAC will be published
26 * soon. In brief terms, RC-MAC is a dual channel protocol wherein the
27 * available bandwidth is dynamically divided into two channels,
28 * a reservation channel and a data channel. The network is assumed
29 * to consist of a single gateway node which services several
30 * non-gateway nodes.
31 *
32 * Time is divided into cycles. The non-gateway nodes transmit RTS packets
33 * on the reservation channel in parallel to scheduled data transmissions
34 * (scheduled in the previous cycle), and the gateway stores these requests
35 * for the duration of the cycle. At the start of the next cycle
36 * the gateway node transmits a CTS which contains packet transmission times
37 * for reserved packets as well as bandwidth allocation information
38 *
39 * This script deploys a single gateway node (current UanMacRc only supports
40 * a single gateway) in the center of a region and then distributes
41 * non-gateway nodes around the gateway with a uniformly distributed range
42 * between each node and the gateway.
43 *
44 * The script supports two simulation types. By default the gateway
45 * dynamically determines the optimal parameter settings and
46 * simulations are run with varying number of nodes (SimMin to SimMax as
47 * set by the command line). If DoNode=0 is given as a command line option
48 * then the mac parameter "a" (approximate expected number of successful
49 * RTS arrivals per cycle) is varied as the simulation parameter.
50 *
51 * For questions about this MAC protocol email "lentracy@gmail.com"
52 */
53
54#include "uan-rc-example.h"
55
56#include "ns3/applications-module.h"
57#include "ns3/callback.h"
58#include "ns3/config.h"
59#include "ns3/core-module.h"
60#include "ns3/log.h"
61#include "ns3/mobility-module.h"
62#include "ns3/network-module.h"
63#include "ns3/stats-module.h"
64
65#include <fstream>
66
67using namespace ns3;
68
69NS_LOG_COMPONENT_DEFINE("UanRcExample");
70
72 : m_simMin(1),
73 m_simMax(1),
74 m_simStep(1),
75 m_numRates(1023),
76 m_totalRate(4096),
77 m_maxRange(3000),
78 m_numNodes(15),
79 m_pktSize(1000),
80 m_doNode(true),
81 m_sifs(Seconds(0.05)),
82 m_simTime(Seconds(5000)),
83 m_gnuplotfile("uan-rc-example.gpl"),
84 m_bytesTotal(0)
85{
86}
87
88void
90{
91 Ptr<Packet> packet;
92 while ((packet = socket->Recv()))
93 {
94 m_bytesTotal += packet->GetSize();
95 }
96}
97
99Experiment::CreateMode(uint32_t kass, uint32_t fc, bool upperblock, std::string name) const
100{
101 std::ostringstream buf;
102 buf << name << " " << kass;
103
104 uint32_t rate = m_totalRate / (m_numRates + 1) * (kass);
105 uint32_t bw = kass * m_totalRate / (m_numRates + 1);
106 uint32_t fcmode;
107 if (upperblock)
108 {
109 fcmode = (m_totalRate - bw) / 2 + fc;
110 }
111 else
112 {
113 fcmode = (uint32_t)((-((double)m_totalRate) + (double)bw) / 2.0 + (double)fc);
114 }
115
116 uint32_t phyrate = m_totalRate;
117
118 UanTxMode mode;
119 mode = UanTxModeFactory::CreateMode(UanTxMode::OTHER, rate, phyrate, fcmode, bw, 2, buf.str());
120 return mode;
121}
122
123// Creates m_numRates different modes each dividing m_totalRate Hz (assumes 1 bit per hz)
124// centered at frequency fc
125void
127{
128 for (uint32_t i = 1; i < m_numRates + 1; i++)
129 {
130 m_controlModes.AppendMode(CreateMode(i, fc, false, "control "));
131 }
132 for (uint32_t i = m_numRates; i > 0; i--)
133 {
134 m_dataModes.AppendMode(CreateMode(i, fc, true, "data "));
135 }
136}
137
140{
141 UanHelper uan;
142
143 m_bytesTotal = 0;
144
145 uint32_t nNodes;
146 uint32_t a;
147 if (m_doNode)
148 {
149 a = 0;
150 nNodes = param;
151 }
152 else
153 {
154 nNodes = m_numNodes;
155 a = param;
156 }
157 Time pDelay = Seconds((double)m_maxRange / 1500.0);
158
159 uan.SetPhy("ns3::UanPhyDual",
160 "SupportedModesPhy1",
162 "SupportedModesPhy2",
164
165 uan.SetMac("ns3::UanMacRcGw",
166 "NumberOfRates",
168 "NumberOfNodes",
169 UintegerValue(nNodes),
170 "MaxReservations",
171 UintegerValue(a),
172 "SIFS",
174 "MaxPropDelay",
175 TimeValue(pDelay),
176 "FrameSize",
178 Ptr<UanChannel> chan = CreateObject<UanChannel>();
179
181 sink.Create(1);
182 NetDeviceContainer sinkDev = uan.Install(sink, chan);
183
184 uan.SetMac("ns3::UanMacRc",
185 "NumberOfRates",
187 "MaxPropDelay",
188 TimeValue(pDelay));
190 nodes.Create(nNodes);
191 NetDeviceContainer devices = uan.Install(nodes, chan);
192
193 MobilityHelper mobility;
194 uint32_t depth = 70;
195 Ptr<ListPositionAllocator> pos = CreateObject<ListPositionAllocator>();
196
197 Ptr<UniformRandomVariable> urv = CreateObject<UniformRandomVariable>();
198 Ptr<UniformRandomVariable> utheta = CreateObject<UniformRandomVariable>();
199 pos->Add(Vector(m_maxRange, m_maxRange, depth));
200
201 for (uint32_t i = 0; i < nNodes; i++)
202 {
203 double theta = utheta->GetValue(0, 2.0 * M_PI);
204 double r = urv->GetValue(0, m_maxRange);
205
206 double x = m_maxRange + r * std::cos(theta);
207 double y = m_maxRange + r * std::sin(theta);
208
209 pos->Add(Vector(x, y, depth));
210 }
211
212 mobility.SetPositionAllocator(pos);
213 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
214 mobility.Install(sink);
215 mobility.Install(nodes);
216
217 PacketSocketHelper pktskth;
218 pktskth.Install(nodes);
219 pktskth.Install(sink);
220
221 PacketSocketAddress socket;
222 socket.SetSingleDevice(sinkDev.Get(0)->GetIfIndex());
223 socket.SetPhysicalAddress(sinkDev.Get(0)->GetAddress());
224 socket.SetProtocol(0);
225
226 OnOffHelper app("ns3::PacketSocketFactory", Address(socket));
227 app.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]"));
228 app.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]"));
229 app.SetAttribute("DataRate", DataRateValue(m_totalRate));
230 app.SetAttribute("PacketSize", UintegerValue(m_pktSize));
231
232 ApplicationContainer apps = app.Install(nodes);
233
234 apps.Start(Seconds(0.5));
235 apps.Stop(m_simTime + Seconds(0.5));
236
237 Ptr<Node> sinkNode = sink.Get(0);
238 TypeId psfid = TypeId::LookupByName("ns3::PacketSocketFactory");
239
240 Ptr<Socket> sinkSocket = Socket::CreateSocket(sinkNode, psfid);
241 sinkSocket->Bind(socket);
242 sinkSocket->SetRecvCallback(MakeCallback(&Experiment::ReceivePacket, this));
243
247
248 return m_bytesTotal;
249}
250
251int
252main(int argc, char* argv[])
253{
254 Experiment exp;
255 bool quiet = false;
256
257 CommandLine cmd(__FILE__);
258 cmd.AddValue("TotalRate", "Total channel capacity", exp.m_totalRate);
259 cmd.AddValue("NumberRates",
260 "Number of divided rates ( (NumberRates+1)%TotalRate should be 0)",
261 exp.m_numRates);
262 cmd.AddValue("MaxRange", "Maximum range between gateway and acoustic node", exp.m_maxRange);
263 cmd.AddValue("SimMin",
264 "Minimum parameter to test (nodes if DoNode=1, \"a\" param otherwise)",
265 exp.m_simMin);
266 cmd.AddValue("SimMax",
267 "Maximum parameter to test (nodes if DoNode=1, \"a\" param otherwise)",
268 exp.m_simMax);
269 cmd.AddValue("SimStep", "Amount to increment param per trial", exp.m_simStep);
270 cmd.AddValue("DataFile", "Filename for GnuPlot", exp.m_gnuplotfile);
271 cmd.AddValue("NumberNodes", "Number of nodes (invalid for doNode=1)", exp.m_numNodes);
272 cmd.AddValue("SIFS", "SIFS time duration", exp.m_sifs);
273 cmd.AddValue("PktSize", "Packet size in bytes", exp.m_pktSize);
274 cmd.AddValue("SimTime", "Simulation time per trial", exp.m_simTime);
275 cmd.AddValue("DoNode",
276 "1 for do max nodes simulation (invalidates AMin and AMax values)",
277 exp.m_doNode);
278 cmd.AddValue("Quiet", "Run in quiet mode (disable logging)", quiet);
279 cmd.Parse(argc, argv);
280
281 if (!quiet)
282 {
283 LogComponentEnable("UanRcExample", LOG_LEVEL_ALL);
284 }
285
286 exp.CreateDualModes(12000);
287
288 ;
289
291 for (uint32_t param = exp.m_simMin; param <= exp.m_simMax; param += exp.m_simStep)
292 {
293 uint32_t bytesRx = exp.Run(param);
294 NS_LOG_DEBUG("param=" << param << ": Received " << bytesRx << " bytes at sink");
295
296 double util = bytesRx * 8.0 / (exp.m_simTime.GetSeconds() * exp.m_totalRate);
297
298 ds.Add(param, util);
299
301 }
302
303 Gnuplot gp;
304 gp.AddDataset(ds);
305 std::ofstream of(exp.m_gnuplotfile);
306 if (!of.is_open())
307 {
308 NS_FATAL_ERROR("Can not open GNU Plot outfile: " << exp.m_gnuplotfile);
309 }
310 gp.GenerateOutput(of);
311
312 return 0;
313}
WiFi adhoc experiment class.
Definition: wifi-adhoc.cc:45
Gnuplot2dDataset Run(const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy, const WifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel)
Run an experiment.
Definition: wifi-adhoc.cc:162
uint32_t m_simMin
Minimum parameter to test.
uint32_t m_bytesTotal
The number of received bytes.
Definition: wifi-adhoc.cc:97
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.
Definition: wifi-adhoc.cc:142
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:101
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.
Definition: command-line.h:232
AttributeValue implementation for DataRate.
Definition: data-rate.h:296
Class to represent a 2D points plot.
Definition: gnuplot.h:116
void Add(double x, double y)
Definition: gnuplot.cc:377
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:370
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:796
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:802
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.
Definition: on-off-helper.h:37
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:77
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:142
static void Run()
Run the simulation.
Definition: simulator.cc:178
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:186
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:72
Hold variables of type string.
Definition: string.h:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
double GetSeconds() const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:403
AttributeValue implementation for Time.
Definition: nstime.h:1413
a unique identifier for an interface.
Definition: type-id.h:59
static TypeId LookupByName(std::string name)
Get a TypeId by name.
Definition: type-id.cc:836
UAN configuration helper.
Definition: uan-helper.h:42
void SetPhy(std::string phyType, Ts &&... args)
Set PHY attributes.
Definition: uan-helper.h:204
void SetMac(std::string type, Ts &&... args)
Set MAC attributes.
Definition: uan-helper.h:197
NetDeviceContainer Install(NodeContainer c) const
This method creates a simple ns3::UanChannel (with a default ns3::UanNoiseModelDefault and ns3::UanPr...
Definition: uan-helper.cc:145
void AppendMode(UanTxMode mode)
Add mode to this list.
Definition: uan-tx-mode.cc:230
AttributeValue implementation for UanModesList.
Definition: uan-tx-mode.h:314
static UanTxMode CreateMode(UanTxMode::ModulationType type, uint32_t dataRateBps, uint32_t phyRateSps, uint32_t cfHz, uint32_t bwHz, uint32_t constSize, std::string name)
Definition: uan-tx-mode.cc:132
Abstraction of packet modulation information.
Definition: uan-tx-mode.h:43
@ OTHER
Unspecified/undefined.
Definition: uan-tx-mode.h:56
Hold an unsigned integer type.
Definition: uinteger.h:45
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:268
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
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:302
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:704
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55