A Discrete-Event Network Simulator
API
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 
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 
49 using namespace ns3;
50 
51 NS_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 
69 void
71 {
72  NS_LOG_DEBUG(Simulator::Now().As(Time::S) << " Resetting data");
73  m_throughputs.push_back(m_bytesTotal * 8.0 / m_simTime.GetSeconds());
74  m_bytesTotal = 0;
75 }
76 
77 void
79 {
80  NS_ASSERT(m_throughputs.size() == m_avgs);
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 
92  SeedManager::SetRun(SeedManager::GetRun() + 1);
93 
94  NS_LOG_DEBUG("Average for cw=" << cw << " over " << m_avgs << " runs: " << avgThroughput);
95 }
96 
97 void
99 {
100  NS_LOG_DEBUG(Simulator::Now().As(Time::S) << " Updating positions");
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  {
108  mp->SetPosition(Vector(uv->GetValue(), uv->GetValue(), 70.0));
109  }
110 }
111 
112 void
114 {
115  Ptr<Packet> packet;
116 
117  while ((packet = socket->Recv()))
118  {
119  m_bytesTotal += packet->GetSize();
120  }
121  packet = nullptr;
122 }
123 
124 void
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
138  Ptr<UanPropModelBh> prop =
139  CreateObjectWithAttributes<UanPropModelBh>("ConfigFile", StringValue("exbhconfig.cfg"));
140 #else
141  Ptr<UanPropModelIdeal> prop = CreateObjectWithAttributes<UanPropModelIdeal>();
142 #endif // UAN_PROP_BH_INSTALLED
144  CreateObjectWithAttributes<UanChannel>("PropagationModel", PointerValue(prop));
145 
146  // Create net device and nodes with UanHelper
148  NetDeviceContainer sinkdev = uan.Install(sink, channel);
149 
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 
177  NS_LOG_DEBUG(
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;
201  Simulator::Schedule(nextEvent, &NetAnimExperiment::ResetData, this);
202  Simulator::Schedule(nextEvent, &NetAnimExperiment::UpdatePositions, this, nc);
203  }
204  Simulator::Schedule(nextEvent, &NetAnimExperiment::IncrementCw, this, cw);
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);
218 
219  m_bytesTotal = 0;
220 
221  std::string traceFileName = "uan-animation.xml";
222  AnimationInterface anim(traceFileName);
223 
224  Simulator::Run();
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 
248  Simulator::Destroy();
249  }
250 }
251 
252 int
253 main(int argc, char** argv)
254 {
255  LogComponentEnable("UanCwExample", LOG_LEVEL_ALL);
256  // LogComponentEnable ("AnimationInterface", LOG_LEVEL_ALL);
257 
258  NetAnimExperiment exp;
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;
285  mode = UanTxModeFactory::CreateMode(UanTxMode::FSK,
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 }
#define min(a, b)
Definition: 80211b.c:42
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:100
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.
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
void SetPosition(const Vector &position)
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.
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
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.
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:200
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:471
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:259
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:44
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
uint32_t GetSize() const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:863
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.
Hold objects of type Ptr<T>.
Definition: pointer.h:37
Object to create transport layer instances that provide a socket API to applications.
void SetRecvCallback(Callback< void, Ptr< Socket >> receivedData)
Notify application when new data is available to be read.
Definition: socket.cc:126
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
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:402
AttributeValue implementation for Time.
Definition: nstime.h:1423
a unique identifier for an interface.
Definition: type-id.h:60
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.
Class used for calculating SINR of packet in UanPhy.
Definition: uan-phy.h:43
Calculate packet error probability, based on received SINR and modulation (mode).
Definition: uan-phy.h:109
Abstraction of packet modulation information.
Definition: uan-tx-mode.h:43
Hold an unsigned integer type.
Definition: uinteger.h:45
double GetValue(double min, double max)
Get the next random value drawn from the distribution.
#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:877
#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 Now()
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:296
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1336
AnimationInterface * anim
NodeContainer nodes
devices
Definition: first.py:35
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:305
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:707
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
cmd
Definition: second.py:33
channel
Definition: third.py:81
mobility
Definition: third.py:96
Ptr< PacketSink > sink
Pointer to the packet sink application.
Definition: wifi-tcp.cc:55