Guest User

Untitled

a guest
Dec 18th, 2017
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.77 KB | None | 0 0
  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/applications-module.h"
  27. #include "ns3/internet-module.h"
  28. #include "ns3/mobility-module.h"
  29. #include "ns3/flow-monitor-module.h"
  30. #include "ns3/point-to-point-module.h"
  31. #include "ns3/wifi-module.h"
  32. #include "ns3/v4ping-helper.h"
  33. #include "ns3/netanim-module.h"
  34. #include <iostream>
  35. #include <cmath>
  36.  
  37. using namespace ns3;
  38.  
  39. class Aodv
  40. {
  41. public:
  42. Aodv ();
  43. bool Configure (int argc, char **argv);
  44. void Run ();
  45. void Report (std::ostream & os);
  46. void RunRandomPing ();
  47. void CheckThroughput();
  48. Ptr<Socket> SetupPacketReceive (Ipv4Address addr, Ptr<Node> node);
  49. void ReceivePacket (Ptr<Socket> socket);
  50. static void CourseChange (std::string foo, Ptr<const MobilityModel> mobility)
  51. {
  52. Vector pos = mobility->GetPosition ();
  53. Vector vel = mobility->GetVelocity ();
  54. if ( pos.x > 490 || pos.x < 10 ||
  55. pos.y > 490 || pos.y < 10 ) {
  56. std::cout << Simulator::Now () << ", model=" << mobility << ", POS: x=" << pos.x << ", y=" << pos.y
  57. << ", z=" << pos.z << "; VEL:" << vel.x << ", y=" << vel.y
  58. << ", z=" << vel.z << ". Walking near the bound!!" << std::endl;
  59. }
  60. };
  61. static inline std::string PrintReceivedPacket (Ptr<Socket> socket, Ptr<Packet> packet, Address senderAddress)
  62. {
  63. std::ostringstream oss;
  64. oss << Simulator::Now ().GetSeconds () << " " << socket->GetNode ()->GetId ();
  65. if (InetSocketAddress::IsMatchingType (senderAddress)) {
  66. InetSocketAddress addr = InetSocketAddress::ConvertFrom (senderAddress);
  67. oss << " received one packet from " << addr.GetIpv4 ();
  68. } else {
  69. oss << " received one packet!";
  70. }
  71. return oss.str ();
  72. }
  73.  
  74. private:
  75. uint32_t size, packetsReceived=0, bytesTotal=0, port=9;
  76. double step, time, txp;
  77. std::string protocol="aodv", dataRate = "2";
  78. bool pcap, printRoutes, netflowMonitor;
  79. NodeContainer nodes;
  80. NetDeviceContainer devices;
  81. Ipv4InterfaceContainer interfaces;
  82. std::ostringstream filename;
  83.  
  84. private:
  85. void CreateNodes ();
  86. /// Create the devices
  87. void CreateDevices ();
  88. /// Create the network
  89. void InstallInternetStack ();
  90. /// Create the simulation applications
  91. void InstallApplications ();
  92. };
  93.  
  94. int main (int argc, char **argv)
  95. {
  96. Aodv test;
  97. if (!test.Configure (argc, argv))
  98. NS_FATAL_ERROR ("Configuration failed. Aborted.");
  99.  
  100. test.Run ();
  101. test.Report (std::cout);
  102. return 0;
  103. }
  104.  
  105. Aodv::Aodv() :
  106. size (20),
  107. step (500),
  108. time (20),
  109. txp(7.5),
  110. dataRate ("2"), // "5_5", "11"
  111. pcap (true),
  112. printRoutes (true),
  113. netflowMonitor (true)
  114. {}
  115.  
  116. bool Aodv::Configure (int argc, char **argv)
  117. {
  118. SeedManager::SetSeed (12345);
  119. CommandLine cmd;
  120.  
  121. cmd.AddValue ("pcap", "Write PCAP traces.", pcap);
  122. cmd.AddValue ("printRoutes", "Print routing table dumps.", printRoutes);
  123. cmd.AddValue ("size", "Number of nodes.", size);
  124. cmd.AddValue ("time", "Simulation time, s.", time);
  125. cmd.AddValue ("step", "Grid step, m", step);
  126.  
  127. cmd.Parse (argc, argv);
  128. return true;
  129. }
  130.  
  131. void Aodv::Run ()
  132. {
  133. filename << dataRate << "Mbps-" << std::to_string (size) << "nodes";
  134. CreateNodes ();
  135. CreateDevices ();
  136. InstallInternetStack ();
  137.  
  138. std::ofstream out ("aodv_throughput-" + filename.str () + ".output.csv");
  139. out << "SimulationSecond,"
  140. << "ReceiveRate,"
  141. << "PacketsReceived,"
  142. << "NumberOfSinks,"
  143. << "RoutingProtocol,"
  144. << "TransmissionPower"
  145. << std::endl;
  146. out.close ();
  147.  
  148. CheckThroughput();
  149. InstallApplications ();
  150.  
  151. std::cout << "Starting simulation for " << time << " s ...\n";
  152.  
  153. AnimationInterface anim ("aodv_anim-" + filename.str () + ".xml");
  154. anim.SetMobilityPollInterval (Seconds (1));
  155. anim.EnablePacketMetadata (true);
  156.  
  157. // Flow monitor
  158. FlowMonitorHelper flowmonHelper;
  159. if(netflowMonitor)
  160. flowmonHelper.InstallAll ();
  161.  
  162. Simulator::Stop (Seconds (time));
  163. Simulator::Run ();
  164.  
  165. if(netflowMonitor) {
  166. flowmonHelper.SerializeToXmlFile("aodv_flowmonitor-" + filename.str () + ".xml", false, false);
  167. }
  168.  
  169. Simulator::Destroy ();
  170. }
  171.  
  172. void Aodv::Report (std::ostream &)
  173. {
  174. }
  175.  
  176. void Aodv::CreateNodes ()
  177. {
  178. std::cout << "Creating " << (unsigned)size << " nodes " << step << " m apart.\n";
  179. nodes.Create (size);
  180. for (uint32_t i = 0; i < size; ++i) {
  181. std::ostringstream os;
  182. os << "node-" << i;
  183. Names::Add (os.str (), nodes.Get (i));
  184. }
  185.  
  186. MobilityHelper mobility;
  187.  
  188. // // location list
  189. // Ptr<ListPositionAllocator> positionAlloc = CreateObject <ListPositionAllocator>();
  190. // for (size_t i = 0; i < size; i++) {
  191. // double x, y;
  192. // x = ( (double)rand() / (double)RAND_MAX ) * step;
  193. // y = ( (double)rand() / (double)RAND_MAX ) * step;
  194. // std::cout << "set node-" << i << " to " << x << "," << y << "...\n";
  195. // positionAlloc->Add(Vector(x, y, 0));
  196. // }
  197. // mobility.SetPositionAllocator(positionAlloc);
  198.  
  199. // // grid position
  200. // mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
  201. // "MinX", DoubleValue (0),
  202. // "MinY", DoubleValue (0),
  203. // "DeltaX", DoubleValue (step/sqrt(size)),
  204. // "DeltaY", DoubleValue (step/sqrt(size)),
  205. // "GridWidth", UintegerValue (sqrt(size)));
  206.  
  207. // random in rectangle
  208. mobility.SetPositionAllocator ( "ns3::RandomRectanglePositionAllocator",
  209. "X", StringValue ("ns3::UniformRandomVariable"
  210. "[Min=" + std::to_string(0) +
  211. "|Max=" + std::to_string(step) + "]"),
  212. "Y", StringValue ("ns3::UniformRandomVariable"
  213. "[Min=" + std::to_string(0) +
  214. "|Max=" + std::to_string(step) + "]"));
  215.  
  216. // // random around corner
  217. // mobility.SetPositionAllocator ( "ns3::RandomDiscPositionAllocator",
  218. // "X", StringValue (std::to_string(step/2)),
  219. // "Y", StringValue (std::to_string(step/2)),
  220. // "Rho", StringValue ("ns3::UniformRandomVariable"
  221. // "[Min=" + std::to_string(-1*step/2) +
  222. // "|Max=" + std::to_string(step/2) + "]"));
  223.  
  224. // // random walk mode Time
  225. // mobility.SetMobilityModel ( "ns3::RandomWalk2dMobilityModel",
  226. // "Mode", StringValue ("Time"),
  227. // "Time", StringValue ("1s"),
  228. // "Speed", StringValue ("ns3::ConstantRandomVariable[Constant=2.0]"),
  229. // "Bounds", RectangleValue(Rectangle (0, 500, 0, 500))
  230. // );
  231.  
  232. mobility.Install (nodes);
  233.  
  234. Config::Connect ("/NodeList/*/$ns3::MobilityModel/CourseChange",
  235. MakeCallback (&CourseChange));
  236. }
  237.  
  238. void Aodv::CreateDevices ()
  239. {
  240. WifiMacHelper wifiMac;
  241. wifiMac.SetType ("ns3::AdhocWifiMac");
  242. YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
  243. YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
  244. wifiPhy.SetChannel (wifiChannel.Create ());
  245. WifiHelper wifi;
  246. wifi.SetStandard (WIFI_PHY_STANDARD_80211b);
  247. wifi.SetRemoteStationManager ( "ns3::ConstantRateWifiManager",
  248. // "DataMode", StringValue ("OfdmRate6Mbps"),
  249. "DataMode", StringValue ("DsssRate" + dataRate + "Mbps"),
  250. "ControlMode", StringValue ("DsssRate" + dataRate + "Mbps"),
  251. "RtsCtsThreshold", UintegerValue (0));
  252. wifiPhy.Set ("TxPowerStart",DoubleValue (txp));
  253. wifiPhy.Set ("TxPowerEnd", DoubleValue (txp));
  254. devices = wifi.Install (wifiPhy, wifiMac, nodes);
  255.  
  256. if (pcap){
  257. wifiPhy.EnablePcapAll ("aodv_pcap-" + filename.str ());
  258. }
  259. }
  260.  
  261. void Aodv::InstallInternetStack ()
  262. {
  263. AodvHelper aodv;
  264. // you can configure AODV attributes here using aodv.Set(name, value)
  265. InternetStackHelper stack;
  266. stack.SetRoutingHelper (aodv); // has effect on the next Install ()
  267. stack.Install (nodes);
  268. Ipv4AddressHelper address;
  269. address.SetBase ("10.0.0.0", "255.0.0.0");
  270. interfaces = address.Assign (devices);
  271.  
  272. if (printRoutes) {
  273. Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper> ("aodv_route-" + filename.str () + ".routes", std::ios::out);
  274. aodv.PrintRoutingTableAllAt (Seconds (8), routingStream);
  275. }
  276.  
  277. OnOffHelper onoff ("ns3::UdpSocketFactory",Address ());
  278. onoff.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]"));
  279. onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0.0]"));
  280.  
  281. for (uint32_t i = 0; i < size; i++) {
  282. Ptr<Socket> sink = SetupPacketReceive (interfaces.GetAddress (i), nodes.Get (i));
  283.  
  284. AddressValue remoteAddress (InetSocketAddress (interfaces.GetAddress (i), port));
  285. onoff.SetAttribute ("Remote", remoteAddress);
  286.  
  287. Ptr<UniformRandomVariable> var = CreateObject<UniformRandomVariable> ();
  288. ApplicationContainer temp = onoff.Install (nodes.Get ( (i + 1) % size ));
  289. temp.Start (Seconds (var->GetValue (10.0,11.0)));
  290. temp.Stop (Seconds (time));
  291. }
  292. }
  293.  
  294. Ptr<Socket> Aodv::SetupPacketReceive (Ipv4Address addr, Ptr<Node> node)
  295. {
  296. TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory");
  297. Ptr<Socket> sink = Socket::CreateSocket (node, tid);
  298. InetSocketAddress local = InetSocketAddress (addr, port);
  299. sink->Bind (local);
  300. sink->SetRecvCallback (MakeCallback (&Aodv::ReceivePacket, this));
  301. return sink;
  302. }
  303.  
  304. void Aodv::InstallApplications ()
  305. {
  306. // V4PingHelper ping (interfaces.GetAddress (size - 1));
  307. // ping.SetAttribute ("Verbose", BooleanValue (true));
  308. //
  309. // ApplicationContainer p = ping.Install (nodes.Get (0));
  310. // p.Start (Seconds (0));
  311. // p.Stop (Seconds (time) - Seconds (0.001));
  312. //
  313. // // move node away
  314. // Ptr<Node> node = nodes.Get (size/2);
  315. // Ptr<MobilityModel> mob = node->GetObject<MobilityModel> ();
  316. // Simulator::Schedule (Seconds (time/3), &MobilityModel::SetPosition, mob, Vector (1e5, 1e5, 1e5));
  317.  
  318. // RunRandomPing();
  319. }
  320.  
  321. void Aodv::RunRandomPing()
  322. {
  323. int src, dst;
  324. src = (int)( ( (double)rand() / (double)RAND_MAX ) * ( size - 1 ) ) ;
  325. dst = (int)( ( (double)rand() / (double)RAND_MAX ) * ( size - 1 ) ) ;
  326.  
  327. std::cout << "Doing ping from node-" << src << "(" << interfaces.GetAddress (src)
  328. << ") to node-" << dst << "(" << interfaces.GetAddress (dst) << ")"
  329. << std::endl;
  330.  
  331. V4PingHelper ping (interfaces.GetAddress (src));
  332. ping.SetAttribute ("Verbose", BooleanValue (true));
  333.  
  334. ApplicationContainer p = ping.Install (nodes.Get (dst));
  335. p.Start (Seconds (0));
  336. p.Stop (Seconds (time) - Seconds (0.001));
  337. }
  338.  
  339. void Aodv::CheckThroughput ()
  340. {
  341. double kbs = (bytesTotal * 8.0) / 1000;
  342. bytesTotal = 0;
  343.  
  344. std::ofstream out ("aodv_throughput-" + filename.str () + ".output.csv", std::ios::app);
  345.  
  346. out << (Simulator::Now ()).GetSeconds () << ","
  347. << kbs << ","
  348. << packetsReceived << ","
  349. << size << ","
  350. << protocol << ","
  351. << txp << ""
  352. << std::endl;
  353.  
  354. out.close ();
  355. packetsReceived = 0;
  356. Simulator::Schedule (Seconds (1.0), &Aodv::CheckThroughput, this);
  357. }
  358.  
  359. void Aodv::ReceivePacket (Ptr<Socket> socket)
  360. {
  361. Ptr<Packet> packet;
  362. Address senderAddress;
  363. while ((packet = socket->RecvFrom (senderAddress))) {
  364. bytesTotal += packet->GetSize ();
  365. packetsReceived += 1;
  366. NS_LOG_UNCOND (PrintReceivedPacket (socket, packet, senderAddress));
  367. }
  368. }
Add Comment
Please, Sign In to add comment