Úlohy:
Vytvorte animáciu siete.
Vytvorte graf závislosti priepustnosti v sieti [Mbit/s] od vzdialenosti vysielačov od seba [m].
Zdroje: src/netanim/examples/dumbbell-animation.cc examples/wireless/wifi-hidden-terminal.cc examples/stats
Prvý príklad ako vytvoriť animáciu pomocou modulu NS3 net-anim je uvedený v prvom projekte z príkladov v zdrojových kódoch. Dumbbell je aplikácia ktorá používa UDP echo client-server aplikáciu. Na jednej strane je N klientov a na druhej strane je M serverov. Spojení sú jednou linkou (p2p).
Netanim je špeciálnou triedou, ktorá využíva volania jednotlivých udalosti (vysielanie, prijímanie, pohyb …), zachytí ich a zapíše informáciu o zmene do XML súbora. Tento súbor sa následne dá prehrať cez aplikáciu Netanim obsiahnutu v NS3.
Druhý príklad je použitie agregátora (špeciálnu triedu FlowMonitor) na získanie počtu (ne)úspešných prenosov. V tejto triede je uložená informácia o toku balíkov medzi 2 uzlami: počet prenesených balíkov, počet zahodených balíkov, suma oneskorení … Na stránke sú zverejnené aj ďalšie použité príklady (examples/wireless/multirate.cc …). Jedným z možných použití a vizualizácie je export do súboru a import v (vyššie spomenutej aplikácie) Netanim.
Tretím príkladom je vykreslenie grafu závislosti — štatistika. Využíva sa knižnica Gnuplot
1 2 |
cp src/netanim/examples/dumbbell-animation.cc scratch/bell.cc cp examples/wireless/wifi-hidden-terminal.cc scratch/wifi.cc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
#include "ns3/gnuplot.h" // prvotne nastavenia v hl.funkcii Gnuplot graf("graf.svg"); graf.SetTerminal("svg"); graf.SetTitle("Ahoj svet"); graf.SetLegend("Vzdialenost [m]","Priepustnost[Mbit/s]"); graf.AppendExtra("set xrange[20:100]"); Gnuplot2dDataset data; data.SetTitle ("strata udajov"); data.SetStyle (Gnuplot2dDataset::LINES_POINTS); //data.SetErrorBars(Gnuplot2dDataset::Y); //pridanie do datasetu data.Add(loss,throughput); //zaverecne spustenie graf.AddDataset (data); std::ofstream plotFile ("graf.plt"); graf.GenerateOutput (plotFile); plotFile.close (); //if(system("gnuplot graf.plt")); |
1 2 3 4 5 6 7 |
# skopirovanie celeho adresara, premenovanie na wifi-example-sim cp -R examples/stats scratch/ mv scratch/stats scratch/wifi-example-sim/ ./waf build ./waf --run "wifi-example-sim --help" cd scratch/wifi-example-sim/ ./wifi-example-db.sh |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
#include "ns3/core-module.h" #include "ns3/internet-module.h" #include "ns3/wifi-module.h" #include "ns3/propagation-module.h" #include "ns3/applications-module.h" #include "ns3/flow-monitor-module.h" #include "ns3/mobility-module.h" #include "ns3/netanim-module.h" #include "ns3/gnuplot.h" using namespace ns3; double experiment (bool enableCtsRts, std::string wifiManager, double width, bool vypis=true) { UintegerValue ctsThr = (enableCtsRts ? UintegerValue (100) : UintegerValue (2200)); Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", ctsThr); NodeContainer nodes; nodes.Create (4); MobilityHelper mob; Ptr<ListPositionAllocator> poz = CreateObject<ListPositionAllocator>(); for (uint8_t i = 0; i < 4; ++i){ poz->Add(Vector(0.0, width*i,0.0));} mob.SetPositionAllocator(poz); mob.Install(nodes); YansWifiChannelHelper kanal; kanal.SetPropagationDelay ("ns3::ConstantSpeedPropagationDelayModel"); kanal.AddPropagationLoss ("ns3::FriisPropagationLossModel"); WifiHelper wifi; wifi.SetStandard (WIFI_PHY_STANDARD_80211b); wifi.SetRemoteStationManager ("ns3::" + wifiManager + "WifiManager"); YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); wifiPhy.SetChannel (kanal.Create()); WifiMacHelper wifiMac; wifiMac.SetType ("ns3::AdhocWifiMac"); // use ad-hoc MAC NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes); InternetStackHelper internet; internet.Install (nodes); Ipv4AddressHelper ipv4; ipv4.SetBase ("10.0.0.0", "255.0.0.0"); ipv4.Assign (devices); ApplicationContainer cbrApps; uint16_t cbrPort = 12345; OnOffHelper onOffHelper ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address ("10.0.0.2"), cbrPort)); onOffHelper.SetAttribute ("PacketSize", UintegerValue (1400)); onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]")); onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]")); // flow 1: node 0 -> node 1 onOffHelper.SetAttribute ("DataRate", StringValue ("3000000bps")); onOffHelper.SetAttribute ("StartTime", TimeValue (Seconds (1.000000))); cbrApps.Add (onOffHelper.Install (nodes.Get (0))); // flow 2: node 2 -> node 1 /** \internal * The slightly different start times and data rates are a workaround * for \bugid{388} and \bugid{912} */ onOffHelper.SetAttribute ("DataRate", StringValue ("3001100bps")); onOffHelper.SetAttribute ("StartTime", TimeValue (Seconds (1.001))); cbrApps.Add (onOffHelper.Install (nodes.Get (2))); /** \internal * We also use separate UDP applications that will send a single * packet before the CBR flows start. * This is a workaround for the lack of perfect ARP, see \bugid{187} */ uint16_t echoPort = 9; UdpEchoClientHelper echoClientHelper (Ipv4Address ("10.0.0.2"), echoPort); echoClientHelper.SetAttribute ("MaxPackets", UintegerValue (1)); echoClientHelper.SetAttribute ("Interval", TimeValue (Seconds (0.1))); echoClientHelper.SetAttribute ("PacketSize", UintegerValue (10)); ApplicationContainer pingApps; // again using different start times to workaround Bug 388 and Bug 912 echoClientHelper.SetAttribute ("StartTime", TimeValue (Seconds (0.001))); pingApps.Add (echoClientHelper.Install (nodes.Get (0))); echoClientHelper.SetAttribute ("StartTime", TimeValue (Seconds (0.006))); pingApps.Add (echoClientHelper.Install (nodes.Get (2))); FlowMonitorHelper flowmon; Ptr<FlowMonitor> monitor = flowmon.InstallAll (); if(vypis){ AnimationInterface a("a.xml"); a.EnablePacketMetadata(); } Simulator::Stop (Seconds (10)); Simulator::Run (); monitor->CheckForLostPackets (); Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ()); FlowMonitor::FlowStatsContainer stats = monitor->GetFlowStats (); double meranie=0; for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i) { // first 2 FlowIds are for ECHO apps, we don't want to display them // // Duration for throughput measurement is 9.0 seconds, since // StartTime of the OnOffApplication is at about "second 1" // and // Simulator::Stops at "second 10". if (i->first > 2) { Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first); if(vypis){ std::cout << "Flow " << i->first - 2 << " (" << t.sourceAddress << " -> " << t.destinationAddress << ")\n"; std::cout << " Tx Packets: " << i->second.txPackets << "\n"; std::cout << " Tx Bytes: " << i->second.txBytes << "\n"; std::cout << " TxOffered: " << i->second.txBytes * 8.0 / 9.0 / 1000 / 1000 << " Mbps\n"; std::cout << " Rx Packets: " << i->second.rxPackets << "\n"; std::cout << " Rx Bytes: " << i->second.rxBytes << "\n"; std::cout << " Throughput: " << i->second.rxBytes * 8.0 / 9.0 / 1000 / 1000 << " Mbps\n";} meranie+=i->second.rxBytes * 8.0 / 9.0 / 1000 / 1000; } } Simulator::Destroy (); return meranie/2; } int main (int argc, char **argv) { double x=100; std::string wifiManager ("Arf"); CommandLine cmd; cmd.AddValue ("wifiManager", "Set wifi rate manager (Aarf, Aarfcd, Amrr, Arf, Cara, Ideal, Minstrel, Onoe, Rraa)", wifiManager); cmd.AddValue ("w", "Nastav vzdialenost medzi 2 uzlami[m]", x); cmd.Parse (argc, argv); // std::cout << "Hidden station experiment with RTS/CTS disabled:\n" << std::flush; experiment (false, wifiManager,x); // std::cout << "------------------------------------------------\n"; // std::cout << "Hidden station experiment with RTS/CTS enabled:\n"; experiment (true, wifiManager,x); Gnuplot graf("MojGraf.svg"); graf.SetTerminal("svg"); graf.SetTitle("priemerna priepustnost A2B"); graf.SetLegend("Vzdialenost anten (m)","Priepustnost (Mbit/s)"); Gnuplot2dDataset data; std::ostringstream str; str << "WifiManager " << wifiManager << " "; data.SetTitle (str.str() ); data.SetStyle (Gnuplot2dDataset::LINES_POINTS); for (double x = 10; x <= 150; x +=20){ data.Add(x,experiment(true, wifiManager, x,false));} graf.AddDataset (data); std::ofstream plotFile ("graf.plt"); graf.GenerateOutput (plotFile); plotFile.close (); if(system("gnuplot graf.plt")); return 0; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
#include "ns3/core-module.h" #include "ns3/point-to-point-module.h" #include "ns3/network-module.h" #include "ns3/applications-module.h" #include "ns3/mobility-module.h" #include "ns3/csma-module.h" #include "ns3/internet-module.h" #include "ns3/netanim-module.h" #include "ns3/wifi-module.h" #include "ns3/flow-monitor-module.h" using namespace ns3; NS_LOG_COMPONENT_DEFINE ("cvicenie 2"); int main (int argc, char *argv[]) { uint32_t mCsma = 3; uint32_t nWifi = 3; uint32_t dataSize = 1024; CommandLine cmd; cmd.AddValue ("m", "Pocet serverov", mCsma); cmd.AddValue ("n", "Pocet wiif stanic.", nWifi); cmd.AddValue ("dSize", "Velkost udajov.", dataSize); cmd.Parse (argc,argv); // L1 NodeContainer nW,nP,nA,nB,nS; nW.Create (nWifi); nP.Create(2);//AP+R1 Names::Add ("AP", nP.Get(1)); Names::Add ("R1", nP.Get(0)); nA.Add(nP.Get(0));//R1 nA.Create(2);//R2 + PCa Names::Add ("R2", nA.Get(1)); Names::Add ("PCa", nA.Get(2)); nB.Add(nA.Get(0));//R1+R2 nB.Add(nA.Get(1));//R1+R2 nB.Create(1);//PCb Names::Add ("PCb", nB.Get(2)); nS.Add(nA.Get(1));//R2 nS.Create(mCsma);//R2+servers Names::Add ("PCs", nS.Get(mCsma)); MobilityHelper mobility; mobility.SetPositionAllocator ("ns3::GridPositionAllocator", "MinX", DoubleValue (0.0), "MinY", DoubleValue (0.0), "DeltaX", DoubleValue (5.0), "DeltaY", DoubleValue (10.0), "GridWidth", UintegerValue (3), "LayoutType", StringValue ("RowFirst")); mobility.SetMobilityModel ("ns3::RandomWalk2dMobilityModel", "Bounds", RectangleValue (Rectangle (-50, 50, -50, 50))); mobility.Install (nW); mobility.SetMobilityModel ("ns3::ConstantPositionMobilityModel"); mobility.Install (nP.Get(1));//"AP" nutne //dalsie nie su nutne mobility.Install (nS); mobility.Install ("PCa"); mobility.Install ("PCb"); mobility.Install ("R1"); // L2 -- nastavenia NIC a kanalov PointToPointHelper p2p; p2p.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); p2p.SetChannelAttribute ("Delay", StringValue ("5ms")); NetDeviceContainer nicP; nicP = p2p.Install (nP); CsmaHelper csma; NetDeviceContainer nicA, nicB, nicS; csma.SetChannelAttribute ("DataRate", StringValue ("1000Mbps")); csma.SetChannelAttribute ("Delay", TimeValue (MicroSeconds (5))); nicA = csma.Install (nA); csma.SetChannelAttribute ("DataRate", StringValue ("1000Mbps")); csma.SetChannelAttribute ("Delay", TimeValue (MicroSeconds (300))); nicB = csma.Install (nB); csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps")); csma.SetChannelAttribute ("Delay", TimeValue (MicroSeconds (700))); nicS = csma.Install (nS); YansWifiChannelHelper wChannel = YansWifiChannelHelper::Default (); YansWifiPhyHelper phy = YansWifiPhyHelper::Default (); phy.SetChannel (wChannel.Create ()); WifiHelper wifi; wifi.SetRemoteStationManager ("ns3::AarfWifiManager"); WifiMacHelper mac; Ssid ssid = Ssid ("eduroam"); mac.SetType ("ns3::StaWifiMac", "Ssid", SsidValue (ssid), "ActiveProbing", BooleanValue (false)); NetDeviceContainer nicW; nicW = wifi.Install (phy, mac, nW); mac.SetType ("ns3::ApWifiMac", "Ssid", SsidValue (ssid)); NetDeviceContainer nicWap; nicWap = wifi.Install (phy, mac, nP.Get(1)); InternetStackHelper stack; stack.InstallAll(); Ipv4AddressHelper address; address.SetBase ("10.2.1.0", "255.255.255.0"); Ipv4InterfaceContainer networkSContainer = address.Assign (nicS); address.SetBase ("10.2.2.0", Ipv4Mask ("/24")); Ipv4InterfaceContainer networkAContainer = address.Assign (nicA); networkAContainer.SetMetric(1,3);//interface,metric networkAContainer.SetMetric(2,4); networkAContainer.SetMetric(0,5); address.SetBase ("10.2.3.0", Ipv4Mask (0xffffff00)); Ipv4InterfaceContainer networkBContainer = address.Assign (nicB); networkBContainer.SetMetric(0,1);//interface,metric networkBContainer.SetMetric(1,2); networkBContainer.SetMetric(2,3); address.SetBase ("10.2.4.0", Ipv4Mask ("/30")); address.Assign (nicP); address.SetBase ("10.1.0.0", Ipv4Mask("255.255.0.0")); // Ipv4InterfaceContainer networkWContainer = address.Assign (nicW); address.Assign (nicW); address.Assign (nicWap); Ipv4GlobalRoutingHelper::PopulateRoutingTables(); //L7 UdpEchoServerHelper echoServer (9); ApplicationContainer serverApps = echoServer.Install ( "PCs") ; serverApps.Start (Seconds (1.0)); serverApps.Stop (Seconds (11.0)); UdpEchoClientHelper echoClient (networkSContainer.GetAddress (mCsma), 9);//mCsma=nS.GetN()-1 echoClient.SetAttribute ("MaxPackets", UintegerValue (5)); echoClient.SetAttribute ("Interval", TimeValue (MilliSeconds (1000))); echoClient.SetAttribute ("PacketSize", UintegerValue (dataSize)); ApplicationContainer clientApps = echoClient.Install (nW.Get(0)); clientApps.Start (Seconds (2.0)); clientApps.Stop (Seconds (11.0)); Simulator::Stop (Seconds (11.0)); AnimationInterface anim ("wireless-animation.xml"); auto bezec = anim.AddResource("/home/student/Documents/share/antenna.png"); anim.UpdateNodeDescription (nP.Get(1), "AP"); anim.UpdateNodeImage(nP.Get(1)->GetId(),bezec); anim.UpdateNodeSize(nP.Get(1)->GetId(),2,2); bezec = anim.AddResource("/home/student/Documents/share/runner.png"); for (uint32_t i = 0; i < nW.GetN (); ++i){ anim.UpdateNodeDescription (nW.Get(i), "bez"+std::to_string(i)); anim.UpdateNodeImage(nW.Get(i)->GetId(),bezec); anim.UpdateNodeSize(nW.Get(i)->GetId(),2,2); } bezec = anim.AddResource("/home/student/Documents/share/server.png"); for (uint32_t i = 1; i < nS.GetN (); ++i){ anim.UpdateNodeDescription (nS.Get(i), "Ser"+std::to_string(i)); anim.UpdateNodeImage(nS.Get(i)->GetId(),bezec); anim.UpdateNodeSize(nS.Get(i)->GetId(),2,2); } bezec = anim.AddResource("/home/student/Documents/share/router.png"); anim.UpdateNodeDescription (nS.Get(0), "R2"); anim.UpdateNodeImage(nS.Get(0)->GetId(),bezec); anim.UpdateNodeSize(nS.Get(0)->GetId(),2,2); anim.UpdateNodeDescription (nP.Get(0), "R1"); anim.UpdateNodeImage(nP.Get(0)->GetId(),bezec); anim.UpdateNodeSize(nP.Get(0)->GetId(),2,2); anim.UpdateNodeColor(nA.Get(2),0,200,0); anim.UpdateNodeColor(nB.Get(2),0,0,200); FlowMonitorHelper flowmon; Ptr<FlowMonitor> monitor = flowmon.InstallAll (); Simulator::Run (); monitor->CheckForLostPackets (); Ptr<Ipv4FlowClassifier> classifier = DynamicCast<Ipv4FlowClassifier> (flowmon.GetClassifier ()); FlowMonitor::FlowStatsContainer stats = monitor->GetFlowStats (); for (std::map<FlowId, FlowMonitor::FlowStats>::const_iterator i = stats.begin (); i != stats.end (); ++i) { Ipv4FlowClassifier::FiveTuple t = classifier->FindFlow (i->first); std::cout << "Flow " << i->first - 2 << " (" << t.sourceAddress << " -> " << t.destinationAddress << ")\n"; std::cout << " Tx Packets: " << i->second.txPackets << "\n"; std::cout << " Tx Bytes: " << i->second.txBytes << "\n"; std::cout << " Rx Packets: " << i->second.rxPackets << "\n"; std::cout << " Rx Bytes: " << i->second.rxBytes << "\n"; std::cout << " DelaySum: " << i->second.delaySum.GetMilliSeconds() << " ms\n"; } Simulator::Destroy (); return 0; } |