tutorial/tutorial-csma-echo-pcap-trace.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Thu, 13 Mar 2008 11:10:38 -0700
changeset 2600 6c389d0c717d
parent 2592 3ebf97150166
child 2816 23c5900c3fdc
permissions -rw-r--r--
add Application::SetNode and NetDevice::SetNode, use them from Node::AddApplication and Node::AddDevice. kill useless "Node" attributes.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1529
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     2
/*
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     3
 * This program is free software; you can redistribute it and/or modify
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     4
 * it under the terms of the GNU General Public License version 2 as
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     5
 * published by the Free Software Foundation;
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     6
 *
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     7
 * This program is distributed in the hope that it will be useful,
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    10
 * GNU General Public License for more details.
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    11
 *
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    12
 * You should have received a copy of the GNU General Public License
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    13
 * along with this program; if not, write to the Free Software
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    15
 */
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    16
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    17
#include "ns3/log.h"
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    18
#include "ns3/ptr.h"
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    19
#include "ns3/internet-node.h"
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    20
#include "ns3/csma-channel.h"
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    21
#include "ns3/mac48-address.h"
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    22
#include "ns3/csma-net-device.h"
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    23
#include "ns3/csma-topology.h"
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    24
#include "ns3/csma-ipv4-topology.h"
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    25
#include "ns3/udp-echo-client.h"
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    26
#include "ns3/udp-echo-server.h"
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    27
#include "ns3/simulator.h"
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    28
#include "ns3/nstime.h"
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    29
#include "ns3/ascii-trace.h"
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    30
#include "ns3/pcap-trace.h"
2494
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2311
diff changeset
    31
#include "ns3/inet-socket-address.h"
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2311
diff changeset
    32
#include "ns3/uinteger.h"
1529
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    33
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    34
NS_LOG_COMPONENT_DEFINE ("UdpEchoSimulation");
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    35
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    36
using namespace ns3;
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    37
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    38
int 
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    39
main (int argc, char *argv[])
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    40
{
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    41
  LogComponentEnable ("UdpEchoSimulation", LOG_LEVEL_INFO);
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    42
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    43
  NS_LOG_INFO ("UDP Echo Simulation");
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    44
2230
9f13ac3291e0 add CreateObject<> to instanciate subclasses of the Object base class. Replaces Create<>.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1539
diff changeset
    45
  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: 1539
diff changeset
    46
  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: 1539
diff changeset
    47
  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: 1539
diff changeset
    48
  Ptr<Node> n3 = CreateObject<InternetNode> ();
1529
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    49
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    50
  Ptr<CsmaChannel> lan = 
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    51
    CsmaTopology::CreateCsmaChannel (DataRate (5000000), MilliSeconds (2));
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    52
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    53
  uint32_t nd0 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n0, lan, 
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    54
    "08:00:2e:00:00:00");
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    55
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    56
  uint32_t nd1 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n1, lan, 
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    57
    "08:00:2e:00:00:01");
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    58
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    59
  uint32_t nd2 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n2, lan, 
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    60
    "08:00:2e:00:00:02");
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    61
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    62
  uint32_t nd3 = CsmaIpv4Topology::AddIpv4CsmaNetDevice (n3, lan, 
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    63
    "08:00:2e:00:00:03");
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    64
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    65
  CsmaIpv4Topology::AddIpv4Address (n0, nd0, "10.1.1.1", "255.255.255.0");
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    66
  CsmaIpv4Topology::AddIpv4Address (n1, nd1, "10.1.1.2", "255.255.255.0");
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    67
  CsmaIpv4Topology::AddIpv4Address (n2, nd2, "10.1.1.3", "255.255.255.0");
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    68
  CsmaIpv4Topology::AddIpv4Address (n3, nd3, "10.1.1.4", "255.255.255.0");
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    69
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    70
  uint16_t port = 7;
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    71
2494
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2311
diff changeset
    72
  Ptr<UdpEchoClient> client = 
2600
6c389d0c717d add Application::SetNode and NetDevice::SetNode, use them from Node::AddApplication and Node::AddDevice. kill useless "Node" attributes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2592
diff changeset
    73
    CreateObject<UdpEchoClient> ("RemoteIpv4", Ipv4Address ("10.1.1.2"),
6c389d0c717d add Application::SetNode and NetDevice::SetNode, use them from Node::AddApplication and Node::AddDevice. kill useless "Node" attributes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2592
diff changeset
    74
                                 "RemotePort", Uinteger (port), 
6c389d0c717d add Application::SetNode and NetDevice::SetNode, use them from Node::AddApplication and Node::AddDevice. kill useless "Node" attributes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2592
diff changeset
    75
                                 "MaxPackets", Uinteger (1), 
6c389d0c717d add Application::SetNode and NetDevice::SetNode, use them from Node::AddApplication and Node::AddDevice. kill useless "Node" attributes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2592
diff changeset
    76
                                 "Interval", Seconds(1.), 
6c389d0c717d add Application::SetNode and NetDevice::SetNode, use them from Node::AddApplication and Node::AddDevice. kill useless "Node" attributes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2592
diff changeset
    77
                                 "PacketSize", Uinteger (1024));
2494
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2311
diff changeset
    78
  n0->AddApplication (client);
1529
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    79
2494
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2311
diff changeset
    80
  Ptr<UdpEchoServer> server = 
2600
6c389d0c717d add Application::SetNode and NetDevice::SetNode, use them from Node::AddApplication and Node::AddDevice. kill useless "Node" attributes.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2592
diff changeset
    81
    CreateObject<UdpEchoServer> ("Port", Uinteger (port));
2494
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2311
diff changeset
    82
  n1->AddApplication (server);
1529
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    83
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    84
  server->Start(Seconds(1.));
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    85
  client->Start(Seconds(2.));
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    86
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    87
  server->Stop (Seconds(10.));
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    88
  client->Stop (Seconds(10.));
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    89
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    90
  AsciiTrace asciitrace ("tutorial.tr");
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    91
  asciitrace.TraceAllQueues ();
2221
8a6f9a7d8c48 Split tutorial.texi into multiple files; some work on the tracing section
Tom Henderson <tomh@tomh.org>
parents: 1539
diff changeset
    92
#if 0
1529
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    93
  asciitrace.TraceAllNetDeviceRx ();
2221
8a6f9a7d8c48 Split tutorial.texi into multiple files; some work on the tracing section
Tom Henderson <tomh@tomh.org>
parents: 1539
diff changeset
    94
#endif
1529
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    95
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    96
  PcapTrace pcaptrace ("tutorial.pcap");
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    97
  pcaptrace.TraceAllIp ();
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    98
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    99
  Simulator::Run ();
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   100
  Simulator::Destroy ();
042ae54d795c tutorial files
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   101
}