examples/csma/csma-ping.cc
author Tom Henderson <tomh@tomh.org>
Fri, 18 Feb 2011 16:05:39 -0800
changeset 6821 203367ae7433
parent 5988 80584c404aba
child 6823 a27f86fb4e55
permissions -rw-r--r--
merge src/simulator into src/core; move src/core to new module layout
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3821
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     2
/*
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     3
 * This program is free software; you can redistribute it and/or modify
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     4
 * it under the terms of the GNU General Public License version 2 as
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     5
 * published by the Free Software Foundation;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     6
 *
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     7
 * This program is distributed in the hope that it will be useful,
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    10
 * GNU General Public License for more details.
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    11
 *
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    12
 * You should have received a copy of the GNU General Public License
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    13
 * along with this program; if not, write to the Free Software
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    15
 */
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    16
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    17
// Network topology
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    18
//
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    19
//       n0    n1   n2   n3
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    20
//       |     |    |    |
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    21
//     =====================
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    22
//
5988
80584c404aba Clean up some stale comments in examples
Tom Henderson <tomh@tomh.org>
parents: 5369
diff changeset
    23
//  node n0,n1,n3 pings to node n2
80584c404aba Clean up some stale comments in examples
Tom Henderson <tomh@tomh.org>
parents: 5369
diff changeset
    24
//  node n0 generates protocol 2 (IGMP) to node n3
3821
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    25
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    26
#include <iostream>
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    27
#include <fstream>
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    28
#include <string>
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    29
#include <cassert>
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    30
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    31
#include "ns3/core-module.h"
6821
203367ae7433 merge src/simulator into src/core; move src/core to new module layout
Tom Henderson <tomh@tomh.org>
parents: 5988
diff changeset
    32
#include "ns3/core-module.h"
3821
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    33
#include "ns3/node-module.h"
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    34
#include "ns3/helper-module.h"
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    35
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    36
using namespace ns3;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    37
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    38
NS_LOG_COMPONENT_DEFINE ("CsmaPingExample");
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    39
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    40
static void SinkRx (Ptr<const Packet> p, const Address &ad)
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    41
{
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    42
  //std::cout << *p << std::endl;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    43
}
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    44
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    45
static void PingRtt (std::string context, Time rtt)
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    46
{
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    47
  //std::cout << context << " " << rtt << std::endl;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    48
}
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    49
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    50
int
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    51
main (int argc, char *argv[])
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    52
{
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    53
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    54
  CommandLine cmd;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    55
  cmd.Parse (argc, argv);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    56
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    57
  // Here, we will explicitly create four nodes.
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    58
  NS_LOG_INFO ("Create nodes.");
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    59
  NodeContainer c;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    60
  c.Create (4);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    61
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    62
  // connect all our nodes to a shared channel.
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    63
  NS_LOG_INFO ("Build Topology.");
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    64
  CsmaHelper csma;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    65
  csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    66
  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    67
  csma.SetDeviceAttribute ("EncapsulationMode", StringValue ("Llc"));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    68
  NetDeviceContainer devs = csma.Install (c);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    69
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    70
  // add an ip stack to all nodes.
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    71
  NS_LOG_INFO ("Add ip stack.");
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    72
  InternetStackHelper ipStack;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    73
  ipStack.Install (c);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    74
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    75
  // assign ip addresses
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    76
  NS_LOG_INFO ("Assign ip addresses.");
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    77
  Ipv4AddressHelper ip;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    78
  ip.SetBase ("192.168.1.0", "255.255.255.0");
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    79
  Ipv4InterfaceContainer addresses = ip.Assign (devs);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    80
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    81
  NS_LOG_INFO ("Create Source");
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    82
  Config::SetDefault ("ns3::Ipv4RawSocketImpl::Protocol", StringValue ("2"));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    83
  InetSocketAddress dst = InetSocketAddress (addresses.GetAddress (3));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    84
  OnOffHelper onoff = OnOffHelper ("ns3::Ipv4RawSocketFactory", dst);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    85
  onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1.0)));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    86
  onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0.0)));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    87
  onoff.SetAttribute ("DataRate", DataRateValue (DataRate (15000)));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    88
  onoff.SetAttribute ("PacketSize", UintegerValue (1200));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    89
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    90
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    91
  ApplicationContainer apps = onoff.Install (c.Get (0));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    92
  apps.Start (Seconds (1.0));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    93
  apps.Stop (Seconds (10.0));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    94
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    95
  NS_LOG_INFO ("Create Sink.");
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    96
  PacketSinkHelper sink = PacketSinkHelper ("ns3::Ipv4RawSocketFactory", dst);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    97
  apps = sink.Install (c.Get (3));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    98
  apps.Start (Seconds (0.0));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    99
  apps.Stop (Seconds (11.0));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   100
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   101
  NS_LOG_INFO ("Create pinger");
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   102
  V4PingHelper ping = V4PingHelper (addresses.GetAddress (2));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   103
  NodeContainer pingers;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   104
  pingers.Add (c.Get (0));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   105
  pingers.Add (c.Get (1));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   106
  pingers.Add (c.Get (3));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   107
  apps = ping.Install (pingers);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   108
  apps.Start (Seconds (2.0));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   109
  apps.Stop (Seconds (5.0));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   110
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   111
  NS_LOG_INFO ("Configure Tracing.");
4264
9d2e96c4e6e4 Piles of changes for tracing rework
Craig Dowell <craigdo@ee.washington.edu>
parents: 4225
diff changeset
   112
  // first, pcap tracing in non-promiscuous mode
9d2e96c4e6e4 Piles of changes for tracing rework
Craig Dowell <craigdo@ee.washington.edu>
parents: 4225
diff changeset
   113
  csma.EnablePcapAll ("csma-ping", false);
9d2e96c4e6e4 Piles of changes for tracing rework
Craig Dowell <craigdo@ee.washington.edu>
parents: 4225
diff changeset
   114
3821
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   115
  // then, print what the packet sink receives.
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   116
  Config::ConnectWithoutContext ("/NodeList/3/ApplicationList/0/$ns3::PacketSink/Rx", 
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   117
                                 MakeCallback (&SinkRx));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   118
  // finally, print the ping rtts.
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   119
  Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::V4Ping/Rtt",
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   120
                   MakeCallback (&PingRtt));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   121
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   122
  Packet::EnablePrinting ();
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   123
  
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   124
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   125
  NS_LOG_INFO ("Run Simulation.");
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   126
  Simulator::Run ();
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   127
  Simulator::Destroy ();
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   128
  NS_LOG_INFO ("Done.");
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   129
}