examples/csma-packet-socket.cc
author Tom Henderson <tomh@tomh.org>
Sun, 25 May 2008 08:05:26 -0700
changeset 3140 a6d8f8880ea9
parent 3026 4ef834f6b67d
child 3257 ba198dad54a2
permissions -rw-r--r--
branch merge
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"
2890
172baa5960ff rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents: 2887
diff changeset
    38
#include "ns3/node-module.h"
2739
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    39
#include "ns3/helper-module.h"
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    40
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    41
using namespace ns3;
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    42
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    43
NS_LOG_COMPONENT_DEFINE ("CsmaPacketSocketExample");
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    44
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    45
int
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    46
main (int argc, char *argv[])
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    47
{
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    48
#if 0 
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    49
  LogComponentEnable ("CsmaPacketSocketExample", LOG_LEVEL_INFO);
2860
9105a5cf6535 use global seed in examples, new regression tests
Craig Dowell <craigdo@ee.washington.edu>
parents: 2739
diff changeset
    50
#endif
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    51
2860
9105a5cf6535 use global seed in examples, new regression tests
Craig Dowell <craigdo@ee.washington.edu>
parents: 2739
diff changeset
    52
  //
9105a5cf6535 use global seed in examples, new regression tests
Craig Dowell <craigdo@ee.washington.edu>
parents: 2739
diff changeset
    53
  // Make the random number generators generate reproducible results.
9105a5cf6535 use global seed in examples, new regression tests
Craig Dowell <craigdo@ee.washington.edu>
parents: 2739
diff changeset
    54
  //
9105a5cf6535 use global seed in examples, new regression tests
Craig Dowell <craigdo@ee.washington.edu>
parents: 2739
diff changeset
    55
  RandomVariable::UseGlobalSeed (1, 1, 2, 3, 5, 8);
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    56
2575
1aae382e65e2 rewrite CommandLine to not handle DefaultValues anymore.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2533
diff changeset
    57
  CommandLine cmd;
1aae382e65e2 rewrite CommandLine to not handle DefaultValues anymore.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2533
diff changeset
    58
  cmd.Parse (argc, argv);
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    59
2739
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    60
  // Here, we will explicitly create four nodes.
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
    61
  NS_LOG_INFO ("Create nodes.");
2739
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    62
  NodeContainer c;
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    63
  c.Create (4);
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    64
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    65
  PacketSocketHelper packetSocket;
2887
9a637e6daee0 Build -> Install
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2860
diff changeset
    66
  packetSocket.Install (c);
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    67
1272
67a2ba1143e0 rename to csma
Craig Dowell <craigdo@ee.washington.edu>
parents: 1208
diff changeset
    68
  // 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
    69
  NS_LOG_INFO ("Create channels.");
2965
4b28e9740e3b get rid of Attribute class. Use AttributeValue subclasses directly.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2890
diff changeset
    70
  Ptr<CsmaChannel> channel = CreateObject<CsmaChannel> ("BitRate", DataRateValue (DataRate(5000000)), 
4b28e9740e3b get rid of Attribute class. Use AttributeValue subclasses directly.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2890
diff changeset
    71
                                                        "Delay", TimeValue (MilliSeconds(2)));
1193
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    72
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    73
  // 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
    74
  NS_LOG_INFO ("Build Topology.");
2739
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    75
  CsmaHelper csma;
2965
4b28e9740e3b get rid of Attribute class. Use AttributeValue subclasses directly.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2890
diff changeset
    76
  csma.SetDeviceParameter ("EncapsulationMode", StringValue ("Llc"));
2887
9a637e6daee0 Build -> Install
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2860
diff changeset
    77
  NetDeviceContainer devs = csma.Install (c, channel);
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
  NS_LOG_INFO ("Create Applications.");
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    80
  // Create the OnOff application to send raw datagrams
2890
172baa5960ff rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents: 2887
diff changeset
    81
  PacketSocketAddress socket;
172baa5960ff rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents: 2887
diff changeset
    82
  socket.SetSingleDevice(devs.Get (0)->GetIfIndex ());
172baa5960ff rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents: 2887
diff changeset
    83
  socket.SetPhysicalAddress (devs.Get (1)->GetAddress ());
172baa5960ff rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents: 2887
diff changeset
    84
  socket.SetProtocol (2);
172baa5960ff rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents: 2887
diff changeset
    85
  OnOffHelper onoff ("ns3::PacketSocketFactory", Address (socket));
2965
4b28e9740e3b get rid of Attribute class. Use AttributeValue subclasses directly.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2890
diff changeset
    86
  onoff.SetAttribute ("OnTime", RandomVariableValue (ConstantVariable (1.0)));
4b28e9740e3b get rid of Attribute class. Use AttributeValue subclasses directly.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2890
diff changeset
    87
  onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0.0)));
2890
172baa5960ff rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents: 2887
diff changeset
    88
2887
9a637e6daee0 Build -> Install
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2860
diff changeset
    89
  ApplicationContainer apps = onoff.Install (c.Get (0));
2739
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    90
  apps.Start (Seconds (1.0));
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    91
  apps.Stop (Seconds (10.0));
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    92
  
2890
172baa5960ff rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents: 2887
diff changeset
    93
  socket.SetSingleDevice (devs.Get (3)->GetIfIndex ());
172baa5960ff rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents: 2887
diff changeset
    94
  socket.SetPhysicalAddress (devs.Get (0)->GetAddress ());
172baa5960ff rework app constructors
Craig Dowell <craigdo@ee.washington.edu>
parents: 2887
diff changeset
    95
  socket.SetProtocol (3);
2965
4b28e9740e3b get rid of Attribute class. Use AttributeValue subclasses directly.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2890
diff changeset
    96
  onoff.SetAttribute ("Remote", AddressValue (socket));
4b28e9740e3b get rid of Attribute class. Use AttributeValue subclasses directly.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2890
diff changeset
    97
  onoff.SetAttribute ("OffTime", RandomVariableValue (ConstantVariable (0.0)));
2887
9a637e6daee0 Build -> Install
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2860
diff changeset
    98
  apps = onoff.Install (c.Get (3));
2739
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
    99
  apps.Start (Seconds (1.0));
5234783968ff convert PacketSocket scripts to PacketSocketHelper.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2602
diff changeset
   100
  apps.Stop (Seconds (10.0));
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   101
 
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   102
  // 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
   103
  // 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
   104
  NS_LOG_INFO ("Configure Tracing.");
3026
4ef834f6b67d remove dead code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2965
diff changeset
   105
  std::ofstream os;
4ef834f6b67d remove dead code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2965
diff changeset
   106
  os.open ("csma-packet-socket.tr");
4ef834f6b67d remove dead code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2965
diff changeset
   107
  csma.EnableAsciiAll (os);
991
e05514a93564 fix tracing
Tom Henderson <tomh@tomh.org>
parents: 990
diff changeset
   108
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
   109
  NS_LOG_INFO ("Run Simulation.");
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   110
  Simulator::Run ();
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   111
  Simulator::Destroy ();
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1494
diff changeset
   112
  NS_LOG_INFO ("Done.");
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   113
}