/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ /* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation; * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ #include "ns3/core-module.h" #include "ns3/point-to-point-module.h" #include "ns3/network-module.h" #include "ns3/applications-module.h" #include "ns3/wifi-module.h" #include "ns3/mobility-module.h" #include "ns3/csma-module.h" #include "ns3/internet-module.h" #include "ns3/olsr-helper.h" #include "ns3/ipv4-static-routing-helper.h" #include "ns3/ipv4-list-routing-helper.h" #include "ns3/config-store-module.h" #include "ns3/packet-sink-helper.h" #include "ns3/onoff-application.h" #include "ns3/flow-monitor-module.h" // Default Network Topology // // Wifi 10.1.3.0 // AP // * * * * // | | | | 10.1.1.0 // n5 n6 n7 n0 -------------- n1 n2 n3 n4 // point-to-point | | | | // ================ // LAN 10.1.2.0 using namespace ns3; using namespace std; #include #include #include #include #include #include #include #include #include #include "ns3/gnuplot.h" NS_LOG_COMPONENT_DEFINE ("ThirdScriptExample"); /* for the netanim void CourseChange (string context, Ptr model) { Vector position = model->GetPosition (); NS_LOG_UNCOND (context << " x = " << position.x << ", y = " << position.y); } */ /// selfish node specification , the min and max cw will be change // modify the CW min void SetCwMin(uint16_t cw) { Config::Set("/NodeList/0/DeviceList/0/$ns3::WifiNetDevice/Mac/$ns3::RegularWifiMac/DcaTxop/CwMin", UintegerValue(cw)); } /* // modify the CW max void SetCwMax(uint16_t cw) { Config::Set("/NodeList/0/DeviceList/0/$ns3::WifiNetDevice/Mac/$ns3::RegularWifiMac/DcaTxop/CwMax", UintegerValue(cw)); } */ uint32_t totalBytesReceived(0), mbs (0); //uint32_t mbs (0); ofstream rdTrace; int const TN= 4; //vector totalBytesReceived(TN); //vector mbs(TN); //-- Callback function is called whenever a packet is received successfully. //-- This function cumulatively add the size of data packet to totalBytesReceived counter. //--------------------------------------------------------------------------------------- void ReceivePacket(string context, Ptr p) { //char c= context.at(10); //int index= c - '0'; //totalBytesReceived[index] += p->GetSize(); totalBytesReceived += p->GetSize(); //cout<< "this is the context : " << context.at(10)<< endl; //cout<< "this is the context : " << totalBytesReceived[0] << endl; } void CalculateThroughput() { //for (int f=0; f recvSink = Socket::CreateSocket (wifiStaNodes.Get(sinkNode), tid);// InetSocketAddress local = InetSocketAddress (Ipv4Address::GetAny (), 80); recvSink->Bind (local); recvSink->SetRecvCallback (MakeCallback (&ReceivePacket)); Ptr source = Socket::CreateSocket (wifiStaNodes.Get(sourceNode), tid); InetSocketAddress remote = InetSocketAddress (interface.GetAddress(sinkNode, 0), 80); source->Connect (remote); if (tracing == true) { AsciiTraceHelper ascii; phy.EnableAsciiAll (ascii.CreateFileStream ("wifisimple-adhoc-grid.tr")); phy.EnablePcap ("wifi-simple-adhoc-grid",staDevices); // Trace routing tables Ptr routingStream = Create ("wifi-simple-adhocgrid.routes",std::ios::out); olsr.PrintRoutingTableAllEvery (Seconds (2),routingStream); } //UdpEchoServerHelper echoServer (9); */ // create application (the on off applicatiopn ) uint16_t port = 9; /* OnOffHelper onoff("ns3::UdpSocketFactory",InetSocketAddress("10.1.1.4", port)); //OnOffHelper onoffHelper ("ns3::UdpSocketFactory", InetSocketAddress (interface.Get(staDevices))); onoff.SetAttribute ("OnTime", StringValue("ns3::ConstantRandomVariable[Constant=1]")); onoff.SetAttribute ("OffTime", StringValue("ns3::ConstantRandomVariable[Constant=0]")); onoff.SetAttribute("DataRate", StringValue("512Kbps")); onoff.SetAttribute("PacketSize", StringValue("512")); ApplicationContainer apps = onoff.Install(wifiStaNodes); apps.Start(Seconds(5.0)); apps.Stop(Seconds(20.0)); // create an application (the packet sink application) PacketSinkHelper sink ("ns3::UdpSocketFactory",InetSocketAddress("10.1.1.4", port)); ApplicationContainer sinkApps= sink.Install(wifiStaNodes); sinkApps.Start (Seconds (1.0)); sinkApps.Stop (Seconds (20.0)); */ int const N= 4; // number of nodes int m [N]; for (int i=0; iGetId (), 0); //OnOffHelper onoffHelper ("ns3::UdpSocketFactory", InetSocketAddress (interfaces, port)); // system("pause"); /* // Calculate Throughput using Flowmonitor FlowMonitorHelper flowmon; Ptr monitor = flowmon.InstallAll(); */ // Now, do the actual simulation. Simulator::Schedule(Seconds(20.0), &SetCwMin, 2); //Simulator::Schedule(Seconds(20.0), &SetCwMax, 2); Simulator::Schedule (Seconds(0.1),&CalculateThroughput); Simulator::Stop (Seconds (155.0)); Simulator::Run (); Simulator::Destroy (); return 0; }