Files
unison/examples/udp-echo.cc

140 lines
4.5 KiB
C++
Raw Normal View History

2007-09-12 15:23:25 -07: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
*/
// Network topology
//
// n0 n1 n2 n3
// | | | |
// =================
// LAN
//
2007-09-13 10:52:41 -07:00
// - UDP flows from n0 to n1 and back
2007-09-12 15:23:25 -07:00
// - DropTail queues
2007-09-12 16:20:07 -07:00
// - Tracing of queues and packet receptions to file "udp-echo.tr"
2007-09-12 15:23:25 -07:00
2008-03-28 13:40:08 -07:00
#include <fstream>
2008-03-27 12:20:26 -07:00
#include "ns3/core-module.h"
#include "ns3/simulator-module.h"
#include "ns3/helper-module.h"
2007-09-12 15:23:25 -07:00
using namespace ns3;
2007-09-13 17:47:42 -07:00
NS_LOG_COMPONENT_DEFINE ("UdpEchoExample");
2007-09-12 15:23:25 -07:00
int
main (int argc, char *argv[])
{
//
// Users may find it convenient to turn on explicit debugging
// for selected modules; the below lines suggest how to do this
//
2007-09-12 16:54:21 -07:00
#if 0
2007-09-13 17:47:42 -07:00
LogComponentEnable ("UdpEchoExample", 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);
2007-09-12 15:23:25 -07:00
#endif
2008-03-02 21:57:28 +01:00
2007-09-12 15:23:25 -07:00
// Allow the user to override any of the defaults and the above Bind() at
// run-time, via command-line arguments
//
CommandLine cmd;
cmd.Parse (argc, argv);
2007-09-12 15:23:25 -07:00
//
// Explicitly create the nodes required by the topology (shown above).
//
2007-09-13 17:47:42 -07:00
NS_LOG_INFO ("Create nodes.");
2008-03-27 12:20:26 -07:00
NodeContainer n;
n.Create (4);
InternetStackHelper internet;
2008-04-07 10:38:37 -07:00
internet.Install (n);
2007-09-12 15:23:25 -07:00
2007-09-13 17:47:42 -07:00
NS_LOG_INFO ("Create channels.");
2007-09-12 15:23:25 -07:00
//
// Explicitly create the channels required by the topology (shown above).
//
2008-03-27 12:20:26 -07:00
CsmaHelper csma;
csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate(5000000)));
csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
2008-04-07 10:38:37 -07:00
NetDeviceContainer d = csma.Install (n);
2007-09-12 15:23:25 -07:00
2008-03-27 12:20:26 -07:00
Ipv4AddressHelper ipv4;
2007-09-12 15:23:25 -07:00
//
// We've got the "hardware" in place. Now we need to add IP addresses.
//
2007-09-13 17:47:42 -07:00
NS_LOG_INFO ("Assign IP Addresses.");
2008-03-27 12:20:26 -07:00
ipv4.SetBase ("10.1.1.0", "255.255.255.0");
Ipv4InterfaceContainer i = ipv4.Assign (d);
2007-09-12 16:20:07 -07:00
2007-09-13 17:47:42 -07:00
NS_LOG_INFO ("Create Applications.");
2007-09-12 15:23:25 -07:00
//
2007-09-13 10:52:41 -07:00
// Create a UdpEchoServer application on node one.
2007-09-12 15:23:25 -07:00
//
uint16_t port = 9; // well-known echo port number
UdpEchoServerHelper server (port);
2008-04-07 10:38:37 -07:00
ApplicationContainer apps = server.Install (n.Get(1));
2008-03-27 12:20:26 -07:00
apps.Start (Seconds (1.0));
apps.Stop (Seconds (10.0));
2007-09-13 10:52:41 -07:00
2007-09-12 15:23:25 -07:00
//
2007-09-12 16:54:21 -07:00
// Create a UdpEchoClient application to send UDP datagrams from node zero to
2007-09-13 10:52:41 -07:00
// node one.
2007-09-12 16:20:07 -07:00
//
2007-09-13 10:52:41 -07:00
uint32_t packetSize = 1024;
uint32_t maxPacketCount = 1;
Time interPacketInterval = Seconds (1.);
UdpEchoClientHelper client (i.GetAddress (1), port);
client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
client.SetAttribute ("Interval", TimeValue (interPacketInterval));
client.SetAttribute ("PacketSize", UintegerValue (packetSize));
2008-04-07 10:38:37 -07:00
apps = client.Install (n.Get (0));
2008-03-27 12:20:26 -07:00
apps.Start (Seconds (2.0));
apps.Stop (Seconds (10.0));
2007-09-13 10:52:41 -07:00
std::ofstream ascii;
ascii.open ("udp-echo.tr");
CsmaHelper::EnablePcapAll ("udp-echo");
CsmaHelper::EnableAsciiAll (ascii);
2007-09-12 16:20:07 -07:00
//
2007-09-12 15:23:25 -07:00
// Now, do the actual simulation.
//
2007-09-13 17:47:42 -07:00
NS_LOG_INFO ("Run Simulation.");
2007-09-12 15:23:25 -07:00
Simulator::Run ();
Simulator::Destroy ();
2007-09-13 17:47:42 -07:00
NS_LOG_INFO ("Done.");
2007-09-12 15:23:25 -07:00
}