A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
dsr.cc
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 "ns3/core-module.h"
33 #include "ns3/network-module.h"
34 #include "ns3/applications-module.h"
35 #include "ns3/mobility-module.h"
36 #include "ns3/config-store-module.h"
37 #include "ns3/wifi-module.h"
38 #include "ns3/internet-module.h"
39 #include "ns3/dsr-module.h"
40 #include <sstream>
41 
42 using namespace ns3;
43 NS_LOG_COMPONENT_DEFINE ("DsrTest");
44 
45 int
46 main (int argc, char *argv[])
47 {
48  //
49  // Users may find it convenient to turn on explicit debugging
50  // for selected modules; the below lines suggest how to do this
51  //
52 #if 0
53  LogComponentEnable ("Ipv4L3Protocol", LOG_LEVEL_ALL);
54  LogComponentEnable ("UdpL4Protocol", LOG_LEVEL_ALL);
55  LogComponentEnable ("UdpSocketImpl", LOG_LEVEL_ALL);
56  LogComponentEnable ("NetDevice", LOG_LEVEL_ALL);
57  LogComponentEnable ("Ipv4EndPointDemux", LOG_LEVEL_ALL);
58 #endif
59 
60 #if 0
61  LogComponentEnable ("DsrOptions", LOG_LEVEL_ALL);
62  LogComponentEnable ("DsrHelper", LOG_LEVEL_ALL);
63  LogComponentEnable ("DsrRouting", LOG_LEVEL_ALL);
64  LogComponentEnable ("DsrOptionHeader", LOG_LEVEL_ALL);
65  LogComponentEnable ("DsrFsHeader", LOG_LEVEL_ALL);
66  LogComponentEnable ("DsrGraReplyTable", LOG_LEVEL_ALL);
67  LogComponentEnable ("DsrSendBuffer", LOG_LEVEL_ALL);
68  LogComponentEnable ("RouteCache", LOG_LEVEL_ALL);
69  LogComponentEnable ("DsrMaintainBuffer", LOG_LEVEL_ALL);
70  LogComponentEnable ("RreqTable", LOG_LEVEL_ALL);
71  LogComponentEnable ("DsrErrorBuffer", LOG_LEVEL_ALL);
72  LogComponentEnable ("DsrNetworkQueue", LOG_LEVEL_ALL);
73 #endif
74 
75  NS_LOG_INFO ("creating the nodes");
76 
77  // General parameters
78  uint32_t nWifis = 50;
79  uint32_t nSinks = 10;
80  double TotalTime = 600.0;
81  double dataTime = 500.0;
82  double ppers = 1;
83  uint32_t packetSize = 64;
84  double dataStart = 100.0; // start sending data at 100s
85 
86  //mobility parameters
87  double pauseTime = 0.0;
88  double nodeSpeed = 20.0;
89  double txpDistance = 250.0;
90 
91  std::string rate = "0.512kbps";
92  std::string dataMode ("DsssRate11Mbps");
93  std::string phyMode ("DsssRate11Mbps");
94 
95  //Allow users to override the default parameters and set it to new ones from CommandLine.
96  CommandLine cmd;
97  cmd.AddValue ("nWifis", "Number of wifi nodes", nWifis);
98  cmd.AddValue ("nSinks", "Number of SINK traffic nodes", nSinks);
99  cmd.AddValue ("rate", "CBR traffic rate(in kbps), Default:8", rate);
100  cmd.AddValue ("nodeSpeed", "Node speed in RandomWayPoint model, Default:20", nodeSpeed);
101  cmd.AddValue ("packetSize", "The packet size", packetSize);
102  cmd.AddValue ("txpDistance", "Specify node's transmit range, Default:300", txpDistance);
103  cmd.AddValue ("pauseTime", "pauseTime for mobility model, Default: 0", pauseTime);
104  cmd.Parse (argc, argv);
105 
108 
109  NodeContainer adhocNodes;
110  adhocNodes.Create (nWifis);
111  NetDeviceContainer allDevices;
112 
113  NS_LOG_INFO ("setting the default phy and channel parameters");
114  Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue (phyMode));
115  Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200"));
116  // disable fragmentation for frames below 2200 bytes
117  Config::SetDefault ("ns3::WifiRemoteStationManager::FragmentationThreshold", StringValue ("2200"));
118 
119  NS_LOG_INFO ("setting the default phy and channel parameters ");
120  WifiHelper wifi;
123 
124  YansWifiChannelHelper wifiChannel;
125  wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel");
126  wifiChannel.AddPropagationLoss ("ns3::RangePropagationLossModel", "MaxRange", DoubleValue (txpDistance));
127  wifiPhy.SetChannel (wifiChannel.Create ());
128 
129  // Add a non-QoS upper mac, and disable rate control
131  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue (dataMode), "ControlMode",
132  StringValue (phyMode));
133 
134  wifiMac.SetType ("ns3::AdhocWifiMac");
135  allDevices = wifi.Install (wifiPhy, wifiMac, adhocNodes);
136 
137  NS_LOG_INFO ("Configure Tracing.");
138 
139  AsciiTraceHelper ascii;
140  Ptr<OutputStreamWrapper> stream = ascii.CreateFileStream ("dsrtest.tr");
141  wifiPhy.EnableAsciiAll (stream);
142 
143  MobilityHelper adhocMobility;
144  ObjectFactory pos;
145  pos.SetTypeId ("ns3::RandomRectanglePositionAllocator");
146  pos.Set ("X", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=300.0]"));
147  pos.Set ("Y", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=1500.0]"));
148  Ptr<PositionAllocator> taPositionAlloc = pos.Create ()->GetObject<PositionAllocator> ();
149 
150  std::ostringstream speedUniformRandomVariableStream;
151  speedUniformRandomVariableStream << "ns3::UniformRandomVariable[Min=0.0|Max="
152  << nodeSpeed
153  << "]";
154 
155  std::ostringstream pauseConstantRandomVariableStream;
156  pauseConstantRandomVariableStream << "ns3::ConstantRandomVariable[Constant="
157  << pauseTime
158  << "]";
159 
160  adhocMobility.SetMobilityModel ("ns3::RandomWaypointMobilityModel",
161  // "Speed", StringValue ("ns3::UniformRandomVariable[Min=0.0|Max=nodeSpeed]"),
162  "Speed", StringValue (speedUniformRandomVariableStream.str ()),
163  "Pause", StringValue (pauseConstantRandomVariableStream.str ()),
164  "PositionAllocator", PointerValue (taPositionAlloc)
165  );
166  adhocMobility.Install (adhocNodes);
167 
168  InternetStackHelper internet;
169  DsrMainHelper dsrMain;
170  DsrHelper dsr;
171  internet.Install (adhocNodes);
172  dsrMain.Install (dsr, adhocNodes);
173 
174  NS_LOG_INFO ("assigning ip address");
175  Ipv4AddressHelper address;
176  address.SetBase ("10.1.1.0", "255.255.255.0");
177  Ipv4InterfaceContainer allInterfaces;
178  allInterfaces = address.Assign (allDevices);
179 
180  uint16_t port = 9;
181  double randomStartTime = (1 / ppers) / nSinks; //distributed btw 1s evenly as we are sending 4pkt/s
182 
183  for (uint32_t i = 0; i < nSinks; ++i)
184  {
185  PacketSinkHelper sink ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), port));
186  ApplicationContainer apps_sink = sink.Install (adhocNodes.Get (i));
187  apps_sink.Start (Seconds (0.0));
188  apps_sink.Stop (Seconds (TotalTime));
189 
190  OnOffHelper onoff1 ("ns3::UdpSocketFactory", Address (InetSocketAddress (allInterfaces.GetAddress (i), port)));
191  onoff1.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"));
192  onoff1.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0.0]"));
193  onoff1.SetAttribute ("PacketSize", UintegerValue (packetSize));
194  onoff1.SetAttribute ("DataRate", DataRateValue (DataRate (rate)));
195 
196  ApplicationContainer apps1 = onoff1.Install (adhocNodes.Get (i + nWifis - nSinks));
197  apps1.Start (Seconds (dataStart + i * randomStartTime));
198  apps1.Stop (Seconds (dataTime + i * randomStartTime));
199  }
200 
201  NS_LOG_INFO ("Run Simulation.");
202  Simulator::Stop (Seconds (TotalTime));
203  Simulator::Run ();
205 }
206 
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())
holds a vector of ns3::Application pointers.
Manage ASCII trace files for device models.
Definition: trace-helper.h:109
an Inet address class
static Ipv4Address GetAny(void)
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Ptr< YansWifiChannel > Create(void) const
void SetRemoteStationManager(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: wifi-helper.cc:68
hold variables of type string
Definition: string.h:19
Make it easy to create and manage PHY objects for the yans model.
void SetType(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())
Helper class that adds DSR routing to nodes.
static void Run(void)
Definition: simulator.cc:157
#define NS_LOG_COMPONENT_DEFINE(name)
Definition: log.h:122
void SetTypeId(TypeId tid)
aggregate IP/TCP/UDP functionality to existing Nodes.
#define NS_LOG_INFO(msg)
Definition: log.h:264
A helper to make it easier to instantiate an ns3::PacketSinkApplication on a set of nodes...
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. ...
static YansWifiPhyHelper Default(void)
helps to create WifiNetDevice objects
Definition: wifi-helper.h:92
A helper to make it easier to instantiate an ns3::OnOffApplication on a set of nodes.
Definition: on-off-helper.h:41
static void SetRun(uint64_t run)
Set the run number of simulation.
NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer c) const
Definition: wifi-helper.cc:97
a polymophic address class
Definition: address.h:86
Class for representing data rates.
Definition: data-rate.h:71
void SetChannel(Ptr< YansWifiChannel > channel)
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
Ptr< Object > Create(void) const
Hold an unsigned integer type.
Definition: uinteger.h:46
holds a vector of ns3::NetDevice pointers
void SetStandard(enum WifiPhyStandard standard)
Definition: wifi-helper.cc:91
static NqosWifiMacHelper Default(void)
void Start(Time start)
Arrange for all of the Applications in this container to Start() at the Time given as a parameter...
create non QoS-enabled MAC layers for a ns3::WifiNetDevice.
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
void SetDefault(std::string name, const AttributeValue &value)
Definition: config.cc:667
keep track of a set of node pointers.
hold objects of type Ptr<T>
Definition: pointer.h:33
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(std::string nodeName) const
void Set(std::string name, const AttributeValue &value)
manage and create wifi channel objects for the yans model.
static void SetSeed(uint32_t seed)
set the seed it will duplicate the seed value 6 times
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...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
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
static void Stop(void)
Definition: simulator.cc:164
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
void Parse(int argc, char *argv[]) const
Definition: command-line.cc:84
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
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...
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
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())
Hold an floating point type.
Definition: double.h:41
Ptr< T > GetObject(void) const
Definition: object.h:332
void SetAttribute(std::string name, const AttributeValue &value)
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
void LogComponentEnable(char const *name, enum LogLevel level)
Definition: log.cc:311
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Allocate a set of positions. The allocation strategy is implemented in subclasses.