A Discrete-Event Network Simulator
API
uan-animation.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2010 Andrea Sacco
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Andrea Sacco <andrea.sacco85@gmail.com>
19  */
20 
21 
39 #include "uan-animation.h"
40 #include "ns3/core-module.h"
41 #include "ns3/network-module.h"
42 #include "ns3/netanim-module.h"
43 #include "ns3/mobility-module.h"
44 #include "ns3/applications-module.h"
45 
46 #include <fstream>
47 
48 using namespace ns3;
49 
50 NS_LOG_COMPONENT_DEFINE ("UanCwExample");
51 
53  : m_numNodes (15),
54  m_dataRate (80),
55  m_depth (70),
56  m_boundary (500),
57  m_packetSize (32),
58  m_bytesTotal (0),
59  m_cwMin (10),
60  m_cwMax (400),
61  m_cwStep (10),
62  m_avgs (3),
63  m_slotTime (Seconds (0.2)),
64  m_simTime (Seconds (1000))
65 {
66 }
67 
68 void
70 {
71  NS_LOG_DEBUG (Simulator::Now ().GetSeconds () << " Resetting data");
72  m_throughputs.push_back (m_bytesTotal * 8.0 / m_simTime.GetSeconds ());
73  m_bytesTotal = 0;
74 }
75 
76 void
78 {
79  NS_ASSERT (m_throughputs.size () == m_avgs);
80 
81  double avgThroughput = 0.0;
82  for (uint32_t i=0; i<m_avgs; i++)
83  {
84  avgThroughput += m_throughputs[i];
85  }
86  avgThroughput /= m_avgs;
87  m_throughputs.clear ();
88 
89  Config::Set ("/NodeList/*/DeviceList/*/Mac/CW", UintegerValue (cw + m_cwStep));
90 
91  SeedManager::SetRun (SeedManager::GetRun () + 1);
92 
93  NS_LOG_DEBUG ("Average for cw=" << cw << " over " << m_avgs << " runs: " << avgThroughput);
94 }
95 void
97 {
98 
99  NS_LOG_DEBUG (Simulator::Now ().GetSeconds () << " Updating positions");
100  NodeContainer::Iterator it = nodes.Begin ();
101  Ptr<UniformRandomVariable> uv = CreateObject<UniformRandomVariable> ();
102  uv->SetAttribute ("Min", DoubleValue (0.0));
103  uv->SetAttribute ("Max", DoubleValue (m_boundary));
104  for (; it != nodes.End (); it++)
105  {
107  mp->SetPosition (Vector (uv->GetValue (), uv->GetValue (), 70.0));
108  }
109 }
110 
111 void
113 {
114  Ptr<Packet> packet;
115 
116  while ((packet = socket->Recv ()))
117  {
118  m_bytesTotal += packet->GetSize ();
119  }
120  packet = 0;
121 }
122 
123 void
125 {
126  uan.SetMac ("ns3::UanMacCw", "CW", UintegerValue (m_cwMin), "SlotTime", TimeValue (m_slotTime));
129  nc.Create (m_numNodes);
130  sink.Create (1);
131 
132  PacketSocketHelper socketHelper;
133  socketHelper.Install (nc);
134  socketHelper.Install (sink);
135 
136 #ifdef UAN_PROP_BH_INSTALLED
137  Ptr<UanPropModelBh> prop = CreateObjectWithAttributes<UanPropModelBh> ("ConfigFile", StringValue ("exbhconfig.cfg"));
138 #else
139  Ptr<UanPropModelIdeal> prop = CreateObjectWithAttributes<UanPropModelIdeal> ();
140 #endif //UAN_PROP_BH_INSTALLED
141  Ptr<UanChannel> channel = CreateObjectWithAttributes<UanChannel> ("PropagationModel", PointerValue (prop));
142 
143  //Create net device and nodes with UanHelper
145  NetDeviceContainer sinkdev = uan.Install (sink, channel);
146 
148  Ptr<ListPositionAllocator> pos = CreateObject<ListPositionAllocator> ();
149 
150  {
151  Ptr<UniformRandomVariable> urv = CreateObject<UniformRandomVariable> ();
152  urv->SetAttribute ("Min", DoubleValue (0.0));
153  urv->SetAttribute ("Max", DoubleValue (m_boundary));
154  pos->Add (Vector (m_boundary / 2.0, m_boundary / 2.0, m_depth));
155  double rsum = 0;
156 
157  double minr = 2 * m_boundary;
158  for (uint32_t i = 0; i < m_numNodes; i++)
159  {
160  double x = urv->GetValue ();
161  double y = urv->GetValue ();
162  double newr = std::sqrt ((x - m_boundary / 2.0) * (x - m_boundary / 2.0)
163  + (y - m_boundary / 2.0) * (y - m_boundary / 2.0));
164  rsum += newr;
165  minr = std::min (minr, newr);
166  pos->Add (Vector (x, y, m_depth));
167  }
168  NS_LOG_DEBUG ("Mean range from gateway: " << rsum / m_numNodes
169  << " min. range " << minr);
170 
171  mobility.SetPositionAllocator (pos);
172  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
173  mobility.Install (sink);
174 
175  NS_LOG_DEBUG ("Position of sink: "
176  << sink.Get (0)->GetObject<MobilityModel> ()->GetPosition ());
177  mobility.Install (nc);
178 
179  PacketSocketAddress socket;
180  socket.SetSingleDevice (sinkdev.Get (0)->GetIfIndex ());
181  socket.SetPhysicalAddress (sinkdev.Get (0)->GetAddress ());
182  socket.SetProtocol (0);
183 
184  OnOffHelper app ("ns3::PacketSocketFactory", Address (socket));
185  app.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"));
186  app.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0.0]"));
187  app.SetAttribute ("DataRate", DataRateValue (m_dataRate));
188  app.SetAttribute ("PacketSize", UintegerValue (m_packetSize));
189 
190  ApplicationContainer apps = app.Install (nc);
191  apps.Start (Seconds (0.5));
192  Time nextEvent = Seconds (0.5);
193 
194 
195  for (uint32_t cw = m_cwMin; cw <= m_cwMax; cw += m_cwStep)
196  {
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) == 0)
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.c_str ());
223 
224  Simulator::Run ();
225  sinkNode = 0;
226  sinkSocket = 0;
227  pos = 0;
228  channel = 0;
229  prop = 0;
230  for (uint32_t i=0; i < nc.GetN (); i++)
231  {
232  nc.Get (i) = 0;
233  }
234  for (uint32_t i=0; i < sink.GetN (); i++)
235  {
236  sink.Get (i) = 0;
237  }
238 
239  for (uint32_t i=0; i < devices.GetN (); i++)
240  {
241  devices.Get (i) = 0;
242  }
243  for (uint32_t i=0; i < sinkdev.GetN (); i++)
244  {
245  sinkdev.Get (i) = 0;
246  }
247 
248  Simulator::Destroy ();
249  }
250 }
251 
252 int
253 main (int argc, char **argv)
254 {
255 
256  LogComponentEnable ("UanCwExample", LOG_LEVEL_ALL);
257  //LogComponentEnable ("AnimationInterface", LOG_LEVEL_ALL);
258 
259  NetAnimExperiment exp;
260 
261  std::string perModel = "ns3::UanPhyPerGenDefault";
262  std::string sinrModel = "ns3::UanPhyCalcSinrDefault";
263 
265  cmd.AddValue ("NumNodes", "Number of transmitting nodes", exp.m_numNodes);
266  cmd.AddValue ("Depth", "Depth of transmitting and sink nodes", exp.m_depth);
267  cmd.AddValue ("RegionSize", "Size of boundary in meters", exp.m_boundary);
268  cmd.AddValue ("PacketSize", "Generated packet size in bytes", exp.m_packetSize);
269  cmd.AddValue ("DataRate", "DataRate in bps", exp.m_dataRate);
270  cmd.AddValue ("CwMin", "Min CW to simulate", exp.m_cwMin);
271  cmd.AddValue ("CwMax", "Max CW to simulate", exp.m_cwMax);
272  cmd.AddValue ("SlotTime", "Slot time duration", exp.m_slotTime);
273  cmd.AddValue ("Averages", "Number of topologies to test for each cw point", exp.m_avgs);
274  cmd.AddValue ("PerModel", "PER model name", perModel);
275  cmd.AddValue ("SinrModel", "SINR model name", sinrModel);
276  cmd.Parse (argc, argv);
277 
278  ObjectFactory obf;
279  obf.SetTypeId (perModel);
280  Ptr<UanPhyPer> per = obf.Create<UanPhyPer> ();
281  obf.SetTypeId (sinrModel);
283 
284  UanHelper uan;
285  UanTxMode mode;
286  mode = UanTxModeFactory::CreateMode (UanTxMode::FSK, exp.m_dataRate,
287  exp.m_dataRate, 12000,
288  exp.m_dataRate, 2,
289  "Default mode");
290  UanModesList myModes;
291  myModes.AppendMode (mode);
292 
293  uan.SetPhy ("ns3::UanPhyGen",
294  "PerModel", PointerValue (per),
295  "SinrModel", PointerValue (sinr),
296  "SupportedModes", UanModesListValue (myModes));
297 
298  exp.Run (uan);
299 
300  per = 0;
301  sinr = 0;
302 
303 }
304 
UAN configuration helper.
Definition: uan-helper.h:40
void ReceivePacket(Ptr< Socket > socket)
Receive packet function.
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56
holds a vector of ns3::Application pointers.
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:102
void ResetData()
Reset data function.
std::vector< Ptr< Node > >::const_iterator Iterator
Node container iterator.
uint32_t GetSize(void) const
Returns the the size in bytes of the packet (including the zero-filled initial payload).
Definition: packet.h:831
Hold variables of type string.
Definition: string.h:41
#define min(a, b)
Definition: 80211b.c:42
void Set(std::string path, const AttributeValue &value)
Definition: config.cc:777
uint32_t m_dataRate
data rate
Definition: uan-animation.h:60
static Vector GetPosition(Ptr< Node > node)
Definition: multirate.cc:341
void AggregateObject(Ptr< Object > other)
Aggregate two Objects together.
Definition: object.cc:252
double GetSeconds(void) const
Get an approximation of the time stored in this instance in the indicated unit.
Definition: nstime.h:355
an address for a packet socket
double m_depth
depth
Definition: uan-animation.h:61
#define NS_ASSERT(condition)
At runtime, in debugging builds, if this condition is not true, the program prints the source file...
Definition: assert.h:67
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
Container for UanTxModes.
Definition: uan-tx-mode.h:257
cmd
Definition: second.py:35
void UpdatePositions(NodeContainer &nodes)
Update positions function.
Object to create transport layer instances that provide a socket API to applications.
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:42
Give ns3::PacketSocket powers to ns3::Node.
void SetSingleDevice(uint32_t device)
Set the address to match only a specified NetDevice.
uint32_t m_cwMax
CW maximum.
Definition: uan-animation.h:66
uint32_t m_packetSize
packet size
Definition: uan-animation.h:63
a polymophic address class
Definition: address.h:90
channel
Definition: third.py:85
mobility
Definition: third.py:101
Keep track of the current position and velocity of an object.
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
uint32_t m_bytesTotal
bytes total
Definition: uan-animation.h:64
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:369
nodes
Definition: first.py:25
void SetMac(std::string type, std::string n0="", const AttributeValue &v0=EmptyAttributeValue(), std::string n1="", const AttributeValue &v1=EmptyAttributeValue(), std::string n2="", const AttributeValue &v2=EmptyAttributeValue(), std::string n3="", const AttributeValue &v3=EmptyAttributeValue(), std::string n4="", const AttributeValue &v4=EmptyAttributeValue(), std::string n5="", const AttributeValue &v5=EmptyAttributeValue(), std::string n6="", const AttributeValue &v6=EmptyAttributeValue(), std::string n7="", const AttributeValue &v7=EmptyAttributeValue())
Set MAC attributes.
Definition: uan-helper.cc:95
AttributeValue implementation for Time.
Definition: nstime.h:1076
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
Hold an unsigned integer type.
Definition: uinteger.h:44
Abstraction of packet modulation information.
Definition: uan-tx-mode.h:41
holds a vector of ns3::NetDevice pointers
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:1489
Calculate packet error probability, based on received SINR and modulation (mode). ...
Definition: uan-phy.h:110
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:128
Time m_slotTime
slot time
Definition: uan-animation.h:70
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
void IncrementCw(uint32_t cw)
Increment CW function.
Parse command-line arguments.
Definition: command-line.h:213
uint32_t GetN(void) const
Get the number of Ptr<Node> stored in this container.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
void SetPhysicalAddress(const Address address)
Set the destination address.
keep track of a set of node pointers.
Time m_simTime
simulation time
Definition: uan-animation.h:71
Hold objects of type Ptr<T>.
Definition: pointer.h:36
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
void Run(UanHelper &uan)
Run function.
void SetPosition(const Vector &position)
Class used for calculating SINR of packet in UanPhy.
Definition: uan-phy.h:44
Helper class for UAN CW MAC example.
Definition: uan-animation.h:34
NetAnimExperiment()
the experiment
Helper class used to assign positions and mobility models to nodes.
Instantiate subclasses of ns3::Object.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
double m_boundary
boundary
Definition: uan-animation.h:62
uint32_t m_numNodes
number of nodes
Definition: uan-animation.h:59
AttributeValue implementation for DataRate.
Definition: data-rate.h:242
#define NS_LOG_DEBUG(msg)
Use NS_LOG to output a message of level LOG_DEBUG.
Definition: log.h:270
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
uint32_t GetN(void) const
Get the number of Ptr<NetDevice> stored in this container.
void AppendMode(UanTxMode mode)
Add mode to this list.
Definition: uan-tx-mode.cc:232
void SetProtocol(uint16_t protocol)
Set the protocol.
void Add(Vector v)
Add a position to the list of positions.
Interface to network animator.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
Print everything.
Definition: log.h:113
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:365
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
uint32_t m_cwMin
CW minimum.
Definition: uan-animation.h:65
devices
Definition: first.py:32
This class can be used to hold variables of floating point type such as &#39;double&#39; or &#39;float&#39;...
Definition: double.h:41
void SetAttribute(std::string name, const AttributeValue &value)
Set a single attribute, raising fatal errors if unsuccessful.
Definition: object-base.cc:185
a unique identifier for an interface.
Definition: type-id.h:58
NetDeviceContainer Install(NodeContainer c) const
This method creates a simple ns3::UanChannel (with a default ns3::UanNoiseModelDefault and ns3::UanPr...
Definition: uan-helper.cc:212
uint32_t m_cwStep
CW step.
Definition: uan-animation.h:67
std::vector< double > m_throughputs
throughputs
Definition: uan-animation.h:73
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
AttributeValue implementation for UanModesList.
Definition: uan-tx-mode.h:314
uint32_t m_avgs
averages
Definition: uan-animation.h:68