A Discrete-Event Network Simulator
API
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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:
58  //\{
60  uint32_t size;
62  double step;
64  double totalTime;
66  bool pcap;
69  //\}
70 
72  //\{
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 {
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 //-----------------------------------------------------------------------------
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 ();
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
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
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
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
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