examples/csma-cd-packet-socket.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Wed, 01 Aug 2007 10:29:40 +0200
changeset 1193 ea2185e4e097
parent 1190 examples/csma-cd-one-subnet.cc@ee1f9e701753
child 1195 53f1175dbe94
permissions -rw-r--r--
example code.
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 
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    29
// - Tracing of queues and packet receptions to file "csma-cd-one-subnet.tr"
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
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    36
#include "ns3/command-line.h"
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    37
#include "ns3/default-value.h"
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    38
#include "ns3/ptr.h"
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    39
#include "ns3/random-variable.h"
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    40
#include "ns3/debug.h"
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    41
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    42
#include "ns3/simulator.h"
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    43
#include "ns3/nstime.h"
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    44
#include "ns3/data-rate.h"
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    45
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    46
#include "ns3/ascii-trace.h"
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    47
#include "ns3/pcap-trace.h"
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    48
#include "ns3/internet-node.h"
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    49
#include "ns3/csma-cd-channel.h"
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    50
#include "ns3/csma-cd-net-device.h"
1167
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 991
diff changeset
    51
#include "ns3/eui48-address.h"
1193
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    52
#include "ns3/packet-socket-address.h"
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    53
#include "ns3/socket.h"
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    54
#include "ns3/onoff-application.h"
1193
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    55
#include "ns3/queue.h"
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    56
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    57
using namespace ns3;
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    58
1193
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    59
static Ptr<CsmaCdNetDevice>
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    60
CreateCsmaCdDevice (Ptr<Node> node, Ptr<CsmaCdChannel> channel)
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    61
{
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    62
  Ptr<CsmaCdNetDevice> device = Create<CsmaCdNetDevice> (node);
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    63
  device->Attach (channel);
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    64
  Ptr<Queue> queue = Queue::CreateDefault ();
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    65
  device->AddQueue (queue);
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    66
  return device;
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    67
}
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    68
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    69
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    70
int main (int argc, char *argv[])
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    71
{
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    72
  CommandLine::Parse (argc, argv);
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    73
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    74
  // Here, we will explicitly create four nodes.  In more sophisticated
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    75
  // topologies, we could configure a node factory.
1193
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    76
  Ptr<Node> n0 = Create<Node> ();
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    77
  Ptr<Node> n1 = Create<Node> (); 
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    78
  Ptr<Node> n2 = Create<Node> (); 
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    79
  Ptr<Node> n3 = Create<Node> ();
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    80
1193
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    81
  // create the shared medium used by all csma/cd devices.
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    82
  Ptr<CsmaCdChannel> channel = Create<CsmaCdChannel> (DataRate(5000000), MilliSeconds(2));
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    83
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    84
  // use a helper function to connect our nodes to the shared channel.
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    85
  Ptr<NetDevice> n0If = CreateCsmaCdDevice (n0, channel);
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    86
  Ptr<NetDevice> n1If = CreateCsmaCdDevice (n1, channel);
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    87
  Ptr<NetDevice> n2If = CreateCsmaCdDevice (n2, channel);
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    88
  Ptr<NetDevice> n3If = CreateCsmaCdDevice (n3, channel);
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    89
1193
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    90
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    91
  // create the address which identifies n1 from n0
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    92
  PacketSocketAddress n0ToN1;
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    93
  n0ToN1.SetDevice (n0If->GetIfIndex ());      // set outgoing interface for outgoing packets
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    94
  n0ToN1.SetPhysicalAddress (n1If->GetAddress ()); // set destination address for outgoing packets
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    95
  n0ToN1.SetProtocol (2);            // set arbitrary protocol for outgoing packets
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    96
1193
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    97
  // create the address which identifies n0 from n3
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    98
  PacketSocketAddress n3ToN0;
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
    99
  n3ToN0.SetDevice (n3If->GetIfIndex ());
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
   100
  n3ToN0.SetPhysicalAddress (n0If->GetAddress ());
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
   101
  n3ToN0.SetProtocol (3);
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   102
  
1193
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
   103
  // Create the OnOff application to send raw datagrams of size
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   104
  // 210 bytes at a rate of 448 Kb/s
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   105
  // from n0 to n1
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   106
  Ptr<OnOffApplication> ooff = Create<OnOffApplication> (
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   107
    n0, 
1193
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
   108
    n0ToN1.ConvertTo (),
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
   109
    "Packet",
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   110
    ConstantVariable(1), 
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   111
    ConstantVariable(0));
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   112
  // Start the application
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   113
  ooff->Start(Seconds(1.0));
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   114
  ooff->Stop (Seconds(10.0));
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   115
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   116
  // Create a similar flow from n3 to n0, starting at time 1.1 seconds
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   117
  ooff = Create<OnOffApplication> (
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   118
    n3, 
1193
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
   119
    n3ToN0.ConvertTo (),
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
   120
    "Packet",
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   121
    ConstantVariable(1), 
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   122
    ConstantVariable(0));
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   123
  // Start the application
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   124
  ooff->Start(Seconds(1.1));
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   125
  ooff->Stop (Seconds(10.0));
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   126
 
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   127
  // Configure tracing of all enqueue, dequeue, and NetDevice receive events
1193
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
   128
  // Trace output will be sent to the csma-cd-packet-socket.tr file
ea2185e4e097 example code.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1190
diff changeset
   129
  AsciiTrace asciitrace ("csma-cd-packet-socket.tr");
991
e05514a93564 fix tracing
Tom Henderson <tomh@tomh.org>
parents: 990
diff changeset
   130
  asciitrace.TraceAllNetDeviceRx ();
e05514a93564 fix tracing
Tom Henderson <tomh@tomh.org>
parents: 990
diff changeset
   131
  asciitrace.TraceAllQueues ();
e05514a93564 fix tracing
Tom Henderson <tomh@tomh.org>
parents: 990
diff changeset
   132
987
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   133
  Simulator::Run ();
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   134
    
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   135
  Simulator::Destroy ();
3f469377ff30 new script for csma-cd
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   136
}