examples/udp-echo.cc
author Craig Dowell <craigdo@ee.washington.edu>
Thu, 13 Sep 2007 11:04:47 -0700
changeset 1502 4b4799567e2a
parent 1500 895ed42278d3
child 1504 36ecc970ba96
permissions -rw-r--r--
add tutorial directory and hello-simulator
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     2
/*
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     3
 * This program is free software; you can redistribute it and/or modify
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     4
 * it under the terms of the GNU General Public License version 2 as
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     5
 * published by the Free Software Foundation;
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     6
 *
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     7
 * This program is distributed in the hope that it will be useful,
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    10
 * GNU General Public License for more details.
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    11
 *
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    12
 * You should have received a copy of the GNU General Public License
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    13
 * along with this program; if not, write to the Free Software
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    15
 */
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    16
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    17
// Network topology
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    18
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    19
//       n0    n1   n2   n3
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    20
//       |     |    |    |
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    21
//       =================
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    22
//              LAN
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    23
//
1500
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
    24
// - UDP flows from n0 to n1 and back
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    25
// - DropTail queues 
1497
dff0a53e60f2 server half
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
    26
// - Tracing of queues and packet receptions to file "udp-echo.tr"
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    27
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    28
#include "ns3/command-line.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    29
#include "ns3/default-value.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    30
#include "ns3/ptr.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    31
#include "ns3/debug.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    32
#include "ns3/simulator.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    33
#include "ns3/nstime.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    34
#include "ns3/data-rate.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    35
#include "ns3/ascii-trace.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    36
#include "ns3/pcap-trace.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    37
#include "ns3/internet-node.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    38
#include "ns3/csma-channel.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    39
#include "ns3/csma-net-device.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    40
#include "ns3/csma-topology.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    41
#include "ns3/csma-ipv4-topology.h"
1502
4b4799567e2a add tutorial directory and hello-simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 1500
diff changeset
    42
#include "ns3/mac48-address.h"
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    43
#include "ns3/ipv4-address.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    44
#include "ns3/inet-socket-address.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    45
#include "ns3/ipv4.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    46
#include "ns3/socket.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    47
#include "ns3/ipv4-route.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    48
#include "ns3/udp-echo-client.h"
1497
dff0a53e60f2 server half
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
    49
#include "ns3/udp-echo-server.h"
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    50
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    51
using namespace ns3;
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    52
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    53
NS_DEBUG_COMPONENT_DEFINE ("UdpEcho");
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    54
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    55
int 
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    56
main (int argc, char *argv[])
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    57
{
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    58
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    59
// Users may find it convenient to turn on explicit debugging
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    60
// for selected modules; the below lines suggest how to do this
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    61
//
1499
93d51d757afa working one way echo app
Craig Dowell <craigdo@ee.washington.edu>
parents: 1497
diff changeset
    62
#if 0
93d51d757afa working one way echo app
Craig Dowell <craigdo@ee.washington.edu>
parents: 1497
diff changeset
    63
  DebugComponentEnable("UdpEcho");
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    64
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    65
  DebugComponentEnable("Object");
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    66
  DebugComponentEnable("Queue");
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    67
  DebugComponentEnable("DropTailQueue");
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    68
  DebugComponentEnable("Channel");
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    69
  DebugComponentEnable("CsmaChannel");
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    70
  DebugComponentEnable("CsmaNetDevice");
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    71
  DebugComponentEnable("Ipv4L3Protocol");
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    72
  DebugComponentEnable("NetDevice");
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    73
  DebugComponentEnable("PacketSocket");
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    74
  DebugComponentEnable("OnOffApplication");
1499
93d51d757afa working one way echo app
Craig Dowell <craigdo@ee.washington.edu>
parents: 1497
diff changeset
    75
  DebugComponentEnable("Socket");
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    76
  DebugComponentEnable("UdpSocket");
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    77
  DebugComponentEnable("UdpL4Protocol");
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    78
  DebugComponentEnable("Ipv4L3Protocol");
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    79
  DebugComponentEnable("Ipv4StaticRouting");
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    80
  DebugComponentEnable("Ipv4Interface");
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    81
  DebugComponentEnable("ArpIpv4Interface");
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    82
  DebugComponentEnable("Ipv4LoopbackInterface");
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    83
  DebugComponentEnable("UdpEchoClient");
1499
93d51d757afa working one way echo app
Craig Dowell <craigdo@ee.washington.edu>
parents: 1497
diff changeset
    84
  DebugComponentEnable("UdpEchoServer");
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    85
#endif
1499
93d51d757afa working one way echo app
Craig Dowell <craigdo@ee.washington.edu>
parents: 1497
diff changeset
    86
93d51d757afa working one way echo app
Craig Dowell <craigdo@ee.washington.edu>
parents: 1497
diff changeset
    87
  DebugComponentEnable("UdpEcho");
93d51d757afa working one way echo app
Craig Dowell <craigdo@ee.washington.edu>
parents: 1497
diff changeset
    88
  DebugComponentEnable("UdpEchoClient");
93d51d757afa working one way echo app
Craig Dowell <craigdo@ee.washington.edu>
parents: 1497
diff changeset
    89
  DebugComponentEnable("UdpEchoServer");
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    90
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    91
// Set up default values for the simulation.  Use the DefaultValue::Bind()
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    92
// technique to tell the system what subclass of Queue to use.  The Bind
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    93
// command command tells the queue factory which class to instantiate when the
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    94
// queue factory is invoked in the topology code
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    95
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    96
  DefaultValue::Bind ("Queue", "DropTailQueue");
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    97
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    98
// Allow the user to override any of the defaults and the above Bind() at
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    99
// run-time, via command-line arguments
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   100
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   101
  CommandLine::Parse (argc, argv);
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   102
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   103
// Explicitly create the nodes required by the topology (shown above).
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   104
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   105
  NS_DEBUG("Create nodes.");
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   106
  Ptr<Node> n0 = Create<InternetNode> ();
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   107
  Ptr<Node> n1 = Create<InternetNode> (); 
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   108
  Ptr<Node> n2 = Create<InternetNode> (); 
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   109
  Ptr<Node> n3 = Create<InternetNode> ();
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   110
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   111
  NS_DEBUG("Create channels.");
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   112
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   113
// Explicitly create the channels required by the topology (shown above).
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   114
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   115
  Ptr<CsmaChannel> lan = CsmaTopology::CreateCsmaChannel(
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   116
    DataRate(5000000), MilliSeconds(2));
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   117
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   118
  NS_DEBUG("Build Topology.");
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   119
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   120
// Now fill out the topology by creating the net devices required to connect
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   121
// the nodes to the channels and hooking them up.  AddIpv4CsmaNetDevice will
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   122
// create a net device, add a MAC address (in memory of the pink flamingo) and
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   123
// connect the net device to a nodes and also to a channel. the 
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   124
// AddIpv4CsmaNetDevice method returns a net device index for the net device
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   125
// created on the node.  Interpret nd0 as the net device we created for node
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   126
// zero.
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   127
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   128
  uint32_t nd0 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n0, lan, 
1502
4b4799567e2a add tutorial directory and hello-simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 1500
diff changeset
   129
    Mac48Address("08:00:2e:00:00:00"));
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   130
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   131
  uint32_t nd1 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n1, lan, 
1502
4b4799567e2a add tutorial directory and hello-simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 1500
diff changeset
   132
    Mac48Address("08:00:2e:00:00:01"));
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   133
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   134
  uint32_t nd2 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n2, lan, 
1502
4b4799567e2a add tutorial directory and hello-simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 1500
diff changeset
   135
    Mac48Address("08:00:2e:00:00:02"));
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   136
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   137
  uint32_t nd3 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n3, lan, 
1502
4b4799567e2a add tutorial directory and hello-simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 1500
diff changeset
   138
    Mac48Address("08:00:2e:00:00:03"));
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   139
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   140
// We've got the "hardware" in place.  Now we need to add IP addresses.
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   141
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   142
  NS_DEBUG("Assign IP Addresses.");
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   143
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   144
// XXX BUGBUG
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   145
// Need a better way to get the interface index.  The point-to-point topology
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   146
// as implemented can't return the index since it creates interfaces on both
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   147
// sides (i.e., it does AddIpv4Addresses, not AddIpv4Address).  We need a
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   148
// method on Ipv4 to find the interface index corresponding to a given ipv4 
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   149
// address.
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   150
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   151
// Assign IP addresses to the net devices and associated interfaces
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   152
// on the lan.  The AddIpv4Address method returns an Ipv4 interface index
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   153
// which we do not need here.
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   154
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   155
  CsmaIpv4Topology::AddIpv4Address (n0, nd0, Ipv4Address("10.1.1.1"), 
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   156
    Ipv4Mask("255.255.255.0"));
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   157
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   158
  CsmaIpv4Topology::AddIpv4Address (n1, nd1, Ipv4Address("10.1.1.2"), 
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   159
    Ipv4Mask("255.255.255.0"));
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   160
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   161
  CsmaIpv4Topology::AddIpv4Address (n2, nd2, Ipv4Address("10.1.1.3"), 
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   162
    Ipv4Mask("255.255.255.0"));
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   163
  
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   164
  CsmaIpv4Topology::AddIpv4Address (n3, nd3, Ipv4Address("10.1.1.4"), 
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   165
    Ipv4Mask("255.255.255.0"));
1497
dff0a53e60f2 server half
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   166
dff0a53e60f2 server half
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   167
  NS_DEBUG("Create Applications.");
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   168
//
1500
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   169
// Create a UdpEchoServer application on node one.
1499
93d51d757afa working one way echo app
Craig Dowell <craigdo@ee.washington.edu>
parents: 1497
diff changeset
   170
//
1500
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   171
  uint16_t port = 80;
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   172
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   173
  Ptr<UdpEchoServer> server = Create<UdpEchoServer> (n1, port);
1499
93d51d757afa working one way echo app
Craig Dowell <craigdo@ee.washington.edu>
parents: 1497
diff changeset
   174
//
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   175
// Create a UdpEchoClient application to send UDP datagrams from node zero to
1500
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   176
// node one.
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   177
//
1500
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   178
  uint32_t packetSize = 1024;
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   179
  uint32_t maxPacketCount = 1;
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   180
  Time interPacketInterval = Seconds (1.);
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   181
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   182
  Ptr<UdpEchoClient> client = Create<UdpEchoClient> (n0, "10.1.1.2", port, 
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   183
    maxPacketCount, interPacketInterval, packetSize);
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   184
//
1500
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   185
// Tell the applications when to start and stop.
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   186
//
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   187
  server->Start(Seconds(1.));
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   188
  client->Start(Seconds(2.));
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   189
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   190
  server->Stop (Seconds(10.));
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   191
  client->Stop (Seconds(10.));
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   192
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   193
// Configure tracing of all enqueue, dequeue, and NetDevice receive events.
1497
dff0a53e60f2 server half
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   194
// Trace output will be sent to the file "udp-echo.tr"
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   195
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   196
  NS_DEBUG("Configure Tracing.");
1497
dff0a53e60f2 server half
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   197
  AsciiTrace asciitrace ("udp-echo.tr");
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   198
  asciitrace.TraceAllNetDeviceRx ();
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   199
  asciitrace.TraceAllQueues ();
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   200
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   201
// Also configure some tcpdump traces; each interface will be traced.
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   202
// The output files will be named:
1497
dff0a53e60f2 server half
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   203
//     udp-echo.pcap-<nodeId>-<interfaceId>
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   204
// and can be read by the "tcpdump -r" command (use "-tt" option to
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   205
// display timestamps correctly)
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   206
//
1497
dff0a53e60f2 server half
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   207
  PcapTrace pcaptrace ("udp-echo.pcap");
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   208
  pcaptrace.TraceAllIp ();
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   209
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   210
// Now, do the actual simulation.
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   211
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   212
  NS_DEBUG("Run Simulation.");
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   213
  Simulator::Run ();
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   214
  Simulator::Destroy ();
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   215
  NS_DEBUG("Done.");
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   216
}