/* -*- 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/network-module.h" #include "ns3/csma-module.h" #include "ns3/internet-module.h" #include "ns3/point-to-point-module.h" #include "ns3/applications-module.h" #include "ns3/ipv4-global-routing-helper.h" // Default Network Topology // // n1 n2 n3 // | | | // =========== // LAN 10.1.2.0 using namespace ns3; NS_LOG_COMPONENT_DEFINE ("SecondScriptExample"); int main (int argc, char *argv[]) { bool verbose = true; uint32_t nCsma = 3; CommandLine cmd; cmd.AddValue ("nCsma", "Number of \"extra\" CSMA nodes/devices", nCsma); cmd.AddValue ("verbose", "Tell echo applications to log if true", verbose); cmd.Parse (argc,argv); if (verbose) { LogComponentEnable ("UdpEchoClientApplication", LOG_LEVEL_INFO); LogComponentEnable ("UdpEchoServerApplication", LOG_LEVEL_INFO); } nCsma = nCsma == 0 ? 1 : nCsma; NodeContainer csmaNodes; csmaNodes.Create (nCsma); CsmaHelper csma; csma.SetChannelAttribute ("DataRate", StringValue ("100Mbps")); csma.SetChannelAttribute ("Delay", TimeValue (NanoSeconds (6560))); NetDeviceContainer csmaDevices; csmaDevices = csma.Install (csmaNodes); InternetStackHelper stack; stack.Install (csmaNodes); Ipv4AddressHelper address; address.SetBase ("10.1.2.0", "255.255.255.0"); Ipv4InterfaceContainer csmaInterfaces; csmaInterfaces = address.Assign (csmaDevices); uint16_t sinkPort = 8080; PacketSinkHelper packetSinkHelper ("ns3::UdpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), sinkPort)); ApplicationContainer sinkApps = packetSinkHelper.Install (csmaNodes.Get (nCsma-1)); sinkApps.Add(packetSinkHelper.Install (csmaNodes.Get (nCsma-2))); sinkApps.Start (Seconds (0.)); sinkApps.Stop (Seconds (20.)); OnOffHelper onOffHelper ("ns3::UdpSocketFactory", Address ()); onOffHelper.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1))); onOffHelper.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0))); AddressValue remoteAddress (InetSocketAddress ("10.1.2.255", sinkPort)); onOffHelper.SetAttribute ("Remote", remoteAddress); ApplicationContainer clientApps = onOffHelper.Install (csmaNodes.Get (0)); clientApps.Start (Seconds (2.0)); clientApps.Stop (Seconds (3.0)); Ipv4GlobalRoutingHelper::PopulateRoutingTables (); csma.EnablePcap ("second", csmaDevices.Get (1), true); Simulator::Run (); Simulator::Destroy (); return 0; }