A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
aodv-example.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2009 IITP RAS
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * This is an example script for AODV manet routing protocol.
7 *
8 * Authors: Pavel Boyko <boyko@iitp.ru>
9 */
10
11#include "ns3/aodv-module.h"
12#include "ns3/core-module.h"
13#include "ns3/internet-module.h"
14#include "ns3/mobility-module.h"
15#include "ns3/network-module.h"
16#include "ns3/ping-helper.h"
17#include "ns3/point-to-point-module.h"
18#include "ns3/yans-wifi-helper.h"
19
20#include <cmath>
21#include <iostream>
22
23using namespace ns3;
24
25/**
26 * \defgroup aodv-examples AODV Examples
27 * \ingroup aodv
28 * \ingroup examples
29 */
30
31/**
32 * \ingroup aodv-examples
33 * \ingroup examples
34 * \brief Test script.
35 *
36 * This script creates 1-dimensional grid topology and then ping last node from the first one:
37 *
38 * [10.0.0.1] <-- step --> [10.0.0.2] <-- step --> [10.0.0.3] <-- step --> [10.0.0.4]
39 *
40 * ping 10.0.0.4
41 *
42 * When 1/3 of simulation time has elapsed, one of the nodes is moved out of
43 * range, thereby breaking the topology. By default, this will result in
44 * stopping ping replies reception after sequence number 33. If the step size is reduced
45 * to cover the gap, then also the following pings can be received.
46 */
48{
49 public:
51 /**
52 * \brief Configure script parameters
53 * \param argc is the command line argument count
54 * \param argv is the command line arguments
55 * \return true on successful configuration
56 */
57 bool Configure(int argc, char** argv);
58 /// Run simulation
59 void Run();
60 /**
61 * Report results
62 * \param os the output stream
63 */
64 void Report(std::ostream& os);
65
66 private:
67 // parameters
68 /// Number of nodes
70 /// Distance between nodes, meters
71 double step;
72 /// Simulation time, seconds
73 double totalTime;
74 /// Write per-device PCAP traces if true
75 bool pcap;
76 /// Print routes if true
78
79 // network
80 /// nodes used in the example
82 /// devices used in the example
84 /// interfaces used in the example
86
87 private:
88 /// Create the nodes
89 void CreateNodes();
90 /// Create the devices
91 void CreateDevices();
92 /// Create the network
94 /// Create the simulation applications
96};
97
98int
99main(int argc, char** argv)
100{
102 if (!test.Configure(argc, argv))
103 {
104 NS_FATAL_ERROR("Configuration failed. Aborted.");
105 }
106
107 test.Run();
108 test.Report(std::cout);
109 return 0;
110}
111
112//-----------------------------------------------------------------------------
114 : size(10),
115 step(50),
116 totalTime(100),
117 pcap(true),
118 printRoutes(true)
119{
120}
121
122bool
123AodvExample::Configure(int argc, char** argv)
124{
125 // Enable AODV logs by default. Comment this if too noisy
126 // LogComponentEnable("AodvRoutingProtocol", LOG_LEVEL_ALL);
127
129 CommandLine cmd(__FILE__);
130
131 cmd.AddValue("pcap", "Write PCAP traces.", pcap);
132 cmd.AddValue("printRoutes", "Print routing table dumps.", printRoutes);
133 cmd.AddValue("size", "Number of nodes.", size);
134 cmd.AddValue("time", "Simulation time, s.", totalTime);
135 cmd.AddValue("step", "Grid step, m", step);
136
137 cmd.Parse(argc, argv);
138 return true;
139}
140
141void
143{
144 // Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", UintegerValue (1)); //
145 // enable rts cts all the time.
146 CreateNodes();
150
151 std::cout << "Starting simulation for " << totalTime << " s ...\n";
152
156}
157
158void
160{
161}
162
163void
165{
166 std::cout << "Creating " << (unsigned)size << " nodes " << step << " m apart.\n";
168 // Name nodes
169 for (uint32_t i = 0; i < size; ++i)
170 {
171 std::ostringstream os;
172 os << "node-" << i;
173 Names::Add(os.str(), nodes.Get(i));
174 }
175 // Create static grid
176 MobilityHelper mobility;
177 mobility.SetPositionAllocator("ns3::GridPositionAllocator",
178 "MinX",
179 DoubleValue(0.0),
180 "MinY",
181 DoubleValue(0.0),
182 "DeltaX",
184 "DeltaY",
185 DoubleValue(0),
186 "GridWidth",
188 "LayoutType",
189 StringValue("RowFirst"));
190 mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
191 mobility.Install(nodes);
192}
193
194void
196{
197 WifiMacHelper wifiMac;
198 wifiMac.SetType("ns3::AdhocWifiMac");
199 YansWifiPhyHelper wifiPhy;
201 wifiPhy.SetChannel(wifiChannel.Create());
202 WifiHelper wifi;
203 wifi.SetRemoteStationManager("ns3::ConstantRateWifiManager",
204 "DataMode",
205 StringValue("OfdmRate6Mbps"),
206 "RtsCtsThreshold",
207 UintegerValue(0));
208 devices = wifi.Install(wifiPhy, wifiMac, nodes);
209
210 if (pcap)
211 {
212 wifiPhy.EnablePcapAll(std::string("aodv"));
213 }
214}
215
216void
218{
219 AodvHelper aodv;
220 // you can configure AODV attributes here using aodv.Set(name, value)
222 stack.SetRoutingHelper(aodv); // has effect on the next Install ()
223 stack.Install(nodes);
224 Ipv4AddressHelper address;
225 address.SetBase("10.0.0.0", "255.0.0.0");
226 interfaces = address.Assign(devices);
227
228 if (printRoutes)
229 {
230 Ptr<OutputStreamWrapper> routingStream =
231 Create<OutputStreamWrapper>("aodv.routes", std::ios::out);
233 }
234}
235
236void
238{
240 ping.SetAttribute("VerboseMode", EnumValue(Ping::VerboseMode::VERBOSE));
241
243 p.Start(Seconds(0));
244 p.Stop(Seconds(totalTime) - Seconds(0.001));
245
246 // move node away
247 Ptr<Node> node = nodes.Get(size / 2);
248 Ptr<MobilityModel> mob = node->GetObject<MobilityModel>();
251 mob,
252 Vector(1e5, 1e5, 1e5));
253}
Test script.
bool Configure(int argc, char **argv)
Configure script parameters.
void CreateDevices()
Create the devices.
void InstallApplications()
Create the simulation applications.
Ipv4InterfaceContainer interfaces
interfaces used in the example
bool printRoutes
Print routes if true.
bool pcap
Write per-device PCAP traces if true.
NodeContainer nodes
nodes used in the example
double totalTime
Simulation time, seconds.
void CreateNodes()
Create the nodes.
void Run()
Run simulation.
uint32_t size
Number of nodes.
void Report(std::ostream &os)
Report results.
void InstallInternetStack()
Create the network.
NetDeviceContainer devices
devices used in the example
double step
Distance between nodes, meters.
Helper class that adds AODV routing to nodes.
Definition aodv-helper.h:25
holds a vector of ns3::Application pointers.
void Start(Time start) const
Start all of the Applications in this container at the start time given as a parameter.
void Stop(Time stop) const
Arrange for all of the Applications in this container to Stop() at the Time given as a parameter.
ApplicationContainer Install(NodeContainer c)
Install an application on each node of the input container configured with all the attributes set wit...
void SetAttribute(const std::string &name, const AttributeValue &value)
Helper function used to set the underlying application attributes.
Parse command-line arguments.
This class can be used to hold variables of floating point type such as 'double' or 'float'.
Definition double.h:31
Hold variables of type enum.
Definition enum.h:52
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.
void SetPosition(const Vector &position)
static void Add(std::string name, Ptr< Object > object)
Add the association between the string "name" and the Ptr<Object> obj.
Definition names.cc:764
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.
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 ...
Create a ping application and associate it to a node.
Definition ping-helper.h:31
Smart pointer class similar to boost::intrusive_ptr.
static void SetSeed(uint32_t seed)
Set the seed.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static void Destroy()
Execute the events scheduled with ScheduleDestroy().
Definition simulator.cc:131
static void Run()
Run the simulation.
Definition simulator.cc:167
static void Stop()
Tell the Simulator the calling event should be the last one executed.
Definition simulator.cc:175
Hold variables of type string.
Definition string.h:45
Hold an unsigned integer type.
Definition uinteger.h:34
helps to create WifiNetDevice objects
create MAC layers for a ns3::WifiNetDevice.
void SetType(std::string type, Args &&... args)
manage and create wifi channel objects for the YANS model.
static YansWifiChannelHelper Default()
Create a channel helper in a default working state.
Ptr< YansWifiChannel > Create() 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.
Ptr< T > Create(Ts &&... args)
Create class instances by constructors with varying numbers of arguments and return them by Ptr.
Definition ptr.h:436
Time Seconds(double value)
Construct a Time in the indicated unit.
Definition nstime.h:1308
Every class exported by the ns3 library is enclosed in the ns3 namespace.
-ns3 Test suite for the ns3 wrapper script