OLD | NEW |
(Empty) | |
| 1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 /* |
| 3 * Copyright (c) 2010 Hemanth Narra |
| 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 * Author: Hemanth Narra <hemanth@ittc.ku.com> |
| 19 * |
| 20 * James P.G. Sterbenz <jpgs@ittc.ku.edu>, director |
| 21 * ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets |
| 22 * Information and Telecommunication Technology Center (ITTC) |
| 23 * and Department of Electrical Engineering and Computer Science |
| 24 * The University of Kansas Lawrence, KS USA. |
| 25 * |
| 26 * Work supported in part by NSF FIND (Future Internet Design) Program |
| 27 * under grant CNS-0626918 (Postmodern Internet Architecture), |
| 28 * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimenta
tion on GENI), |
| 29 * US Department of Defense (DoD), and ITTC at The University of Kansas. |
| 30 */ |
| 31 #include "ns3/core-module.h" |
| 32 #include "ns3/common-module.h" |
| 33 #include "ns3/node-module.h" |
| 34 #include "ns3/helper-module.h" |
| 35 #include "ns3/mobility-module.h" |
| 36 #include "ns3/contrib-module.h" |
| 37 #include "ns3/wifi-module.h" |
| 38 #include "ns3/dsdv-helper.h" |
| 39 #include <iostream> |
| 40 #include <cmath> |
| 41 |
| 42 using namespace ns3; |
| 43 |
| 44 uint16_t port = 9; |
| 45 |
| 46 NS_LOG_COMPONENT_DEFINE ("DsdvManetExample"); |
| 47 |
| 48 class DsdvManetExample |
| 49 { |
| 50 public: |
| 51 DsdvManetExample (); |
| 52 void CaseRun (uint32_t nWifis, |
| 53 uint32_t nSinks, |
| 54 double totalTime, |
| 55 std::string rate, |
| 56 std::string phyMode, |
| 57 uint32_t nodeSpeed, |
| 58 uint32_t periodicUpdateInterval, |
| 59 uint32_t settlingTime, |
| 60 double dataStart, |
| 61 bool printRoutes, |
| 62 std::string CSVfileName); |
| 63 |
| 64 private: |
| 65 uint32_t m_nWifis; |
| 66 uint32_t m_nSinks; |
| 67 double m_totalTime; |
| 68 std::string m_rate; |
| 69 std::string m_phyMode; |
| 70 uint32_t m_nodeSpeed; |
| 71 uint32_t m_periodicUpdateInterval; |
| 72 uint32_t m_settlingTime; |
| 73 double m_dataStart; |
| 74 uint32_t bytesTotal; |
| 75 uint32_t packetsReceived; |
| 76 bool m_printRoutes; |
| 77 std::string m_CSVfileName; |
| 78 |
| 79 NodeContainer nodes; |
| 80 NetDeviceContainer devices; |
| 81 Ipv4InterfaceContainer interfaces; |
| 82 |
| 83 private: |
| 84 void CreateNodes (); |
| 85 void CreateDevices (std::string tr_name); |
| 86 void InstallInternetStack (std::string tr_name); |
| 87 void InstallApplications (); |
| 88 void SetupMobility (); |
| 89 void ReceivePacket (Ptr <Socket> ); |
| 90 Ptr <Socket> SetupPacketReceive (Ipv4Address, Ptr <Node> ); |
| 91 void CheckThroughput (); |
| 92 |
| 93 }; |
| 94 |
| 95 int main (int argc, char **argv) |
| 96 { |
| 97 DsdvManetExample test; |
| 98 uint32_t nWifis = 30; |
| 99 uint32_t nSinks = 10; |
| 100 double totalTime = 100.0; |
| 101 std::string rate ("8kbps"); |
| 102 std::string phyMode ("DsssRate11Mbps"); |
| 103 uint32_t nodeSpeed = 10; // in m/s |
| 104 std::string appl = "all"; |
| 105 uint32_t periodicUpdateInterval = 15; |
| 106 uint32_t settlingTime = 6; |
| 107 double dataStart = 50.0; |
| 108 bool printRoutingTable = true; |
| 109 std::string CSVfileName = "DsdvManetExample.csv"; |
| 110 |
| 111 CommandLine cmd; |
| 112 cmd.AddValue ("nWifis", "Number of wifi nodes[Default:30]", nWifis); |
| 113 cmd.AddValue ("nSinks", "Number of wifi sink nodes[Default:10]", nSinks); |
| 114 cmd.AddValue ("totalTime", "Total Simulation time[Default:100]", totalTime); |
| 115 cmd.AddValue ("phyMode", "Wifi Phy mode[Default:DsssRate11Mbps]", phyMode); |
| 116 cmd.AddValue ("rate", "CBR traffic rate[Default:8kbps]", rate); |
| 117 cmd.AddValue ("nodeSpeed", "Node speed in RandomWayPoint model[Default:10]", n
odeSpeed); |
| 118 cmd.AddValue ("periodicUpdateInterval", "Periodic Interval Time[Default=15]",
periodicUpdateInterval); |
| 119 cmd.AddValue ("settlingTime", "Settling Time before sending out an update for
changed metric[Default=6]", settlingTime); |
| 120 cmd.AddValue ("dataStart", "Time at which nodes start to transmit data[Default
=50.0]", dataStart); |
| 121 cmd.AddValue ("printRoutingTable", "print routing table for nodes[Default:1]",
printRoutingTable); |
| 122 cmd.AddValue ("CSVfileName", "The name of the CSV output file name[Default:Dsd
vManetExample.csv]", CSVfileName); |
| 123 cmd.Parse (argc, argv); |
| 124 |
| 125 std::ofstream out (CSVfileName.c_str ()); |
| 126 out << "SimulationSecond," << |
| 127 "ReceiveRate," << |
| 128 "PacketsReceived," << |
| 129 "NumberOfSinks," << |
| 130 std::endl; |
| 131 out.close (); |
| 132 |
| 133 SeedManager::SetSeed (12345); |
| 134 |
| 135 Config::SetDefault ("ns3::OnOffApplication::PacketSize", StringValue ("1000"))
; |
| 136 Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue (rate)); |
| 137 Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode", StringVal
ue (phyMode)); |
| 138 Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringVa
lue ("2000")); |
| 139 |
| 140 test = DsdvManetExample (); |
| 141 test.CaseRun (nWifis, nSinks, totalTime, rate, phyMode, nodeSpeed, periodicUpd
ateInterval, |
| 142 settlingTime, dataStart, printRoutingTable, CSVfileName); |
| 143 |
| 144 return 0; |
| 145 } |
| 146 |
| 147 DsdvManetExample::DsdvManetExample () |
| 148 : bytesTotal (0), |
| 149 packetsReceived (0) |
| 150 { |
| 151 } |
| 152 |
| 153 void |
| 154 DsdvManetExample::ReceivePacket (Ptr <Socket> socket) |
| 155 { |
| 156 NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << " Received one packet!"); |
| 157 Ptr <Packet> packet; |
| 158 while (packet = socket->Recv ()) |
| 159 { |
| 160 bytesTotal += packet->GetSize (); |
| 161 packetsReceived += 1; |
| 162 } |
| 163 } |
| 164 |
| 165 void |
| 166 DsdvManetExample::CheckThroughput () |
| 167 { |
| 168 double kbs = (bytesTotal * 8.0) / 1000; |
| 169 bytesTotal = 0; |
| 170 |
| 171 std::ofstream out (m_CSVfileName.c_str (), std::ios::app); |
| 172 |
| 173 out << (Simulator::Now ()).GetSeconds () << "," << kbs << "," << packetsReceiv
ed << "," << m_nSinks << std::endl; |
| 174 |
| 175 out.close (); |
| 176 packetsReceived = 0; |
| 177 Simulator::Schedule (Seconds (1.0), &DsdvManetExample::CheckThroughput, this); |
| 178 } |
| 179 |
| 180 Ptr <Socket> |
| 181 DsdvManetExample::SetupPacketReceive (Ipv4Address addr, Ptr <Node> node) |
| 182 { |
| 183 |
| 184 TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory"); |
| 185 Ptr <Socket> sink = Socket::CreateSocket (node, tid); |
| 186 InetSocketAddress local = InetSocketAddress (addr, port); |
| 187 sink->Bind (local); |
| 188 sink->SetRecvCallback (MakeCallback ( &DsdvManetExample::ReceivePacket, this))
; |
| 189 |
| 190 return sink; |
| 191 } |
| 192 |
| 193 void |
| 194 DsdvManetExample::CaseRun (uint32_t nWifis, uint32_t nSinks, double totalTime, s
td::string rate, |
| 195 std::string phyMode, uint32_t nodeSpeed, uint32_t per
iodicUpdateInterval, uint32_t settlingTime, |
| 196 double dataStart, bool printRoutes, std::string CSVfi
leName) |
| 197 { |
| 198 m_nWifis = nWifis; |
| 199 m_nSinks = nSinks; |
| 200 m_totalTime = totalTime; |
| 201 m_rate = rate; |
| 202 m_phyMode = phyMode; |
| 203 m_nodeSpeed = nodeSpeed; |
| 204 m_periodicUpdateInterval = periodicUpdateInterval; |
| 205 m_settlingTime = settlingTime; |
| 206 m_dataStart = dataStart; |
| 207 m_printRoutes = printRoutes; |
| 208 m_CSVfileName = CSVfileName; |
| 209 |
| 210 std::stringstream ss; |
| 211 ss << m_nWifis; |
| 212 std::string t_nodes = ss.str (); |
| 213 |
| 214 std::stringstream ss3; |
| 215 ss3 << m_totalTime; |
| 216 std::string sTotalTime = ss3.str (); |
| 217 |
| 218 std::string tr_name = "Dsdv_Manet_" + t_nodes + "Nodes_" + sTotalTime + "SimTi
me"; |
| 219 std::cout << "Trace file generated is " << tr_name << ".tr\n"; |
| 220 |
| 221 CreateNodes (); |
| 222 CreateDevices (tr_name); |
| 223 SetupMobility (); |
| 224 InstallInternetStack (tr_name); |
| 225 InstallApplications (); |
| 226 |
| 227 std::cout << "\nStarting simulation for " << m_totalTime << " s ...\n"; |
| 228 |
| 229 CheckThroughput (); |
| 230 |
| 231 Simulator::Stop (Seconds (m_totalTime)); |
| 232 Simulator::Run (); |
| 233 Simulator::Destroy (); |
| 234 } |
| 235 |
| 236 void |
| 237 DsdvManetExample::CreateNodes () |
| 238 { |
| 239 std::cout << "Creating " << (unsigned) m_nWifis << " nodes.\n"; |
| 240 nodes.Create (m_nWifis); |
| 241 NS_ASSERT_MSG (m_nWifis > m_nSinks, "Sinks must be less or equal to the number
of nodes in network"); |
| 242 } |
| 243 |
| 244 void |
| 245 DsdvManetExample::SetupMobility () |
| 246 { |
| 247 MobilityHelper mobility; |
| 248 ObjectFactory pos; |
| 249 pos.SetTypeId ("ns3::RandomRectanglePositionAllocator"); |
| 250 pos.Set ("X", RandomVariableValue (UniformVariable (0, 1000))); |
| 251 pos.Set ("Y", RandomVariableValue (UniformVariable (0, 1000))); |
| 252 |
| 253 Ptr <PositionAllocator> taPositionAlloc = pos.Create ()->GetObject <PositionAl
locator> (); |
| 254 mobility.SetMobilityModel ("ns3::RandomWaypointMobilityModel", "Speed", Random
VariableValue (ConstantVariable (m_nodeSpeed)), |
| 255 "Pause", RandomVariableValue (ConstantVariable (2.0
)), "PositionAllocator", PointerValue (taPositionAlloc)); |
| 256 mobility.SetPositionAllocator (taPositionAlloc); |
| 257 mobility.Install (nodes); |
| 258 } |
| 259 |
| 260 void |
| 261 DsdvManetExample::CreateDevices (std::string tr_name) |
| 262 { |
| 263 NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default (); |
| 264 wifiMac.SetType ("ns3::AdhocWifiMac"); |
| 265 YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); |
| 266 YansWifiChannelHelper wifiChannel; |
| 267 wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel"); |
| 268 wifiChannel.AddPropagationLoss ("ns3::FriisPropagationLossModel"); |
| 269 wifiPhy.SetChannel (wifiChannel.Create ()); |
| 270 WifiHelper wifi; |
| 271 wifi.SetStandard (WIFI_PHY_STANDARD_80211b); |
| 272 wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode", Stri
ngValue (m_phyMode), "ControlMode", |
| 273 StringValue (m_phyMode)); |
| 274 devices = wifi.Install (wifiPhy, wifiMac, nodes); |
| 275 |
| 276 AsciiTraceHelper ascii; |
| 277 wifiPhy.EnableAsciiAll (ascii.CreateFileStream (tr_name + ".tr")); |
| 278 wifiPhy.EnablePcapAll (tr_name); |
| 279 } |
| 280 |
| 281 void |
| 282 DsdvManetExample::InstallInternetStack (std::string tr_name) |
| 283 { |
| 284 DsdvHelper dsdv; |
| 285 dsdv.Set ("PeriodicUpdateInterval", TimeValue (Seconds (m_periodicUpdateInterv
al))); |
| 286 dsdv.Set ("SettlingTime", TimeValue (Seconds (m_settlingTime))); |
| 287 InternetStackHelper stack; |
| 288 stack.SetRoutingHelper (dsdv); |
| 289 stack.Install (nodes); |
| 290 Ipv4AddressHelper address; |
| 291 address.SetBase ("10.1.1.0", "255.255.255.0"); |
| 292 interfaces = address.Assign (devices); |
| 293 if (m_printRoutes) |
| 294 { |
| 295 Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ((tr_
name + ".routes"), std::ios::out); |
| 296 dsdv.PrintRoutingTableAllAt (Seconds (m_periodicUpdateInterval), routingSt
ream); |
| 297 } |
| 298 } |
| 299 |
| 300 void |
| 301 DsdvManetExample::InstallApplications () |
| 302 { |
| 303 for (uint32_t i = 0; i <= m_nSinks - 1; i++ ) |
| 304 { |
| 305 Ptr<Node> node = NodeList::GetNode (i); |
| 306 Ipv4Address nodeAddress = node->GetObject<Ipv4> ()->GetAddress (1, 0).GetL
ocal (); |
| 307 Ptr<Socket> sink = SetupPacketReceive (nodeAddress, node); |
| 308 } |
| 309 |
| 310 for (uint32_t clientNode = 0; clientNode <= m_nWifis - 1; clientNode++ ) |
| 311 { |
| 312 for (uint32_t j = 0; j <= m_nSinks - 1; j++ ) |
| 313 { |
| 314 OnOffHelper onoff1 ("ns3::UdpSocketFactory", Address (InetSocketAddres
s (interfaces.GetAddress (j), port))); |
| 315 onoff1.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (
1))); |
| 316 onoff1.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable
(0))); |
| 317 |
| 318 if (j != clientNode) |
| 319 { |
| 320 ApplicationContainer apps1 = onoff1.Install (nodes.Get (clientNode
)); |
| 321 UniformVariable var; |
| 322 apps1.Start (Seconds (var.GetValue (m_dataStart, m_dataStart + 1))
); |
| 323 apps1.Stop (Seconds (m_totalTime)); |
| 324 } |
| 325 } |
| 326 } |
| 327 } |
| 328 |
OLD | NEW |