Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <cassert>
  5. #include "ns3/core-module.h"
  6. #include "ns3/network-module.h"
  7. #include "ns3/internet-module.h"
  8. #include "ns3/point-to-point-module.h"
  9. #include "ns3/applications-module.h"
  10. #include "ns3/flow-monitor-module.h"
  11. #include "ns3/ipv4-static-routing-helper.h"
  12. #include "ns3/gnuplot.h"
  13. #include "ns3/stats-module.h"
  14. #include "ns3/netanim-module.h"
  15.  
  16.  
  17. using namespace ns3;
  18.  
  19. NS_LOG_COMPONENT_DEFINE ("Uloha1LOG");
  20.  
  21. int main (int argc, char *argv[]) // Deklarace funkce main
  22. {
  23.  
  24. bool verbose = true;
  25. std::string animFile = "uloha1.xml"; // Název souboru pro animaci
  26.  
  27. if (verbose)
  28. {
  29. LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO);
  30. LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO);
  31. }
  32.  
  33. NodeContainer n; // Vytvoření kontejneru uzlů
  34. n.Create (5);
  35. Ptr<Node> Node0 = n.Get (0); // Vytvoření ukazatele na uzel
  36. Ptr<Node> Node1 = n.Get (1);
  37. Ptr<Node> Node2 = n.Get (2);
  38. Ptr<Node> Node3 = n.Get (3);
  39. Ptr<Node> Node4 = n.Get (4);
  40.  
  41. PointToPointHelper pointToPoint1; // Vytvoření point-to-point linek
  42. pointToPoint1.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
  43. pointToPoint1.SetChannelAttribute ("Delay", StringValue ("3ms"));
  44.  
  45. NetDeviceContainer devices1;
  46. NetDeviceContainer devices2;
  47. NetDeviceContainer devices3;
  48. NetDeviceContainer devices4;
  49. NetDeviceContainer devices5;
  50. devices1 = pointToPoint1.Install (Node0, Node1); // Nainstalování zařízení do uzlů
  51. devices2 = pointToPoint1.Install (Node1, Node2);
  52. devices3 = pointToPoint1.Install (Node1, Node3);
  53. devices4 = pointToPoint1.Install (Node2, Node4);
  54. devices5 = pointToPoint1.Install (Node3, Node4);
  55.  
  56. InternetStackHelper stack;
  57. stack.Install (n); // Nainstalování protokolové sady na uzly z kontejneru
  58.  
  59. Ipv4AddressHelper address1;
  60. address1.SetBase ("192.168.1.0", "255.255.255.0");
  61. Ipv4AddressHelper address2;
  62. address2.SetBase ("192.168.2.0", "255.255.255.0");
  63. Ipv4AddressHelper address3;
  64. address3.SetBase ("192.168.3.0", "255.255.255.0");
  65. Ipv4AddressHelper address4;
  66. address4.SetBase ("192.168.4.0", "255.255.255.0");
  67. Ipv4AddressHelper address5;
  68. address5.SetBase ("192.168.5.0", "255.255.255.0");
  69.  
  70. Ipv4InterfaceContainer interface1 = address1.Assign (devices1); // Propojení IP adresy a rozhraní zařízení
  71. Ipv4InterfaceContainer interface2 = address2.Assign (devices2);
  72. Ipv4InterfaceContainer interface3 = address3.Assign (devices3);
  73. Ipv4InterfaceContainer interface4 = address4.Assign (devices4);
  74. Ipv4InterfaceContainer interface5 = address5.Assign (devices5);
  75.  
  76.  
  77.  
  78. //-------------------------------------Smerovani-------------------------------------------------------------------------
  79. Ptr<Ipv4> ipv4N0 = Node0->GetObject<Ipv4> ();
  80. Ptr<Ipv4> ipv4N1 = Node1->GetObject<Ipv4> ();
  81. Ptr<Ipv4> ipv4N2 = Node2->GetObject<Ipv4> ();
  82. Ptr<Ipv4> ipv4N3 = Node3->GetObject<Ipv4> ();
  83. Ptr<Ipv4> ipv4N4 = Node4->GetObject<Ipv4> ();
  84. Ipv4StaticRoutingHelper ipv4RoutingHelper;// Povolení statického směrování
  85.  
  86. Ptr<Ipv4StaticRouting> staticRoutingN0 =
  87. ipv4RoutingHelper.GetStaticRouting (ipv4N0);
  88. Ptr<Ipv4StaticRouting> staticRoutingN1 =
  89. ipv4RoutingHelper.GetStaticRouting (ipv4N1);
  90. Ptr<Ipv4StaticRouting> staticRoutingN2 =
  91. ipv4RoutingHelper.GetStaticRouting (ipv4N2);
  92. Ptr<Ipv4StaticRouting> staticRoutingN3 =
  93. ipv4RoutingHelper.GetStaticRouting (ipv4N3);
  94. Ptr<Ipv4StaticRouting> staticRoutingN4 =
  95. ipv4RoutingHelper.GetStaticRouting (ipv4N4);
  96.  
  97.  
  98.  
  99. staticRoutingN0->AddHostRouteTo (Ipv4Address ("192.168.4.2"),
  100. Ipv4Address ("192.168.1.2"), 1);// cesta z n0 do n4 přes n1
  101.  
  102. staticRoutingN0->AddHostRouteTo (Ipv4Address ("192.168.5.2"),
  103. Ipv4Address ("192.168.1.2"), 1);// cesta z n0 do n4 přes n1
  104.  
  105.  
  106. staticRoutingN1->AddHostRouteTo (Ipv4Address ("192.168.4.2"),
  107. Ipv4Address ("192.168.2.2"), 2, 5);// cesta z n1 do n4 přes n2
  108.  
  109. staticRoutingN1->AddHostRouteTo (Ipv4Address ("192.168.5.2"),
  110. Ipv4Address ("192.168.2.2"), 2, 5);// cesta z n1 do n4 přes n2
  111.  
  112. staticRoutingN2->AddHostRouteTo (Ipv4Address ("192.168.5.2"),
  113. Ipv4Address ("192.168.4.2"), 2);// cesta z n2 do n4 přes n4
  114.  
  115. staticRoutingN4->AddHostRouteTo (Ipv4Address ("192.168.1.1"),
  116. Ipv4Address ("192.168.4.1"), 1, 5);// cesta z n4 do n0 přes n2
  117.  
  118. staticRoutingN2->AddHostRouteTo (Ipv4Address ("192.168.1.1"),
  119. Ipv4Address ("192.168.2.1"), 1);// cesta z n2 do n0 přes n1
  120.  
  121.  
  122.  
  123.  
  124. staticRoutingN1->AddHostRouteTo (Ipv4Address ("192.168.4.2"),
  125. Ipv4Address ("192.168.3.2"), 3, 5);// cesta z n1 do n4 přes n3
  126.  
  127. staticRoutingN1->AddHostRouteTo (Ipv4Address ("192.168.5.2"),
  128. Ipv4Address ("192.168.3.2"), 3, 5);// cesta z n1 do n4 přes n3
  129.  
  130. staticRoutingN3->AddHostRouteTo (Ipv4Address ("192.168.4.2"),
  131. Ipv4Address ("192.168.5.2"), 2);// cesta z n3 do n4 přes n4
  132.  
  133. staticRoutingN4->AddHostRouteTo (Ipv4Address ("192.168.1.1"),
  134. Ipv4Address ("192.168.3.1"), 1, 3);// cesta z n4 do n0 přes n2
  135.  
  136.  
  137.  
  138.  
  139.  
  140. //-----------------------------------End Sm�rov�n�-----------------------------------------------------------------------
  141.  
  142.  
  143.  
  144. //-----------------------------------On/Off TCP------------------------------------------------------------------------
  145. // Vytvoření On/Off aplikace využívající TCP protokol
  146. OnOffHelper onoffTCP ("ns3::TcpSocketFactory", Address
  147. (InetSocketAddress (interface4.GetAddress (1), 80)));
  148. onoffTCP.SetAttribute ("DataRate", StringValue ("0.3Mbps"));
  149. onoffTCP.SetAttribute ("PacketSize", UintegerValue (512));
  150. onoffTCP.SetAttribute ("OnTime", StringValue
  151. ("ns3::ConstantRandomVariable[Constant=1]"));
  152. onoffTCP.SetAttribute ("OffTime", StringValue
  153. ("ns3::ConstantRandomVariable[Constant=0]"));
  154. ApplicationContainer appTCP = onoffTCP.Install (Node0);
  155. appTCP.Start (Seconds (1.0));
  156. appTCP.Stop (Seconds (10.0));
  157.  
  158.  
  159. // Vytvoření příjemce paketů
  160. PacketSinkHelper sinkTCP ("ns3::TcpSocketFactory", Address
  161. (InetSocketAddress (Ipv4Address::GetAny (), 80)));
  162. appTCP = sinkTCP.Install (Node4);
  163. appTCP.Start (Seconds (1.0));
  164. appTCP.Stop (Seconds (10.0));
  165. //---------------------------------End On/Off TCP----------------------------------------------------------------------
  166.  
  167.  
  168.  
  169. //-----------------------------------On/Off UDP------------------------------------------------------------------------
  170.  
  171. //---------------------------------End On/Off UDP----------------------------------------------------------------------
  172.  
  173.  
  174. //-------------------------------------Echo UDP------------------------------------------------------------------------
  175.  
  176. //-----------------------------------End Echo UDP----------------------------------------------------------------------
  177.  
  178.  
  179. //------------------------------------Gnuplot--------------------------------------------------------------------------
  180. GnuplotHelper plotHelper;
  181. plotHelper.ConfigurePlot ("gnuplotPackets","Velikost paketů na uzlu Node0 za"
  182. "čas aplikace On/Off protokolu TCP","Čas [s]","Velikost paketu [B]","png");
  183. plotHelper.PlotProbe ("ns3::Ipv4PacketProbe","/NodeList/0/$ns3::Ipv4L3Protocol/Tx",
  184. "OutputBytes","Pakety odeslané z uzlu Node0",GnuplotAggregator::KEY_BELOW);
  185. plotHelper.PlotProbe ("ns3::Ipv4PacketProbe","/NodeList/0/$ns3::Ipv4L3Protocol/Rx",
  186. "OutputBytes","Pakety přijaté na uzel Node0",GnuplotAggregator::KEY_BELOW);
  187. //----------------------------------End Gnuplot------------------------------------------------------------------------
  188.  
  189.  
  190.  
  191. //-------------------------------------Trasovac� soubory---------------------------------------------------------------------------
  192.  
  193. // Vytvoření trasovacích souborů
  194. AsciiTraceHelper ascii;
  195. pointToPoint1.EnableAsciiAll (ascii.CreateFileStream ("uloha1.tr"));
  196. pointToPoint1.EnablePcapAll ("uloha1");
  197.  
  198. // Výpis směrovací tabulky
  199. // Výpis směrovací tabulky do souboru
  200. Ipv4GlobalRoutingHelper table;
  201. Ptr<OutputStreamWrapper> routingStream = Create<OutputStreamWrapper>
  202. ("static-routing", std::ios::out);
  203. table.PrintRoutingTableAllAt (Seconds (10), routingStream);
  204.  
  205.  
  206. //-----------------------------------End Trace-------------------------------------------------------------------------
  207.  
  208.  
  209.  
  210. //---------------------------------Flow monitor------------------------------------------------------------------------
  211. FlowMonitorHelper flowHelper;
  212. Ptr<FlowMonitor> flowMonitor;
  213. flowMonitor = flowHelper.InstallAll();
  214. Simulator::Stop (Seconds (11));
  215. // Zastavení simulace
  216.  
  217. AnimationInterface anim (animFile);
  218. anim.SetConstantPosition (Node0, 0, 10);
  219. // Nastavení pozice uzlu
  220. anim.SetConstantPosition (Node1, 5, 10);
  221. anim.SetConstantPosition (Node2, 10, 5);
  222. anim.SetConstantPosition (Node3, 10, 15);
  223. anim.SetConstantPosition (Node4, 15, 10);
  224. anim.EnablePacketMetadata (true);
  225.  
  226.  
  227. Simulator::Run ();
  228.  
  229. flowMonitor->CheckForLostPackets ();
  230. Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier>
  231. (flowHelper.GetClassifier ());
  232. std::map<FlowId, FlowMonitor::FlowStats> stats = flowMonitor->GetFlowStats ();
  233. for(std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin ();
  234. i != stats.end (); ++i){Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first);
  235. NS_LOG_UNCOND("\n Flow ID: "<< i->first <<" Src Addr: "<<t.sourceAddress <<" Dst Addr: "<< t.destinationAddress);
  236. NS_LOG_UNCOND(" Tx Bytes: "<< i->second.txBytes);
  237. NS_LOG_UNCOND(" Rx Bytes: "<< i->second.rxBytes);
  238. NS_LOG_UNCOND(" Tx Packets: "<< i->second.txPackets);
  239. NS_LOG_UNCOND(" Rx Packets: "<< i->second.rxPackets);
  240. NS_LOG_UNCOND(" Mean Delay: "<< i->second.delaySum.GetSeconds() / (i->second.rxPackets) * 1000 <<" ms");
  241. NS_LOG_UNCOND(" Mean Jitter: "<< i->second.jitterSum.GetSeconds () / (i->second.rxPackets - 1) * 1000<<" ms");
  242. NS_LOG_UNCOND(" Throughput: "<< i->second.rxBytes * 8.0 / (i->second.timeLastRxPacket.GetSeconds () - i->second.timeFirstTxPacket.GetSeconds ()) / 1024 <<" Kbps");
  243. }
  244. flowMonitor->SerializeToXmlFile("uloha1Flowmon",true,true);
  245. //-------------------------------End Flow monitor----------------------------------------------------------------------
  246.  
  247.  
  248.  
  249.  
  250. std::cout << "\n Animation Trace file created: " << animFile.c_str ()<< std::endl; // Výpis o vytvoření souboru do terminálu
  251.  
  252. Simulator::Destroy ();
  253. return 0;
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement