Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.30 KB | None | 0 0
  1. /*Zistite simuláciou najvhodnejší transportný protokol pre sieť (rôzne druhy TCP a UDP a pod.):
  2.  
  3. Topológiu:
  4. Uzly v sieti s počtom (XXX) sú rovnomerne rozmiestnené na ploche 1000*1000 [m].
  5. -- komunikácia je cez protokol: 802.11e; OLSR;
  6. -- ich model pohybu je: náhodný pohyb po ceste s náhodne meniacou sa rýchlosťou (3b)
  7. - fyzický model šírenia signálu nastavte:
  8. -- straty sú závisle podla Jakes modelu
  9. -- ConstantSpeed oneskorenia signálu
  10. - aplikačnú vrstvu naprogramujte tak, aby dva uzly posielali 10MB bajtov po max. (YYY):
  11. -- náhodne vybrané uzoly N1 a N2 ( N1 != N2 ) začali komunikovať v čase, 10s.
  12. -- náhodne vybrané uzoly N4 a N5 ( N4 != N5 ) začali komunikovať v čase, 12s.
  13. -- náhodne vybrané uzoly N6 a N7 ( N6 != N7 ) začali komunikovať v čase, 15s.
  14. -- náhodne vybrané uzoly N8 a N9 ( N8 != N9 ) začali komunikovať v čase, 16s.
  15. -- náhodne vybrané uzoly N10 a N11 ( N10 != N11 ) začali komunikovať v čase, 21s.
  16.  
  17. - dopíšte do programu kód pre grafickú vizualizáciu siete pre program "NetAnim" (3b)
  18. - dopíšte do programu kód pre vykreslenie grafu závislosti (využite knižnicu "gnuplot")
  19. -- parametra straty údajov/balíka od počtu uzlov (XXX). (4b) spolu so štandardnou odchylkou (2b)
  20. -- parametra straty údajov/balíka od veľkosti balíka (YYY). (4b) spolu so štandardnou odchylkou.
  21. -- parametra priepustnosť od veľkosti balíka (YYY). (4b) spolu so štandardnou odchylkou. (2b).
  22.  
  23. Premenné (XXX) a (YYY) vhodne zvoľte na vynesenie do grafu. Simuláciu ukončite tiež vhodným časom.
  24. Spolu 3+3+4+2+4+4+2= 22Bodov*/
  25.  
  26. #include "ns3/core-module.h"
  27. #include "ns3/network-module.h"
  28. #include "ns3/mobility-module.h"
  29. #include "ns3/config-store-module.h"
  30. #include "ns3/wifi-module.h"
  31. #include "ns3/internet-module.h"
  32. #include "ns3/olsr-helper.h"
  33. #include "ns3/flow-monitor-module.h"
  34. #include "myapp.h"
  35. #include "ns3/netanim-module.h"
  36. #include "ns3/gnuplot.h"
  37. #include "ns3/flow-monitor-module.h"
  38. #include <ns3/flow-monitor-helper.h>
  39.  
  40. #include <iostream>
  41. #include <fstream>
  42. #include <vector>
  43. #include <string>
  44.  
  45. NS_LOG_COMPONENT_DEFINE ("Zadanie_PS2");
  46.  
  47. using namespace ns3;
  48. void ThroughputMonitor (FlowMonitorHelper *fmhelper, Ptr<FlowMonitor> flowMon,Gnuplot2dDataset DataSet);
  49. uint32_t MacTxDropCount, PhyTxDropCount, PhyRxDropCount;
  50.  
  51. void MacTxDrop(Ptr<const Packet> p) //Trace zdroj indikuje paket, ktoré bolo zrušené prístroja pred prenosom
  52. {
  53. NS_LOG_INFO("Packet Drop");
  54. MacTxDropCount++;
  55. }
  56.  
  57. void PrintDrop()
  58. {
  59. std::cout << Simulator::Now().GetSeconds() << "\t" << MacTxDropCount << "\t"<< PhyTxDropCount << "\t" << PhyRxDropCount << "\n";
  60. Simulator::Schedule(Seconds(5.0), &PrintDrop);
  61. }
  62.  
  63. void PhyTxDrop(Ptr<const Packet> p) //Trace zdroj indikuje paket bola vypustená zariadením počas prenosu
  64. {
  65. NS_LOG_INFO("Packet Drop");
  66. PhyTxDropCount++;
  67. }
  68.  
  69. void PhyRxDrop(Ptr<const Packet> p) //Trace zdroj indikuje paket, ktoré bolo zrušené zariadenie počas prijímania
  70. {
  71. NS_LOG_INFO("Packet Drop");
  72. PhyRxDropCount++;
  73. }
  74.  
  75. int main (int argc, char *argv[])
  76. {
  77. std::string phyMode ("DsssRate1Mbps");
  78. double distance = 250; //
  79. uint32_t numNodes = 25; // 5x5
  80. double interval = 0.001; // seconds
  81. uint32_t packetSize = 600; // bytes
  82. uint32_t numPackets = 10000000;
  83. std::string rtslimit = "1500";
  84. CommandLine cmd;
  85.  
  86. cmd.AddValue ("phyMode", "Wifi Phy mode", phyMode); // Mode PHY krytie, ktoré sú prítomné v oblasti skenovania 802.11 protokolu
  87. cmd.AddValue ("distance", "distance (m)", distance);
  88. cmd.AddValue ("packetSize", "distance (m)", packetSize);
  89. cmd.AddValue ("rtslimit", "RTS/CTS Threshold (bytes)", rtslimit); //mechanizmus používaný 802.11 bezdrôtovej siete k zníženiu kolízie zavedené problémom skrytého uzla
  90. cmd.Parse (argc, argv);
  91.  
  92. // Convert to time object
  93. Time interPacketInterval = Seconds (interval);
  94. // turn off RTS/CTS for frames below 2200 bytes
  95. Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue (rtslimit));
  96. // Fix non-unicast data rate to be the same as that of unicast
  97. Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue (phyMode));
  98.  
  99. NodeContainer c;
  100. c.Create (numNodes);
  101.  
  102. // The below set of helpers will help us to put together the wifi NICs we want
  103. WifiHelper wifi;
  104.  
  105. YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
  106. // set it to zero; otherwise, gain will be added
  107. wifiPhy.Set ("RxGain", DoubleValue (-10) );
  108. // ns-3 supports RadioTap and Prism tracing extensions for 802.11b
  109. wifiPhy.SetPcapDataLinkType (YansWifiPhyHelper::DLT_IEEE802_11_RADIO);
  110.  
  111. YansWifiChannelHelper wifiChannel; // Vytvorenie pomocníka kanála bez akejkoľvek sady parametrov
  112. wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel"); // oneskorenie
  113. wifiChannel.AddPropagationLoss ("ns3::JakesPropagationLossModel"); // Loss - Jakes model
  114. wifiPhy.SetChannel (wifiChannel.Create ());
  115.  
  116. // Add a non-QoS upper mac, and disable rate control
  117. NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
  118. wifi.SetStandard (WIFI_PHY_STANDARD_80211b);
  119. wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
  120. "DataMode",StringValue (phyMode),
  121. "ControlMode",StringValue (phyMode));
  122. // Set it to adhoc mode
  123. wifiMac.SetType ("ns3::AdhocWifiMac");
  124. NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, c);
  125.  
  126. MobilityHelper mobility; // Helper trieda slúži na priradenie pozície a modelov mobility uzly
  127. mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
  128. "MinX", DoubleValue (0.0),
  129. "MinY", DoubleValue (0.0),
  130. "DeltaX", DoubleValue (distance),
  131. "DeltaY", DoubleValue (distance),
  132. "GridWidth", UintegerValue (5),
  133. "LayoutType", StringValue ("RowFirst"));
  134. mobility.SetMobilityModel ("ns3::RandomDirection2dMobilityModel", // nastaví MobilityModel podtriedy
  135. "Bounds", StringValue("0|1000|0|1000"), //hranica
  136. "Speed", StringValue("ns3::UniformRandomVariable[Min=1.0|Max=30.0]"),
  137. "Pause", StringValue("ns3::ConstantRandomVariable[Constant=2.0]"));
  138. mobility.Install (c);
  139.  
  140. // Enable OLSR
  141. OlsrHelper olsr;
  142.  
  143. Ipv4ListRoutingHelper list; // Uložiť do interného zoznamu odkaz na vstup smerovanie pomocníka a súvisiace prioritu. Títo pomocníci budú použité neskôr v NS3 :: Ipv4ListRoutingHelper :: Create metódu na vytvorenie NS3 :: Ipv4ListRouting objekt a pridajte doň smerovacích protokolov vytvorené s pomocníkmi.
  144. list.Add (olsr, 10);
  145.  
  146. InternetStackHelper internet;
  147. internet.SetRoutingHelper (list); // has effect on the next Install ()
  148. internet.Install (c);
  149.  
  150. Ipv4AddressHelper ipv4;
  151. NS_LOG_INFO ("Assign IP Addresses.");
  152. ipv4.SetBase ("10.1.1.0", "255.255.255.0");
  153. Ipv4InterfaceContainer ifcont = ipv4.Assign (devices);
  154.  
  155. // Create Apps
  156. uint16_t sinkPort = 10; // use the same for all apps
  157.  
  158. // UDP connection from N0 to N24
  159. Address sinkAddress1 (InetSocketAddress (ifcont.GetAddress (24), sinkPort)); // interface of n24
  160. PacketSinkHelper packetSinkHelper1 ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
  161. ApplicationContainer sinkApps1 = packetSinkHelper1.Install (c.Get (24)); //n2 as sink
  162. sinkApps1.Start (Seconds (0.));
  163. sinkApps1.Stop (Seconds (100.));
  164.  
  165. Ptr<Socket> ns3UdpSocket1 = Socket::CreateSocket (c.Get (0), UdpSocketFactory::GetTypeId ()); //source at n0
  166.  
  167. // Create UDP application at n0
  168. Ptr<MyApp> app1 = CreateObject<MyApp> ();
  169. app1->Setup (ns3UdpSocket1, sinkAddress1, packetSize, numPackets, DataRate ("1Mbps"));
  170. c.Get (0)->AddApplication (app1);
  171. app1->SetStartTime (Seconds (10.));
  172. app1->SetStopTime (Seconds (100.));
  173.  
  174. // UDP connection from N10 to N14
  175.  
  176. Address sinkAddress2 (InetSocketAddress (ifcont.GetAddress (14), sinkPort)); // interface of n14
  177. PacketSinkHelper packetSinkHelper2 ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
  178. ApplicationContainer sinkApps2 = packetSinkHelper2.Install (c.Get (14)); //n14 as sink
  179. sinkApps2.Start (Seconds (0.));
  180. sinkApps2.Stop (Seconds (100.));
  181.  
  182. Ptr<Socket> ns3UdpSocket2 = Socket::CreateSocket (c.Get (10), UdpSocketFactory::GetTypeId ()); //source at n10
  183.  
  184. // Create UDP application at n10
  185. Ptr<MyApp> app2 = CreateObject<MyApp> ();
  186. app2->Setup (ns3UdpSocket2, sinkAddress2, packetSize, numPackets, DataRate ("1Mbps"));
  187. c.Get (10)->AddApplication (app2);
  188. app2->SetStartTime (Seconds (12.));
  189. app2->SetStopTime (Seconds (100.));
  190.  
  191. // UDP connection from N20 to N4
  192.  
  193. Address sinkAddress3 (InetSocketAddress (ifcont.GetAddress (4), sinkPort)); // interface of n4
  194. PacketSinkHelper packetSinkHelper3 ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
  195. ApplicationContainer sinkApps3 = packetSinkHelper3.Install (c.Get (4)); //n2 as sink
  196. sinkApps3.Start (Seconds (0.));
  197. sinkApps3.Stop (Seconds (100.));
  198.  
  199. Ptr<Socket> ns3UdpSocket3 = Socket::CreateSocket (c.Get (20), UdpSocketFactory::GetTypeId ()); //source at n20
  200.  
  201. // Create UDP application at n20
  202. Ptr<MyApp> app3 = CreateObject<MyApp> ();
  203. app3->Setup (ns3UdpSocket3, sinkAddress3, packetSize, numPackets, DataRate ("1Mbps"));
  204. c.Get (20)->AddApplication (app3);
  205. app3->SetStartTime (Seconds (15.));
  206. app3->SetStopTime (Seconds (100.));
  207.  
  208. // UDP connection from N19 to N4
  209. Address sinkAddress4 (InetSocketAddress (ifcont.GetAddress (4), sinkPort)); // interface of n4
  210. PacketSinkHelper packetSinkHelper4 ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
  211. ApplicationContainer sinkApps4 = packetSinkHelper4.Install (c.Get (4)); //n2 as sink
  212. sinkApps4.Start (Seconds (0.));
  213. sinkApps4.Stop (Seconds (100.));
  214.  
  215. Ptr<Socket> ns3UdpSocket4 = Socket::CreateSocket (c.Get (19), UdpSocketFactory::GetTypeId ()); //source at n19
  216.  
  217. // Create UDP application at n19
  218. Ptr<MyApp> app4 = CreateObject<MyApp> ();
  219. app4->Setup (ns3UdpSocket4, sinkAddress4, packetSize, numPackets, DataRate ("1Mbps"));
  220. c.Get (19)->AddApplication (app4);
  221. app4->SetStartTime (Seconds (16.));
  222. app4->SetStopTime (Seconds (100.));
  223.  
  224. // UDP connection from N10 to N15
  225. Address sinkAddress5 (InetSocketAddress (ifcont.GetAddress (15), sinkPort)); // interface of n15
  226. PacketSinkHelper packetSinkHelper5 ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
  227. ApplicationContainer sinkApps5 = packetSinkHelper5.Install (c.Get (15)); //n15 as sink
  228. sinkApps5.Start (Seconds (0.));
  229. sinkApps5.Stop (Seconds (100.));
  230.  
  231. Ptr<Socket> ns3UdpSocket5 = Socket::CreateSocket (c.Get (10), UdpSocketFactory::GetTypeId ()); //source at n10
  232.  
  233. // Create UDP application at n10
  234. Ptr<MyApp> app5 = CreateObject<MyApp> ();
  235. app5->Setup (ns3UdpSocket5, sinkAddress5, packetSize, numPackets, DataRate ("1Mbps"));
  236. c.Get (10)->AddApplication (app5);
  237. app5->SetStartTime (Seconds (21.));
  238. app5->SetStopTime (Seconds (100.));
  239.  
  240. // Install FlowMonitor on all nodes
  241. FlowMonitorHelper flowmon;
  242. Ptr<FlowMonitor> monitor = flowmon.InstallAll();
  243.  
  244.  
  245. // Trace Collisions
  246. Config::ConnectWithoutContext("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Mac/MacTxDrop", MakeCallback(&MacTxDrop));
  247. Config::ConnectWithoutContext("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyRxDrop", MakeCallback(&PhyRxDrop));
  248. Config::ConnectWithoutContext("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyTxDrop", MakeCallback(&PhyTxDrop));
  249.  
  250. PrintDrop();
  251.  
  252. // Print per flow statistics
  253.  
  254. Gnuplot2dDataset dataset;
  255.  
  256. monitor->CheckForLostPackets ();
  257. Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());
  258. std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ();
  259.  
  260. //double localThrou=0;
  261.  
  262. for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator iter = stats.begin (); iter != stats.end (); ++iter)
  263. {
  264. Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (iter->first);
  265.  
  266. if ((t.sourceAddress == Ipv4Address("10.1.1.1") && t.destinationAddress == Ipv4Address("10.1.1.25"))
  267. || (t.sourceAddress == Ipv4Address("10.1.1.11") && t.destinationAddress == Ipv4Address("10.1.1.15"))
  268. || (t.sourceAddress == Ipv4Address("10.1.1.20") && t.destinationAddress == Ipv4Address("10.1.1.5"))
  269. || (t.sourceAddress == Ipv4Address("10.1.1.11") && t.destinationAddress == Ipv4Address("10.1.1.16"))
  270. || (t.sourceAddress == Ipv4Address("10.1.1.21") && t.destinationAddress == Ipv4Address("10.1.1.5")))
  271. {
  272. double localThrou = (iter->second.rxBytes * 8.0 / (iter->second.timeLastRxPacket.GetSeconds()-iter->second.timeFirstTxPacket.GetSeconds()) / 1024);
  273. dataset.Add((double)Simulator::Now().GetSeconds(),(double) localThrou);
  274. }
  275. }
  276.  
  277. //Gnuplot parameters
  278.  
  279. std::string fileNameWithNoExtension = "Zadanie_PLOT_";
  280. std::string graphicsFileName = fileNameWithNoExtension + ".png";
  281. std::string plotFileName = fileNameWithNoExtension + ".plt";
  282. std::string plotTitle = "FlowVSThroughput";
  283. std::string dataTitle = "Throughput";
  284.  
  285. // Instantiate the plot and set its title.
  286. Gnuplot gnuplot (graphicsFileName);
  287. gnuplot.SetTitle (plotTitle);
  288.  
  289. // Make the graphics file, which the plot file will be when it
  290. // is used with Gnuplot, be a PNG file.
  291. gnuplot.SetTerminal ("png");
  292.  
  293. // Set the labels for each axis.
  294. gnuplot.SetLegend ("Flow", "Throughput");
  295.  
  296.  
  297. dataset.SetTitle (dataTitle);
  298. dataset.SetStyle (Gnuplot2dDataset::LINES_POINTS);
  299.  
  300. //flowMonitor declaration
  301. FlowMonitorHelper fmHelper;
  302. Ptr<FlowMonitor> allMon = fmHelper.InstallAll();
  303. // call the flow monitor function
  304. ThroughputMonitor(&fmHelper, allMon, dataset);
  305.  
  306. Simulator::Schedule(Seconds(5.0), &PrintDrop);
  307.  
  308. AnimationInterface anim ("zadanie_anim"); // Mandatory
  309.  
  310. Simulator::Stop (Seconds (100.0));
  311. Simulator::Run ();
  312.  
  313. //Gnuplot
  314. gnuplot.AddDataset (dataset);
  315. // Open the plot file.
  316. std::ofstream plotFile (plotFileName.c_str());
  317. // Write the plot file.
  318. gnuplot.GenerateOutput (plotFile);
  319. // Close the plot file.
  320. plotFile.close ();
  321.  
  322. Simulator::Destroy ();
  323.  
  324. return 0;
  325. }
  326.  
  327. void ThroughputMonitor (FlowMonitorHelper *fmhelper, Ptr<FlowMonitor> flowMon,Gnuplot2dDataset DataSet)
  328. {
  329. double localThrou=0;
  330. std::map<FlowId, FlowMonitor::FlowStats> flowStats = flowMon->GetFlowStats();
  331. Ptr<Ipv4FlowClassifier> classing = DynamicCast<Ipv4FlowClassifier> (fmhelper->GetClassifier());
  332. for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator stats = flowStats.begin (); stats != flowStats.end (); ++stats)
  333. {
  334. Ipv4FlowClassifier::FiveTuple fiveTuple = classing->FindFlow (stats->first);
  335. std::cout<<"Flow ID : " << stats->first <<" ; "<< fiveTuple.sourceAddress <<" -----> "<<fiveTuple.destinationAddress<<std::endl;
  336. std::cout<<"Tx Packets = " << stats->second.txPackets<<std::endl;
  337. std::cout<<"Rx Packets = " << stats->second.rxPackets<<std::endl;
  338. std::cout<<"Duration : "<<(stats->second.timeLastRxPacket.GetSeconds()-stats->second.timeFirstTxPacket.GetSeconds())<<std::endl;
  339. std::cout<<"Last Received Packet : "<< stats->second.timeLastRxPacket.GetSeconds()<<" Seconds"<<std::endl;
  340. std::cout<<"Throughput: " << stats->second.rxBytes * 8.0 / (stats->second.timeLastRxPacket.GetSeconds()-stats->second.timeFirstTxPacket.GetSeconds())/1024/1024 << " Mbps"<<std::endl;
  341. localThrou=(stats->second.rxBytes * 8.0 / (stats->second.timeLastRxPacket.GetSeconds()-stats->second.timeFirstTxPacket.GetSeconds())/1024/1024);
  342. // updata gnuplot data
  343. DataSet.Add((double)Simulator::Now().GetSeconds(),(double) localThrou);
  344. std::cout<<"---------------------------------------------------------------------------"<<std::endl;
  345. }
  346. Simulator::Schedule(Seconds(1),&ThroughputMonitor, fmhelper, flowMon,DataSet);
  347. //if(flowToXml)
  348. {
  349. flowMon->SerializeToXmlFile ("ThroughputMonitor.xml", true, true);
  350. }
  351.  
  352. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement