A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
uan-rc-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 
54 #include "uan-rc-example.h"
55 #include "ns3/core-module.h"
56 #include "ns3/network-module.h"
57 #include "ns3/applications-module.h"
58 #include "ns3/mobility-module.h"
59 #include "ns3/log.h"
60 #include "ns3/config.h"
61 #include "ns3/callback.h"
62 #include "ns3/tools-module.h"
63 
64 #include <fstream>
65 
66 using namespace ns3;
67 
68 NS_LOG_COMPONENT_DEFINE ("UanRcExample");
69 
70 
72  : m_simMin (1),
73  m_simMax (1),
74  m_simStep (1),
75  m_numRates (1023),
76  m_totalRate (4096),
77  m_maxRange (3000),
78  m_numNodes (15),
79  m_pktSize (1000),
80  m_doNode (true),
81  m_sifs (Seconds (0.05)),
82  m_simTime (Seconds (5000)),
83  m_gnuplotfile ("uan-rc-example.gpl"),
84  m_bytesTotal (0)
85 {
86 }
87 
88 void
90 {
91  Ptr<Packet> packet;
92  while ((packet = socket->Recv ()))
93  {
94  m_bytesTotal += packet->GetSize ();
95  }
96 }
97 
99 Experiment::CreateMode (uint32_t kass,
100  uint32_t fc,
101  bool upperblock,
102  std::string name)
103 {
104 
105  std::ostringstream buf;
106  buf << name << " " << kass;
107 
108  uint32_t rate = m_totalRate/(m_numRates+1)* (kass);
109  uint32_t bw = kass * m_totalRate / (m_numRates+1);
110  uint32_t fcmode;
111  if(upperblock)
112  fcmode = (m_totalRate - bw)/2 + fc;
113  else
114  fcmode = (uint32_t)((-((double) m_totalRate ) + (double) bw)/2.0 + (double) fc);
115 
116 
117  uint32_t phyrate = m_totalRate;
118 
119  UanTxMode mode;
120  mode = UanTxModeFactory::CreateMode (UanTxMode::OTHER,
121  rate,
122  phyrate,
123  fcmode,
124  bw,
125  2,
126  buf.str ());
127  return mode;
128 }
129 
130 //Creates m_numRates different modes each dividing m_totalRate Hz (assumes 1 bit per hz)
131 //centered at frequency fc
132 void
134 {
135 
136 
137  for (uint32_t i=1; i < m_numRates+1; i++)
138  {
139  m_controlModes.AppendMode (CreateMode (i, fc, false, "control "));
140  }
141  for (uint32_t i=m_numRates; i > 0; i--)
142  {
143  m_dataModes.AppendMode (CreateMode (i, fc, true, "data "));
144  }
145 }
146 
147 uint32_t
148 Experiment::Run (uint32_t param)
149 {
150 
151  UanHelper uan;
152 
153  m_bytesTotal=0;
154 
155  uint32_t nNodes;
156  uint32_t a;
157  if(m_doNode)
158  {
159  a=0;
160  nNodes = param;
161  }
162  else
163  {
164  nNodes = m_numNodes;
165  a = param;
166  }
167  Time pDelay = Seconds ((double) m_maxRange / 1500.0);
168 
169  uan.SetPhy ("ns3::UanPhyDual",
170  "SupportedModesPhy1", UanModesListValue (m_dataModes),
171  "SupportedModesPhy2", UanModesListValue (m_controlModes));
172 
173  uan.SetMac ("ns3::UanMacRcGw",
174  "NumberOfRates", UintegerValue (m_numRates),
175  "NumberOfNodes", UintegerValue (nNodes),
176  "MaxReservations", UintegerValue (a),
177  "RetryRate", DoubleValue (1/30.0),
178  "SIFS", TimeValue (m_sifs),
179  "MaxPropDelay", TimeValue (pDelay),
180  "FrameSize", UintegerValue (m_pktSize));
181  Ptr<UanChannel> chan = CreateObject<UanChannel>();
182 
183  NodeContainer sink;
184  sink.Create (1);
185  NetDeviceContainer sinkDev = uan.Install (sink, chan);
186 
187  uan.SetMac ("ns3::UanMacRc",
188  "NumberOfRates", UintegerValue (m_numRates),
189  "MaxPropDelay", TimeValue (pDelay),
190  "RetryRate", DoubleValue (1.0/100.0));
191  NodeContainer nodes;
192  nodes.Create (nNodes);
193  NetDeviceContainer devices = uan.Install (nodes, chan);
194 
195  MobilityHelper mobility;
196  uint32_t depth = 70;
197  Ptr<ListPositionAllocator> pos = CreateObject<ListPositionAllocator> ();
198 
199  Ptr<UniformRandomVariable> urv = CreateObject<UniformRandomVariable> ();
200  Ptr<UniformRandomVariable> utheta = CreateObject<UniformRandomVariable> ();
201  pos->Add (Vector (m_maxRange, m_maxRange, depth));
202 
203  for (uint32_t i=0; i<nNodes; i++)
204  {
205  double theta = utheta->GetValue (0, 2.0*M_PI);
206  double r = urv->GetValue (0,m_maxRange);
207 
208  double x = m_maxRange + r*std::cos (theta);
209  double y = m_maxRange + r*std::sin (theta);
210 
211  pos->Add (Vector (x, y, depth));
212 
213  }
214 
215  mobility.SetPositionAllocator (pos);
216  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
217  mobility.Install (sink);
218  mobility.Install (nodes);
219 
220  PacketSocketHelper pktskth;
221  pktskth.Install (nodes);
222  pktskth.Install (sink);
223 
224  PacketSocketAddress socket;
225  socket.SetSingleDevice (sinkDev.Get (0)->GetIfIndex ());
226  socket.SetPhysicalAddress (sinkDev.Get (0)->GetAddress ());
227  socket.SetProtocol (0);
228 
229  OnOffHelper app ("ns3::PacketSocketFactory", Address (socket));
230  app.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
231  app.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
232  app.SetAttribute ("DataRate", DataRateValue (m_totalRate));
233  app.SetAttribute ("PacketSize", UintegerValue (m_pktSize));
234 
235  ApplicationContainer apps = app.Install (nodes);
236 
237  apps.Start (Seconds (0.5));
238  apps.Stop (m_simTime + Seconds (0.5));
239 
240  Ptr<Node> sinkNode = sink.Get (0);
241  TypeId psfid = TypeId::LookupByName ("ns3::PacketSocketFactory");
242 
243  Ptr<Socket> sinkSocket = Socket::CreateSocket (sinkNode, psfid);
244  sinkSocket->Bind (socket);
246 
247  Simulator::Stop (m_simTime + Seconds (0.6));
248  Simulator::Run ();
249  Simulator::Destroy ();
250 
251  return m_bytesTotal;
252 }
253 int
254 main (int argc, char *argv[])
255 {
256 
257  LogComponentEnable ("UanRcExample", LOG_LEVEL_ALL);
258 
259  Experiment exp;
260 
261  CommandLine cmd;
262  cmd.AddValue ("TotalRate", "Total channel capacity", exp.m_totalRate);
263  cmd.AddValue ("NumberRates", "Number of divided rates ( (NumberRates+1)%TotalRate should be 0)", exp.m_numRates);
264  cmd.AddValue ("MaxRange", "Maximum range between gateway and acoustic node", exp.m_maxRange);
265  cmd.AddValue ("SimMin", "Minimum parameter to test (nodes if DoNode=1, \"a\" param otherwise)", exp.m_simMin);
266  cmd.AddValue ("SimMax", "Maximum parameter to test (nodes if DoNode=1, \"a\" param otherwise)", exp.m_simMax);
267  cmd.AddValue ("SimStep", "Ammount to increment param per trial", exp.m_simStep);
268  cmd.AddValue ("DataFile", "Filename for GnuPlot", exp.m_gnuplotfile);
269  cmd.AddValue ("NumberNodes", "Number of nodes (invalid for doNode=1)", exp.m_numNodes);
270  cmd.AddValue ("SIFS", "SIFS time duration", exp.m_sifs);
271  cmd.AddValue ("PktSize", "Packet size in bytes", exp.m_pktSize);
272  cmd.AddValue ("SimTime", "Simulation time per trial", exp.m_simTime);
273  cmd.AddValue ("DoNode", "1 for do max nodes simulation (invalidates AMin and AMax values)", exp.m_doNode);
274  cmd.Parse (argc, argv);
275 
276 
277 
278  exp.CreateDualModes (12000);
279 
280  ;
281 
282  Gnuplot2dDataset ds;
283  for (uint32_t param=exp.m_simMin; param<=exp.m_simMax; param += exp.m_simStep)
284  {
285  uint32_t bytesRx = exp.Run (param);
286  NS_LOG_DEBUG ("param=" << param << ": Received " << bytesRx << " bytes at sink");
287 
288  double util = bytesRx*8.0/(exp.m_simTime.GetSeconds ()*exp.m_totalRate);
289 
290  ds.Add (param, util);
291 
292  SeedManager::SetRun (SeedManager::GetRun () + 1);
293  }
294 
295  Gnuplot gp;
296  gp.AddDataset (ds);
297  std::ofstream of (exp.m_gnuplotfile.c_str ());
298  if (!of.is_open ())
299  {
300  NS_FATAL_ERROR ("Can not open GNU Plot outfile: " << exp.m_gnuplotfile);
301  }
302  gp.GenerateOutput (of);
303 
304 }