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/stats-module.h"
63 
64 #include <fstream>
65 
66 using namespace ns3;
67 
68 NS_LOG_COMPONENT_DEFINE ("UanRcExample")
69  ;
70 
71 
73  : m_simMin (1),
74  m_simMax (1),
75  m_simStep (1),
76  m_numRates (1023),
77  m_totalRate (4096),
78  m_maxRange (3000),
79  m_numNodes (15),
80  m_pktSize (1000),
81  m_doNode (true),
82  m_sifs (Seconds (0.05)),
83  m_simTime (Seconds (5000)),
84  m_gnuplotfile ("uan-rc-example.gpl"),
85  m_bytesTotal (0)
86 {
87 }
88 
89 void
91 {
92  Ptr<Packet> packet;
93  while ((packet = socket->Recv ()))
94  {
95  m_bytesTotal += packet->GetSize ();
96  }
97 }
98 
100 Experiment::CreateMode (uint32_t kass,
101  uint32_t fc,
102  bool upperblock,
103  std::string name)
104 {
105 
106  std::ostringstream buf;
107  buf << name << " " << kass;
108 
109  uint32_t rate = m_totalRate/(m_numRates+1)* (kass);
110  uint32_t bw = kass * m_totalRate / (m_numRates+1);
111  uint32_t fcmode;
112  if(upperblock)
113  fcmode = (m_totalRate - bw)/2 + fc;
114  else
115  fcmode = (uint32_t)((-((double) m_totalRate ) + (double) bw)/2.0 + (double) fc);
116 
117 
118  uint32_t phyrate = m_totalRate;
119 
120  UanTxMode mode;
121  mode = UanTxModeFactory::CreateMode (UanTxMode::OTHER,
122  rate,
123  phyrate,
124  fcmode,
125  bw,
126  2,
127  buf.str ());
128  return mode;
129 }
130 
131 //Creates m_numRates different modes each dividing m_totalRate Hz (assumes 1 bit per hz)
132 //centered at frequency fc
133 void
135 {
136 
137 
138  for (uint32_t i=1; i < m_numRates+1; i++)
139  {
140  m_controlModes.AppendMode (CreateMode (i, fc, false, "control "));
141  }
142  for (uint32_t i=m_numRates; i > 0; i--)
143  {
144  m_dataModes.AppendMode (CreateMode (i, fc, true, "data "));
145  }
146 }
147 
148 uint32_t
149 Experiment::Run (uint32_t param)
150 {
151 
152  UanHelper uan;
153 
154  m_bytesTotal=0;
155 
156  uint32_t nNodes;
157  uint32_t a;
158  if(m_doNode)
159  {
160  a=0;
161  nNodes = param;
162  }
163  else
164  {
165  nNodes = m_numNodes;
166  a = param;
167  }
168  Time pDelay = Seconds ((double) m_maxRange / 1500.0);
169 
170  uan.SetPhy ("ns3::UanPhyDual",
171  "SupportedModesPhy1", UanModesListValue (m_dataModes),
172  "SupportedModesPhy2", UanModesListValue (m_controlModes));
173 
174  uan.SetMac ("ns3::UanMacRcGw",
175  "NumberOfRates", UintegerValue (m_numRates),
176  "NumberOfNodes", UintegerValue (nNodes),
177  "MaxReservations", UintegerValue (a),
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));
191  nodes.Create (nNodes);
192  NetDeviceContainer devices = uan.Install (nodes, chan);
193 
194  MobilityHelper mobility;
195  uint32_t depth = 70;
196  Ptr<ListPositionAllocator> pos = CreateObject<ListPositionAllocator> ();
197 
198  Ptr<UniformRandomVariable> urv = CreateObject<UniformRandomVariable> ();
199  Ptr<UniformRandomVariable> utheta = CreateObject<UniformRandomVariable> ();
200  pos->Add (Vector (m_maxRange, m_maxRange, depth));
201 
202  for (uint32_t i=0; i<nNodes; i++)
203  {
204  double theta = utheta->GetValue (0, 2.0*M_PI);
205  double r = urv->GetValue (0,m_maxRange);
206 
207  double x = m_maxRange + r*std::cos (theta);
208  double y = m_maxRange + r*std::sin (theta);
209 
210  pos->Add (Vector (x, y, depth));
211 
212  }
213 
214  mobility.SetPositionAllocator (pos);
215  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
216  mobility.Install (sink);
217  mobility.Install (nodes);
218 
219  PacketSocketHelper pktskth;
220  pktskth.Install (nodes);
221  pktskth.Install (sink);
222 
223  PacketSocketAddress socket;
224  socket.SetSingleDevice (sinkDev.Get (0)->GetIfIndex ());
225  socket.SetPhysicalAddress (sinkDev.Get (0)->GetAddress ());
226  socket.SetProtocol (0);
227 
228  OnOffHelper app ("ns3::PacketSocketFactory", Address (socket));
229  app.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
230  app.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
231  app.SetAttribute ("DataRate", DataRateValue (m_totalRate));
232  app.SetAttribute ("PacketSize", UintegerValue (m_pktSize));
233 
234  ApplicationContainer apps = app.Install (nodes);
235 
236  apps.Start (Seconds (0.5));
237  apps.Stop (m_simTime + Seconds (0.5));
238 
239  Ptr<Node> sinkNode = sink.Get (0);
240  TypeId psfid = TypeId::LookupByName ("ns3::PacketSocketFactory");
241 
242  Ptr<Socket> sinkSocket = Socket::CreateSocket (sinkNode, psfid);
243  sinkSocket->Bind (socket);
245 
246  Simulator::Stop (m_simTime + Seconds (0.6));
247  Simulator::Run ();
248  Simulator::Destroy ();
249 
250  return m_bytesTotal;
251 }
252 int
253 main (int argc, char *argv[])
254 {
255 
256  LogComponentEnable ("UanRcExample", LOG_LEVEL_ALL);
257 
258  Experiment exp;
259 
260  CommandLine cmd;
261  cmd.AddValue ("TotalRate", "Total channel capacity", exp.m_totalRate);
262  cmd.AddValue ("NumberRates", "Number of divided rates ( (NumberRates+1)%TotalRate should be 0)", exp.m_numRates);
263  cmd.AddValue ("MaxRange", "Maximum range between gateway and acoustic node", exp.m_maxRange);
264  cmd.AddValue ("SimMin", "Minimum parameter to test (nodes if DoNode=1, \"a\" param otherwise)", exp.m_simMin);
265  cmd.AddValue ("SimMax", "Maximum parameter to test (nodes if DoNode=1, \"a\" param otherwise)", exp.m_simMax);
266  cmd.AddValue ("SimStep", "Amount to increment param per trial", exp.m_simStep);
267  cmd.AddValue ("DataFile", "Filename for GnuPlot", exp.m_gnuplotfile);
268  cmd.AddValue ("NumberNodes", "Number of nodes (invalid for doNode=1)", exp.m_numNodes);
269  cmd.AddValue ("SIFS", "SIFS time duration", exp.m_sifs);
270  cmd.AddValue ("PktSize", "Packet size in bytes", exp.m_pktSize);
271  cmd.AddValue ("SimTime", "Simulation time per trial", exp.m_simTime);
272  cmd.AddValue ("DoNode", "1 for do max nodes simulation (invalidates AMin and AMax values)", exp.m_doNode);
273  cmd.Parse (argc, argv);
274 
275 
276 
277  exp.CreateDualModes (12000);
278 
279  ;
280 
281  Gnuplot2dDataset ds;
282  for (uint32_t param=exp.m_simMin; param<=exp.m_simMax; param += exp.m_simStep)
283  {
284  uint32_t bytesRx = exp.Run (param);
285  NS_LOG_DEBUG ("param=" << param << ": Received " << bytesRx << " bytes at sink");
286 
287  double util = bytesRx*8.0/(exp.m_simTime.GetSeconds ()*exp.m_totalRate);
288 
289  ds.Add (param, util);
290 
291  SeedManager::SetRun (SeedManager::GetRun () + 1);
292  }
293 
294  Gnuplot gp;
295  gp.AddDataset (ds);
296  std::ofstream of (exp.m_gnuplotfile.c_str ());
297  if (!of.is_open ())
298  {
299  NS_FATAL_ERROR ("Can not open GNU Plot outfile: " << exp.m_gnuplotfile);
300  }
301  gp.GenerateOutput (of);
302 
303 }
UAN configuration helper.
Definition: uan-helper.h:39
Helper class for UAN CW MAC example.
Definition: multirate.cc:71
holds a vector of ns3::Application pointers.
keep track of time values and allow control of global simulation resolution
Definition: nstime.h:81
uint32_t m_bytesTotal
Total bytes received.
Definition: wifi-adhoc.cc:48
UanModesList m_controlModes
List of UanTxModes used for control channels.
tuple devices
Definition: first.py:32
NS_LOG_COMPONENT_DEFINE("GrantedTimeWindowMpiInterface")
Class to represent a 2D points plot.
Definition: gnuplot.h:113
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.
an address for a packet socket
int main(int argc, char *argv[])
uint32_t GetSize(void) const
Definition: packet.h:650
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:756
void ReceivePacket(Ptr< Socket > socket)
Definition: multirate.cc:165
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)
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
a polymophic address class
Definition: address.h:86
Time m_sifs
SIFS time duration.
tuple nodes
Definition: first.py:25
std::string m_gnuplotfile
Filename for GnuPlot.
double GetSeconds(void) const
Definition: nstime.h:274
UanModesList m_dataModes
List of UanTxModes used for data channels.
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:367
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
Hold an unsigned integer type.
Definition: uinteger.h:46
Gnuplot2dDataset Run(const WifiHelper &wifi, const YansWifiPhyHelper &wifiPhy, const NqosWifiMacHelper &wifiMac, const YansWifiChannelHelper &wifiChannel, const MobilityHelper &mobility)
Definition: multirate.cc:373
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
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:762
void Add(double x, double y)
Definition: gnuplot.cc:359
void SetRecvCallback(Callback< void, Ptr< Socket > >)
Notify application when new data is available to be read.
Definition: socket.cc:128
uint32_t m_totalRate
Total channel capacity.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
Parse command-line arguments.
Definition: command-line.h:152
Time m_simTime
Simulation run time, default 1000 s.
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.
virtual Ptr< Packet > Recv(uint32_t maxSize, uint32_t flags)=0
Read data from the socket.
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())
uint32_t m_simMin
Minimum parameter to test.
void Install(Ptr< Node > node) const
Aggregate an instance of a ns3::PacketSocketFactory onto the provided node.
void CreateDualModes(uint32_t fc)
Create m_numRates matching control and data modes.
UanTxMode CreateMode(uint32_t kass, uint32_t fc, bool upperblock, std::string name)
Create a UanTxMode.
uint32_t m_pktSize
Packet size in bytes.
Helper class used to assign positions and mobility models to nodes.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter...
uint32_t m_simMax
Maximum parameter to test.
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
#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)
uint32_t m_numRates
Number of divided rates ( (NumberRates+1)TotalRate should be 0).
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...
bool m_doNode
1 for do max nodes simulation (invalidates AMin and AMax values).
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
uint32_t m_simStep
Amount to increment param per trial.
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
uint32_t m_maxRange
Maximum range between gateway and acoustic node.
void SetPhy(std::string phyType, 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 PHY attributes.
Definition: uan-helper.cc:114
void SetAttribute(std::string name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
uint32_t m_numNodes
Number of transmitting nodes.
void LogComponentEnable(char const *name, enum LogLevel level)
Definition: log.cc:311
Attribute Value class for UanTxModes.