A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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  ;
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 ().GetSeconds () << " 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 void
98 {
99 
100  NS_LOG_DEBUG (Simulator::Now ().GetSeconds () << " Updating positions");
101  NodeContainer::Iterator 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  {
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 = 0;
122 }
123 
124 void
126 {
127  uan.SetMac ("ns3::UanMacCw", "CW", UintegerValue (m_cwMin), "SlotTime", TimeValue (m_slotTime));
129  NodeContainer sink = NodeContainer ();
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 = CreateObjectWithAttributes<UanPropModelBh> ("ConfigFile", StringValue ("exbhconfig.cfg"));
139 #else
140  Ptr<UanPropModelIdeal> prop = CreateObjectWithAttributes<UanPropModelIdeal> ();
141 #endif //UAN_PROP_BH_INSTALLED
142  Ptr<UanChannel> channel = CreateObjectWithAttributes<UanChannel> ("PropagationModel", PointerValue (prop));
143 
144  //Create net device and nodes with UanHelper
145  NetDeviceContainer devices = uan.Install (nc, channel);
146  NetDeviceContainer sinkdev = uan.Install (sink, channel);
147 
148  MobilityHelper mobility;
149  Ptr<ListPositionAllocator> pos = CreateObject<ListPositionAllocator> ();
150 
151  {
152  Ptr<UniformRandomVariable> urv = CreateObject<UniformRandomVariable> ();
153  urv->SetAttribute ("Min", DoubleValue (0.0));
154  urv->SetAttribute ("Max", DoubleValue (m_boundary));
155  pos->Add (Vector (m_boundary / 2.0, m_boundary / 2.0, m_depth));
156  double rsum = 0;
157 
158  double minr = 2 * m_boundary;
159  for (uint32_t i = 0; i < m_numNodes; i++)
160  {
161  double x = urv->GetValue ();
162  double y = urv->GetValue ();
163  double newr = std::sqrt ((x - m_boundary / 2.0) * (x - m_boundary / 2.0)
164  + (y - m_boundary / 2.0) * (y - m_boundary / 2.0));
165  rsum += newr;
166  minr = std::min (minr, newr);
167  pos->Add (Vector (x, y, m_depth));
168  }
169  NS_LOG_DEBUG ("Mean range from gateway: " << rsum / m_numNodes
170  << " min. range " << minr);
171 
172  mobility.SetPositionAllocator (pos);
173  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
174  mobility.Install (sink);
175 
176  NS_LOG_DEBUG ("Position of sink: "
177  << sink.Get (0)->GetObject<MobilityModel> ()->GetPosition ());
178  mobility.Install (nc);
179 
180  PacketSocketAddress socket;
181  socket.SetSingleDevice (sinkdev.Get (0)->GetIfIndex ());
182  socket.SetPhysicalAddress (sinkdev.Get (0)->GetAddress ());
183  socket.SetProtocol (0);
184 
185  OnOffHelper app ("ns3::PacketSocketFactory", Address (socket));
186  app.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"));
187  app.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0.0]"));
188  app.SetAttribute ("DataRate", DataRateValue (m_dataRate));
189  app.SetAttribute ("PacketSize", UintegerValue (m_packetSize));
190 
191  ApplicationContainer apps = app.Install (nc);
192  apps.Start (Seconds (0.5));
193  Time nextEvent = Seconds (0.5);
194 
195 
196  for (uint32_t cw = m_cwMin; cw <= m_cwMax; cw += m_cwStep)
197  {
198 
199  for (uint32_t an = 0; an < m_avgs; an++)
200  {
201  nextEvent += m_simTime;
202  Simulator::Schedule (nextEvent, &NetAnimExperiment::ResetData, this);
203  Simulator::Schedule (nextEvent, &NetAnimExperiment::UpdatePositions, this, nc);
204  }
205  Simulator::Schedule (nextEvent, &NetAnimExperiment::IncrementCw, this, cw);
206  }
207  apps.Stop (nextEvent + m_simTime);
208 
209  Ptr<Node> sinkNode = sink.Get (0);
210  TypeId psfid = TypeId::LookupByName ("ns3::PacketSocketFactory");
211  if (sinkNode->GetObject<SocketFactory> (psfid) == 0)
212  {
213  Ptr<PacketSocketFactory> psf = CreateObject<PacketSocketFactory> ();
214  sinkNode->AggregateObject (psf);
215  }
216  Ptr<Socket> sinkSocket = Socket::CreateSocket (sinkNode, psfid);
217  sinkSocket->Bind (socket);
219 
220  m_bytesTotal = 0;
221 
222  std::string traceFileName = "uan-animation.xml";
223  AnimationInterface anim(traceFileName.c_str ());
224 
225  Simulator::Run ();
226  sinkNode = 0;
227  sinkSocket = 0;
228  pos = 0;
229  channel = 0;
230  prop = 0;
231  for (uint32_t i=0; i < nc.GetN (); i++)
232  {
233  nc.Get (i) = 0;
234  }
235  for (uint32_t i=0; i < sink.GetN (); i++)
236  {
237  sink.Get (i) = 0;
238  }
239 
240  for (uint32_t i=0; i < devices.GetN (); i++)
241  {
242  devices.Get (i) = 0;
243  }
244  for (uint32_t i=0; i < sinkdev.GetN (); i++)
245  {
246  sinkdev.Get (i) = 0;
247  }
248 
249  Simulator::Destroy ();
250  }
251 }
252 
253 int
254 main (int argc, char **argv)
255 {
256 
257  LogComponentEnable ("UanCwExample", LOG_LEVEL_ALL);
258  //LogComponentEnable ("AnimationInterface", LOG_LEVEL_ALL);
259 
260  NetAnimExperiment exp;
261 
262  std::string perModel = "ns3::UanPhyPerGenDefault";
263  std::string sinrModel = "ns3::UanPhyCalcSinrDefault";
264 
265  CommandLine cmd;
266  cmd.AddValue ("NumNodes", "Number of transmitting nodes", exp.m_numNodes);
267  cmd.AddValue ("Depth", "Depth of transmitting and sink nodes", exp.m_depth);
268  cmd.AddValue ("RegionSize", "Size of boundary in meters", exp.m_boundary);
269  cmd.AddValue ("PacketSize", "Generated packet size in bytes", exp.m_packetSize);
270  cmd.AddValue ("DataRate", "DataRate in bps", exp.m_dataRate);
271  cmd.AddValue ("CwMin", "Min CW to simulate", exp.m_cwMin);
272  cmd.AddValue ("CwMax", "Max CW to simulate", exp.m_cwMax);
273  cmd.AddValue ("SlotTime", "Slot time duration", exp.m_slotTime);
274  cmd.AddValue ("Averages", "Number of topologies to test for each cw point", exp.m_avgs);
275  cmd.AddValue ("PerModel", "PER model name", perModel);
276  cmd.AddValue ("SinrModel", "SINR model name", sinrModel);
277  cmd.Parse (argc, argv);
278 
279  ObjectFactory obf;
280  obf.SetTypeId (perModel);
281  Ptr<UanPhyPer> per = obf.Create<UanPhyPer> ();
282  obf.SetTypeId (sinrModel);
284 
285  UanHelper uan;
286  UanTxMode mode;
287  mode = UanTxModeFactory::CreateMode (UanTxMode::FSK, exp.m_dataRate,
288  exp.m_dataRate, 12000,
289  exp.m_dataRate, 2,
290  "Default mode");
291  UanModesList myModes;
292  myModes.AppendMode (mode);
293 
294  uan.SetPhy ("ns3::UanPhyGen",
295  "PerModel", PointerValue (per),
296  "SinrModel", PointerValue (sinr),
297  "SupportedModes", UanModesListValue (myModes));
298 
299  exp.Run (uan);
300 
301  per = 0;
302  sinr = 0;
303 
304 }
305 
UAN configuration helper.
Definition: uan-helper.h:39
void ReceivePacket(Ptr< Socket > socket)
holds a vector of ns3::Application pointers.
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
tuple devices
Definition: first.py:32
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
std::vector< Ptr< Node > >::const_iterator Iterator
hold variables of type string
Definition: string.h:19
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
static Vector GetPosition(Ptr< Node > node)
Definition: multirate.cc:315
an address for a packet socket
#define NS_ASSERT(condition)
Definition: assert.h:64
void SetTypeId(TypeId tid)
uint32_t GetSize(void) const
Definition: packet.h:650
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
Container for UanTxModes.
Definition: uan-tx-mode.h:255
void UpdatePositions(NodeContainer &nodes)
void Set(std::string path, const AttributeValue &value)
Definition: config.cc:662
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
a 3d vector
Definition: vector.h:31
Give ns3::PacketSocket powers to ns3::Node.
void SetSingleDevice(uint32_t device)
uint32_t m_packetSize
Definition: uan-animation.h:46
a polymophic address class
Definition: address.h:86
uint32_t GetN(void) const
Get the number of Ptr stored in this container.
uint32_t GetN(void) const
Get the number of Ptr stored in this container.
tuple nodes
Definition: first.py:25
Keep track of the current position and velocity of an object.
double GetSeconds(void) const
Definition: nstime.h:274
uint32_t m_bytesTotal
Definition: uan-animation.h:47
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
int main(int argc, char **argv)
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:91
hold objects of type ns3::Time
Definition: nstime.h:961
Ptr< Object > Create(void) const
Hold an unsigned integer type.
Definition: uinteger.h:46
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:1238
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
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)
void AggregateObject(Ptr< Object > other)
Definition: object.cc:243
Parse command-line arguments.
Definition: command-line.h:152
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
void SetPhysicalAddress(const Address address)
keep track of a set of node pointers.
hold objects of type Ptr
Definition: pointer.h:33
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
Iterator Begin(void) const
Get an iterator which refers to the first Node in the container.
void SetMobilityModel(std::string type, 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(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue())
double GetValue(double min, double max)
Returns a random double from the uniform distribution with the specified range.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
void Run(UanHelper &uan)
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
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...
hold objects of type ns3::DataRate
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:408
Time Now(void)
create an ns3::Time instance which contains the current simulation time.
Definition: simulator.cc:287
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
#define NS_LOG_DEBUG(msg)
Definition: log.h:289
void AppendMode(UanTxMode mode)
Add mode to this list.
Definition: uan-tx-mode.cc:232
void SetProtocol(uint16_t protocol)
Interface to network animator.
void Parse(int argc, char *argv[])
Parse the program arguments.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
NetDeviceContainer Install(NodeContainer c) const
This method creates a simple ns3::UanChannel (with a default ns3::UanNoiseModelDefault and ns3::UanPr...
Definition: uan-helper.cc:208
Hold a floating point type.
Definition: double.h:41
void SetAttribute(std::string name, const AttributeValue &value)
Definition: object-base.cc:161
Ptr< T > GetObject(void) const
Definition: object.h:361
ApplicationContainer Install(NodeContainer c) const
Install an ns3::OnOffApplication on each node of the input container configured with all the attribut...
a unique identifier for an interface.
Definition: type-id.h:49
std::vector< double > m_throughputs
Definition: uan-animation.h:56
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
void LogComponentEnable(char const *name, enum LogLevel level)
Definition: log.cc:311
Attribute Value class for UanTxModes.