|
|
| 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 |
|
| 47 |
static void PingRtt (std::string context, Time rtt) |
| 48 |
{ |
| 49 |
std::cout << "Received ping reply at " << Simulator::Now ().GetSeconds () |
| 50 |
<< " with RTT of " << rtt.GetMilliSeconds () << "ms" << std::endl; |
| 51 |
} |
| 52 |
|
| 53 |
class OlsrExample |
| 54 |
{ |
| 55 |
public: |
| 56 |
OlsrExample (); |
| 57 |
/// Configure script parameters, \return true on successful configuration |
| 58 |
bool Configure (int argc, char **argv); |
| 59 |
/// Run simulation |
| 60 |
void Run (); |
| 61 |
/// Report results |
| 62 |
void Report (std::ostream & os); |
| 63 |
|
| 64 |
private: |
| 65 |
///\name parameters |
| 66 |
//\{ |
| 67 |
/// Number of nodes |
| 68 |
uint32_t size; |
| 69 |
/// Distance between nodes, meters |
| 70 |
double step; |
| 71 |
/// Simulation time, seconds |
| 72 |
double totalTime; |
| 73 |
/// Ping start time (should allow for unicast routing to settle) |
| 74 |
static const double pingStartTime = 15; |
| 75 |
/// Write per-device PCAP traces if true |
| 76 |
bool pcap; |
| 77 |
/// Write ASCII traces if true |
| 78 |
bool ascii; |
| 79 |
//\} |
| 80 |
|
| 81 |
///\name network |
| 82 |
//\{ |
| 83 |
NodeContainer nodes; |
| 84 |
NetDeviceContainer devices; |
| 85 |
Ipv4InterfaceContainer interfaces; |
| 86 |
//\} |
| 87 |
|
| 88 |
private: |
| 89 |
void CreateNodes (); |
| 90 |
void CreateDevices (); |
| 91 |
void InstallInternetStack (); |
| 92 |
void InstallApplications (); |
| 93 |
std::ofstream m_ascii; |
| 94 |
}; |
| 95 |
|
| 96 |
int main (int argc, char **argv) |
| 97 |
{ |
| 98 |
OlsrExample test; |
| 99 |
if (! test.Configure(argc, argv)) |
| 100 |
NS_FATAL_ERROR ("Configuration failed. Aborted."); |
| 101 |
|
| 102 |
test.Run (); |
| 103 |
test.Report (std::cout); |
| 104 |
return 0; |
| 105 |
} |
| 106 |
|
| 107 |
//----------------------------------------------------------------------------- |
| 108 |
OlsrExample::OlsrExample () : |
| 109 |
size (4), |
| 110 |
step (120), |
| 111 |
totalTime (60), |
| 112 |
pcap (true), |
| 113 |
ascii (true) |
| 114 |
{ |
| 115 |
} |
| 116 |
|
| 117 |
bool |
| 118 |
OlsrExample::Configure (int argc, char **argv) |
| 119 |
{ |
| 120 |
// Enable AODV logs by default. Comment this if too noisy |
| 121 |
// LogComponentEnable("OlsrRoutingProtocol", LOG_LEVEL_ALL); |
| 122 |
|
| 123 |
SeedManager::SetSeed(12345); |
| 124 |
CommandLine cmd; |
| 125 |
|
| 126 |
cmd.AddValue ("pcap", "Write PCAP traces.", pcap); |
| 127 |
cmd.AddValue ("ascii", "Write ASCII traces.", ascii); |
| 128 |
cmd.AddValue ("size", "Number of nodes.", size); |
| 129 |
cmd.AddValue ("time", "Simulation time, s.", totalTime); |
| 130 |
cmd.AddValue ("step", "Grid step, m", step); |
| 131 |
|
| 132 |
cmd.Parse (argc, argv); |
| 133 |
return true; |
| 134 |
} |
| 135 |
|
| 136 |
void |
| 137 |
OlsrExample::Run () |
| 138 |
{ |
| 139 |
// Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", UintegerValue (1)); // enable rts cts all the time. |
| 140 |
CreateNodes (); |
| 141 |
CreateDevices (); |
| 142 |
InstallInternetStack (); |
| 143 |
InstallApplications (); |
| 144 |
|
| 145 |
std::cout << "Starting simulation for " << totalTime << " s ...\n"; |
| 146 |
std::cout << "Sending a ping at " << pingStartTime << " s ...\n"; |
| 147 |
|
| 148 |
Simulator::Stop (Seconds (totalTime)); |
| 149 |
Simulator::Run (); |
| 150 |
Simulator::Destroy (); |
| 151 |
} |
| 152 |
|
| 153 |
void |
| 154 |
OlsrExample::Report (std::ostream &) |
| 155 |
{ |
| 156 |
} |
| 157 |
|
| 158 |
void |
| 159 |
OlsrExample::CreateNodes () |
| 160 |
{ |
| 161 |
std::cout << "Creating " << (unsigned)size << " nodes " << step << " m apart.\n"; |
| 162 |
nodes.Create (size); |
| 163 |
// Name nodes |
| 164 |
for (uint32_t i = 0; i < size; ++i) |
| 165 |
{ |
| 166 |
std::ostringstream os; |
| 167 |
os << "node-" << i; |
| 168 |
Names::Add (os.str (), nodes.Get (i)); |
| 169 |
} |
| 170 |
// Create static grid |
| 171 |
MobilityHelper mobility; |
| 172 |
mobility.SetPositionAllocator ("ns3::GridPositionAllocator", |
| 173 |
"MinX", DoubleValue (0.0), |
| 174 |
"MinY", DoubleValue (0.0), |
| 175 |
"DeltaX", DoubleValue (step), |
| 176 |
"DeltaY", DoubleValue (0), |
| 177 |
"GridWidth", UintegerValue (size), |
| 178 |
"LayoutType", StringValue ("RowFirst")); |
| 179 |
mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); |
| 180 |
mobility.Install (nodes); |
| 181 |
} |
| 182 |
|
| 183 |
void |
| 184 |
OlsrExample::CreateDevices () |
| 185 |
{ |
| 186 |
NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); |
| 187 |
wifiMac.SetType ("ns3::AdhocWifiMac"); |
| 188 |
YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); |
| 189 |
YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default (); |
| 190 |
wifiPhy.SetChannel (wifiChannel.Create ()); |
| 191 |
WifiHelper wifi = WifiHelper::Default (); |
| 192 |
wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", StringValue ("wifia-6mbs"), "RtsCtsThreshold", UintegerValue (0)); |
| 193 |
devices = wifi.Install (wifiPhy, wifiMac, nodes); |
| 194 |
|
| 195 |
if (pcap) |
| 196 |
{ |
| 197 |
wifiPhy.EnablePcapAll (std::string ("olsr")); |
| 198 |
} |
| 199 |
if (ascii) |
| 200 |
{ |
| 201 |
m_ascii.open ("olsr.tr"); |
| 202 |
wifiPhy.EnableAsciiAll (m_ascii); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
void |
| 207 |
OlsrExample::InstallInternetStack () |
| 208 |
{ |
| 209 |
OlsrHelper olsr; |
| 210 |
// you can configure OLSR attributes here using olsr.Set(name, value) |
| 211 |
InternetStackHelper stack; |
| 212 |
stack.SetRoutingHelper (olsr); |
| 213 |
stack.Install (nodes); |
| 214 |
Ipv4AddressHelper address; |
| 215 |
address.SetBase ("10.0.0.0", "255.0.0.0"); |
| 216 |
interfaces = address.Assign (devices); |
| 217 |
} |
| 218 |
|
| 219 |
void |
| 220 |
OlsrExample::InstallApplications () |
| 221 |
{ |
| 222 |
V4PingHelper ping (interfaces.GetAddress (size - 1)); |
| 223 |
//ping.SetAttribute ("Verbose", BooleanValue (true)); Uncomment me after AODV merge |
| 224 |
|
| 225 |
ApplicationContainer p = ping.Install (nodes.Get (0)); |
| 226 |
NS_ASSERT (pingStartTime < totalTime); |
| 227 |
p.Start (Seconds (pingStartTime)); |
| 228 |
p.Stop (Seconds (totalTime)); |
| 229 |
|
| 230 |
// print the ping rtt |
| 231 |
Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::V4Ping/Rtt", |
| 232 |
MakeCallback (&PingRtt)); |
| 233 |
|
| 234 |
// move node away |
| 235 |
Ptr<Node> node = nodes.Get (size/2); |
| 236 |
Ptr<MobilityModel> mob = node->GetObject<MobilityModel> (); |
| 237 |
Simulator::Schedule (Seconds (totalTime/3), &MobilityModel::SetPosition, mob, Vector (1e5, 1e5, 1e5)); |
| 238 |
} |
| 239 |
|