A Discrete-Event Network Simulator
API
dsr.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2011 Yufei Cheng
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: Yufei Cheng <yfcheng@ittc.ku.edu>
19  *
20  * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director
21  * ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets
22  * Information and Telecommunication Technology Center (ITTC)
23  * and Department of Electrical Engineering and Computer Science
24  * The University of Kansas Lawrence, KS USA.
25  *
26  * Work supported in part by NSF FIND (Future Internet Design) Program
27  * under grant CNS-0626918 (Postmodern Internet Architecture),
28  * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI),
29  * US Department of Defense (DoD), and ITTC at The University of Kansas.
30  */
31 
32 #include <sstream>
33 #include "ns3/core-module.h"
34 #include "ns3/network-module.h"
35 #include "ns3/applications-module.h"
36 #include "ns3/mobility-module.h"
37 #include "ns3/config-store-module.h"
38 #include "ns3/internet-module.h"
39 #include "ns3/dsr-module.h"
40 #include "ns3/yans-wifi-helper.h"
41 
42 using namespace ns3;
43 
44 NS_LOG_COMPONENT_DEFINE ("DsrTest");
45 
46 int
47 main (int argc, char *argv[])
48 {
49  //
50  // Users may find it convenient to turn on explicit debugging
51  // for selected modules; the below lines suggest how to do this
52  //
53 #if 0
54  LogComponentEnable ("Ipv4L3Protocol", LOG_LEVEL_ALL);
55  LogComponentEnable ("UdpL4Protocol", LOG_LEVEL_ALL);
56  LogComponentEnable ("UdpSocketImpl", LOG_LEVEL_ALL);
57  LogComponentEnable ("NetDevice", LOG_LEVEL_ALL);
58  LogComponentEnable ("Ipv4EndPointDemux", LOG_LEVEL_ALL);
59 #endif
60 
61 #if 0
62  LogComponentEnable ("DsrOptions", LOG_LEVEL_ALL);
63  LogComponentEnable ("DsrHelper", LOG_LEVEL_ALL);
64  LogComponentEnable ("DsrRouting", LOG_LEVEL_ALL);
65  LogComponentEnable ("DsrOptionHeader", LOG_LEVEL_ALL);
66  LogComponentEnable ("DsrFsHeader", LOG_LEVEL_ALL);
67  LogComponentEnable ("DsrGraReplyTable", LOG_LEVEL_ALL);
68  LogComponentEnable ("DsrSendBuffer", LOG_LEVEL_ALL);
69  LogComponentEnable ("DsrRouteCache", LOG_LEVEL_ALL);
70  LogComponentEnable ("DsrMaintainBuffer", LOG_LEVEL_ALL);
71  LogComponentEnable ("DsrRreqTable", LOG_LEVEL_ALL);
72  LogComponentEnable ("DsrErrorBuffer", LOG_LEVEL_ALL);
73  LogComponentEnable ("DsrNetworkQueue", LOG_LEVEL_ALL);
74 #endif
75 
76  NS_LOG_INFO ("creating the nodes");
77 
78  // General parameters
79  uint32_t nWifis = 50;
80  uint32_t nSinks = 10;
81  double TotalTime = 600.0;
82  double dataTime = 500.0;
83  double ppers = 1;
84  uint32_t packetSize = 64;
85  double dataStart = 100.0; // start sending data at 100s
86 
87  //mobility parameters
88  double pauseTime = 0.0;
89  double nodeSpeed = 20.0;
90  double txpDistance = 250.0;
91 
92  std::string rate = "0.512kbps";
93  std::string dataMode ("DsssRate11Mbps");
94  std::string phyMode ("DsssRate11Mbps");
95 
96  //Allow users to override the default parameters and set it to new ones from CommandLine.
97  CommandLine cmd (__FILE__);
98  cmd.AddValue ("nWifis", "Number of wifi nodes", nWifis);
99  cmd.AddValue ("nSinks", "Number of SINK traffic nodes", nSinks);
100  cmd.AddValue ("rate", "CBR traffic rate(in kbps), Default:8", rate);
101  cmd.AddValue ("nodeSpeed", "Node speed in RandomWayPoint model, Default:20", nodeSpeed);
102  cmd.AddValue ("packetSize", "The packet size", packetSize);
103  cmd.AddValue ("txpDistance", "Specify node's transmit range, Default:300", txpDistance);
104  cmd.AddValue ("pauseTime", "pauseTime for mobility model, Default: 0", pauseTime);
105  cmd.Parse (argc, argv);
106 
109 
110  NodeContainer adhocNodes;
111  adhocNodes.Create (nWifis);
112  NetDeviceContainer allDevices;
113 
114  NS_LOG_INFO ("setting the default phy and channel parameters");
115  Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue (phyMode));
116  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200"));
117  // disable fragmentation for frames below 2200 bytes
118  Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
119 
120  NS_LOG_INFO ("setting the default phy and channel parameters ");
122  wifi.SetStandard (WIFI_STANDARD_80211b);
123  YansWifiPhyHelper wifiPhy;
124 
125  YansWifiChannelHelper wifiChannel;
126  wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
127  wifiChannel.AddPropagationLoss ("ns3::RangePropagationLossModel", "MaxRange", DoubleValue (txpDistance));
128  wifiPhy.SetChannel (wifiChannel.Create ());
129 
130  // Add a mac and disable rate control
131  WifiMacHelper wifiMac;
132  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue (dataMode), "ControlMode",
133  StringValue (phyMode));
134 
135  wifiMac.SetType ("ns3::AdhocWifiMac");
136  allDevices = wifi.Install (wifiPhy, wifiMac, adhocNodes);
137 
138  NS_LOG_INFO ("Configure Tracing.");
139 
140  AsciiTraceHelper ascii;
141  Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream ("dsrtest.tr");
142  wifiPhy.EnableAsciiAll (stream);
143 
144  MobilityHelper adhocMobility;
145  ObjectFactory pos;
146  pos.SetTypeId ("ns3::RandomRectanglePositionAllocator");
147  pos.Set ("X", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=300.0]"));
148  pos.Set ("Y", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1500.0]"));
149  Ptr<PositionAllocator> taPositionAlloc = pos.Create ()->GetObject<PositionAllocator> ();
150 
151  std::ostringstream speedUniformRandomVariableStream;
152  speedUniformRandomVariableStream << "ns3::UniformRandomVariable[Min=0.0|Max="
153  << nodeSpeed
154  << "]";
155 
156  std::ostringstream pauseConstantRandomVariableStream;
157  pauseConstantRandomVariableStream << "ns3::ConstantRandomVariable[Constant="
158  << pauseTime
159  << "]";
160 
161  adhocMobility.SetMobilityModel ("ns3::RandomWaypointMobilityModel",
162  // "Speed", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=nodeSpeed]"),
163  "Speed", StringValue (speedUniformRandomVariableStream.str ()),
164  "Pause", StringValue (pauseConstantRandomVariableStream.str ()),
165  "PositionAllocator", PointerValue (taPositionAlloc)
166  );
167  adhocMobility.Install (adhocNodes);
168 
169  InternetStackHelper internet;
170  DsrMainHelper dsrMain;
171  DsrHelper dsr;
172  internet.Install (adhocNodes);
173  dsrMain.Install (dsr, adhocNodes);
174 
175  NS_LOG_INFO ("assigning ip address");
177  address.SetBase ("10.1.1.0", "255.255.255.0");
178  Ipv4InterfaceContainer allInterfaces;
179  allInterfaces = address.Assign (allDevices);
180 
181  uint16_t port = 9;
182  double randomStartTime = (1 / ppers) / nSinks; //distributed btw 1s evenly as we are sending 4pkt/s
183 
184  for (uint32_t i = 0; i < nSinks; ++i)
185  {
186  PacketSinkHelper sink ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), port));
187  ApplicationContainer apps_sink = sink.Install (adhocNodes.Get (i));
188  apps_sink.Start (Seconds (0.0));
189  apps_sink.Stop (Seconds (TotalTime));
190 
191  OnOffHelper onoff1 ("ns3::UdpSocketFactory", Address (InetSocketAddress (allInterfaces.GetAddress (i), port)));
192  onoff1.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"));
193  onoff1.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0.0]"));
194  onoff1.SetAttribute ("PacketSize", UintegerValue (packetSize));
195  onoff1.SetAttribute ("DataRate", DataRateValue (DataRate (rate)));
196 
197  ApplicationContainer apps1 = onoff1.Install (adhocNodes.Get (i + nWifis - nSinks));
198  apps1.Start (Seconds (dataStart + i * randomStartTime));
199  apps1.Stop (Seconds (dataTime + i * randomStartTime));
200  }
201 
202  NS_LOG_INFO ("Run Simulation.");
203  Simulator::Stop (Seconds (TotalTime));
204  Simulator::Run ();
206 }
207 
a polymophic address class
Definition: address.h:91
holds a vector of ns3::Application pointers.
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter.
void Stop(Time stop)
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
void EnableAsciiAll(std::string prefix)
Enable ascii trace output on each device (which is of the appropriate type) in the set of all nodes c...
Manage ASCII trace files for device models.
Definition: trace-helper.h:163
Ptr< OutputStreamWrapper > CreateFileStream(std::string filename, std::ios::openmode filemode=std::ios::out)
Create and initialize an output stream object we'll use to write the traced bits.
Parse command-line arguments.
Definition: command-line.h:229
Class for representing data rates.
Definition: data-rate.h:89
AttributeValue implementation for DataRate.
Definition: data-rate.h:298
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
DSR helper class to manage creation of DSR routing instance and to insert it on a node as a sublayer ...
Definition: dsr-helper.h:53
Helper class that adds DSR routing to nodes.
void Install(DsrHelper &dsrHelper, NodeContainer nodes)
Install routing to the nodes.
an Inet address class
aggregate IP/TCP/UDP functionality to existing Nodes.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
static Ipv4Address GetAny(void)
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Helper class used to assign positions and mobility models to nodes.
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())
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
holds a vector of ns3::NetDevice pointers
keep track of a set of node pointers.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
Instantiate subclasses of ns3::Object.
void Set(const std::string &name, const AttributeValue &value, Args &&... args)
Set an attribute to be set during construction.
Ptr< Object > Create(void) const
Create an Object instance of the configured TypeId.
void SetTypeId(TypeId tid)
Set the TypeId of the Objects to be created by this factory.
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:43
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes.
Hold objects of type Ptr<T>.
Definition: pointer.h:37
Allocate a set of positions.
static void SetRun(uint64_t run)
Set the run number of simulation.
static void SetSeed(uint32_t seed)
Set the seed.
static void Stop(void)
Tell the Simulator the calling event should be the last one executed.
Definition: simulator.cc:180
static void Destroy(void)
Execute the events scheduled with ScheduleDestroy().
Definition: simulator.cc:136
static void Run(void)
Run the simulation.
Definition: simulator.cc:172
Hold variables of type string.
Definition: string.h:41
Hold an unsigned integer type.
Definition: uinteger.h:44
helps to create WifiNetDevice objects
Definition: wifi-helper.h:274
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
manage and create wifi channel objects for the YANS model.
void SetPropagationDelay(std::string name, 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())
Ptr< YansWifiChannel > Create(void) const
void AddPropagationLoss(std::string name, 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())
Make it easy to create and manage PHY objects for the YANS model.
void SetChannel(Ptr< YansWifiChannel > channel)
uint16_t port
Definition: dsdv-manet.cc:45
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:849
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
#define NS_LOG_INFO(msg)
Use NS_LOG to output a message of level LOG_INFO.
Definition: log.h:281
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
@ WIFI_STANDARD_80211b
address
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.
@ LOG_LEVEL_ALL
Print everything.
Definition: log.h:116
void LogComponentEnable(char const *name, enum LogLevel level)
Enable the logging output associated with that log component.
Definition: log.cc:361
cmd
Definition: second.py:35
wifi
Definition: third.py:96
static const uint32_t packetSize
Ptr< PacketSink > sink
Definition: wifi-tcp.cc:56