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 
48 {
49 public:
50  AodvExample ();
57  bool Configure (int argc, char **argv);
59  void Run ();
64  void Report (std::ostream & os);
65 
66 private:
67 
68  // parameters
70  uint32_t size;
72  double step;
74  double totalTime;
76  bool pcap;
79 
80  // network
87 
88 private:
90  void CreateNodes ();
92  void CreateDevices ();
94  void InstallInternetStack ();
96  void InstallApplications ();
97 };
98 
99 int main (int argc, char **argv)
100 {
101  AodvExample test;
102  if (!test.Configure (argc, argv))
103  NS_FATAL_ERROR ("Configuration failed. Aborted.");
104 
105  test.Run ();
106  test.Report (std::cout);
107  return 0;
108 }
109 
110 //-----------------------------------------------------------------------------
112  size (10),
113  step (100),
114  totalTime (100),
115  pcap (true),
116  printRoutes (true)
117 {
118 }
119 
120 bool
121 AodvExample::Configure (int argc, char **argv)
122 {
123  // Enable AODV logs by default. Comment this if too noisy
124  // LogComponentEnable("AodvRoutingProtocol", LOG_LEVEL_ALL);
125 
126  SeedManager::SetSeed (12345);
128 
129  cmd.AddValue ("pcap", "Write PCAP traces.", pcap);
130  cmd.AddValue ("printRoutes", "Print routing table dumps.", printRoutes);
131  cmd.AddValue ("size", "Number of nodes.", size);
132  cmd.AddValue ("time", "Simulation time, s.", totalTime);
133  cmd.AddValue ("step", "Grid step, m", step);
134 
135  cmd.Parse (argc, argv);
136  return true;
137 }
138 
139 void
141 {
142 // Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", UintegerValue (1)); // enable rts cts all the time.
143  CreateNodes ();
144  CreateDevices ();
147 
148  std::cout << "Starting simulation for " << totalTime << " s ...\n";
149 
150  Simulator::Stop (Seconds (totalTime));
151  Simulator::Run ();
152  Simulator::Destroy ();
153 }
154 
155 void
156 AodvExample::Report (std::ostream &)
157 {
158 }
159 
160 void
162 {
163  std::cout << "Creating " << (unsigned)size << " nodes " << step << " m apart.\n";
164  nodes.Create (size);
165  // Name nodes
166  for (uint32_t i = 0; i < size; ++i)
167  {
168  std::ostringstream os;
169  os << "node-" << i;
170  Names::Add (os.str (), nodes.Get (i));
171  }
172  // Create static grid
174  mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
175  "MinX", DoubleValue (0.0),
176  "MinY", DoubleValue (0.0),
177  "DeltaX", DoubleValue (step),
178  "DeltaY", DoubleValue (0),
179  "GridWidth", UintegerValue (size),
180  "LayoutType", StringValue ("RowFirst"));
181  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
182  mobility.Install (nodes);
183 }
184 
185 void
187 {
188  WifiMacHelper wifiMac;
189  wifiMac.SetType ("ns3::AdhocWifiMac");
190  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
191  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
192  wifiPhy.SetChannel (wifiChannel.Create ());
194  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate6Mbps"), "RtsCtsThreshold", UintegerValue (0));
195  devices = wifi.Install (wifiPhy, wifiMac, nodes);
196 
197  if (pcap)
198  {
199  wifiPhy.EnablePcapAll (std::string ("aodv"));
200  }
201 }
202 
203 void
205 {
206  AodvHelper aodv;
207  // you can configure AODV attributes here using aodv.Set(name, value)
209  stack.SetRoutingHelper (aodv); // has effect on the next Install ()
210  stack.Install (nodes);
212  address.SetBase ("10.0.0.0", "255.0.0.0");
213  interfaces = address.Assign (devices);
214 
215  if (printRoutes)
216  {
217  Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ("aodv.routes", std::ios::out);
218  aodv.PrintRoutingTableAllAt (Seconds (8), routingStream);
219  }
220 }
221 
222 void
224 {
225  V4PingHelper ping (interfaces.GetAddress (size - 1));
226  ping.SetAttribute ("Verbose", BooleanValue (true));
227 
228  ApplicationContainer p = ping.Install (nodes.Get (0));
229  p.Start (Seconds (0));
230  p.Stop (Seconds (totalTime) - Seconds (0.001));
231 
232  // move node away
233  Ptr<Node> node = nodes.Get (size/2);
235  Simulator::Schedule (Seconds (totalTime/3), &MobilityModel::SetPosition, mob, Vector (1e5, 1e5, 1e5));
236 }
237 
holds a vector of ns3::Application pointers.
AttributeValue implementation for Boolean.
Definition: boolean.h:36
holds a vector of std::pair of Ptr<Ipv4> and interface index.
Hold variables of type string.
Definition: string.h:41
Make it easy to create and manage PHY objects for the yans model.
void SetAttribute(std::string name, const AttributeValue &value)
Configure ping applications attribute.
bool Configure(int argc, char **argv)
Configure script parameters.
Definition: aodv.cc:121
uint32_t size
Number of nodes.
Definition: aodv.cc:70
aggregate IP/TCP/UDP functionality to existing Nodes.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:162
cmd
Definition: second.py:35
Test script.
Definition: aodv.cc:47
helps to create WifiNetDevice objects
Definition: wifi-helper.h:230
stack
Definition: first.py:34
static void SetPosition(Ptr< Node > node, Vector position)
Definition: wifi-ap.cc:89
bool printRoutes
Print routes if true.
Definition: aodv.cc:78
Ptr< YansWifiChannel > Create(void) const
mobility
Definition: third.py:101
Ipv4InterfaceContainer interfaces
interfaces used in the example
Definition: aodv.cc:86
Keep track of the current position and velocity of an object.
void SetChannel(Ptr< YansWifiChannel > channel)
void CreateDevices()
Create the devices.
Definition: aodv.cc:186
NodeContainer nodes
nodes used in the example
Definition: aodv.cc:82
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 ...
void InstallInternetStack()
Create the network.
Definition: aodv.cc:204
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const
Hold an unsigned integer type.
Definition: uinteger.h:44
holds a vector of ns3::NetDevice pointers
void InstallApplications()
Create the simulation applications.
Definition: aodv.cc:223
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:213
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:459
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
address
Definition: first.py:37
void Report(std::ostream &os)
Report results.
Definition: aodv.cc:156
manage and create wifi channel objects for the yans model.
bool pcap
Write per-device PCAP traces if true.
Definition: aodv.cc:76
create MAC layers for a ns3::WifiNetDevice.
AodvExample()
Definition: aodv.cc:111
double totalTime
Simulation time, seconds.
Definition: aodv.cc:74
virtual 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(), std::string n8="", const AttributeValue &v8=EmptyAttributeValue(), std::string n9="", const AttributeValue &v9=EmptyAttributeValue(), std::string n10="", const AttributeValue &v10=EmptyAttributeValue())
wifi
Definition: third.py:89
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...
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:1014
NetDeviceContainer devices
devices used in the example
Definition: aodv.cc:84
Ptr< Node > Get(uint32_t i) const
Get the Ptr<Node> stored in this container at a given index.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
void Create(uint32_t n)
Create n nodes and append pointers to them to the end of this NodeContainer.
Helper class that adds AODV routing to nodes.
Definition: aodv-helper.h:34
This class can be used to hold variables of floating point type such as &#39;double&#39; or &#39;float&#39;...
Definition: double.h:41
void CreateNodes()
Create the nodes.
Definition: aodv.cc:161
Create a IPv4 ping application and associate it to a node.
Definition: v4ping-helper.h:37
static void PrintRoutingTableAllAt(Time printTime, Ptr< OutputStreamWrapper > stream, Time::Unit unit=Time::S)
prints the routing tables of all nodes at a particular time.
void Run()
Run simulation.
Definition: aodv.cc:140
double step
Distance between nodes, meters.
Definition: aodv.cc:72