A Discrete-Event Network Simulator
API
aodv.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2009 IITP RAS
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  * This is an example script for AODV manet routing protocol.
19  *
20  * Authors: Pavel Boyko <boyko@iitp.ru>
21  */
22 
23 #include <iostream>
24 #include <cmath>
25 #include "ns3/aodv-module.h"
26 #include "ns3/core-module.h"
27 #include "ns3/network-module.h"
28 #include "ns3/internet-module.h"
29 #include "ns3/mobility-module.h"
30 #include "ns3/point-to-point-module.h"
31 #include "ns3/v4ping-helper.h"
32 #include "ns3/yans-wifi-helper.h"
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 
73  // parameters
75  uint32_t size;
77  double step;
79  double totalTime;
81  bool pcap;
84 
85  // network
92 
93 private:
95  void CreateNodes ();
97  void CreateDevices ();
99  void InstallInternetStack ();
101  void InstallApplications ();
102 };
103 
104 int main (int argc, char **argv)
105 {
106  AodvExample test;
107  if (!test.Configure (argc, argv))
108  NS_FATAL_ERROR ("Configuration failed. Aborted.");
109 
110  test.Run ();
111  test.Report (std::cout);
112  return 0;
113 }
114 
115 //-----------------------------------------------------------------------------
117  size (10),
118  step (50),
119  totalTime (100),
120  pcap (true),
121  printRoutes (true)
122 {
123 }
124 
125 bool
126 AodvExample::Configure (int argc, char **argv)
127 {
128  // Enable AODV logs by default. Comment this if too noisy
129  // LogComponentEnable("AodvRoutingProtocol", LOG_LEVEL_ALL);
130 
131  SeedManager::SetSeed (12345);
132  CommandLine cmd (__FILE__);
133 
134  cmd.AddValue ("pcap", "Write PCAP traces.", pcap);
135  cmd.AddValue ("printRoutes", "Print routing table dumps.", printRoutes);
136  cmd.AddValue ("size", "Number of nodes.", size);
137  cmd.AddValue ("time", "Simulation time, s.", totalTime);
138  cmd.AddValue ("step", "Grid step, m", step);
139 
140  cmd.Parse (argc, argv);
141  return true;
142 }
143 
144 void
146 {
147 // Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", UintegerValue (1)); // enable rts cts all the time.
148  CreateNodes ();
149  CreateDevices ();
152 
153  std::cout << "Starting simulation for " << totalTime << " s ...\n";
154 
155  Simulator::Stop (Seconds (totalTime));
156  Simulator::Run ();
157  Simulator::Destroy ();
158 }
159 
160 void
161 AodvExample::Report (std::ostream &)
162 {
163 }
164 
165 void
167 {
168  std::cout << "Creating " << (unsigned)size << " nodes " << step << " m apart.\n";
169  nodes.Create (size);
170  // Name nodes
171  for (uint32_t i = 0; i < size; ++i)
172  {
173  std::ostringstream os;
174  os << "node-" << i;
175  Names::Add (os.str (), nodes.Get (i));
176  }
177  // Create static grid
179  mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
180  "MinX", DoubleValue (0.0),
181  "MinY", DoubleValue (0.0),
182  "DeltaX", DoubleValue (step),
183  "DeltaY", DoubleValue (0),
184  "GridWidth", UintegerValue (size),
185  "LayoutType", StringValue ("RowFirst"));
186  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
187  mobility.Install (nodes);
188 }
189 
190 void
192 {
193  WifiMacHelper wifiMac;
194  wifiMac.SetType ("ns3::AdhocWifiMac");
195  YansWifiPhyHelper wifiPhy;
196  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
197  wifiPhy.SetChannel (wifiChannel.Create ());
199  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate6Mbps"), "RtsCtsThreshold", UintegerValue (0));
200  devices = wifi.Install (wifiPhy, wifiMac, nodes);
201 
202  if (pcap)
203  {
204  wifiPhy.EnablePcapAll (std::string ("aodv"));
205  }
206 }
207 
208 void
210 {
211  AodvHelper aodv;
212  // you can configure AODV attributes here using aodv.Set(name, value)
214  stack.SetRoutingHelper (aodv); // has effect on the next Install ()
215  stack.Install (nodes);
217  address.SetBase ("10.0.0.0", "255.0.0.0");
218  interfaces = address.Assign (devices);
219 
220  if (printRoutes)
221  {
222  Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ("aodv.routes", std::ios::out);
223  aodv.PrintRoutingTableAllAt (Seconds (8), routingStream);
224  }
225 }
226 
227 void
229 {
230  V4PingHelper ping (interfaces.GetAddress (size - 1));
231  ping.SetAttribute ("Verbose", BooleanValue (true));
232 
233  ApplicationContainer p = ping.Install (nodes.Get (0));
234  p.Start (Seconds (0));
235  p.Stop (Seconds (totalTime) - Seconds (0.001));
236 
237  // move node away
238  Ptr<Node> node = nodes.Get (size/2);
240  Simulator::Schedule (Seconds (totalTime/3), &MobilityModel::SetPosition, mob, Vector (1e5, 1e5, 1e5));
241 }
242 
Test script.
Definition: aodv.cc:53
bool Configure(int argc, char **argv)
Configure script parameters.
Definition: aodv.cc:126
void CreateDevices()
Create the devices.
Definition: aodv.cc:191
void InstallApplications()
Create the simulation applications.
Definition: aodv.cc:228
Ipv4InterfaceContainer interfaces
interfaces used in the example
Definition: aodv.cc:91
bool printRoutes
Print routes if true.
Definition: aodv.cc:83
bool pcap
Write per-device PCAP traces if true.
Definition: aodv.cc:81
NodeContainer nodes
nodes used in the example
Definition: aodv.cc:87
double totalTime
Simulation time, seconds.
Definition: aodv.cc:79
void CreateNodes()
Create the nodes.
Definition: aodv.cc:166
void Run()
Run simulation.
Definition: aodv.cc:145
uint32_t size
Number of nodes.
Definition: aodv.cc:75
void Report(std::ostream &os)
Report results.
Definition: aodv.cc:161
void InstallInternetStack()
Create the network.
Definition: aodv.cc:209
NetDeviceContainer devices
devices used in the example
Definition: aodv.cc:89
AodvExample()
Definition: aodv.cc:116
double step
Distance between nodes, meters.
Definition: aodv.cc:77
Helper class that adds AODV routing to nodes.
Definition: aodv-helper.h:35
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.
AttributeValue implementation for Boolean.
Definition: boolean.h:37
Parse command-line arguments.
Definition: command-line.h:229
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition: double.h:41
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
static void PrintRoutingTableAllAt(Time printTime, Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S)
prints the routing tables of all nodes at a particular time.
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(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:470
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 ...
Hold variables of type string.
Definition: string.h:41
Hold an unsigned integer type.
Definition: uinteger.h:44
Create a IPv4 ping application and associate it to a node.
Definition: v4ping-helper.h:38
ApplicationContainer Install(NodeContainer nodes) const
Install a Ping application on each Node in the provided NodeContainer.
void SetAttribute(std::string name, const AttributeValue &value)
Configure ping applications attribute.
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.
Ptr< YansWifiChannel > Create(void) 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:165
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1244
address
Definition: first.py:44
stack
Definition: first.py:41
Every class exported by the ns3 library is enclosed in the ns3 namespace.
cmd
Definition: second.py:35
wifi
Definition: third.py:96
mobility
Definition: third.py:108
static void SetPosition(Ptr< Node > node, Vector position)
Definition: wifi-ap.cc:89