examples/udp-echo.cc
author Craig Dowell <craigdo@ee.washington.edu>
Mon, 28 Sep 2009 17:18:12 -0700
changeset 5275 d4008f2981ba
parent 4577 84c133267507
permissions -rw-r--r--
Windows needs entire path
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
2788
ad1404dcfad4 port to new pcap/ascii trace helpers
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2768
diff changeset
    28
#include <fstream>
2768
fcb615f2c57c port to helper API
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2600
diff changeset
    29
#include "ns3/core-module.h"
fcb615f2c57c port to helper API
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2600
diff changeset
    30
#include "ns3/simulator-module.h"
fcb615f2c57c port to helper API
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2600
diff changeset
    31
#include "ns3/helper-module.h"
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    32
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    33
using namespace ns3;
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    34
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    35
NS_LOG_COMPONENT_DEFINE ("UdpEchoExample");
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    36
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    37
int 
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    38
main (int argc, char *argv[])
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    39
{
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    40
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    41
// 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
    42
// 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
    43
//
1499
93d51d757afa working one way echo app
Craig Dowell <craigdo@ee.washington.edu>
parents: 1497
diff changeset
    44
#if 0
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    45
  LogComponentEnable ("UdpEchoExample", LOG_LEVEL_INFO);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    46
  LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    47
  LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_ALL);
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    48
#endif
4577
84c133267507 Allow UDP echo client to specify packet contents
Craig Dowell <craigdo@ee.washington.edu>
parents: 4264
diff changeset
    49
//
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    50
// 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
    51
// run-time, via command-line arguments
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    52
//
2575
1aae382e65e2 rewrite CommandLine to not handle DefaultValues anymore.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2533
diff changeset
    53
  CommandLine cmd;
1aae382e65e2 rewrite CommandLine to not handle DefaultValues anymore.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2533
diff changeset
    54
  cmd.Parse (argc, argv);
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    55
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    56
// 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
    57
//
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    58
  NS_LOG_INFO ("Create nodes.");
2768
fcb615f2c57c port to helper API
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2600
diff changeset
    59
  NodeContainer n;
fcb615f2c57c port to helper API
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2600
diff changeset
    60
  n.Create (4);
fcb615f2c57c port to helper API
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2600
diff changeset
    61
fcb615f2c57c port to helper API
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2600
diff changeset
    62
  InternetStackHelper internet;
2887
9a637e6daee0 Build -> Install
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2838
diff changeset
    63
  internet.Install (n);
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
  NS_LOG_INFO ("Create channels.");
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    66
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    67
// 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
    68
//
2768
fcb615f2c57c port to helper API
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2600
diff changeset
    69
  CsmaHelper csma;
3381
3cdd9d60f7c7 bug 232, references to Parameter obsolete
Craig Dowell <craigdo@ee.washington.edu>
parents: 3257
diff changeset
    70
  csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate(5000000)));
3cdd9d60f7c7 bug 232, references to Parameter obsolete
Craig Dowell <craigdo@ee.washington.edu>
parents: 3257
diff changeset
    71
  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
3629
367ae1cba60f further cleanup of MTU in CSMA
craigdo@ee.washington.edu
parents: 3506
diff changeset
    72
  csma.SetDeviceAttribute ("Mtu", UintegerValue (1400));
2887
9a637e6daee0 Build -> Install
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2838
diff changeset
    73
  NetDeviceContainer d = csma.Install (n);
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    74
2768
fcb615f2c57c port to helper API
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2600
diff changeset
    75
  Ipv4AddressHelper ipv4;
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    76
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    77
// 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
    78
//
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    79
  NS_LOG_INFO ("Assign IP Addresses.");
2768
fcb615f2c57c port to helper API
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2600
diff changeset
    80
  ipv4.SetBase ("10.1.1.0", "255.255.255.0");
2888
872dc8466352 Ipv4AddressHelper::Allocate -> Ipv4AddressHelper::Assign
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2887
diff changeset
    81
  Ipv4InterfaceContainer i = ipv4.Assign (d);
1497
dff0a53e60f2 server half
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
    82
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
    83
  NS_LOG_INFO ("Create Applications.");
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    84
//
1500
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
    85
// Create a UdpEchoServer application on node one.
1499
93d51d757afa working one way echo app
Craig Dowell <craigdo@ee.washington.edu>
parents: 1497
diff changeset
    86
//
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
    87
  uint16_t port = 9;  // well-known echo port number
3382
d5f8e5fae1c6 fix bug 234 which changes required methods on helpers to constructor parameters. Update tutorial content to reflect. Change RemoteIpv4 attribute to RemoteAddress.
Craig Dowell <craigdo@ee.washington.edu>
parents: 3381
diff changeset
    88
  UdpEchoServerHelper server (port);
2887
9a637e6daee0 Build -> Install
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2838
diff changeset
    89
  ApplicationContainer apps = server.Install (n.Get(1));
2768
fcb615f2c57c port to helper API
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2600
diff changeset
    90
  apps.Start (Seconds (1.0));
fcb615f2c57c port to helper API
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2600
diff changeset
    91
  apps.Stop (Seconds (10.0));
1500
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
    92
1499
93d51d757afa working one way echo app
Craig Dowell <craigdo@ee.washington.edu>
parents: 1497
diff changeset
    93
//
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    94
// 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
    95
// node one.
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    96
//
1500
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
    97
  uint32_t packetSize = 1024;
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
    98
  uint32_t maxPacketCount = 1;
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
    99
  Time interPacketInterval = Seconds (1.);
3382
d5f8e5fae1c6 fix bug 234 which changes required methods on helpers to constructor parameters. Update tutorial content to reflect. Change RemoteIpv4 attribute to RemoteAddress.
Craig Dowell <craigdo@ee.washington.edu>
parents: 3381
diff changeset
   100
  UdpEchoClientHelper client (i.GetAddress (1), port);
d5f8e5fae1c6 fix bug 234 which changes required methods on helpers to constructor parameters. Update tutorial content to reflect. Change RemoteIpv4 attribute to RemoteAddress.
Craig Dowell <craigdo@ee.washington.edu>
parents: 3381
diff changeset
   101
  client.SetAttribute ("MaxPackets", UintegerValue (maxPacketCount));
d5f8e5fae1c6 fix bug 234 which changes required methods on helpers to constructor parameters. Update tutorial content to reflect. Change RemoteIpv4 attribute to RemoteAddress.
Craig Dowell <craigdo@ee.washington.edu>
parents: 3381
diff changeset
   102
  client.SetAttribute ("Interval", TimeValue (interPacketInterval));
d5f8e5fae1c6 fix bug 234 which changes required methods on helpers to constructor parameters. Update tutorial content to reflect. Change RemoteIpv4 attribute to RemoteAddress.
Craig Dowell <craigdo@ee.washington.edu>
parents: 3381
diff changeset
   103
  client.SetAttribute ("PacketSize", UintegerValue (packetSize));
2887
9a637e6daee0 Build -> Install
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2838
diff changeset
   104
  apps = client.Install (n.Get (0));
2768
fcb615f2c57c port to helper API
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2600
diff changeset
   105
  apps.Start (Seconds (2.0));
fcb615f2c57c port to helper API
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2600
diff changeset
   106
  apps.Stop (Seconds (10.0));
1500
895ed42278d3 finish up basic echo apps
Craig Dowell <craigdo@ee.washington.edu>
parents: 1499
diff changeset
   107
4577
84c133267507 Allow UDP echo client to specify packet contents
Craig Dowell <craigdo@ee.washington.edu>
parents: 4264
diff changeset
   108
#if 0
84c133267507 Allow UDP echo client to specify packet contents
Craig Dowell <craigdo@ee.washington.edu>
parents: 4264
diff changeset
   109
//
84c133267507 Allow UDP echo client to specify packet contents
Craig Dowell <craigdo@ee.washington.edu>
parents: 4264
diff changeset
   110
// Users may find it convenient to initialize echo packets with actual data;
84c133267507 Allow UDP echo client to specify packet contents
Craig Dowell <craigdo@ee.washington.edu>
parents: 4264
diff changeset
   111
// the below lines suggest how to do this
84c133267507 Allow UDP echo client to specify packet contents
Craig Dowell <craigdo@ee.washington.edu>
parents: 4264
diff changeset
   112
//
84c133267507 Allow UDP echo client to specify packet contents
Craig Dowell <craigdo@ee.washington.edu>
parents: 4264
diff changeset
   113
  client.SetFill(apps.Get (0), "Hello World");
84c133267507 Allow UDP echo client to specify packet contents
Craig Dowell <craigdo@ee.washington.edu>
parents: 4264
diff changeset
   114
84c133267507 Allow UDP echo client to specify packet contents
Craig Dowell <craigdo@ee.washington.edu>
parents: 4264
diff changeset
   115
  client.SetFill(apps.Get (0), 0xa5, 1024);
84c133267507 Allow UDP echo client to specify packet contents
Craig Dowell <craigdo@ee.washington.edu>
parents: 4264
diff changeset
   116
84c133267507 Allow UDP echo client to specify packet contents
Craig Dowell <craigdo@ee.washington.edu>
parents: 4264
diff changeset
   117
  uint8_t fill[] = {0, 1, 2, 3, 4, 5, 6};
84c133267507 Allow UDP echo client to specify packet contents
Craig Dowell <craigdo@ee.washington.edu>
parents: 4264
diff changeset
   118
  client.SetFill(apps.Get (0), fill, sizeof(fill), 1024);
84c133267507 Allow UDP echo client to specify packet contents
Craig Dowell <craigdo@ee.washington.edu>
parents: 4264
diff changeset
   119
#endif
84c133267507 Allow UDP echo client to specify packet contents
Craig Dowell <craigdo@ee.washington.edu>
parents: 4264
diff changeset
   120
2789
47e92324cf4b configure tracing post-topology construction, not before.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2788
diff changeset
   121
  std::ofstream ascii;
47e92324cf4b configure tracing post-topology construction, not before.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2788
diff changeset
   122
  ascii.open ("udp-echo.tr");
4264
9d2e96c4e6e4 Piles of changes for tracing rework
Craig Dowell <craigdo@ee.washington.edu>
parents: 3629
diff changeset
   123
  CsmaHelper::EnablePcapAll ("udp-echo", false);
2996
a83b94e277d4 EnablePcap()->EnablePcapAll(); EnableAscii()->EnableAsciiAll()
Tom Henderson <tomh@tomh.org>
parents: 2888
diff changeset
   124
  CsmaHelper::EnableAsciiAll (ascii);
2789
47e92324cf4b configure tracing post-topology construction, not before.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2788
diff changeset
   125
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   126
//
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   127
// Now, do the actual simulation.
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   128
//
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
   129
  NS_LOG_INFO ("Run Simulation.");
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   130
  Simulator::Run ();
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   131
  Simulator::Destroy ();
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1502
diff changeset
   132
  NS_LOG_INFO ("Done.");
1496
0ad4c18b475c start of udp echo applications
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   133
}