From defeb1445d2b2da4c9fdb4c40bf32795ac5398d5 Mon Sep 17 00:00:00 2001 From: Mathieu Lacage Date: Wed, 1 Aug 2007 10:29:40 +0200 Subject: [PATCH] example code. --HG-- rename : examples/csma-cd-one-subnet.cc => examples/csma-cd-packet-socket.cc --- examples/csma-cd-packet-socket.cc | 136 ++++++++++++++++++++++++++++++ examples/wscript | 1 + 2 files changed, 137 insertions(+) create mode 100644 examples/csma-cd-packet-socket.cc diff --git a/examples/csma-cd-packet-socket.cc b/examples/csma-cd-packet-socket.cc new file mode 100644 index 000000000..9f3cc1877 --- /dev/null +++ b/examples/csma-cd-packet-socket.cc @@ -0,0 +1,136 @@ +/* -*- 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 + */ + +// Port of ns-2/tcl/ex/simple.tcl to ns-3 +// +// Network topology +// +// n0 n1 n2 n3 +// | | | | +// ===================== +// +// - CBR/UDP flows from n0 to n1, and from n3 to n0 +// - UDP packet size of 210 bytes, with per-packet interval 0.00375 sec. +// (i.e., DataRate of 448,000 bps) +// - DropTail queues +// - Tracing of queues and packet receptions to file "csma-cd-one-subnet.tr" + +#include +#include +#include +#include + +#include "ns3/command-line.h" +#include "ns3/default-value.h" +#include "ns3/ptr.h" +#include "ns3/random-variable.h" +#include "ns3/debug.h" + +#include "ns3/simulator.h" +#include "ns3/nstime.h" +#include "ns3/data-rate.h" + +#include "ns3/ascii-trace.h" +#include "ns3/pcap-trace.h" +#include "ns3/internet-node.h" +#include "ns3/csma-cd-channel.h" +#include "ns3/csma-cd-net-device.h" +#include "ns3/eui48-address.h" +#include "ns3/packet-socket-address.h" +#include "ns3/socket.h" +#include "ns3/onoff-application.h" +#include "ns3/queue.h" + +using namespace ns3; + +static Ptr +CreateCsmaCdDevice (Ptr node, Ptr channel) +{ + Ptr device = Create (node); + device->Attach (channel); + Ptr queue = Queue::CreateDefault (); + device->AddQueue (queue); + return device; +} + + +int main (int argc, char *argv[]) +{ + CommandLine::Parse (argc, argv); + + // Here, we will explicitly create four nodes. In more sophisticated + // topologies, we could configure a node factory. + Ptr n0 = Create (); + Ptr n1 = Create (); + Ptr n2 = Create (); + Ptr n3 = Create (); + + // create the shared medium used by all csma/cd devices. + Ptr channel = Create (DataRate(5000000), MilliSeconds(2)); + + // use a helper function to connect our nodes to the shared channel. + Ptr n0If = CreateCsmaCdDevice (n0, channel); + Ptr n1If = CreateCsmaCdDevice (n1, channel); + Ptr n2If = CreateCsmaCdDevice (n2, channel); + Ptr n3If = CreateCsmaCdDevice (n3, channel); + + + // create the address which identifies n1 from n0 + PacketSocketAddress n0ToN1; + n0ToN1.SetDevice (n0If->GetIfIndex ()); // set outgoing interface for outgoing packets + n0ToN1.SetPhysicalAddress (n1If->GetAddress ()); // set destination address for outgoing packets + n0ToN1.SetProtocol (2); // set arbitrary protocol for outgoing packets + + // create the address which identifies n0 from n3 + PacketSocketAddress n3ToN0; + n3ToN0.SetDevice (n3If->GetIfIndex ()); + n3ToN0.SetPhysicalAddress (n0If->GetAddress ()); + n3ToN0.SetProtocol (3); + + // Create the OnOff application to send raw datagrams of size + // 210 bytes at a rate of 448 Kb/s + // from n0 to n1 + Ptr ooff = Create ( + n0, + n0ToN1.ConvertTo (), + "Packet", + ConstantVariable(1), + ConstantVariable(0)); + // Start the application + ooff->Start(Seconds(1.0)); + ooff->Stop (Seconds(10.0)); + + // Create a similar flow from n3 to n0, starting at time 1.1 seconds + ooff = Create ( + n3, + n3ToN0.ConvertTo (), + "Packet", + ConstantVariable(1), + ConstantVariable(0)); + // Start the application + ooff->Start(Seconds(1.1)); + ooff->Stop (Seconds(10.0)); + + // Configure tracing of all enqueue, dequeue, and NetDevice receive events + // Trace output will be sent to the csma-cd-packet-socket.tr file + AsciiTrace asciitrace ("csma-cd-packet-socket.tr"); + asciitrace.TraceAllNetDeviceRx (); + asciitrace.TraceAllQueues (); + + Simulator::Run (); + + Simulator::Destroy (); +} diff --git a/examples/wscript b/examples/wscript index e1b960ddc..f455a2c1b 100644 --- a/examples/wscript +++ b/examples/wscript @@ -11,4 +11,5 @@ def build(bld): obj = create_ns_prog('simple-point-to-point', 'simple-point-to-point.cc', deps=['point-to-point', 'internet-node']) obj = create_ns_prog('csma-cd-one-subnet', 'csma-cd-one-subnet.cc', deps=['csma-cd', 'internet-node']) + obj = create_ns_prog('csma-cd-packet-socket', 'csma-cd-packet-socket.cc', deps=['csma-cd', 'internet-node'])