/* -*- Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */ /* * Copyright (c) 2011 University of Kansas * * 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 * * Author: Justin Rohrer * * James P.G. Sterbenz , director * ResiliNets Research Group http://wiki.ittc.ku.edu/resilinets * Information and Telecommunication Technology Center (ITTC) * and Department of Electrical Engineering and Computer Science * The University of Kansas Lawrence, KS USA. * * Work supported in part by NSF FIND (Future Internet Design) Program * under grant CNS-0626918 (Postmodern Internet Architecture), * NSF grant CNS-1050226 (Multilayer Network Resilience Analysis and Experimentation on GENI), * US Department of Defense (DoD), and ITTC at The University of Kansas. */ /* * This example program allows one to run ns-3 DSDV, AODV, or OLSR under * a typical random waypoint mobility model. * * By default, the simulation runs for 200 simulated seconds, of which * the first 50 are used for start-up time. The number of nodes is 50. * Nodes move according to RandomWaypointMobilityModel with a speed of * 20 m/s and no pause time within a 300x1500 m region. The WiFi is * in ad hoc mode with a 2 Mb/s rate (802.11b) and a Friis loss model. * The transmit power is set to 7.5 dBm. * * It is possible to change the mobility and density of the network by * directly modifying the speed and the number of nodes. It is also * possible to change the characteristics of the network by changing * the transmit power (as power increases, the impact of mobility * decreases and the effective density increases). * * By default, OLSR is used, but specifying a value of 2 for the protocol * will cause AODV to be used, and specifying a value of 3 will cause * DSDV to be used. * * By default, there are 10 source/sink data pairs sending UDP data * at an application rate of 2.048 Kb/s each. This is typically done * at a rate of 4 64-byte packets per second. Application data is * started at a random time between 50 and 51 seconds and continues * to the end of the simulation. * * The program outputs a few items: * - packet receptions are notified to stdout such as: * received one packet from * - each second, the data reception statistics are tabulated and output * to a comma-separated value (csv) file * - some tracing and flow monitor configuration that used to work is * left commented inline in the program */ #include #include #include #include "ns3/core-module.h" #include "ns3/network-module.h" #include "ns3/internet-module.h" #include "ns3/mobility-module.h" #include "ns3/wifi-module.h" #include "ns3/aodv-module.h" #include "ns3/olsr-module.h" #include "ns3/dsdv-module.h" #include "ns3/flow-monitor-helper.h" #include "ns3/applications-module.h" using namespace ns3; typedef map counter; counter cntPackets; NS_LOG_COMPONENT_DEFINE ("manet-routing-compare"); class RoutingExperiment { public: RoutingExperiment (); void Run (int nWifis,int nSinks, int protocol, double txp, std::string CSVfileName); //static void SetMACParam (ns3::NetDeviceContainer & devices, // int slotDistance); //std::string CommandSetup (int argc, char **argv); private: Ptr SetupPacketReceive (Ipv4Address addr, Ptr node); void ReceivePacket (Ptr socket); void CheckThroughput (); uint32_t port; uint32_t bytesTotal; uint32_t packetsReceived; std::string m_CSVfileName; std::string m_flowFileName; int m_nWifis; int m_nSinks; std::string m_protocolName; double m_txp; }; RoutingExperiment::RoutingExperiment () : port (9), bytesTotal (0), packetsReceived (0), m_CSVfileName ("manet-routing") { } void RoutingExperiment::ReceivePacket (Ptr socket) { Ptr packet; int sid; while (packet = socket->Recv ()) { bytesTotal += packet->GetSize (); packetsReceived += 1; SocketAddressTag tag; bool found; found = packet->PeekPacketTag (tag); if (found) { InetSocketAddress addr = InetSocketAddress::ConvertFrom (tag.GetAddress ()); sid = socket->GetNode ()->GetId (); NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << " " << sid << " received one packet from " << addr.GetIpv4 ()); cntPackets[sid] += 1; //cast addr to void, to suppress 'addr' set but not used //compiler warning in optimized builds (void) addr; } else { NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << " " << socket->GetNode ()->GetId () << " received one packet!"); } } } void RoutingExperiment::CheckThroughput () { double mbs = (bytesTotal * 8.0) / 1000 / 1000; bytesTotal = 0; std::ofstream out (m_CSVfileName.c_str (), std::ios::app); out << (Simulator::Now ()).GetSeconds () << "," << mbs << "," << packetsReceived << "," << m_nSinks << "," << m_protocolName << "," << m_txp << "" << std::endl; out.close (); std::ofstream flow("databyFlow.csv",std::ios::app); flow << (Simulator::Now ()).GetSeconds (); for (int i = 0; i < m_nSinks; i++) flow << "," << cntPackets[i]; flow << std::endl; flow.close(); for (int i = 0; i < m_nSinks; i++) cntPackets[i] = 0; packetsReceived = 0; Simulator::Schedule (Seconds (1.0), &RoutingExperiment::CheckThroughput, this); } Ptr RoutingExperiment::SetupPacketReceive (Ipv4Address addr, Ptr node) { TypeId tid = TypeId::LookupByName ("ns3::UdpSocketFactory"); Ptr sink = Socket::CreateSocket (node, tid); InetSocketAddress local = InetSocketAddress (addr, port); sink->Bind (local); sink->SetRecvCallback (MakeCallback (&RoutingExperiment::ReceivePacket, this)); return sink; } /*std::string RoutingExperiment::CommandSetup (int argc, char **argv) { CommandLine cmd; cmd.AddValue ("CSVfileName", "The name of the CSV output file name", m_CSVfileName); cmd.Parse (argc, argv); return m_CSVfileName; }*/ int main (int argc, char *argv[]) { RoutingExperiment experiment; //std::string CSVfileName = experiment.CommandSetup (argc,argv); //blank out the last output file and write the column headers std::string CSVfileName; int nWifis = 10; int nSinks = 2; int protocol = 2; double txp= 16.0; CommandLine cmd; cmd.AddValue ("CSVfileName", "The name of the CSV output file name", CSVfileName); cmd.AddValue("nWifis", "The number of nodes in network", nWifis); cmd.AddValue("nSinks", "The number of Sink to be set up", nSinks); cmd.AddValue("protocol", "1:olsr;2:aodv;3:dsdv", protocol); cmd.AddValue("txp", "Transmission Power", txp); cmd.Parse(argc,argv); std::ofstream outFile(CSVfileName.c_str ()); outFile <<" SimulationSecond," <<" ReceiveRate," <<" PacketsReceived," <<" NumberOfSinks," <<" RoutingProtocol," <<" TransmissionPower(dBm)" < taPositionAlloc = pos.Create ()->GetObject (); mobilityAdhoc.SetMobilityModel ("ns3::RandomWaypointMobilityModel", "Speed", RandomVariableValue (UniformVariable (0.0, nodeSpeed)), "Pause", RandomVariableValue (ConstantVariable (nodePause)), "PositionAllocator", PointerValue (taPositionAlloc));*/ Ptr taPositionAlloc = CreateObject (); mobilityAdhoc.SetMobilityModel("ns3::ConstantPositionMobilityModel"); mobilityAdhoc.SetPositionAllocator (taPositionAlloc); for(int yi = 0; yi < nWifis/10 ; yi++) { for(int xi = 0; xi < 10 ; xi++) taPositionAlloc->Add (Vector (xi, yi, 0.0)); } mobilityAdhoc.Install (adhocNodes); OnOffHelper onoff1 ("ns3::UdpSocketFactory",Address ()); onoff1.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1))); onoff1.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0))); for (int i = 0; i < nSinks; i++) { cntPackets[i] = 0; Ptr sink = SetupPacketReceive (adhocInterfaces.GetAddress (i), adhocNodes.Get (i)); AddressValue remoteAddress (InetSocketAddress (adhocInterfaces.GetAddress (i), port)); onoff1.SetAttribute ("Remote", remoteAddress); UniformVariable var; ApplicationContainer temp = onoff1.Install (adhocNodes.Get (nWifis - i - 1)); temp.Start (Seconds (var.GetValue (50.0,51.0))); temp.Stop (Seconds (TotalTime)); } std::stringstream ss; ss << nWifis; std::string nodes = ss.str (); /*std::stringstream ss2; ss2 << nodeSpeed; std::string sNodeSpeed = ss2.str (); std::stringstream ss3; ss3 << nodePause; std::string sNodePause = ss3.str ();*/ std::stringstream ss3; ss3 << nSinks; std::string flows = ss3.str(); std::stringstream ss4; ss4 << rate; std::string sRate = ss4.str (); NS_LOG_INFO ("Configure Tracing."); //tr_name = tr_name + "_" + m_protocolName +"_" + nodes + "nodes_" + sNodeSpeed + "speed_" + sNodePause + "pause_" + sRate + "rate"; tr_name = tr_name + "_" + m_protocolName +"_" + nodes + "nodes_" + sRate + "rate_" + flows + "Flows"; //AsciiTraceHelper ascii; //Ptr osw = ascii.CreateFileStream ( (tr_name + ".tr").c_str()); //wifiPhy.EnableAsciiAll (osw); //std::ofstream os; //os.open ((tr_name + ".mob").c_str()); //MobilityHelper::EnableAsciiAll (os); Ptr flowmon; FlowMonitorHelper flowmonHelper; flowmon = flowmonHelper.InstallAll (); /*flowmon.SetAttribute("DelayBinWidth",DoubleValue(0.001)); flowmon.SetAttribute("jitterBinWidth",DoubleValue(0.001)); flowmon.SetAttribute("PacketSizeBinWidth",DoubleValue(20));*/ NS_LOG_INFO ("Run Simulation."); CheckThroughput (); Simulator::Stop (Seconds (TotalTime)); Simulator::Run (); flowmon->SerializeToXmlFile ((tr_name + ".xml").c_str(), false, false); Simulator::Destroy (); }