|
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 OLSR manet routing protocol. |
19 |
* |
20 |
* Authors: Pavel Boyko <boyko@iitp.ru> |
21 |
*/ |
22 |
|
23 |
#include "ns3/olsr-module.h" |
24 |
#include "ns3/core-module.h" |
25 |
#include "ns3/common-module.h" |
26 |
#include "ns3/node-module.h" |
27 |
#include "ns3/helper-module.h" |
28 |
#include "ns3/mobility-module.h" |
29 |
#include "ns3/contrib-module.h" |
30 |
#include "ns3/wifi-module.h" |
31 |
#include "ns3/v4ping-helper.h" |
32 |
#include <iostream> |
33 |
#include <cmath> |
34 |
|
35 |
using namespace ns3; |
36 |
|
37 |
/** |
38 |
* \brief Test script. |
39 |
* |
40 |
* This script creates 1-dimensional grid topology and then ping last node from the first one: |
41 |
* |
42 |
* [10.0.0.1] <-- step --> [10.0.0.2] <-- step --> [10.0.0.3] <-- step --> [10.0.04] |
43 |
* |
44 |
* ping 10.0.0.4 |
45 |
*/ |
46 |
class OlsrExample |
47 |
{ |
48 |
public: |
49 |
OlsrExample (); |
50 |
/// Configure script parameters, \return true on successful configuration |
51 |
bool Configure (int argc, char **argv); |
52 |
/// Run simulation |
53 |
void Run (); |
54 |
/// Report results |
55 |
void Report (std::ostream & os); |
56 |
|
57 |
private: |
58 |
///\name parameters |
59 |
//\{ |
60 |
/// Number of nodes |
61 |
uint32_t size; |
62 |
/// Distance between nodes, meters |
63 |
double step; |
64 |
/// Simulation time, seconds |
65 |
double totalTime; |
66 |
/// Write per-device PCAP traces if true |
67 |
bool pcap; |
68 |
//\} |
69 |
|
70 |
///\name network |
71 |
//\{ |
72 |
NodeContainer nodes; |
73 |
NetDeviceContainer devices; |
74 |
Ipv4InterfaceContainer interfaces; |
75 |
//\} |
76 |
|
77 |
private: |
78 |
void CreateNodes (); |
79 |
void CreateDevices (); |
80 |
void InstallInternetStack (); |
81 |
void InstallApplications (); |
82 |
}; |
83 |
|
84 |
int main (int argc, char **argv) |
85 |
{ |
86 |
OlsrExample test; |
87 |
if (! test.Configure(argc, argv)) |
88 |
NS_FATAL_ERROR ("Configuration failed. Aborted."); |
89 |
|
90 |
test.Run (); |
91 |
test.Report (std::cout); |
92 |
return 0; |
93 |
} |
94 |
|
95 |
//----------------------------------------------------------------------------- |
96 |
OlsrExample::OlsrExample () : |
97 |
size (4), |
98 |
step (120), |
99 |
totalTime (60), |
100 |
pcap (true) |
101 |
{ |
102 |
} |
103 |
|
104 |
bool |
105 |
OlsrExample::Configure (int argc, char **argv) |
106 |
{ |
107 |
// Enable AODV logs by default. Comment this if too noisy |
108 |
// LogComponentEnable("OlsrRoutingProtocol", LOG_LEVEL_ALL); |
109 |
|
110 |
SeedManager::SetSeed(12345); |
111 |
CommandLine cmd; |
112 |
|
113 |
cmd.AddValue ("pcap", "Write PCAP traces.", pcap); |
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 |
123 |
OlsrExample::Run () |
124 |
{ |
125 |
// Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", UintegerValue (1)); // enable rts cts all the time. |
126 |
CreateNodes (); |
127 |
CreateDevices (); |
128 |
InstallInternetStack (); |
129 |
InstallApplications (); |
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 |
OlsrExample::Report (std::ostream &) |
140 |
{ |
141 |
} |
142 |
|
143 |
void |
144 |
OlsrExample::CreateNodes () |
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 |
156 |
MobilityHelper mobility; |
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 |
169 |
OlsrExample::CreateDevices () |
170 |
{ |
171 |
NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); |
172 |
wifiMac.SetType ("ns3::AdhocWifiMac"); |
173 |
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); |
174 |
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default (); |
175 |
wifiPhy.SetChannel (wifiChannel.Create ()); |
176 |
WifiHelper wifi = WifiHelper::Default (); |
177 |
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("wifia-6mbs"), "RtsCtsThreshold", UintegerValue (0)); |
178 |
devices = wifi.Install (wifiPhy, wifiMac, nodes); |
179 |
|
180 |
if (pcap) |
181 |
{ |
182 |
wifiPhy.EnablePcapAll (std::string ("olsr")); |
183 |
} |
184 |
} |
185 |
|
186 |
void |
187 |
OlsrExample::InstallInternetStack () |
188 |
{ |
189 |
OlsrHelper olsr; |
190 |
// you can configure OLSR attributes here using olsr.Set(name, value) |
191 |
InternetStackHelper stack; |
192 |
stack.SetRoutingHelper (olsr); |
193 |
stack.Install (nodes); |
194 |
Ipv4AddressHelper address; |
195 |
address.SetBase ("10.0.0.0", "255.0.0.0"); |
196 |
interfaces = address.Assign (devices); |
197 |
} |
198 |
|
199 |
void |
200 |
OlsrExample::InstallApplications () |
201 |
{ |
202 |
V4PingHelper ping (interfaces.GetAddress (size - 1)); |
203 |
//ping.SetAttribute ("Verbose", BooleanValue (true)); Uncomment me after AODV merge |
204 |
|
205 |
ApplicationContainer p = ping.Install (nodes.Get (0)); |
206 |
p.Start (Seconds (0)); |
207 |
p.Stop (Seconds (totalTime)); |
208 |
|
209 |
// move node away |
210 |
Ptr<Node> node = nodes.Get (size/2); |
211 |
Ptr<MobilityModel> mob = node->GetObject<MobilityModel> (); |
212 |
Simulator::Schedule (Seconds (totalTime/3), &MobilityModel::SetPosition, mob, Vector (1e5, 1e5, 1e5)); |
213 |
} |
214 |
|