Files
unison/examples/csma-packet-socket.cc

126 lines
4.3 KiB
C++
Raw Normal View History

2007-08-01 10:29:40 +02:00
/* -*- 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
2007-08-09 15:56:28 -07:00
// - Tracing of queues and packet receptions to file "csma-one-subnet.tr"
2007-08-01 10:29:40 +02:00
#include <iostream>
#include <fstream>
#include <string>
#include <cassert>
#include "ns3/core-module.h"
#include "ns3/simulator-module.h"
#include "ns3/helper-module.h"
2007-08-01 10:29:40 +02:00
#include "ns3/ascii-trace.h"
#include "ns3/pcap-trace.h"
using namespace ns3;
2007-09-13 17:47:42 -07:00
NS_LOG_COMPONENT_DEFINE ("CsmaPacketSocketExample");
int
main (int argc, char *argv[])
2007-08-01 10:29:40 +02:00
{
2007-09-13 17:47:42 -07:00
#if 0
LogComponentEnable ("CsmaPacketSocketExample", LOG_LEVEL_INFO);
LogComponentEnable("Object", LOG_LEVEL_ALL);
LogComponentEnable("Queue", LOG_LEVEL_ALL);
LogComponentEnable("DropTailQueue", LOG_LEVEL_ALL);
LogComponentEnable("Channel", LOG_LEVEL_ALL);
LogComponentEnable("CsmaChannel", LOG_LEVEL_ALL);
LogComponentEnable("NetDevice", LOG_LEVEL_ALL);
LogComponentEnable("CsmaNetDevice", LOG_LEVEL_ALL);
LogComponentEnable("Ipv4L3Protocol", LOG_LEVEL_ALL);
LogComponentEnable("PacketSocket", LOG_LEVEL_ALL);
LogComponentEnable("Socket", LOG_LEVEL_ALL);
LogComponentEnable("UdpSocket", LOG_LEVEL_ALL);
LogComponentEnable("UdpL4Protocol", LOG_LEVEL_ALL);
LogComponentEnable("Ipv4L3Protocol", LOG_LEVEL_ALL);
LogComponentEnable("Ipv4StaticRouting", LOG_LEVEL_ALL);
LogComponentEnable("Ipv4Interface", LOG_LEVEL_ALL);
LogComponentEnable("ArpIpv4Interface", LOG_LEVEL_ALL);
LogComponentEnable("Ipv4LoopbackInterface", LOG_LEVEL_ALL);
LogComponentEnable("OnOffApplication", LOG_LEVEL_ALL);
LogComponentEnable("PacketSinkApplication", LOG_LEVEL_ALL);
LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_ALL);
LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_ALL);
#endif
CommandLine cmd;
cmd.Parse (argc, argv);
2007-08-01 10:29:40 +02:00
// Here, we will explicitly create four nodes.
2007-09-13 17:47:42 -07:00
NS_LOG_INFO ("Create nodes.");
NodeContainer c;
c.Create (4);
PacketSocketHelper packetSocket;
packetSocket.Build (c);
2007-08-01 10:29:40 +02:00
2007-08-09 15:56:28 -07:00
// create the shared medium used by all csma devices.
2007-09-13 17:47:42 -07:00
NS_LOG_INFO ("Create channels.");
2008-03-11 13:30:12 -07:00
Ptr<CsmaChannel> channel = CreateObject<CsmaChannel> ("BitRate", DataRate(5000000),
"Delay", MilliSeconds(2));
2007-08-01 10:29:40 +02:00
// use a helper function to connect our nodes to the shared channel.
2007-09-13 17:47:42 -07:00
NS_LOG_INFO ("Build Topology.");
CsmaHelper csma;
csma.SetDeviceParameter ("EncapsulationMode", String ("Llc"));
NetDeviceContainer devs = csma.Build (c, channel);
2007-09-13 17:47:42 -07:00
NS_LOG_INFO ("Create Applications.");
// Create the OnOff application to send raw datagrams
OnOffHelper onoff;
onoff.SetAppAttribute ("OnTime", ConstantVariable (1.0));
onoff.SetAppAttribute ("OffTime", ConstantVariable (0.0));
onoff.SetPacketRemote (devs.Get (0), devs.Get (1)->GetAddress (), 2);
ApplicationContainer apps = onoff.Build (c.Get (0));
apps.Start (Seconds (1.0));
apps.Stop (Seconds (10.0));
onoff.SetPacketRemote (devs.Get (3), devs.Get (0)->GetAddress (), 3);
apps = onoff.Build (c.Get (3));
apps.Start (Seconds (1.0));
apps.Stop (Seconds (10.0));
2007-08-01 10:29:40 +02:00
// Configure tracing of all enqueue, dequeue, and NetDevice receive events
2007-08-09 15:56:28 -07:00
// Trace output will be sent to the csma-packet-socket.tr file
2007-09-13 17:47:42 -07:00
NS_LOG_INFO ("Configure Tracing.");
2007-08-09 15:56:28 -07:00
AsciiTrace asciitrace ("csma-packet-socket.tr");
2007-08-01 10:29:40 +02:00
asciitrace.TraceAllNetDeviceRx ();
asciitrace.TraceAllQueues ();
2007-09-13 17:47:42 -07:00
NS_LOG_INFO ("Run Simulation.");
2007-08-01 10:29:40 +02:00
Simulator::Run ();
Simulator::Destroy ();
2007-09-13 17:47:42 -07:00
NS_LOG_INFO ("Done.");
2007-08-01 10:29:40 +02:00
}