A Discrete-Event Network Simulator
API
aodv.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009 IITP RAS
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * This is an example script for AODV manet routing protocol.
18  *
19  * Authors: Pavel Boyko <boyko@iitp.ru>
20  */
21 
22 #include "ns3/aodv-module.h"
23 #include "ns3/core-module.h"
24 #include "ns3/internet-module.h"
25 #include "ns3/mobility-module.h"
26 #include "ns3/network-module.h"
27 #include "ns3/ping-helper.h"
28 #include "ns3/point-to-point-module.h"
29 #include "ns3/yans-wifi-helper.h"
30 
31 #include <cmath>
32 #include <iostream>
33 
34 using namespace ns3;
35 
53 {
54  public:
55  AodvExample();
62  bool Configure(int argc, char** argv);
64  void Run();
69  void Report(std::ostream& os);
70 
71  private:
72  // parameters
74  uint32_t size;
76  double step;
78  double totalTime;
80  bool pcap;
83 
84  // network
91 
92  private:
94  void CreateNodes();
96  void CreateDevices();
98  void InstallInternetStack();
100  void InstallApplications();
101 };
102 
103 int
104 main(int argc, char** argv)
105 {
106  AodvExample test;
107  if (!test.Configure(argc, argv))
108  {
109  NS_FATAL_ERROR("Configuration failed. Aborted.");
110  }
111 
112  test.Run();
113  test.Report(std::cout);
114  return 0;
115 }
116 
117 //-----------------------------------------------------------------------------
119  : size(10),
120  step(50),
121  totalTime(100),
122  pcap(true),
123  printRoutes(true)
124 {
125 }
126 
127 bool
128 AodvExample::Configure(int argc, char** argv)
129 {
130  // Enable AODV logs by default. Comment this if too noisy
131  // LogComponentEnable("AodvRoutingProtocol", LOG_LEVEL_ALL);
132 
133  SeedManager::SetSeed(12345);
134  CommandLine cmd(__FILE__);
135 
136  cmd.AddValue("pcap", "Write PCAP traces.", pcap);
137  cmd.AddValue("printRoutes", "Print routing table dumps.", printRoutes);
138  cmd.AddValue("size", "Number of nodes.", size);
139  cmd.AddValue("time", "Simulation time, s.", totalTime);
140  cmd.AddValue("step", "Grid step, m", step);
141 
142  cmd.Parse(argc, argv);
143  return true;
144 }
145 
146 void
148 {
149  // Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", UintegerValue (1)); //
150  // enable rts cts all the time.
151  CreateNodes();
152  CreateDevices();
155 
156  std::cout << "Starting simulation for " << totalTime << " s ...\n";
157 
158  Simulator::Stop(Seconds(totalTime));
159  Simulator::Run();
160  Simulator::Destroy();
161 }
162 
163 void
164 AodvExample::Report(std::ostream&)
165 {
166 }
167 
168 void
170 {
171  std::cout << "Creating " << (unsigned)size << " nodes " << step << " m apart.\n";
172  nodes.Create(size);
173  // Name nodes
174  for (uint32_t i = 0; i < size; ++i)
175  {
176  std::ostringstream os;
177  os << "node-" << i;
178  Names::Add(os.str(), nodes.Get(i));
179  }
180  // Create static grid
182  mobility.SetPositionAllocator("ns3::GridPositionAllocator",
183  "MinX",
184  DoubleValue(0.0),
185  "MinY",
186  DoubleValue(0.0),
187  "DeltaX",
188  DoubleValue(step),
189  "DeltaY",
190  DoubleValue(0),
191  "GridWidth",
193  "LayoutType",
194  StringValue("RowFirst"));
195  mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
196  mobility.Install(nodes);
197 }
198 
199 void
201 {
202  WifiMacHelper wifiMac;
203  wifiMac.SetType("ns3::AdhocWifiMac");
204  YansWifiPhyHelper wifiPhy;
205  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default();
206  wifiPhy.SetChannel(wifiChannel.Create());
208  wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
209  "DataMode",
210  StringValue("OfdmRate6Mbps"),
211  "RtsCtsThreshold",
212  UintegerValue(0));
213  devices = wifi.Install(wifiPhy, wifiMac, nodes);
214 
215  if (pcap)
216  {
217  wifiPhy.EnablePcapAll(std::string("aodv"));
218  }
219 }
220 
221 void
223 {
224  AodvHelper aodv;
225  // you can configure AODV attributes here using aodv.Set(name, value)
227  stack.SetRoutingHelper(aodv); // has effect on the next Install ()
228  stack.Install(nodes);
230  address.SetBase("10.0.0.0", "255.0.0.0");
231  interfaces = address.Assign(devices);
232 
233  if (printRoutes)
234  {
235  Ptr<OutputStreamWrapper> routingStream =
236  Create<OutputStreamWrapper>("aodv.routes", std::ios::out);
237  Ipv4RoutingHelper::PrintRoutingTableAllAt(Seconds(8), routingStream);
238  }
239 }
240 
241 void
243 {
245  ping.SetAttribute("VerboseMode", EnumValue(Ping::VerboseMode::VERBOSE));
246 
247  ApplicationContainer p = ping.Install(nodes.Get(0));
248  p.Start(Seconds(0));
249  p.Stop(Seconds(totalTime) - Seconds(0.001));
250 
251  // move node away
252  Ptr<Node> node = nodes.Get(size / 2);
254  Simulator::Schedule(Seconds(totalTime / 3),
255  &MobilityModel::SetPosition,
256  mob,
257  Vector(1e5, 1e5, 1e5));
258 }
Test script.
Definition: aodv.cc:53
bool Configure(int argc, char **argv)
Configure script parameters.
Definition: aodv.cc:128
void CreateDevices()
Create the devices.
Definition: aodv.cc:200
void InstallApplications()
Create the simulation applications.
Definition: aodv.cc:242
Ipv4InterfaceContainer interfaces
interfaces used in the example
Definition: aodv.cc:90
bool printRoutes
Print routes if true.
Definition: aodv.cc:82
bool pcap
Write per-device PCAP traces if true.
Definition: aodv.cc:80
NodeContainer nodes
nodes used in the example
Definition: aodv.cc:86
double totalTime
Simulation time, seconds.
Definition: aodv.cc:78
void CreateNodes()
Create the nodes.
Definition: aodv.cc:169
void Run()
Run simulation.
Definition: aodv.cc:147
uint32_t size
Number of nodes.
Definition: aodv.cc:74
void Report(std::ostream &os)
Report results.
Definition: aodv.cc:164
void InstallInternetStack()
Create the network.
Definition: aodv.cc:222
NetDeviceContainer devices
devices used in the example
Definition: aodv.cc:88
AodvExample()
Definition: aodv.cc:118
double step
Distance between nodes, meters.
Definition: aodv.cc:76
Helper class that adds AODV routing to nodes.
Definition: aodv-helper.h:36
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
Parse command-line arguments.
Definition: command-line.h:232
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:42
Hold variables of type enum.
Definition: enum.h:62
aggregate IP/TCP/UDP functionality to existing Nodes.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
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.
Keep track of the current position and velocity of an object.
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.
Ptr< T > GetObject() const
Get a pointer to the requested aggregated Object.
Definition: object.h:471
void EnablePcapAll(std::string prefix, bool promiscuous=false)
Enable pcap output on each device (which is of the appropriate type) in the set of all nodes created ...
Create a ping application and associate it to a node.
Definition: ping-helper.h:48
void SetAttribute(std::string name, const AttributeValue &value)
Configure ping applications attribute.
Definition: ping-helper.cc:46
ApplicationContainer Install(NodeContainer nodes) const
Install a Ping application on each Node in the provided NodeContainer.
Definition: ping-helper.cc:65
Hold variables of type string.
Definition: string.h:56
Hold an unsigned integer type.
Definition: uinteger.h:45
helps to create WifiNetDevice objects
Definition: wifi-helper.h:324
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
manage and create wifi channel objects for the YANS model.
Ptr< YansWifiChannel > Create() const
Make it easy to create and manage PHY objects for the YANS model.
void SetChannel(Ptr< YansWifiChannel > channel)
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1326
address
Definition: first.py:47
stack
Definition: first.py:44
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:40
wifi
Definition: third.py:95
mobility
Definition: third.py:105