Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.34 KB | None | 0 0
  1. /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
  2. /*
  3. * Copyright (c) 2008,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. * Author: Kirill Andreev <andreev@iitp.ru>
  19. *
  20. *
  21. * By default this script creates m_xSize * m_ySize square grid topology with
  22. * IEEE802.11s stack installed at each node with peering management
  23. * and HWMP protocol.
  24. * The side of the square cell is defined by m_step parameter.
  25. * When topology is created, UDP ping is installed to opposite corners
  26. * by diagonals. packet size of the UDP ping and interval between two
  27. * successive packets is configurable.
  28. *
  29. * m_xSize * step
  30. * |<--------->|
  31. * step
  32. * |<--->|
  33. * * --- * --- * <---Ping sink _
  34. * | \ | / | ^
  35. * | \ | / | |
  36. * * --- * --- * m_ySize * step |
  37. * | / | \ | |
  38. * | / | \ | |
  39. * * --- * --- * _
  40. * ^ Ping source
  41. *
  42. * See also MeshTest::Configure to read more about configurable
  43. * parameters.
  44. */
  45.  
  46.  
  47. #include "ns3/core-module.h"
  48. #include "ns3/internet-module.h"
  49. #include "ns3/network-module.h"
  50. #include "ns3/applications-module.h"
  51. #include "ns3/wifi-module.h"
  52. #include "ns3/mesh-module.h"
  53. #include "ns3/mobility-module.h"
  54. #include "ns3/mesh-helper.h"
  55. #include "ns3/netanim-module.h"
  56. //added lib
  57. #include "ns3/gnuplot.h"
  58. #include "ns3/flow-monitor-module.h"
  59. #include <ns3/flow-monitor-helper.h>
  60.  
  61.  
  62. #include <iostream>
  63. #include <sstream>
  64. #include <fstream>
  65.  
  66. using namespace ns3;
  67.  
  68. void ThroughputMonitor (FlowMonitorHelper *fmhelper, Ptr<FlowMonitor> flowMon,Gnuplot2dDataset DataSet);
  69.  
  70. NS_LOG_COMPONENT_DEFINE ("TestMeshScript");
  71.  
  72. class MeshTest
  73. {
  74. public:
  75. /// Init test
  76. MeshTest ();
  77. /// Configure test from command line arguments
  78. void Configure (int argc, char ** argv);
  79. /// Run test
  80. int Run ();
  81. private:
  82. int m_xSize;
  83. int m_ySize;
  84. double m_step;
  85. double m_randomStart;
  86. double m_totalTime;
  87. double m_packetInterval;
  88. uint16_t m_packetSize;
  89. uint32_t m_nIfaces;
  90. bool m_chan;
  91. bool m_pcap;
  92. std::string m_stack;
  93. std::string m_root;
  94. /// List of network nodes
  95. NodeContainer nodes;
  96. /// List of all mesh point devices
  97. NetDeviceContainer meshDevices;
  98. //Addresses of interfaces:
  99. Ipv4InterfaceContainer interfaces;
  100. // MeshHelper. Report is not static methods
  101. MeshHelper mesh;
  102. private:
  103. /// Create nodes and setup their mobility
  104. void CreateNodes ();
  105. /// Install internet m_stack on nodes
  106. void InstallInternetStack ();
  107. /// Install applications
  108. void InstallApplication ();
  109. /// Print mesh devices diagnostics
  110. void Report ();
  111. };
  112. MeshTest::MeshTest () :
  113. m_xSize (3),
  114. m_ySize (2),
  115. m_step (100.0),
  116. m_randomStart (0.1),
  117. m_totalTime (100.0),
  118. m_packetInterval (0.1),
  119. m_packetSize (1024),
  120. m_nIfaces (1),
  121. m_chan (true),
  122. m_pcap (false),
  123. m_stack ("ns3::Dot11sStack"),
  124. m_root ("ff:ff:ff:ff:ff:ff")
  125. {
  126. }
  127. void
  128. MeshTest::Configure (int argc, char *argv[])
  129. {
  130. CommandLine cmd;
  131.  
  132. cmd.AddValue ("x-size", "Number of nodes in a row grid. [6]", m_xSize);
  133. cmd.AddValue ("y-size", "Number of rows in a grid. [1]", m_ySize);
  134. cmd.AddValue ("step", "Size of edge in our grid, meters. [100 m]", m_step);
  135. /*
  136. * As soon as starting node means that it sends a beacon,
  137. * simultaneous start is not good.
  138. */
  139. cmd.AddValue ("start", "Maximum random start delay, seconds. [0.1 s]", m_randomStart);
  140. cmd.AddValue ("time", "Simulation time, seconds [100 s]", m_totalTime);
  141. cmd.AddValue ("packet-interval", "Interval between packets in UDP ping, seconds [0.001 s]", m_packetInterval);
  142. cmd.AddValue ("packet-size", "Size of packets in UDP ping", m_packetSize);
  143. cmd.AddValue ("interfaces", "Number of radio interfaces used by each mesh point. [1]", m_nIfaces);
  144. cmd.AddValue ("channels", "Use different frequency channels for different interfaces. [0]", m_chan);
  145. cmd.AddValue ("pcap", "Enable PCAP traces on interfaces. [0]", m_pcap);
  146. cmd.AddValue ("stack", "Type of protocol stack. ns3::Dot11sStack by default", m_stack);
  147. cmd.AddValue ("root", "Mac address of root mesh point in HWMP", m_root);
  148.  
  149. cmd.Parse (argc, argv);
  150. NS_LOG_DEBUG ("Grid:" << m_xSize << "*" << m_ySize);
  151. NS_LOG_DEBUG ("Simulation time: " << m_totalTime << " s");
  152. }
  153. void
  154. MeshTest::CreateNodes ()
  155. {
  156. /*
  157. * Create m_ySize*m_xSize stations to form a grid topology
  158. */
  159. // nodes.Create (m_ySize*m_xSize);
  160. nodes.Create (6);
  161. // Configure YansWifiChannel
  162. YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
  163. YansWifiChannelHelper wifiChannel = YansWifiChannelHelper::Default ();
  164. wifiPhy.SetChannel (wifiChannel.Create ());
  165. /*
  166. * Create mesh helper and set stack installer to it
  167. * Stack installer creates all needed protocols and install them to
  168. * mesh point device
  169. */
  170. mesh = MeshHelper::Default ();
  171. if (!Mac48Address (m_root.c_str ()).IsBroadcast ())
  172. {
  173. mesh.SetStackInstaller (m_stack, "Root", Mac48AddressValue (Mac48Address (m_root.c_str ())));
  174. }
  175. else
  176. {
  177. //If root is not set, we do not use "Root" attribute, because it
  178. //is specified only for 11s
  179. mesh.SetStackInstaller (m_stack);
  180. }
  181. if (m_chan)
  182. {
  183. mesh.SetSpreadInterfaceChannels (MeshHelper::SPREAD_CHANNELS);
  184. }
  185. else
  186. {
  187. mesh.SetSpreadInterfaceChannels (MeshHelper::ZERO_CHANNEL);
  188. }
  189. mesh.SetMacType ("RandomStart", TimeValue (Seconds (m_randomStart)));
  190. // Set number of interfaces - default is single-interface mesh point
  191. mesh.SetNumberOfInterfaces (m_nIfaces);
  192. // Install protocols and return container if MeshPointDevices
  193. meshDevices = mesh.Install (wifiPhy, nodes);
  194. // Setup mobility - static grid topology
  195. //MobilityHelper mobility;
  196.  
  197. //mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
  198. // "MinX", DoubleValue (0.0),
  199. // "MinY", DoubleValue (0.0),
  200. // "DeltaX", DoubleValue (m_step),
  201. // "DeltaY", DoubleValue (m_step),
  202. // "GridWidth", UintegerValue (m_xSize),
  203. // "LayoutType", StringValue ("RowFirst"));
  204.  
  205.  
  206. //mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel");
  207. //mobility.Install (nodes);
  208.  
  209.  
  210. Ptr<ListPositionAllocator> nodePosition = CreateObject<ListPositionAllocator>();
  211. nodePosition->Add(Vector(100.0,200.0,0.0));
  212. nodePosition->Add(Vector(100.0,100.0,0.0));
  213. nodePosition->Add(Vector(100.0,300.0,0.0));
  214. nodePosition->Add(Vector(200.0,200.0,0.0));
  215. nodePosition->Add(Vector(200.0,100.0,0.0));
  216. nodePosition->Add(Vector(200.0,300.0,0.0));
  217.  
  218.  
  219. MobilityHelper mobility;
  220. mobility.SetMobilityModel("ns3::ConstantPositionMobilityModel");
  221. mobility.SetPositionAllocator(nodePosition);
  222. mobility.Install(nodes);
  223.  
  224.  
  225.  
  226. if (m_pcap)
  227. wifiPhy.EnablePcapAll (std::string ("mp-"));
  228. }
  229. void
  230. MeshTest::InstallInternetStack ()
  231. {
  232. InternetStackHelper internetStack;
  233. internetStack.Install (nodes);
  234. Ipv4AddressHelper address;
  235. address.SetBase ("10.1.1.0", "255.255.255.0");
  236. interfaces = address.Assign (meshDevices);
  237. }
  238. void
  239. MeshTest::InstallApplication ()
  240. {
  241. UdpEchoServerHelper echoServer (9);
  242. ApplicationContainer serverApps = echoServer.Install (nodes.Get (0));
  243. serverApps.Start (Seconds (0.0));
  244. serverApps.Stop (Seconds (m_totalTime));
  245. UdpEchoClientHelper echoClient (interfaces.GetAddress (0), 9);
  246. echoClient.SetAttribute ("MaxPackets", UintegerValue ((uint32_t)(m_totalTime*(1/m_packetInterval))));
  247. echoClient.SetAttribute ("Interval", TimeValue (Seconds (m_packetInterval)));
  248. echoClient.SetAttribute ("PacketSize", UintegerValue (m_packetSize));
  249. ApplicationContainer clientApps = echoClient.Install (nodes.Get (4));
  250. clientApps.Start (Seconds (0.0));
  251. clientApps.Stop (Seconds (m_totalTime));
  252. }
  253. int
  254. MeshTest::Run ()
  255. {
  256. CreateNodes ();
  257. InstallInternetStack ();
  258. InstallApplication ();
  259.  
  260. //Gnuplot parameters
  261.  
  262. std::string fileNameWithNoExtension = "FlowVSThroughput_";
  263. std::string graphicsFileName = fileNameWithNoExtension + ".png";
  264. std::string plotFileName = fileNameWithNoExtension + ".plt";
  265. std::string plotTitle = "Flow vs Throughput";
  266. std::string dataTitle = "Throughput";
  267.  
  268. // Instantiate the plot and set its title.
  269. Gnuplot gnuplot (graphicsFileName);
  270. gnuplot.SetTitle (plotTitle);
  271.  
  272. // Make the graphics file, which the plot file will be when it
  273. // is used with Gnuplot, be a PNG file.
  274. gnuplot.SetTerminal ("png");
  275.  
  276. // Set the labels for each axis.
  277. gnuplot.SetLegend ("Flow", "Throughput");
  278.  
  279.  
  280. Gnuplot2dDataset dataset;
  281. dataset.SetTitle (dataTitle);
  282. dataset.SetStyle (Gnuplot2dDataset::LINES_POINTS);
  283.  
  284. //flowMonitor declaration
  285. FlowMonitorHelper fmHelper;
  286. Ptr<FlowMonitor> allMon = fmHelper.InstallAll();
  287. // call the flow monitor function
  288. ThroughputMonitor(&fmHelper, allMon, dataset);
  289.  
  290.  
  291.  
  292.  
  293. Simulator::Schedule (Seconds (m_totalTime), &MeshTest::Report, this);
  294. Simulator::Stop (Seconds (m_totalTime));
  295.  
  296.  
  297. AnimationInterface anim ("simulatorsembug.xml");
  298.  
  299.  
  300.  
  301.  
  302. Simulator::Run ();
  303.  
  304. //Gnuplot ...continued
  305. gnuplot.AddDataset (dataset);
  306. // Open the plot file.
  307. std::ofstream plotFile (plotFileName.c_str());
  308. // Write the plot file.
  309. gnuplot.GenerateOutput (plotFile);
  310. // Close the plot file.
  311. plotFile.close ();
  312.  
  313.  
  314. Simulator::Destroy ();
  315. return 0;
  316. }
  317. void
  318. MeshTest::Report ()
  319. {
  320. unsigned n (0);
  321. for (NetDeviceContainer::Iterator i = meshDevices.Begin (); i != meshDevices.End (); ++i, ++n)
  322. {
  323. std::ostringstream os;
  324. os << "mp-report-" << n << ".xml";
  325. std::cerr << "Printing mesh point device #" << n << " diagnostics to " << os.str () << "\n";
  326. std::ofstream of;
  327. of.open (os.str ().c_str ());
  328. if (!of.is_open ())
  329. {
  330. std::cerr << "Error: Can't open file " << os.str () << "\n";
  331. return;
  332. }
  333. mesh.Report (*i, of);
  334. of.close ();
  335. }
  336. }
  337. int
  338. main (int argc, char *argv[])
  339. {
  340. MeshTest t;
  341. t.Configure (argc, argv);
  342. return t.Run ();
  343. }
  344.  
  345. void ThroughputMonitor (FlowMonitorHelper *fmhelper, Ptr<FlowMonitor> flowMon,Gnuplot2dDataset DataSet)
  346. {
  347. double localThrou=0;
  348. std::map<FlowId, FlowMonitor::FlowStats> flowStats = flowMon->GetFlowStats();
  349. Ptr<Ipv4FlowClassifier> classing = DynamicCast<Ipv4FlowClassifier> (fmhelper->GetClassifier());
  350. for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator stats = flowStats.begin (); stats != flowStats.end (); ++stats)
  351. {
  352. if(stats->first == 2){//IFFFFFFFFFFFFFFFFFFFFFFF
  353. Ipv4FlowClassifier::FiveTuple fiveTuple = classing->FindFlow (stats->first);
  354. std::cout<<"Flow ID : " << stats->first <<" ; "<< fiveTuple.sourceAddress <<" -----> "<<fiveTuple.destinationAddress<<std::endl;
  355. std::cout<<"Tx Packets = " << stats->second.txPackets<<std::endl;
  356. std::cout<<"Rx Packets = " << stats->second.rxPackets<<std::endl;
  357. std::cout<<"Duration : "<<(stats->second.timeLastRxPacket.GetSeconds()-stats->second.timeFirstTxPacket.GetSeconds())<<std::endl;
  358. std::cout<<"Last Received Packet : "<< stats->second.timeLastRxPacket.GetSeconds()<<" Seconds"<<std::endl;
  359. std::cout<<"Throughput: " << stats->second.rxBytes * 8.0 / (stats->second.timeLastRxPacket.GetSeconds()-stats->second.timeFirstTxPacket.GetSeconds())/1024/1024 << " Mbps"<<std::endl;
  360. localThrou=(stats->second.rxBytes * 8.0 / (stats->second.timeLastRxPacket.GetSeconds()-stats->second.timeFirstTxPacket.GetSeconds())/1024/1024);
  361. // updata gnuplot data
  362. DataSet.Add((double)Simulator::Now().GetSeconds(),(double) localThrou);
  363. std::cout<<"---------------------------------------------------------------------------"<<std::endl;
  364. }//IFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
  365. }
  366. Simulator::Schedule(Seconds(1),&ThroughputMonitor, fmhelper, flowMon,DataSet);
  367. //if(flowToXml)
  368. {
  369. flowMon->SerializeToXmlFile ("ThroughputMonitor.xml", true, true);
  370. }
  371.  
  372. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement