Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Zistite simuláciou najvhodnejší transportný protokol pre sieť (rôzne druhy TCP a UDP a pod.):
- Topológiu:
- Uzly v sieti s počtom (XXX) sú rovnomerne rozmiestnené na ploche 1000*1000 [m].
- -- komunikácia je cez protokol: 802.11e; OLSR;
- -- ich model pohybu je: náhodný pohyb po ceste s náhodne meniacou sa rýchlosťou (3b)
- - fyzický model šírenia signálu nastavte:
- -- straty sú závisle podla Jakes modelu
- -- ConstantSpeed oneskorenia signálu
- - aplikačnú vrstvu naprogramujte tak, aby dva uzly posielali 10MB bajtov po max. (YYY):
- -- náhodne vybrané uzoly N1 a N2 ( N1 != N2 ) začali komunikovať v čase, 10s.
- -- náhodne vybrané uzoly N4 a N5 ( N4 != N5 ) začali komunikovať v čase, 12s.
- -- náhodne vybrané uzoly N6 a N7 ( N6 != N7 ) začali komunikovať v čase, 15s.
- -- náhodne vybrané uzoly N8 a N9 ( N8 != N9 ) začali komunikovať v čase, 16s.
- -- náhodne vybrané uzoly N10 a N11 ( N10 != N11 ) začali komunikovať v čase, 21s.
- - dopíšte do programu kód pre grafickú vizualizáciu siete pre program "NetAnim" (3b)
- - dopíšte do programu kód pre vykreslenie grafu závislosti (využite knižnicu "gnuplot")
- -- parametra straty údajov/balíka od počtu uzlov (XXX). (4b) spolu so štandardnou odchylkou (2b)
- -- parametra straty údajov/balíka od veľkosti balíka (YYY). (4b) spolu so štandardnou odchylkou.
- -- parametra priepustnosť od veľkosti balíka (YYY). (4b) spolu so štandardnou odchylkou. (2b).
- Premenné (XXX) a (YYY) vhodne zvoľte na vynesenie do grafu. Simuláciu ukončite tiež vhodným časom.
- Spolu 3+3+4+2+4+4+2= 22Bodov*/
- #include "ns3/core-module.h"
- #include "ns3/network-module.h"
- #include "ns3/mobility-module.h"
- #include "ns3/config-store-module.h"
- #include "ns3/wifi-module.h"
- #include "ns3/internet-module.h"
- #include "ns3/olsr-helper.h"
- #include "ns3/flow-monitor-module.h"
- #include "myapp.h"
- #include "ns3/netanim-module.h"
- #include "ns3/gnuplot.h"
- #include "ns3/flow-monitor-module.h"
- #include <ns3/flow-monitor-helper.h>
- #include <iostream>
- #include <fstream>
- #include <vector>
- #include <string>
- NS_LOG_COMPONENT_DEFINE ("Zadanie_PS2");
- using namespace ns3;
- void ThroughputMonitor (FlowMonitorHelper *fmhelper, Ptr<FlowMonitor> flowMon,Gnuplot2dDataset DataSet);
- uint32_t MacTxDropCount, PhyTxDropCount, PhyRxDropCount;
- void MacTxDrop(Ptr<const Packet> p) //Trace zdroj indikuje paket, ktoré bolo zrušené prístroja pred prenosom
- {
- NS_LOG_INFO("Packet Drop");
- MacTxDropCount++;
- }
- void PrintDrop()
- {
- std::cout << Simulator::Now().GetSeconds() << "\t" << MacTxDropCount << "\t"<< PhyTxDropCount << "\t" << PhyRxDropCount << "\n";
- Simulator::Schedule(Seconds(5.0), &PrintDrop);
- }
- void PhyTxDrop(Ptr<const Packet> p) //Trace zdroj indikuje paket bola vypustená zariadením počas prenosu
- {
- NS_LOG_INFO("Packet Drop");
- PhyTxDropCount++;
- }
- void PhyRxDrop(Ptr<const Packet> p) //Trace zdroj indikuje paket, ktoré bolo zrušené zariadenie počas prijímania
- {
- NS_LOG_INFO("Packet Drop");
- PhyRxDropCount++;
- }
- int main (int argc, char *argv[])
- {
- std::string phyMode ("DsssRate1Mbps");
- double distance = 250; //
- uint32_t numNodes = 25; // 5x5
- double interval = 0.001; // seconds
- uint32_t packetSize = 600; // bytes
- uint32_t numPackets = 10000000;
- std::string rtslimit = "1500";
- CommandLine cmd;
- cmd.AddValue ("phyMode", "Wifi Phy mode", phyMode); // Mode PHY krytie, ktoré sú prítomné v oblasti skenovania 802.11 protokolu
- cmd.AddValue ("distance", "distance (m)", distance);
- cmd.AddValue ("packetSize", "distance (m)", packetSize);
- 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
- cmd.Parse (argc, argv);
- // Convert to time object
- Time interPacketInterval = Seconds (interval);
- // turn off RTS/CTS for frames below 2200 bytes
- Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue (rtslimit));
- // Fix non-unicast data rate to be the same as that of unicast
- Config::SetDefault ("ns3::WifiRemoteStationManager::NonUnicastMode", StringValue (phyMode));
- NodeContainer c;
- c.Create (numNodes);
- // The below set of helpers will help us to put together the wifi NICs we want
- WifiHelper wifi;
- YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default ();
- // set it to zero; otherwise, gain will be added
- wifiPhy.Set ("RxGain", DoubleValue (-10) );
- // ns-3 supports RadioTap and Prism tracing extensions for 802.11b
- wifiPhy.SetPcapDataLinkType (YansWifiPhyHelper::DLT_IEEE802_11_RADIO);
- YansWifiChannelHelper wifiChannel; // Vytvorenie pomocníka kanála bez akejkoľvek sady parametrov
- wifiChannel.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel"); // oneskorenie
- wifiChannel.AddPropagationLoss ("ns3::JakesPropagationLossModel"); // Loss - Jakes model
- wifiPhy.SetChannel (wifiChannel.Create ());
- // Add a non-QoS upper mac, and disable rate control
- NqosWifiMacHelper wifiMac = NqosWifiMacHelper::Default ();
- wifi.SetStandard (WIFI_PHY_STANDARD_80211b);
- wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager",
- "DataMode",StringValue (phyMode),
- "ControlMode",StringValue (phyMode));
- // Set it to adhoc mode
- wifiMac.SetType ("ns3::AdhocWifiMac");
- NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, c);
- MobilityHelper mobility; // Helper trieda slúži na priradenie pozície a modelov mobility uzly
- mobility.SetPositionAllocator ("ns3::GridPositionAllocator",
- "MinX", DoubleValue (0.0),
- "MinY", DoubleValue (0.0),
- "DeltaX", DoubleValue (distance),
- "DeltaY", DoubleValue (distance),
- "GridWidth", UintegerValue (5),
- "LayoutType", StringValue ("RowFirst"));
- mobility.SetMobilityModel ("ns3::RandomDirection2dMobilityModel", // nastaví MobilityModel podtriedy
- "Bounds", StringValue("0|1000|0|1000"), //hranica
- "Speed", StringValue("ns3::UniformRandomVariable[Min=1.0|Max=30.0]"),
- "Pause", StringValue("ns3::ConstantRandomVariable[Constant=2.0]"));
- mobility.Install (c);
- // Enable OLSR
- OlsrHelper olsr;
- 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.
- list.Add (olsr, 10);
- InternetStackHelper internet;
- internet.SetRoutingHelper (list); // has effect on the next Install ()
- internet.Install (c);
- Ipv4AddressHelper ipv4;
- NS_LOG_INFO ("Assign IP Addresses.");
- ipv4.SetBase ("10.1.1.0", "255.255.255.0");
- Ipv4InterfaceContainer ifcont = ipv4.Assign (devices);
- // Create Apps
- uint16_t sinkPort = 10; // use the same for all apps
- // UDP connection from N0 to N24
- Address sinkAddress1 (InetSocketAddress (ifcont.GetAddress (24), sinkPort)); // interface of n24
- PacketSinkHelper packetSinkHelper1 ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
- ApplicationContainer sinkApps1 = packetSinkHelper1.Install (c.Get (24)); //n2 as sink
- sinkApps1.Start (Seconds (0.));
- sinkApps1.Stop (Seconds (100.));
- Ptr<Socket> ns3UdpSocket1 = Socket::CreateSocket (c.Get (0), UdpSocketFactory::GetTypeId ()); //source at n0
- // Create UDP application at n0
- Ptr<MyApp> app1 = CreateObject<MyApp> ();
- app1->Setup (ns3UdpSocket1, sinkAddress1, packetSize, numPackets, DataRate ("1Mbps"));
- c.Get (0)->AddApplication (app1);
- app1->SetStartTime (Seconds (10.));
- app1->SetStopTime (Seconds (100.));
- // UDP connection from N10 to N14
- Address sinkAddress2 (InetSocketAddress (ifcont.GetAddress (14), sinkPort)); // interface of n14
- PacketSinkHelper packetSinkHelper2 ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
- ApplicationContainer sinkApps2 = packetSinkHelper2.Install (c.Get (14)); //n14 as sink
- sinkApps2.Start (Seconds (0.));
- sinkApps2.Stop (Seconds (100.));
- Ptr<Socket> ns3UdpSocket2 = Socket::CreateSocket (c.Get (10), UdpSocketFactory::GetTypeId ()); //source at n10
- // Create UDP application at n10
- Ptr<MyApp> app2 = CreateObject<MyApp> ();
- app2->Setup (ns3UdpSocket2, sinkAddress2, packetSize, numPackets, DataRate ("1Mbps"));
- c.Get (10)->AddApplication (app2);
- app2->SetStartTime (Seconds (12.));
- app2->SetStopTime (Seconds (100.));
- // UDP connection from N20 to N4
- Address sinkAddress3 (InetSocketAddress (ifcont.GetAddress (4), sinkPort)); // interface of n4
- PacketSinkHelper packetSinkHelper3 ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
- ApplicationContainer sinkApps3 = packetSinkHelper3.Install (c.Get (4)); //n2 as sink
- sinkApps3.Start (Seconds (0.));
- sinkApps3.Stop (Seconds (100.));
- Ptr<Socket> ns3UdpSocket3 = Socket::CreateSocket (c.Get (20), UdpSocketFactory::GetTypeId ()); //source at n20
- // Create UDP application at n20
- Ptr<MyApp> app3 = CreateObject<MyApp> ();
- app3->Setup (ns3UdpSocket3, sinkAddress3, packetSize, numPackets, DataRate ("1Mbps"));
- c.Get (20)->AddApplication (app3);
- app3->SetStartTime (Seconds (15.));
- app3->SetStopTime (Seconds (100.));
- // UDP connection from N19 to N4
- Address sinkAddress4 (InetSocketAddress (ifcont.GetAddress (4), sinkPort)); // interface of n4
- PacketSinkHelper packetSinkHelper4 ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
- ApplicationContainer sinkApps4 = packetSinkHelper4.Install (c.Get (4)); //n2 as sink
- sinkApps4.Start (Seconds (0.));
- sinkApps4.Stop (Seconds (100.));
- Ptr<Socket> ns3UdpSocket4 = Socket::CreateSocket (c.Get (19), UdpSocketFactory::GetTypeId ()); //source at n19
- // Create UDP application at n19
- Ptr<MyApp> app4 = CreateObject<MyApp> ();
- app4->Setup (ns3UdpSocket4, sinkAddress4, packetSize, numPackets, DataRate ("1Mbps"));
- c.Get (19)->AddApplication (app4);
- app4->SetStartTime (Seconds (16.));
- app4->SetStopTime (Seconds (100.));
- // UDP connection from N10 to N15
- Address sinkAddress5 (InetSocketAddress (ifcont.GetAddress (15), sinkPort)); // interface of n15
- PacketSinkHelper packetSinkHelper5 ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
- ApplicationContainer sinkApps5 = packetSinkHelper5.Install (c.Get (15)); //n15 as sink
- sinkApps5.Start (Seconds (0.));
- sinkApps5.Stop (Seconds (100.));
- Ptr<Socket> ns3UdpSocket5 = Socket::CreateSocket (c.Get (10), UdpSocketFactory::GetTypeId ()); //source at n10
- // Create UDP application at n10
- Ptr<MyApp> app5 = CreateObject<MyApp> ();
- app5->Setup (ns3UdpSocket5, sinkAddress5, packetSize, numPackets, DataRate ("1Mbps"));
- c.Get (10)->AddApplication (app5);
- app5->SetStartTime (Seconds (21.));
- app5->SetStopTime (Seconds (100.));
- // Install FlowMonitor on all nodes
- FlowMonitorHelper flowmon;
- Ptr<FlowMonitor> monitor = flowmon.InstallAll();
- // Trace Collisions
- Config::ConnectWithoutContext("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Mac/MacTxDrop", MakeCallback(&MacTxDrop));
- Config::ConnectWithoutContext("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyRxDrop", MakeCallback(&PhyRxDrop));
- Config::ConnectWithoutContext("/NodeList/*/DeviceList/*/$ns3::WifiNetDevice/Phy/PhyTxDrop", MakeCallback(&PhyTxDrop));
- PrintDrop();
- // Print per flow statistics
- Gnuplot2dDataset dataset;
- monitor->CheckForLostPackets ();
- Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ());
- std::map<FlowId, FlowMonitor::FlowStats> stats = monitor->GetFlowStats ();
- //double localThrou=0;
- for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator iter = stats.begin (); iter != stats.end (); ++iter)
- {
- Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (iter->first);
- if ((t.sourceAddress == Ipv4Address("10.1.1.1") && t.destinationAddress == Ipv4Address("10.1.1.25"))
- || (t.sourceAddress == Ipv4Address("10.1.1.11") && t.destinationAddress == Ipv4Address("10.1.1.15"))
- || (t.sourceAddress == Ipv4Address("10.1.1.20") && t.destinationAddress == Ipv4Address("10.1.1.5"))
- || (t.sourceAddress == Ipv4Address("10.1.1.11") && t.destinationAddress == Ipv4Address("10.1.1.16"))
- || (t.sourceAddress == Ipv4Address("10.1.1.21") && t.destinationAddress == Ipv4Address("10.1.1.5")))
- {
- double localThrou = (iter->second.rxBytes * 8.0 / (iter->second.timeLastRxPacket.GetSeconds()-iter->second.timeFirstTxPacket.GetSeconds()) / 1024);
- dataset.Add((double)Simulator::Now().GetSeconds(),(double) localThrou);
- }
- }
- //Gnuplot parameters
- std::string fileNameWithNoExtension = "Zadanie_PLOT_";
- std::string graphicsFileName = fileNameWithNoExtension + ".png";
- std::string plotFileName = fileNameWithNoExtension + ".plt";
- std::string plotTitle = "FlowVSThroughput";
- std::string dataTitle = "Throughput";
- // Instantiate the plot and set its title.
- Gnuplot gnuplot (graphicsFileName);
- gnuplot.SetTitle (plotTitle);
- // Make the graphics file, which the plot file will be when it
- // is used with Gnuplot, be a PNG file.
- gnuplot.SetTerminal ("png");
- // Set the labels for each axis.
- gnuplot.SetLegend ("Flow", "Throughput");
- dataset.SetTitle (dataTitle);
- dataset.SetStyle (Gnuplot2dDataset::LINES_POINTS);
- //flowMonitor declaration
- FlowMonitorHelper fmHelper;
- Ptr<FlowMonitor> allMon = fmHelper.InstallAll();
- // call the flow monitor function
- ThroughputMonitor(&fmHelper, allMon, dataset);
- Simulator::Schedule(Seconds(5.0), &PrintDrop);
- AnimationInterface anim ("zadanie_anim"); // Mandatory
- Simulator::Stop (Seconds (100.0));
- Simulator::Run ();
- //Gnuplot
- gnuplot.AddDataset (dataset);
- // Open the plot file.
- std::ofstream plotFile (plotFileName.c_str());
- // Write the plot file.
- gnuplot.GenerateOutput (plotFile);
- // Close the plot file.
- plotFile.close ();
- Simulator::Destroy ();
- return 0;
- }
- void ThroughputMonitor (FlowMonitorHelper *fmhelper, Ptr<FlowMonitor> flowMon,Gnuplot2dDataset DataSet)
- {
- double localThrou=0;
- std::map<FlowId, FlowMonitor::FlowStats> flowStats = flowMon->GetFlowStats();
- Ptr<Ipv4FlowClassifier> classing = DynamicCast<Ipv4FlowClassifier> (fmhelper->GetClassifier());
- for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator stats = flowStats.begin (); stats != flowStats.end (); ++stats)
- {
- Ipv4FlowClassifier::FiveTuple fiveTuple = classing->FindFlow (stats->first);
- std::cout<<"Flow ID : " << stats->first <<" ; "<< fiveTuple.sourceAddress <<" -----> "<<fiveTuple.destinationAddress<<std::endl;
- std::cout<<"Tx Packets = " << stats->second.txPackets<<std::endl;
- std::cout<<"Rx Packets = " << stats->second.rxPackets<<std::endl;
- std::cout<<"Duration : "<<(stats->second.timeLastRxPacket.GetSeconds()-stats->second.timeFirstTxPacket.GetSeconds())<<std::endl;
- std::cout<<"Last Received Packet : "<< stats->second.timeLastRxPacket.GetSeconds()<<" Seconds"<<std::endl;
- std::cout<<"Throughput: " << stats->second.rxBytes * 8.0 / (stats->second.timeLastRxPacket.GetSeconds()-stats->second.timeFirstTxPacket.GetSeconds())/1024/1024 << " Mbps"<<std::endl;
- localThrou=(stats->second.rxBytes * 8.0 / (stats->second.timeLastRxPacket.GetSeconds()-stats->second.timeFirstTxPacket.GetSeconds())/1024/1024);
- // updata gnuplot data
- DataSet.Add((double)Simulator::Now().GetSeconds(),(double) localThrou);
- std::cout<<"---------------------------------------------------------------------------"<<std::endl;
- }
- Simulator::Schedule(Seconds(1),&ThroughputMonitor, fmhelper, flowMon,DataSet);
- //if(flowToXml)
- {
- flowMon->SerializeToXmlFile ("ThroughputMonitor.xml", true, true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement