A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
uan-cw-example.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 University of Washington
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: Leonard Tracy <lentracy@gmail.com>
19  */
20 
21 
39 #include "uan-cw-example.h"
40 #include "ns3/core-module.h"
41 #include "ns3/network-module.h"
42 #include "ns3/mobility-module.h"
43 #include "ns3/tools-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 Experiment::Experiment ()
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  m_gnudatfile ("uan-cw-example.gpl"),
66  m_asciitracefile ("uan-cw-example.asc"),
67  m_bhCfgFile ("uan-apps/dat/default.cfg")
68 {
69 }
70 
71 void
72 Experiment::ResetData ()
73 {
74  NS_LOG_DEBUG (Simulator::Now ().GetSeconds () << " Resetting data");
75  m_throughputs.push_back (m_bytesTotal * 8.0 / m_simTime.GetSeconds ());
76  m_bytesTotal = 0;
77 }
78 
79 void
80 Experiment::IncrementCw (uint32_t cw)
81 {
82  NS_ASSERT (m_throughputs.size () == m_avgs);
83 
84  double avgThroughput = 0.0;
85  for (uint32_t i=0; i<m_avgs; i++)
86  {
87  avgThroughput += m_throughputs[i];
88  }
89  avgThroughput /= m_avgs;
90  m_data.Add (cw, avgThroughput);
91  m_throughputs.clear ();
92 
93  Config::Set ("/NodeList/*/DeviceList/*/Mac/CW", UintegerValue (cw + m_cwStep));
94 
96 
97  NS_LOG_DEBUG ("Average for cw=" << cw << " over " << m_avgs << " runs: " << avgThroughput);
98 }
99 void
100 Experiment::UpdatePositions (NodeContainer &nodes)
101 {
102 
103  NS_LOG_DEBUG (Simulator::Now ().GetSeconds () << " Updating positions");
104  NodeContainer::Iterator it = nodes.Begin ();
105  Ptr<UniformRandomVariable> uv = CreateObject<UniformRandomVariable> ();
106  for (; it != nodes.End (); it++)
107  {
109  mp->SetPosition (Vector (uv->GetValue (0, m_boundary), uv->GetValue (0, m_boundary), 70.0));
110  }
111 }
112 
113 void
114 Experiment::ReceivePacket (Ptr<Socket> socket)
115 {
116  Ptr<Packet> packet;
117 
118  while ((packet = socket->Recv ()))
119  {
120  m_bytesTotal += packet->GetSize ();
121  }
122  packet = 0;
123 }
124 
126 Experiment::Run (UanHelper &uan)
127 {
128  uan.SetMac ("ns3::UanMacCw", "CW", UintegerValue (m_cwMin), "SlotTime", TimeValue (m_slotTime));
130  NodeContainer sink = NodeContainer ();
131  nc.Create (m_numNodes);
132  sink.Create (1);
133 
134  PacketSocketHelper socketHelper;
135  socketHelper.Install (nc);
136  socketHelper.Install (sink);
137 
138 #ifdef UAN_PROP_BH_INSTALLED
139  Ptr<UanPropModelBh> prop = CreateObjectWithAttributes<UanPropModelBh> ("ConfigFile", StringValue ("exbhconfig.cfg"));
140 #else
141  Ptr<UanPropModelIdeal> prop = CreateObjectWithAttributes<UanPropModelIdeal> ();
142 #endif //UAN_PROP_BH_INSTALLED
143  Ptr<UanChannel> channel = CreateObjectWithAttributes<UanChannel> ("PropagationModel", PointerValue (prop));
144 
145  //Create net device and nodes with UanHelper
146  NetDeviceContainer devices = uan.Install (nc, channel);
147  NetDeviceContainer sinkdev = uan.Install (sink, channel);
148 
149  MobilityHelper mobility;
150  Ptr<ListPositionAllocator> pos = CreateObject<ListPositionAllocator> ();
151 
152  {
153  Ptr<UniformRandomVariable> urv = CreateObject<UniformRandomVariable> ();
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 (0, m_boundary);
161  double y = urv->GetValue (0, m_boundary);
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  }
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]"));
187  app.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=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, &Experiment::ResetData, this);
203  Simulator::Schedule (nextEvent, &Experiment::UpdatePositions, this, nc);
204  }
205  Simulator::Schedule (nextEvent, &Experiment::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);
218  sinkSocket->SetRecvCallback (MakeCallback (&Experiment::ReceivePacket, this));
219 
220  m_bytesTotal = 0;
221 
222  std::ofstream ascii (m_asciitracefile.c_str ());
223  if (!ascii.is_open ())
224  {
225  NS_FATAL_ERROR ("Could not open ascii trace file: "
226  << m_asciitracefile);
227  }
228  uan.EnableAsciiAll (ascii);
229 
230  Simulator::Run ();
231  sinkNode = 0;
232  sinkSocket = 0;
233  pos = 0;
234  channel = 0;
235  prop = 0;
236  for (uint32_t i=0; i < nc.GetN (); i++)
237  {
238  nc.Get (i) = 0;
239  }
240  for (uint32_t i=0; i < sink.GetN (); i++)
241  {
242  sink.Get (i) = 0;
243  }
244 
245  for (uint32_t i=0; i < devices.GetN (); i++)
246  {
247  devices.Get (i) = 0;
248  }
249  for (uint32_t i=0; i < sinkdev.GetN (); i++)
250  {
251  sinkdev.Get (i) = 0;
252  }
253 
255  return m_data;
256  }
257 }
258 
259 int
260 main (int argc, char **argv)
261 {
262 
263  LogComponentEnable ("UanCwExample", LOG_LEVEL_ALL);
264 
265  Experiment exp;
266 
267  std::string gnudatfile ("cwexpgnuout.dat");
268  std::string perModel = "ns3::UanPhyPerGenDefault";
269  std::string sinrModel = "ns3::UanPhyCalcSinrDefault";
270 
271  CommandLine cmd;
272  cmd.AddValue ("NumNodes", "Number of transmitting nodes", exp.m_numNodes);
273  cmd.AddValue ("Depth", "Depth of transmitting and sink nodes", exp.m_depth);
274  cmd.AddValue ("RegionSize", "Size of boundary in meters", exp.m_boundary);
275  cmd.AddValue ("PacketSize", "Generated packet size in bytes", exp.m_packetSize);
276  cmd.AddValue ("DataRate", "DataRate in bps", exp.m_dataRate);
277  cmd.AddValue ("CwMin", "Min CW to simulate", exp.m_cwMin);
278  cmd.AddValue ("CwMax", "Max CW to simulate", exp.m_cwMax);
279  cmd.AddValue ("SlotTime", "Slot time duration", exp.m_slotTime);
280  cmd.AddValue ("Averages", "Number of topologies to test for each cw point", exp.m_avgs);
281  cmd.AddValue ("GnuFile", "Name for GNU Plot output", exp.m_gnudatfile);
282  cmd.AddValue ("PerModel", "PER model name", perModel);
283  cmd.AddValue ("SinrModel", "SINR model name", sinrModel);
284  cmd.Parse (argc, argv);
285 
286  ObjectFactory obf;
287  obf.SetTypeId (perModel);
288  Ptr<UanPhyPer> per = obf.Create<UanPhyPer> ();
289  obf.SetTypeId (sinrModel);
291 
292  UanHelper uan;
293  UanTxMode mode;
294  mode = UanTxModeFactory::CreateMode (UanTxMode::FSK, exp.m_dataRate,
295  exp.m_dataRate, 12000,
296  exp.m_dataRate, 2,
297  "Default mode");
298  UanModesList myModes;
299  myModes.AppendMode (mode);
300 
301  uan.SetPhy ("ns3::UanPhyGen",
302  "PerModel", PointerValue (per),
303  "SinrModel", PointerValue (sinr),
304  "SupportedModes", UanModesListValue (myModes));
305 
306  Gnuplot gp;
307  Gnuplot2dDataset ds;
308  ds = exp.Run (uan);
309 
310  gp.AddDataset (ds);
311 
312  std::ofstream of (exp.m_gnudatfile.c_str ());
313  if (!of.is_open ())
314  {
315  NS_FATAL_ERROR ("Can not open GNU Plot outfile: " << exp.m_gnudatfile);
316  }
317  gp.GenerateOutput (of);
318 
319  per = 0;
320  sinr = 0;
321 
322 }
323 
Helper class for UAN CW MAC example.
holds a vector of ns3::Application pointers.
keep track of time unit.
Definition: nstime.h:149
Class to represent a 2D points plot. Set the line or points style using SetStyle() and set points usi...
Definition: gnuplot.h:107
hold variables of type string
Definition: string.h:19
Ptr< NetDevice > Get(uint32_t i) const
Get the Ptr<NetDevice> stored in this container at a given index.
void GenerateOutput(std::ostream &os) const
Definition: gnuplot.cc:626
an address for a packet socket
#define NS_ASSERT(condition)
Definition: assert.h:64
static void Run(void)
Definition: simulator.cc:157
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
void SetTypeId(TypeId tid)
uint32_t GetSize(void) const
Definition: packet.h:620
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:620
Iterator End(void) const
Get an iterator which indicates past-the-last Node in the container.
Container for UanTxModes.
Definition: uan-tx-mode.h:162
void Set(std::string path, const AttributeValue &value)
Definition: config.cc:662
static EventId Schedule(Time const &time, MEM mem_ptr, OBJ obj)
Definition: simulator.h:820
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:41
a 3d vector
Definition: vector.h:31
Give ns3::PacketSocket powers to ns3::Node.
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
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
static void SetRun(uint64_t run)
Set the run number of simulation.
a polymophic address class
Definition: address.h:86
static uint64_t GetRun(void)
uint32_t GetN(void) const
Get the number of Ptr<Node> stored in this container.
uint32_t GetN(void) const
Get the number of Ptr<NetDevice> stored in this container.
Keep track of the current position and velocity of an object.
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
a simple class to generate gnuplot-ready plotting commands from a set of datasets.
Definition: gnuplot.h:343
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())
Definition: uan-helper.cc:73
hold objects of type ns3::Time
Definition: nstime.h:700
Ptr< Object > Create(void) const
Hold an unsigned integer type.
Definition: uinteger.h:46
Abstraction of packet modulation information.
Definition: uan-tx-mode.h:36
holds a vector of ns3::NetDevice pointers
Callback< R > MakeCallback(R(T::*memPtr)(void), OBJ objPtr)
Definition: callback.h:502
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:127
static Ptr< Socket > CreateSocket(Ptr< Node > node, TypeId tid)
Definition: socket.cc:70
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
void AggregateObject(Ptr< Object > other)
Definition: object.cc:242
parse command-line argumentsInstances of this class can be used to parse command-line arguments: user...
Definition: command-line.h:50
static void Destroy(void)
Definition: simulator.cc:121
virtual int Bind(const Address &address)=0
Allocate a local endpoint for this socket.
keep track of a set of node pointers.
hold objects of type Ptr<T>
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
static Time Now(void)
Definition: simulator.cc:179
static void EnableAsciiAll(std::ostream &os)
Definition: uan-helper.cc:184
void SetPosition(const Vector &position)
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...
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
hold objects of type ns3::DataRate
void AddValue(const std::string &name, const std::string &help, T &value)
Definition: command-line.h:134
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
#define NS_LOG_DEBUG(msg)
Definition: log.h:255
void Parse(int argc, char *argv[]) const
Definition: command-line.cc:84
void AppendMode(UanTxMode mode)
Definition: uan-tx-mode.cc:232
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
NetDeviceContainer Install(NodeContainer c) const
Definition: uan-helper.cc:190
Ptr< T > GetObject(void) const
Definition: object.h:332
a unique identifier for an interface.
Definition: type-id.h:44
void LogComponentEnable(char const *name, enum LogLevel level)
Definition: log.cc:311
static TypeId LookupByName(std::string name)
Definition: type-id.cc:415