A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
uan-animation.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2010 Andrea Sacco
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Andrea Sacco <andrea.sacco85@gmail.com>
7 */
8
9/**
10 * \file uan-animation.cc
11 * \ingroup uan
12 *
13 * This example showcases the "CW-MAC" described in System Design Considerations for Undersea
14 * Networks article in the IEEE Journal on Selected Areas of Communications 2008 by Nathan Parrish,
15 * Leonard Tracy and Sumit Roy. The MAC protocol is implemented in the class UanMacCw. CW-MAC is
16 * similar in nature to the IEEE 802.11 DCF with a constant backoff window. It requires two
17 * parameters to be set, the slot time and the contention window size. The contention window size
18 * is the backoff window size in slots, and the slot time is the duration of each slot. These
19 * parameters should be set according to the overall network size, internode spacing and the number
20 * of nodes in the network.
21 *
22 * This example deploys nodes randomly (according to RNG seed of course) in a finite square region
23 * with the X and Y coordinates of the nodes distributed uniformly. The CW parameter is varied
24 * throughout the simulation in order to show the variation in throughput with respect to changes in
25 * CW.
26 */
27
28#include "uan-animation.h"
29
30#include "ns3/applications-module.h"
31#include "ns3/core-module.h"
32#include "ns3/mobility-module.h"
33#include "ns3/netanim-module.h"
34#include "ns3/network-module.h"
35
36#include <fstream>
37
38using namespace ns3;
39
40NS_LOG_COMPONENT_DEFINE("UanCwExample");
41
43 : m_numNodes(15),
44 m_dataRate(80),
45 m_depth(70),
46 m_boundary(500),
47 m_packetSize(32),
48 m_bytesTotal(0),
49 m_cwMin(10),
50 m_cwMax(400),
51 m_cwStep(10),
52 m_avgs(3),
53 m_slotTime(Seconds(0.2)),
54 m_simTime(Seconds(1000))
55{
56}
57
58void
60{
61 NS_LOG_DEBUG(Simulator::Now().As(Time::S) << " Resetting data");
63 m_bytesTotal = 0;
64}
65
66void
68{
70
71 double avgThroughput = 0.0;
72 for (uint32_t i = 0; i < m_avgs; i++)
73 {
74 avgThroughput += m_throughputs[i];
75 }
76 avgThroughput /= m_avgs;
77 m_throughputs.clear();
78
79 Config::Set("/NodeList/*/DeviceList/*/Mac/CW", UintegerValue(cw + m_cwStep));
80
82
83 NS_LOG_DEBUG("Average for cw=" << cw << " over " << m_avgs << " runs: " << avgThroughput);
84}
85
86void
88{
89 NS_LOG_DEBUG(Simulator::Now().As(Time::S) << " Updating positions");
90 auto it = nodes.Begin();
92 uv->SetAttribute("Min", DoubleValue(0.0));
93 uv->SetAttribute("Max", DoubleValue(m_boundary));
94 for (; it != nodes.End(); it++)
95 {
96 Ptr<MobilityModel> mp = (*it)->GetObject<MobilityModel>();
97 mp->SetPosition(Vector(uv->GetValue(), uv->GetValue(), 70.0));
98 }
99}
100
101void
103{
104 Ptr<Packet> packet;
105
106 while ((packet = socket->Recv()))
107 {
108 m_bytesTotal += packet->GetSize();
109 }
110 packet = nullptr;
111}
112
113void
115{
116 uan.SetMac("ns3::UanMacCw", "CW", UintegerValue(m_cwMin), "SlotTime", TimeValue(m_slotTime));
119 nc.Create(m_numNodes);
120 sink.Create(1);
121
122 PacketSocketHelper socketHelper;
123 socketHelper.Install(nc);
124 socketHelper.Install(sink);
125
126#ifdef UAN_PROP_BH_INSTALLED
128 CreateObjectWithAttributes<UanPropModelBh>("ConfigFile", StringValue("exbhconfig.cfg"));
129#else
131#endif // UAN_PROP_BH_INSTALLED
132 Ptr<UanChannel> channel =
133 CreateObjectWithAttributes<UanChannel>("PropagationModel", PointerValue(prop));
134
135 // Create net device and nodes with UanHelper
136 NetDeviceContainer devices = uan.Install(nc, channel);
137 NetDeviceContainer sinkdev = uan.Install(sink, channel);
138
139 MobilityHelper mobility;
141
142 {
144 urv->SetAttribute("Min", DoubleValue(0.0));
145 urv->SetAttribute("Max", DoubleValue(m_boundary));
146 pos->Add(Vector(m_boundary / 2.0, m_boundary / 2.0, m_depth));
147 double rsum = 0;
148
149 double minr = 2 * m_boundary;
150 for (uint32_t i = 0; i < m_numNodes; i++)
151 {
152 double x = urv->GetValue();
153 double y = urv->GetValue();
154 double newr = std::sqrt((x - m_boundary / 2.0) * (x - m_boundary / 2.0) +
155 (y - m_boundary / 2.0) * (y - m_boundary / 2.0));
156 rsum += newr;
157 minr = std::min(minr, newr);
158 pos->Add(Vector(x, y, m_depth));
159 }
160 NS_LOG_DEBUG("Mean range from gateway: " << rsum / m_numNodes << " min. range " << minr);
161
162 mobility.SetPositionAllocator(pos);
163 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
164 mobility.Install(sink);
165
167 "Position of sink: " << sink.Get(0)->GetObject<MobilityModel>()->GetPosition());
168 mobility.Install(nc);
169
170 PacketSocketAddress socket;
171 socket.SetSingleDevice(sinkdev.Get(0)->GetIfIndex());
172 socket.SetPhysicalAddress(sinkdev.Get(0)->GetAddress());
173 socket.SetProtocol(0);
174
175 OnOffHelper app("ns3::PacketSocketFactory", Address(socket));
176 app.SetAttribute("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1.0]"));
177 app.SetAttribute("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0.0]"));
178 app.SetAttribute("DataRate", DataRateValue(m_dataRate));
179 app.SetAttribute("PacketSize", UintegerValue(m_packetSize));
180
181 ApplicationContainer apps = app.Install(nc);
182 apps.Start(Seconds(0.5));
183 Time nextEvent = Seconds(0.5);
184
185 for (uint32_t cw = m_cwMin; cw <= m_cwMax; cw += m_cwStep)
186 {
187 for (uint32_t an = 0; an < m_avgs; an++)
188 {
189 nextEvent += m_simTime;
192 }
194 }
195 apps.Stop(nextEvent + m_simTime);
196
197 Ptr<Node> sinkNode = sink.Get(0);
198 TypeId psfid = TypeId::LookupByName("ns3::PacketSocketFactory");
199 if (!sinkNode->GetObject<SocketFactory>(psfid))
200 {
202 sinkNode->AggregateObject(psf);
203 }
204 Ptr<Socket> sinkSocket = Socket::CreateSocket(sinkNode, psfid);
205 sinkSocket->Bind(socket);
206 sinkSocket->SetRecvCallback(MakeCallback(&NetAnimExperiment::ReceivePacket, this));
207
208 m_bytesTotal = 0;
209
210 std::string traceFileName = "uan-animation.xml";
211 AnimationInterface anim(traceFileName);
212
214 sinkNode = nullptr;
215 sinkSocket = nullptr;
216 pos = nullptr;
217 channel = nullptr;
218 prop = nullptr;
219 for (uint32_t i = 0; i < nc.GetN(); i++)
220 {
221 nc.Get(i) = nullptr;
222 }
223 for (uint32_t i = 0; i < sink.GetN(); i++)
224 {
225 sink.Get(i) = nullptr;
226 }
227
228 for (uint32_t i = 0; i < devices.GetN(); i++)
229 {
230 devices.Get(i) = nullptr;
231 }
232 for (uint32_t i = 0; i < sinkdev.GetN(); i++)
233 {
234 sinkdev.Get(i) = nullptr;
235 }
236
238 }
239}
240
241int
242main(int argc, char** argv)
243{
244 LogComponentEnable("UanCwExample", LOG_LEVEL_ALL);
245 // LogComponentEnable ("AnimationInterface", LOG_LEVEL_ALL);
246
248
249 std::string perModel = "ns3::UanPhyPerGenDefault";
250 std::string sinrModel = "ns3::UanPhyCalcSinrDefault";
251
252 CommandLine cmd(__FILE__);
253 cmd.AddValue("NumNodes", "Number of transmitting nodes", exp.m_numNodes);
254 cmd.AddValue("Depth", "Depth of transmitting and sink nodes", exp.m_depth);
255 cmd.AddValue("RegionSize", "Size of boundary in meters", exp.m_boundary);
256 cmd.AddValue("PacketSize", "Generated packet size in bytes", exp.m_packetSize);
257 cmd.AddValue("DataRate", "DataRate in bps", exp.m_dataRate);
258 cmd.AddValue("CwMin", "Min CW to simulate", exp.m_cwMin);
259 cmd.AddValue("CwMax", "Max CW to simulate", exp.m_cwMax);
260 cmd.AddValue("SlotTime", "Slot time duration", exp.m_slotTime);
261 cmd.AddValue("Averages", "Number of topologies to test for each cw point", exp.m_avgs);
262 cmd.AddValue("PerModel", "PER model name", perModel);
263 cmd.AddValue("SinrModel", "SINR model name", sinrModel);
264 cmd.Parse(argc, argv);
265
266 ObjectFactory obf;
267 obf.SetTypeId(perModel);
268 Ptr<UanPhyPer> per = obf.Create<UanPhyPer>();
269 obf.SetTypeId(sinrModel);
271
272 UanHelper uan;
273 UanTxMode mode;
275 exp.m_dataRate,
276 exp.m_dataRate,
277 12000,
278 exp.m_dataRate,
279 2,
280 "Default mode");
281 UanModesList myModes;
282 myModes.AppendMode(mode);
283
284 uan.SetPhy("ns3::UanPhyGen",
285 "PerModel",
286 PointerValue(per),
287 "SinrModel",
288 PointerValue(sinr),
289 "SupportedModes",
290 UanModesListValue(myModes));
291
292 exp.Run(uan);
293
294 per = nullptr;
295 sinr = nullptr;
296
297 return 0;
298}
Helper class for UAN CW MAC example.
void IncrementCw(uint32_t cw)
Increment CW function.
double m_boundary
boundary
std::vector< double > m_throughputs
throughputs
uint32_t m_cwMin
CW minimum.
uint32_t m_cwMax
CW maximum.
void Run(UanHelper &uan)
Run function.
Time m_simTime
simulation time
double m_depth
depth
uint32_t m_dataRate
data rate
uint32_t m_numNodes
number of nodes
Time m_slotTime
slot time
uint32_t m_packetSize
packet size
void ReceivePacket(Ptr< Socket > socket)
Receive packet function.
void ResetData()
Reset data function.
NetAnimExperiment()
the experiment
uint32_t m_avgs
averages
void UpdatePositions(NodeContainer &nodes) const
Update positions function.
uint32_t m_bytesTotal
bytes total
uint32_t m_cwStep
CW step.
a polymophic address class
Definition address.h:90
Interface to network animator.
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.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
Helper class used to assign positions and mobility models to nodes.
Keep track of the current position and velocity of an object.
Vector GetPosition() const
holds a vector of ns3::NetDevice pointers
uint32_t GetN() const
Get the number of Ptr<NetDevice> stored in this container.
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.
Iterator End() const
Get an iterator which indicates past-the-last Node in the container.
uint32_t GetN() const
Get the number of Ptr<Node> stored in this container.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Iterator Begin() const
Get an iterator which refers to the first Node in the container.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Instantiate subclasses of ns3::Object.
Ptr< Object > Create() const
Create an Object instance of the configured TypeId.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
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.
AttributeValue implementation for Pointer.
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 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 Time Now()
Return the current simulation virtual time.
Definition simulator.cc:197
static void Run()
Run the simulation.
Definition simulator.cc:167
Object to create transport layer instances that provide a socket API to applications.
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
@ S
second
Definition nstime.h:105
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 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.
void AppendMode(UanTxMode mode)
Add mode to this list.
Class used for calculating SINR of packet in UanPhy.
Definition uan-phy.h:33
Calculate packet error probability, based on received SINR and modulation (mode).
Definition uan-phy.h:99
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
@ FSK
Frequency shift keying.
Definition uan-tx-mode.h:44
Hold an unsigned integer type.
Definition uinteger.h:34
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file,...
Definition assert.h:55
void Set(std::string path, const AttributeValue &value)
Definition config.cc:869
#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
Ptr< T > CreateObjectWithAttributes(Args... args)
Allocate an Object on the heap and initialize with a set of attributes.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
AnimationInterface * anim
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