/* -*- 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 * * Author: John Abraham * */ #include #include #include "ns3/core-module.h" #include "ns3/network-module.h" #include "ns3/internet-module.h" #include "ns3/point-to-point-module.h" #include "ns3/netanim-module.h" #include "ns3/applications-module.h" #include "ns3/point-to-point-layout-module.h" using namespace ns3; using namespace std; //Illustrates a Callback void QueueDropCallBack ( Ptr p) { NS_LOG_UNCOND ("Packet Id:" << p->GetUid() << " Dropped at:" << Simulator::Now().GetSeconds ()); } // Illustrates /* Use of Simulator::Schedule Ptr DynamicCast GetInstanceTypeId */ static void PrintTxQueue (Ptr n) { Ptr q; uint32_t deviceIndex = 0; // Its the device Index of the device connecting to the left router Ptr p2pnd = DynamicCast (n->GetDevice (deviceIndex)); if (!p2pnd) return; q = p2pnd->GetQueue(); Ptr pdq = 0; // Now try to obtain the specific queue type if (q->GetInstanceTypeId ().GetName () == "ns3::DropTailQueue") { pdq = DynamicCast (q); // Uncomment the below to see a usage of TraceConnectWithoutContext // pdq->TraceConnectWithoutContext("Drop",MakeCallback(&QueueDropCallBack)); } if (!pdq) // Could not obtain Ptr to DropTailQueue return; if (pdq->GetMode () == ns3::DropTailQueue::PACKETS) { NS_LOG_UNCOND (Simulator::Now().GetSeconds () << "\t" << q->GetNPackets () << "\t" << q->GetTotalDroppedPackets ()); } else { NS_LOG_UNCOND (Simulator::Now().GetSeconds () << "\t" << q->GetNBytes () << "\t" << q->GetTotalDroppedBytes ()); } if (!Simulator::IsFinished ()) Simulator::Schedule (Seconds (0.1), &PrintTxQueue, n); } int main (int argc, char *argv[]) { Config::SetDefault ("ns3::OnOffApplication::PacketSize", UintegerValue (512)); Config::SetDefault ("ns3::OnOffApplication::DataRate", StringValue ("10Mbps")); uint32_t nLeftLeaf = 5; uint32_t nRightLeaf = 5; uint32_t nLeaf = 0; // If non-zero, number of both left and right uint16_t port = 0; // If non zero, port to bind to for anim connection std::string animFile; // Name of file for animation output uint32_t maxPackets = 100; uint32_t maxBytes = 100000; uint32_t modeBytes = 0; CommandLine cmd; cmd.AddValue ("nLeftLeaf", "Number of left side leaf nodes", nLeftLeaf); cmd.AddValue ("nRightLeaf","Number of right side leaf nodes", nRightLeaf); cmd.AddValue ("nLeaf", "Number of left and right side leaf nodes", nLeaf); cmd.AddValue ("port", "Port Number for Remote Animation", port); cmd.AddValue ("animFile", "File Name for Animation Output", animFile); cmd.AddValue ("maxPackets","Max Packets allowed in the DropTail queue", maxPackets); cmd.AddValue ("maxBytes", "Max Bytes allowed in the DropTail queue", maxBytes); cmd.AddValue ("modeBytes", "Change default Mode from Packets to bytes.Set it equal to 1", modeBytes); cmd.Parse (argc,argv); if (!modeBytes) { Config::SetDefault ("ns3::DropTailQueue::Mode", EnumValue(DropTailQueue::PACKETS)); Config::SetDefault ("ns3::DropTailQueue::MaxPackets", UintegerValue (maxPackets)); } else { Config::SetDefault ("ns3::DropTailQueue::Mode", EnumValue(DropTailQueue::BYTES)); Config::SetDefault ("ns3::DropTailQueue::MaxBytes", UintegerValue (maxBytes)); } if (nLeaf > 0) { nLeftLeaf = nLeaf; nRightLeaf = nLeaf; } // Create the point-to-point link helpers PointToPointHelper pointToPointRouter; pointToPointRouter.SetDeviceAttribute ("DataRate", StringValue ("1Mbps")); pointToPointRouter.SetChannelAttribute ("Delay", StringValue ("50ms")); PointToPointHelper pointToPointLeaf; pointToPointLeaf.SetDeviceAttribute ("DataRate", StringValue ("10Mbps")); pointToPointLeaf.SetChannelAttribute ("Delay", StringValue ("1ms")); PointToPointDumbbellHelper d (nLeftLeaf, pointToPointLeaf, nRightLeaf, pointToPointLeaf, pointToPointRouter); // Install Stack InternetStackHelper stack; d.InstallStack (stack); // Assign IP Addresses d.AssignIpv4Addresses (Ipv4AddressHelper ("10.1.1.0", "255.255.255.0"), Ipv4AddressHelper ("10.2.1.0", "255.255.255.0"), Ipv4AddressHelper ("10.3.1.0", "255.255.255.0")); // Install on/off app on all right side nodes OnOffHelper clientHelper ("ns3::UdpSocketFactory", Address ()); clientHelper.SetAttribute ("OnTime", RandomVariableValue (UniformVariable (0, 1))); clientHelper.SetAttribute ("OffTime", RandomVariableValue (UniformVariable (0, 1))); ApplicationContainer clientApps; for (uint32_t i = 0; i < d.RightCount (); ++i) { // Create an on/off app sending packets to the same leaf right side AddressValue remoteAddress (InetSocketAddress (d.GetLeftIpv4Address (i), 1000)); clientHelper.SetAttribute ("Remote", remoteAddress); clientApps.Add (clientHelper.Install (d.GetRight (i))); } clientApps.Start (Seconds (0.0)); clientApps.Stop (Seconds (10.0)); // Set the bounding box for animation d.BoundingBox (1, 1, 100, 100); // Create the animation object and configure for specified output AnimationInterface anim; if (!animFile.empty ()) { anim.SetOutputFile (animFile); } anim.SetXMLOutput (); anim.StartAnimation (); // Set up the acutal simulation Ipv4GlobalRoutingHelper::PopulateRoutingTables (); Ptr rightRouter = d.GetRight (); NS_ASSERT (rightRouter); Simulator::Schedule (Seconds (0.1), &PrintTxQueue, rightRouter); std::cout << "Running the simulation" << std::endl; if (modeBytes == 0) std::cout << std::setiosflags(std::ios::left) << "Time\tPackets\tDrops" << std::endl; else std::cout << std::setiosflags(std::ios::left) << "Time\tBytes\tDrops" << std::endl; Simulator::Run (); std::cout << "Destroying the simulation" << std::endl; Simulator::Destroy (); return 0; }