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/tools-module.h"
45 #include "ns3/applications-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 ().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 = 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