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 "ns3/aodv-module.h"
24 #include "ns3/core-module.h"
25 #include "ns3/network-module.h"
26 #include "ns3/internet-module.h"
27 #include "ns3/mobility-module.h"
28 #include "ns3/point-to-point-module.h"
29 #include "ns3/wifi-module.h"
30 #include "ns3/v4ping-helper.h"
31 #include <iostream>
32 #include <cmath>
33 
34 using namespace ns3;
35 
46 {
47 public:
48  AodvExample ();
50  bool Configure (int argc, char **argv);
52  void Run ();
54  void Report (std::ostream & os);
55 
56 private:
57 
58  // parameters
60  uint32_t size;
62  double step;
64  double totalTime;
66  bool pcap;
69 
70  // network
74 
75 private:
76  void CreateNodes ();
77  void CreateDevices ();
78  void InstallInternetStack ();
79  void InstallApplications ();
80 };
81 
82 int main (int argc, char **argv)
83 {
85  if (!test.Configure (argc, argv))
86  NS_FATAL_ERROR ("Configuration failed. Aborted.");
87 
88  test.Run ();
89  test.Report (std::cout);
90  return 0;
91 }
92 
93 //-----------------------------------------------------------------------------
95  size (10),
96  step (100),
97  totalTime (10),
98  pcap (true),
99  printRoutes (true)
100 {
101 }
102 
103 bool
104 AodvExample::Configure (int argc, char **argv)
105 {
106  // Enable AODV logs by default. Comment this if too noisy
107  // LogComponentEnable("AodvRoutingProtocol", LOG_LEVEL_ALL);
108 
109  SeedManager::SetSeed (12345);
111 
112  cmd.AddValue ("pcap", "Write PCAP traces.", pcap);
113  cmd.AddValue ("printRoutes", "Print routing table dumps.", printRoutes);
114  cmd.AddValue ("size", "Number of nodes.", size);
115  cmd.AddValue ("time", "Simulation time, s.", totalTime);
116  cmd.AddValue ("step", "Grid step, m", step);
117 
118  cmd.Parse (argc, argv);
119  return true;
120 }
121 
122 void
124 {
125 // Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", UintegerValue (1)); // enable rts cts all the time.
126  CreateNodes ();
127  CreateDevices ();
130 
131  std::cout << "Starting simulation for " << totalTime << " s ...\n";
132 
133  Simulator::Stop (Seconds (totalTime));
134  Simulator::Run ();
135  Simulator::Destroy ();
136 }
137 
138 void
139 AodvExample::Report (std::ostream &)
140 {
141 }
142 
143 void
145 {
146  std::cout << "Creating " << (unsigned)size << " nodes " << step << " m apart.\n";
147  nodes.Create (size);
148  // Name nodes
149  for (uint32_t i = 0; i < size; ++i)
150  {
151  std::ostringstream os;
152  os << "node-" << i;
153  Names::Add (os.str (), nodes.Get (i));
154  }
155  // Create static grid
157  mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
158  "MinX", DoubleValue (0.0),
159  "MinY", DoubleValue (0.0),
160  "DeltaX", DoubleValue (step),
161  "DeltaY", DoubleValue (0),
162  "GridWidth", UintegerValue (size),
163  "LayoutType", StringValue ("RowFirst"));
164  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
165  mobility.Install (nodes);
166 }
167 
168 void
170 {
171  WifiMacHelper wifiMac;
172  wifiMac.SetType ("ns3::AdhocWifiMac");
173  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
174  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
175  wifiPhy.SetChannel (wifiChannel.Create ());
177  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate6Mbps"), "RtsCtsThreshold", UintegerValue (0));
178  devices = wifi.Install (wifiPhy, wifiMac, nodes);
179 
180  if (pcap)
181  {
182  wifiPhy.EnablePcapAll (std::string ("aodv"));
183  }
184 }
185 
186 void
188 {
189  AodvHelper aodv;
190  // you can configure AODV attributes here using aodv.Set(name, value)
192  stack.SetRoutingHelper (aodv); // has effect on the next Install ()
193  stack.Install (nodes);
195  address.SetBase ("10.0.0.0", "255.0.0.0");
196  interfaces = address.Assign (devices);
197 
198  if (printRoutes)
199  {
200  Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ("aodv.routes", std::ios::out);
201  aodv.PrintRoutingTableAllAt (Seconds (8), routingStream);
202  }
203 }
204 
205 void
207 {
208  V4PingHelper ping (interfaces.GetAddress (size - 1));
209  ping.SetAttribute ("Verbose", BooleanValue (true));
210 
211  ApplicationContainer p = ping.Install (nodes.Get (0));
212  p.Start (Seconds (0));
213  p.Stop (Seconds (totalTime) - Seconds (0.001));
214 
215  // move node away
216  Ptr<Node> node = nodes.Get (size/2);
218  Simulator::Schedule (Seconds (totalTime/3), &MobilityModel::SetPosition, mob, Vector (1e5, 1e5, 1e5));
219 }
220 
holds a vector of ns3::Application pointers.
AttributeValue implementation for Boolean.
Definition: boolean.h:34
holds a vector of std::pair of Ptr 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:683
Ptr< T > GetObject(void) const
Get a pointer to the requested aggregated Object.
Definition: object.h:462
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:104
uint32_t size
Number of nodes.
Definition: aodv.cc:60
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
Test script.
Definition: aodv.cc:45
helps to create WifiNetDevice objects
Definition: wifi-helper.h:231
tuple cmd
Definition: second.py:35
static void SetPosition(Ptr< Node > node, Vector position)
Definition: wifi-ap.cc:86
bool printRoutes
Print routes if true.
Definition: aodv.cc:68
virtual NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer c) const
Definition: wifi-helper.cc:712
Ipv4InterfaceContainer interfaces
Definition: aodv.cc:73
Keep track of the current position and velocity of an object.
void SetChannel(Ptr< YansWifiChannel > channel)
void CreateDevices()
Definition: aodv.cc:169
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
tuple mobility
Definition: third.py:101
NodeContainer nodes
Definition: aodv.cc:71
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()
Definition: aodv.cc:187
Hold an unsigned integer type.
Definition: uinteger.h:44
holds a vector of ns3::NetDevice pointers
void InstallApplications()
Definition: aodv.cc:206
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:205
Every class exported by the ns3 library is enclosed in the ns3 namespace.
keep track of a set of node pointers.
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())
static void PrintRoutingTableAllAt(Time printTime, Ptr< OutputStreamWrapper > stream)
prints the routing tables of all nodes at a particular time.
void Install(std::string nodeName) const
Aggregate implementations of the ns3::Ipv4, ns3::Ipv6, ns3::Udp, and ns3::Tcp classes onto the provid...
void Report(std::ostream &os)
Report results.
Definition: aodv.cc:139
manage and create wifi channel objects for the yans model.
bool pcap
Write per-device PCAP traces if true.
Definition: aodv.cc:66
create MAC layers for a ns3::WifiNetDevice.
AodvExample()
Definition: aodv.cc:94
tuple stack
Definition: first.py:34
double totalTime
Simulation time, seconds.
Definition: aodv.cc:64
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())
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...
Ipv4InterfaceContainer Assign(const NetDeviceContainer &c)
Assign IP addresses to the net devices specified in the container based on the current network prefix...
void AddValue(const std::string &name, const std::string &help, T &value)
Add a program argument, assigning to POD.
Definition: command-line.h:495
Ptr< Node > Get(uint32_t i) const
Get the Ptr stored in this container at a given index.
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition: nstime.h:895
NetDeviceContainer devices
Definition: aodv.cc:72
void Parse(int argc, char *argv[])
Parse the program arguments.
A helper class to make life easier while doing simple IPv4 address assignment in scripts.
tuple wifi
Definition: third.py:89
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:35
tuple address
Definition: first.py:37
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Set the position allocator which will be used to allocate the initial position of every node initiali...
This class can be used to hold variables of floating point type such as 'double' or 'float'...
Definition: double.h:41
void CreateNodes()
Definition: aodv.cc:144
Create a IPv5 ping application and associate it to a node.
Definition: v4ping-helper.h:37
void test(void)
Example use of ns3::SystemThread.
void SetRoutingHelper(const Ipv4RoutingHelper &routing)
void Run()
Run simulation.
Definition: aodv.cc:123
void SetBase(Ipv4Address network, Ipv4Mask mask, Ipv4Address base="0.0.0.1")
Set the base network number, network mask and base address.
double step
Distance between nodes, meters.
Definition: aodv.cc:62
Ipv4Address GetAddress(uint32_t i, uint32_t j=0) const