A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
aodv.cc
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:
58  //\{
60  uint32_t size;
62  double step;
64  double totalTime;
66  bool pcap;
69  //\}
70 
72  //\{
73  NodeContainer nodes;
74  NetDeviceContainer devices;
75  Ipv4InterfaceContainer interfaces;
76  //\}
77 
78 private:
79  void CreateNodes ();
80  void CreateDevices ();
81  void InstallInternetStack ();
82  void InstallApplications ();
83 };
84 
85 int main (int argc, char **argv)
86 {
87  AodvExample test;
88  if (!test.Configure (argc, argv))
89  NS_FATAL_ERROR ("Configuration failed. Aborted.");
90 
91  test.Run ();
92  test.Report (std::cout);
93  return 0;
94 }
95 
96 //-----------------------------------------------------------------------------
97 AodvExample::AodvExample () :
98  size (10),
99  step (100),
100  totalTime (10),
101  pcap (true),
102  printRoutes (true)
103 {
104 }
105 
106 bool
107 AodvExample::Configure (int argc, char **argv)
108 {
109  // Enable AODV logs by default. Comment this if too noisy
110  // LogComponentEnable("AodvRoutingProtocol", LOG_LEVEL_ALL);
111 
112  SeedManager::SetSeed (12345);
113  CommandLine cmd;
114 
115  cmd.AddValue ("pcap", "Write PCAP traces.", pcap);
116  cmd.AddValue ("printRoutes", "Print routing table dumps.", printRoutes);
117  cmd.AddValue ("size", "Number of nodes.", size);
118  cmd.AddValue ("time", "Simulation time, s.", totalTime);
119  cmd.AddValue ("step", "Grid step, m", step);
120 
121  cmd.Parse (argc, argv);
122  return true;
123 }
124 
125 void
127 {
128 // Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", UintegerValue (1)); // enable rts cts all the time.
129  CreateNodes ();
130  CreateDevices ();
131  InstallInternetStack ();
132  InstallApplications ();
133 
134  std::cout << "Starting simulation for " << totalTime << " s ...\n";
135 
136  Simulator::Stop (Seconds (totalTime));
137  Simulator::Run ();
138  Simulator::Destroy ();
139 }
140 
141 void
142 AodvExample::Report (std::ostream &)
143 {
144 }
145 
146 void
147 AodvExample::CreateNodes ()
148 {
149  std::cout << "Creating " << (unsigned)size << " nodes " << step << " m apart.\n";
150  nodes.Create (size);
151  // Name nodes
152  for (uint32_t i = 0; i < size; ++i)
153  {
154  std::ostringstream os;
155  os << "node-" << i;
156  Names::Add (os.str (), nodes.Get (i));
157  }
158  // Create static grid
159  MobilityHelper mobility;
160  mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
161  "MinX", DoubleValue (0.0),
162  "MinY", DoubleValue (0.0),
163  "DeltaX", DoubleValue (step),
164  "DeltaY", DoubleValue (0),
165  "GridWidth", UintegerValue (size),
166  "LayoutType", StringValue ("RowFirst"));
167  mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
168  mobility.Install (nodes);
169 }
170 
171 void
172 AodvExample::CreateDevices ()
173 {
174  NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
175  wifiMac.SetType ("ns3::AdhocWifiMac");
176  YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
177  YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
178  wifiPhy.SetChannel (wifiChannel.Create ());
179  WifiHelper wifi = WifiHelper::Default ();
180  wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("OfdmRate6Mbps"), "RtsCtsThreshold", UintegerValue (0));
181  devices = wifi.Install (wifiPhy, wifiMac, nodes);
182 
183  if (pcap)
184  {
185  wifiPhy.EnablePcapAll (std::string ("aodv"));
186  }
187 }
188 
189 void
190 AodvExample::InstallInternetStack ()
191 {
192  AodvHelper aodv;
193  // you can configure AODV attributes here using aodv.Set(name, value)
194  InternetStackHelper stack;
195  stack.SetRoutingHelper (aodv); // has effect on the next Install ()
196  stack.Install (nodes);
197  Ipv4AddressHelper address;
198  address.SetBase ("10.0.0.0", "255.0.0.0");
199  interfaces = address.Assign (devices);
200 
201  if (printRoutes)
202  {
203  Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ("aodv.routes", std::ios::out);
204  aodv.PrintRoutingTableAllAt (Seconds (8), routingStream);
205  }
206 }
207 
208 void
209 AodvExample::InstallApplications ()
210 {
211  V4PingHelper ping (interfaces.GetAddress (size - 1));
212  ping.SetAttribute ("Verbose", BooleanValue (true));
213 
214  ApplicationContainer p = ping.Install (nodes.Get (0));
215  p.Start (Seconds (0));
216  p.Stop (Seconds (totalTime) - Seconds (0.001));
217 
218  // move node away
219  Ptr<Node> node = nodes.Get (size/2);
221  Simulator::Schedule (Seconds (totalTime/3), &MobilityModel::SetPosition, mob, Vector (1e5, 1e5, 1e5));
222 }
223 
holds a vector of ns3::Application pointers.
Hold a bool native type.
Definition: boolean.h:38
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 SetAttribute(std::string name, const AttributeValue &value)
Configure ping applications attribute.
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())
bool Configure(int argc, char **argv)
Configure script parameters,.
Definition: aodv.cc:107
uint32_t size
Number of nodes.
Definition: aodv.cc:60
aggregate IP/TCP/UDP functionality to existing Nodes.
Test script.
Definition: aodv.cc:45
helps to create WifiNetDevice objects
Definition: wifi-helper.h:92
a 3d vector
Definition: vector.h:31
bool printRoutes
Print routes if true.
Definition: aodv.cc:68
#define NS_FATAL_ERROR(msg)
fatal error handling
Definition: fatal-error.h:72
NetDeviceContainer Install(const WifiPhyHelper &phy, const WifiMacHelper &mac, NodeContainer c) const
Definition: wifi-helper.cc:97
Keep track of the current position and velocity of an object.
void SetChannel(Ptr< YansWifiChannel > channel)
void Install(Ptr< Node > node) const
"Layout" a single node according to the current position allocator type.
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 an unsigned integer type.
Definition: uinteger.h:46
holds a vector of ns3::NetDevice pointers
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
keep track of a set of node pointers.
void PrintRoutingTableAllAt(Time printTime, Ptr< OutputStreamWrapper > stream) const
prints the routing tables of all nodes at a particular time.
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 Report(std::ostream &os)
Report results.
Definition: aodv.cc:142
manage and create wifi channel objects for the yans model.
bool pcap
Write per-device PCAP traces if true.
Definition: aodv.cc:66
double totalTime
Simulation time, seconds.
Definition: aodv.cc:64
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...
Time Seconds(double seconds)
create ns3::Time instances in units of seconds.
Definition: nstime.h:586
void AddValue(const std::string &name, const std::string &help, T &value)
Definition: command-line.h:134
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 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
void SetPositionAllocator(Ptr< PositionAllocator > allocator)
Hold an floating point type.
Definition: double.h:41
Ptr< T > GetObject(void) const
Definition: object.h:332
create a pinger application and associate it to a node
Definition: v4ping-helper.h:16
void SetRoutingHelper(const Ipv4RoutingHelper &routing)
void Run()
Run simulation.
Definition: aodv.cc:126
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