#include "string.h" #include "ns3/core-module.h" #include "ns3/propagation-module.h" #include "ns3/network-module.h" #include "ns3/applications-module.h" #include "ns3/mobility-module.h" #include "ns3/wifi-module.h" #include "ns3/internet-module.h" #include "ns3/flow-monitor-module.h" using namespace ns3; uint32_t macTxDropCount(0), phyTxDropCount(0), phyRxDropCount(0); void MacTxDrop(Ptr p) { macTxDropCount++; } void PhyTxDrop(Ptr p) { phyTxDropCount++; } void PhyRxDrop(Ptr p) { phyRxDropCount++; } void ResetDropCounters() { macTxDropCount = 0; phyTxDropCount = 0; phyRxDropCount = 0; } void principal(size_t &tamanho) { // número de nós size_t num_nos = 3; // 1. Create 2 nodes NodeContainer nodes; nodes.Create (num_nos); // 2. Place nodes for (size_t i = 0; i < num_nos; ++i) { // Pega a característica do Nó e posiciona constante nodes.Get(i)-> AggregateObject (CreateObject ()); } // 3. Create propagation loss matrix Ptr lossModel = CreateObject (); lossModel->SetDefaultLoss (200); // set default loss to 200 dB (no link) for (size_t i = 0; i < num_nos-1; i++) { // set symmetric loss i <-> i+1 to 50 dB lossModel->SetLoss (nodes.Get (i)-> GetObject(), nodes.Get (i+1)->GetObject(), 50); } // 4. Criar e configurar o canal wifi Ptr wifiChannel = CreateObject (); wifiChannel->SetPropagationLossModel (lossModel); wifiChannel->SetPropagationDelayModel (CreateObject ()); // 5. Isntalar os dispositivos wirelles WifiHelper wifi; wifi.SetStandard (WIFI_PHY_STANDARD_80211b); //Config::SetDefault ("ns3::WifiRemoteStationManager::RtsCtsThreshold", StringValue ("2200")); wifi.SetRemoteStationManager ("ns3::ConstantRateWifiManager", "DataMode",StringValue ("DsssRate11Mbps")); YansWifiPhyHelper wifiPhy = YansWifiPhyHelper::Default (); wifiPhy.SetChannel (wifiChannel); WifiMacHelper wifiMac; wifiMac.SetType ("ns3::AdhocWifiMac"); NetDeviceContainer devices = wifi.Install (wifiPhy, wifiMac, nodes); //// Enable pcap? wifiPhy.SetPcapDataLinkType (YansWifiPhyHelper::DLT_IEEE802_11_RADIO); wifiPhy.EnablePcap ("exposed-terminal", nodes); // 6. Install TCP/IP stack & assign IP addresses InternetStackHelper internet; internet.Install (nodes); Ipv4AddressHelper ipv4; ipv4.SetBase ("10.0.0.0", "255.0.0.0"); ipv4.Assign (devices); // 7.1 Install applications: two CBR streams ApplicationContainer cbrApps; OnOffHelper onOffHelper ("ns3::UdpSocketFactory", Address()); onOffHelper.SetAttribute ("PacketSize", UintegerValue (tamanho)); onOffHelper.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1.0]")); onOffHelper.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0.0]")); onOffHelper.SetAttribute ("DataRate", StringValue ("2Mbps")); //// flow 1: node 1 -> node 0 onOffHelper.SetAttribute ("StartTime", TimeValue (Seconds (1.0))); onOffHelper.SetAttribute ("Remote", AddressValue(InetSocketAddress (Ipv4Address ("10.0.0.1"),9000))); cbrApps.Add (onOffHelper.Install (nodes.Get (1))); // 7.2 Install applications: two UDP sinks ApplicationContainer sinkApps; PacketSinkHelper packetSinkHelper ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), 9000)); sinkApps.Add (packetSinkHelper.Install (nodes.Get (0))); sinkApps.Add (packetSinkHelper.Install (nodes.Get (1))); // 8.1 Install FlowMonitor on all nodes FlowMonitorHelper flowmon; Ptr monitor = flowmon.InstallAll (); // 8.2 Monitor 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)); // 9. Run simulation for 150 seconds Simulator::Stop (Seconds (150)); Simulator::Run (); // 10. Print statistics monitor->CheckForLostPackets (); Ptr classifier = DynamicCast (flowmon.GetClassifier ()); FlowMonitor::FlowStatsContainer stats = monitor->GetFlowStats (); for (std::map::const_iterator i = stats.begin (); i != stats.end (); ++i) { std::cout << i->second.txPackets << ";" << i->second.rxPackets << ";"; std::cout << i->second.txBytes << ";" << i->second.rxBytes << ";"; } std::cout << (macTxDropCount + phyTxDropCount + phyRxDropCount) << "\n"; // Collisions should be in phyRxDropCount, as Yans wifi set collided frames snr on reception, but it's not possible to differentiate from propagation loss. In this experiment, this is not an issue. //// Export flowmon data? ////monitor->SerializeToXmlFile("exposed-terminal.flowmon", true, true); // 11. Cleanup Simulator::Destroy (); ResetDropCounters(); } int main (int argc, char *argv[]) { // Defaults size_t vezes = 10; size_t tamanho = 100; // Parse command line CommandLine cmd; cmd.AddValue("tamanho", "Tamanho do pacote. (100 Bytes)", tamanho); cmd.AddValue("vezes", "Número de execuções. (10 vezes)", vezes); cmd.Parse (argc, argv); // Run experiment std::cout << "F1 Tx Packets;F1 Rx Packets;F1 Tx Bytes;F1 Rx Bytes;Collisions\n"; for (size_t i = 0; i < vezes; i++) { principal(tamanho); } return 0; }