examples/csma-packet-socket.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Tue, 25 Mar 2008 14:16:40 -0700
changeset 2739 5234783968ff
parent 2602 d9262bff6df2
child 2860 9105a5cf6535
permissions -rw-r--r--
convert PacketSocket scripts to PacketSocketHelper.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     2
/*
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     3
 * This program is free software; you can redistribute it and/or modify
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     4
 * it under the terms of the GNU General Public License version 2 as
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     5
 * published by the Free Software Foundation;
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     6
 *
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     7
 * This program is distributed in the hope that it will be useful,
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    10
 * GNU General Public License for more details.
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    11
 *
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    12
 * You should have received a copy of the GNU General Public License
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    13
 * along with this program; if not, write to the Free Software
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    15
 */
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    16
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    17
// Port of ns-2/tcl/ex/simple.tcl to ns-3
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    18
//
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    19
// Network topology
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    20
//
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    21
//       n0    n1   n2   n3
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    22
//       |     |    |    |
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    23
//     =====================
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    24
//
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    25
// - CBR/UDP flows from n0 to n1, and from n3 to n0
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    26
// - UDP packet size of 210 bytes, with per-packet interval 0.00375 sec.
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    27
//   (i.e., DataRate of 448,000 bps)
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    28
// - DropTail queues 
1272
67a2ba1143e0 rename to csma
Craig Dowell <craigdo@ee.washington.edu>
parents: 1208
diff changeset
    29
// - Tracing of queues and packet receptions to file "csma-one-subnet.tr"
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    30
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    31
#include <iostream>
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    32
#include <fstream>
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    33
#include <string>
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    34
#include <cassert>
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    35
2739
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    36
#include "ns3/core-module.h"
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    37
#include "ns3/simulator-module.h"
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    38
#include "ns3/helper-module.h"
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    39
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    40
#include "ns3/ascii-trace.h"
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    41
#include "ns3/pcap-trace.h"
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    42
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    43
using namespace ns3;
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    44
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    45
NS_LOG_COMPONENT_DEFINE ("CsmaPacketSocketExample");
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    46
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    47
int
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    48
main (int argc, char *argv[])
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    49
{
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    50
#if 0 
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    51
  LogComponentEnable ("CsmaPacketSocketExample", LOG_LEVEL_INFO);
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    52
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    53
  LogComponentEnable("Object", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    54
  LogComponentEnable("Queue", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    55
  LogComponentEnable("DropTailQueue", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    56
  LogComponentEnable("Channel", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    57
  LogComponentEnable("CsmaChannel", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    58
  LogComponentEnable("NetDevice", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    59
  LogComponentEnable("CsmaNetDevice", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    60
  LogComponentEnable("Ipv4L3Protocol", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    61
  LogComponentEnable("PacketSocket", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    62
  LogComponentEnable("Socket", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    63
  LogComponentEnable("UdpSocket", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    64
  LogComponentEnable("UdpL4Protocol", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    65
  LogComponentEnable("Ipv4L3Protocol", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    66
  LogComponentEnable("Ipv4StaticRouting", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    67
  LogComponentEnable("Ipv4Interface", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    68
  LogComponentEnable("ArpIpv4Interface", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    69
  LogComponentEnable("Ipv4LoopbackInterface", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    70
  LogComponentEnable("OnOffApplication", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    71
  LogComponentEnable("PacketSinkApplication", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    72
  LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    73
  LogComponentEnable("UdpEchoServerApplication", LOG_LEVEL_ALL);
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    74
#endif
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    75
2575
1aae382e65e2 rewrite CommandLine to not handle DefaultValues anymore.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2533
diff changeset
    76
  CommandLine cmd;
1aae382e65e2 rewrite CommandLine to not handle DefaultValues anymore.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2533
diff changeset
    77
  cmd.Parse (argc, argv);
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    78
2739
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    79
  // Here, we will explicitly create four nodes.
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    80
  NS_LOG_INFO ("Create nodes.");
2739
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    81
  NodeContainer c;
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    82
  c.Create (4);
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    83
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    84
  PacketSocketHelper packetSocket;
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    85
  packetSocket.Build (c);
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    86
1272
67a2ba1143e0 rename to csma
Craig Dowell <craigdo@ee.washington.edu>
parents: 1208
diff changeset
    87
  // create the shared medium used by all csma devices.
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    88
  NS_LOG_INFO ("Create channels.");
2592
3ebf97150166 get rid of CreateObjectWith
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2577
diff changeset
    89
  Ptr<CsmaChannel> channel = CreateObject<CsmaChannel> ("BitRate", DataRate(5000000), 
3ebf97150166 get rid of CreateObjectWith
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2577
diff changeset
    90
                                                        "Delay", MilliSeconds(2));
1193
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    91
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    92
  // use a helper function to connect our nodes to the shared channel.
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    93
  NS_LOG_INFO ("Build Topology.");
2739
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    94
  CsmaHelper csma;
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    95
  csma.SetDeviceParameter ("EncapsulationMode", String ("Llc"));
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    96
  NetDeviceContainer devs = csma.Build (c, channel);
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    97
2739
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    98
  NS_LOG_INFO ("Create Applications.");
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    99
  // Create the OnOff application to send raw datagrams
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
   100
  OnOffHelper onoff;
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
   101
  onoff.SetAppAttribute ("OnTime", ConstantVariable (1.0));
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
   102
  onoff.SetAppAttribute ("OffTime", ConstantVariable (0.0));
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
   103
  onoff.SetPacketRemote (devs.Get (0), devs.Get (1)->GetAddress (), 2);
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
   104
  ApplicationContainer apps = onoff.Build (c.Get (0));
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
   105
  apps.Start (Seconds (1.0));
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
   106
  apps.Stop (Seconds (10.0));
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   107
  
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   108
2739
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
   109
  onoff.SetPacketRemote (devs.Get (3), devs.Get (0)->GetAddress (), 3);
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
   110
  apps = onoff.Build (c.Get (3));
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
   111
  apps.Start (Seconds (1.0));
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
   112
  apps.Stop (Seconds (10.0));
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   113
 
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   114
  // Configure tracing of all enqueue, dequeue, and NetDevice receive events
1272
67a2ba1143e0 rename to csma
Craig Dowell <craigdo@ee.washington.edu>
parents: 1208
diff changeset
   115
  // Trace output will be sent to the csma-packet-socket.tr file
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
   116
  NS_LOG_INFO ("Configure Tracing.");
1272
67a2ba1143e0 rename to csma
Craig Dowell <craigdo@ee.washington.edu>
parents: 1208
diff changeset
   117
  AsciiTrace asciitrace ("csma-packet-socket.tr");
991
e05514a93564 fix tracing
Tom Henderson <tomh@tomh.org>
parents: 990
diff changeset
   118
  asciitrace.TraceAllNetDeviceRx ();
e05514a93564 fix tracing
Tom Henderson <tomh@tomh.org>
parents: 990
diff changeset
   119
  asciitrace.TraceAllQueues ();
e05514a93564 fix tracing
Tom Henderson <tomh@tomh.org>
parents: 990
diff changeset
   120
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
   121
  NS_LOG_INFO ("Run Simulation.");
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   122
  Simulator::Run ();
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   123
  Simulator::Destroy ();
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
   124
  NS_LOG_INFO ("Done.");
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   125
}