examples/udp-echo.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Tue, 11 Mar 2008 13:30:12 -0700
changeset 2592 3ebf97150166
parent 2577 5b41cb5c3fcf
child 2600 6c389d0c717d
permissions -rw-r--r--
get rid of CreateObjectWith
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/ptr.h"
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    30
#include "ns3/log.h"
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    31
#include "ns3/simulator.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    32
#include "ns3/nstime.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    33
#include "ns3/data-rate.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    34
#include "ns3/ascii-trace.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    35
#include "ns3/pcap-trace.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    36
#include "ns3/internet-node.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    37
#include "ns3/csma-channel.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    38
#include "ns3/csma-net-device.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    39
#include "ns3/csma-topology.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    40
#include "ns3/csma-ipv4-topology.h"
1502
4b4799567e2a add tutorial directory and hello-simulator
Craig Dowell <craigdo@ee.washington.edu>
parents: 1500
diff changeset
    41
#include "ns3/mac48-address.h"
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    42
#include "ns3/ipv4-address.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    43
#include "ns3/inet-socket-address.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    44
#include "ns3/ipv4.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    45
#include "ns3/socket.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    46
#include "ns3/ipv4-route.h"
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    47
#include "ns3/udp-echo-client.h"
1497
dff0a53e60f2 server half
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
    48
#include "ns3/udp-echo-server.h"
2494
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2230
diff changeset
    49
#include "ns3/uinteger.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
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    53
NS_LOG_COMPONENT_DEFINE ("UdpEchoExample");
1496
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
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    63
  LogComponentEnable ("UdpEchoExample", LOG_LEVEL_INFO);
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    64
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    65
  LogComponentEnable("Object", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    66
  LogComponentEnable("Queue", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    67
  LogComponentEnable("DropTailQueue", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    68
  LogComponentEnable("Channel", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    69
  LogComponentEnable("CsmaChannel", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    70
  LogComponentEnable("NetDevice", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    71
  LogComponentEnable("CsmaNetDevice", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    72
  LogComponentEnable("Ipv4L3Protocol", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    73
  LogComponentEnable("PacketSocket", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    74
  LogComponentEnable("Socket", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    75
  LogComponentEnable("UdpSocket", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    76
  LogComponentEnable("UdpL4Protocol", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    77
  LogComponentEnable("Ipv4L3Protocol", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    78
  LogComponentEnable("Ipv4StaticRouting", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    79
  LogComponentEnable("Ipv4Interface", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    80
  LogComponentEnable("ArpIpv4Interface", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    81
  LogComponentEnable("Ipv4LoopbackInterface", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    82
  LogComponentEnable("OnOffApplication", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    83
  LogComponentEnable("PacketSinkApplication", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    84
  LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    85
  LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_ALL);
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    86
#endif
2533
80cdc3eec057 do not use Queue::CreateDefault
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2494
diff changeset
    87
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    88
// 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
    89
// run-time, via command-line arguments
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    90
//
2575
1aae382e65e2 rewrite CommandLine to not handle DefaultValues anymore.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2533
diff changeset
    91
  CommandLine cmd;
1aae382e65e2 rewrite CommandLine to not handle DefaultValues anymore.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2533
diff changeset
    92
  cmd.Parse (argc, argv);
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    93
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    94
// 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
    95
//
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    96
  NS_LOG_INFO ("Create nodes.");
2230
9f13ac3291e0 add CreateObject<> to instanciate subclasses of the Object base class. Replaces Create<>.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1514
diff changeset
    97
  Ptr<Node> n0 = CreateObject<InternetNode> ();
9f13ac3291e0 add CreateObject<> to instanciate subclasses of the Object base class. Replaces Create<>.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1514
diff changeset
    98
  Ptr<Node> n1 = CreateObject<InternetNode> (); 
9f13ac3291e0 add CreateObject<> to instanciate subclasses of the Object base class. Replaces Create<>.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1514
diff changeset
    99
  Ptr<Node> n2 = CreateObject<InternetNode> (); 
9f13ac3291e0 add CreateObject<> to instanciate subclasses of the Object base class. Replaces Create<>.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1514
diff changeset
   100
  Ptr<Node> n3 = CreateObject<InternetNode> ();
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   101
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
   102
  NS_LOG_INFO ("Create channels.");
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   103
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   104
// 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
   105
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   106
  Ptr<CsmaChannel> lan = CsmaTopology::CreateCsmaChannel(
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   107
    DataRate(5000000), MilliSeconds(2));
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   108
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
   109
  NS_LOG_INFO ("Build Topology.");
1496
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
// 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
   112
// 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
   113
// 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
   114
// 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
   115
// 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
   116
// 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
   117
// zero.
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   118
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   119
  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
   120
    Mac48Address("08:00:2e:00:00:00"));
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   121
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   122
  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
   123
    Mac48Address("08:00:2e:00:00:01"));
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   124
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   125
  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
   126
    Mac48Address("08:00:2e:00:00:02"));
1496
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 nd3 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n3, 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:03"));
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
// 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
   132
//
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
   133
  NS_LOG_INFO ("Assign IP Addresses.");
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   134
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   135
// XXX BUGBUG
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   136
// 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
   137
// 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
   138
// 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
   139
// 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
   140
// address.
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
// 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
   143
// 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
   144
// which we do not need here.
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   145
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   146
  CsmaIpv4Topology::AddIpv4Address (n0, nd0, Ipv4Address("10.1.1.1"), 
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   147
    Ipv4Mask("255.255.255.0"));
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   148
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   149
  CsmaIpv4Topology::AddIpv4Address (n1, nd1, Ipv4Address("10.1.1.2"), 
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   150
    Ipv4Mask("255.255.255.0"));
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   151
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   152
  CsmaIpv4Topology::AddIpv4Address (n2, nd2, Ipv4Address("10.1.1.3"), 
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   153
    Ipv4Mask("255.255.255.0"));
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 (n3, nd3, Ipv4Address("10.1.1.4"), 
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   156
    Ipv4Mask("255.255.255.0"));
1497
dff0a53e60f2 server half
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   157
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
   158
  NS_LOG_INFO ("Create Applications.");
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   159
//
1500
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   160
// Create a UdpEchoServer application on node one.
1499
93d51d757afa working one way echo app
Craig Dowell <craigdo@ee.washington.edu>
parents: 1497
diff changeset
   161
//
1514
addb79f47ba5 change port number used by example scripts to 'discard' or 'echo' instead of '80'
Tom Henderson <tomh@tomh.org>
parents: 1504
diff changeset
   162
  uint16_t port = 9;  // well-known echo port number
1500
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   163
2592
3ebf97150166 get rid of CreateObjectWith
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2577
diff changeset
   164
  Ptr<UdpEchoServer> server = CreateObject<UdpEchoServer> ("Node", n1, 
2494
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2230
diff changeset
   165
                                                               "Port", Uinteger (port));
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2230
diff changeset
   166
  n1->AddApplication (server);
1499
93d51d757afa working one way echo app
Craig Dowell <craigdo@ee.washington.edu>
parents: 1497
diff changeset
   167
//
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   168
// 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
   169
// node one.
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   170
//
1500
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   171
  uint32_t packetSize = 1024;
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   172
  uint32_t maxPacketCount = 1;
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   173
  Time interPacketInterval = Seconds (1.);
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   174
2494
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2230
diff changeset
   175
  Ptr<UdpEchoClient> client = 
2592
3ebf97150166 get rid of CreateObjectWith
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2577
diff changeset
   176
    CreateObject<UdpEchoClient> ("Node", n0, 
2494
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2230
diff changeset
   177
                                     "RemoteIpv4", Ipv4Address ("10.1.1.2"),
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2230
diff changeset
   178
                                     "RemotePort", Uinteger (port),
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2230
diff changeset
   179
                                     "MaxPackets", Uinteger (maxPacketCount), 
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2230
diff changeset
   180
                                     "Interval", interPacketInterval, 
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2230
diff changeset
   181
                                     "PacketSize", Uinteger (packetSize));
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2230
diff changeset
   182
  n0->AddApplication (client);
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   183
//
1500
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   184
// Tell the applications when to start and stop.
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   185
//
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   186
  server->Start(Seconds(1.));
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   187
  client->Start(Seconds(2.));
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   188
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   189
  server->Stop (Seconds(10.));
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   190
  client->Stop (Seconds(10.));
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   191
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   192
// Configure tracing of all enqueue, dequeue, and NetDevice receive events.
1497
dff0a53e60f2 server half
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   193
// 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
   194
//
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
   195
  NS_LOG_INFO ("Configure Tracing.");
1497
dff0a53e60f2 server half
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   196
  AsciiTrace asciitrace ("udp-echo.tr");
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   197
  asciitrace.TraceAllNetDeviceRx ();
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   198
  asciitrace.TraceAllQueues ();
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   199
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   200
// 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
   201
// The output files will be named:
1497
dff0a53e60f2 server half
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   202
//     udp-echo.pcap-<nodeId>-<interfaceId>
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   203
// 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
   204
// display timestamps correctly)
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   205
//
1497
dff0a53e60f2 server half
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   206
  PcapTrace pcaptrace ("udp-echo.pcap");
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   207
  pcaptrace.TraceAllIp ();
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   208
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   209
// Now, do the actual simulation.
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   210
//
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
   211
  NS_LOG_INFO ("Run Simulation.");
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   212
  Simulator::Run ();
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   213
  Simulator::Destroy ();
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
   214
  NS_LOG_INFO ("Done.");
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   215
}