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