src/csma/examples/csma-ping.cc
author Mitch Watrous
Mon, 13 Aug 2012 16:08:13 -0700
changeset 8966 060dba23e9bb
parent 7170 8417cff6e353
child 9000 3ec20a64fd08
permissions -rw-r--r--
Replace src/application usage of RandomVariable with RandomVariableStream
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"
6823
a27f86fb4e55 Merge node and common modules into new network module
Tom Henderson <tomh@tomh.org>
parents: 6821
diff changeset
    32
#include "ns3/network-module.h"
6848
1f453ad50ef3 Converts csma, emu, tap-bridge, point-to-point, wifi and wimax modules into modular format
Lalith Suresh <suresh.lalith@gmail.com>
parents: 6847
diff changeset
    33
#include "ns3/csma-module.h"
6847
138f00c56381 Move applications to a single module
Mitch Watrous <watrous@u.washington.edu>
parents: 6845
diff changeset
    34
#include "ns3/applications-module.h"
7089
ebe626d82692 Bug 1105 Move topology helpers into separate per-device modules
John Abraham<john.abraham@gatech.edu>
parents: 6865
diff changeset
    35
#include "ns3/internet-module.h"
3821
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    36
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    37
using namespace ns3;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    38
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    39
NS_LOG_COMPONENT_DEFINE ("CsmaPingExample");
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    40
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    41
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
    42
{
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    43
  //std::cout << *p << std::endl;
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
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    46
static void PingRtt (std::string context, Time rtt)
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    47
{
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    48
  //std::cout << context << " " << rtt << std::endl;
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
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    51
int
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    52
main (int argc, char *argv[])
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
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    55
  CommandLine cmd;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    56
  cmd.Parse (argc, argv);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    57
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    58
  // Here, we will explicitly create four nodes.
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    59
  NS_LOG_INFO ("Create nodes.");
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    60
  NodeContainer c;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    61
  c.Create (4);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    62
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    63
  // connect all our nodes to a shared channel.
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    64
  NS_LOG_INFO ("Build Topology.");
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    65
  CsmaHelper csma;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    66
  csma.SetChannelAttribute ("DataRate", DataRateValue (DataRate (5000000)));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    67
  csma.SetChannelAttribute ("Delay", TimeValue (MilliSeconds (2)));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    68
  csma.SetDeviceAttribute ("EncapsulationMode", StringValue ("Llc"));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    69
  NetDeviceContainer devs = csma.Install (c);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    70
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    71
  // add an ip stack to all nodes.
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    72
  NS_LOG_INFO ("Add ip stack.");
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    73
  InternetStackHelper ipStack;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    74
  ipStack.Install (c);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    75
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    76
  // assign ip addresses
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    77
  NS_LOG_INFO ("Assign ip addresses.");
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    78
  Ipv4AddressHelper ip;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    79
  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
    80
  Ipv4InterfaceContainer addresses = ip.Assign (devs);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    81
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    82
  NS_LOG_INFO ("Create Source");
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    83
  Config::SetDefault ("ns3::Ipv4RawSocketImpl::Protocol", StringValue ("2"));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    84
  InetSocketAddress dst = InetSocketAddress (addresses.GetAddress (3));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    85
  OnOffHelper onoff = OnOffHelper ("ns3::Ipv4RawSocketFactory", dst);
8966
060dba23e9bb Replace src/application usage of RandomVariable with RandomVariableStream
Mitch Watrous
parents: 7170
diff changeset
    86
  onoff.SetAttribute ("OnTime", StringValue ("ns3::ConstantRandomVariable[Constant=1]"));
060dba23e9bb Replace src/application usage of RandomVariable with RandomVariableStream
Mitch Watrous
parents: 7170
diff changeset
    87
  onoff.SetAttribute ("OffTime", StringValue ("ns3::ConstantRandomVariable[Constant=0]"));
3821
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    88
  onoff.SetAttribute ("DataRate", DataRateValue (DataRate (15000)));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    89
  onoff.SetAttribute ("PacketSize", UintegerValue (1200));
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
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    92
  ApplicationContainer apps = onoff.Install (c.Get (0));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    93
  apps.Start (Seconds (1.0));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    94
  apps.Stop (Seconds (10.0));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    95
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    96
  NS_LOG_INFO ("Create Sink.");
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    97
  PacketSinkHelper sink = PacketSinkHelper ("ns3::Ipv4RawSocketFactory", dst);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    98
  apps = sink.Install (c.Get (3));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    99
  apps.Start (Seconds (0.0));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   100
  apps.Stop (Seconds (11.0));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   101
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   102
  NS_LOG_INFO ("Create pinger");
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   103
  V4PingHelper ping = V4PingHelper (addresses.GetAddress (2));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   104
  NodeContainer pingers;
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   105
  pingers.Add (c.Get (0));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   106
  pingers.Add (c.Get (1));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   107
  pingers.Add (c.Get (3));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   108
  apps = ping.Install (pingers);
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   109
  apps.Start (Seconds (2.0));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   110
  apps.Stop (Seconds (5.0));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   111
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   112
  NS_LOG_INFO ("Configure Tracing.");
4264
9d2e96c4e6e4 Piles of changes for tracing rework
Craig Dowell <craigdo@ee.washington.edu>
parents: 4225
diff changeset
   113
  // first, pcap tracing in non-promiscuous mode
9d2e96c4e6e4 Piles of changes for tracing rework
Craig Dowell <craigdo@ee.washington.edu>
parents: 4225
diff changeset
   114
  csma.EnablePcapAll ("csma-ping", false);
9d2e96c4e6e4 Piles of changes for tracing rework
Craig Dowell <craigdo@ee.washington.edu>
parents: 4225
diff changeset
   115
3821
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   116
  // then, print what the packet sink receives.
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   117
  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
   118
                                 MakeCallback (&SinkRx));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   119
  // finally, print the ping rtts.
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   120
  Config::Connect ("/NodeList/*/ApplicationList/*/$ns3::V4Ping/Rtt",
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   121
                   MakeCallback (&PingRtt));
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   122
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   123
  Packet::EnablePrinting ();
7170
8417cff6e353 csma coding style changes
Josh Pelkey <jpelkey@gatech.edu>
parents: 7089
diff changeset
   124
3821
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   125
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   126
  NS_LOG_INFO ("Run Simulation.");
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   127
  Simulator::Run ();
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   128
  Simulator::Destroy ();
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   129
  NS_LOG_INFO ("Done.");
9ea825f066a2 Apply icmp-application patch frm lacage
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   130
}