examples/tutorial/sixth.cc
author Tom Henderson <tomh@tomh.org>
Sat, 05 Mar 2011 11:47:37 -0800
changeset 6865 220373544a6c
parent 6848 1f453ad50ef3
child 7196 0f12b1572bca
permissions -rw-r--r--
remove src/helper module
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6020
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     1
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     2
/*
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     3
 * This program is free software; you can redistribute it and/or modify
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     4
 * it under the terms of the GNU General Public License version 2 as
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     5
 * published by the Free Software Foundation;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     6
 *
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     7
 * This program is distributed in the hope that it will be useful,
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     8
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
     9
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    10
 * GNU General Public License for more details.
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    11
 *
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    12
 * You should have received a copy of the GNU General Public License
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    13
 * along with this program; if not, write to the Free Software
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    14
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    15
 */
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    16
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    17
#include <fstream>
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    18
#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
    19
#include "ns3/network-module.h"
6834
036f9a0b9899 Rename internet-stack to internet, and organize module
Tom Henderson <tomh@tomh.org>
parents: 6823
diff changeset
    20
#include "ns3/internet-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
    21
#include "ns3/point-to-point-module.h"
6847
138f00c56381 Move applications to a single module
Mitch Watrous <watrous@u.washington.edu>
parents: 6834
diff changeset
    22
#include "ns3/applications-module.h"
6020
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    23
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    24
using namespace ns3;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    25
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    26
NS_LOG_COMPONENT_DEFINE ("SixthScriptExample");
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    27
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    28
// ===========================================================================
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    29
//
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    30
//         node 0                 node 1
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    31
//   +----------------+    +----------------+
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    32
//   |    ns-3 TCP    |    |    ns-3 TCP    |
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    33
//   +----------------+    +----------------+
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    34
//   |    10.1.1.1    |    |    10.1.1.2    |
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    35
//   +----------------+    +----------------+
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    36
//   | point-to-point |    | point-to-point |
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    37
//   +----------------+    +----------------+
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    38
//           |                     |
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    39
//           +---------------------+
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    40
//                5 Mbps, 2 ms
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    41
//
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    42
//
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    43
// We want to look at changes in the ns-3 TCP congestion window.  We need
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    44
// to crank up a flow and hook the CongestionWindow attribute on the socket
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    45
// of the sender.  Normally one would use an on-off application to generate a
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    46
// flow, but this has a couple of problems.  First, the socket of the on-off 
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    47
// application is not created until Application Start time, so we wouldn't be 
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    48
// able to hook the socket (now) at configuration time.  Second, even if we 
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    49
// could arrange a call after start time, the socket is not public so we 
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    50
// couldn't get at it.
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    51
//
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    52
// So, we can cook up a simple version of the on-off application that does what
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    53
// we want.  On the plus side we don't need all of the complexity of the on-off
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    54
// application.  On the minus side, we don't have a helper, so we have to get
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    55
// a little more involved in the details, but this is trivial.
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    56
//
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    57
// So first, we create a socket and do the trace connect on it; then we pass 
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    58
// this socket into the constructor of our simple application which we then 
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    59
// install in the source node.
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    60
// ===========================================================================
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    61
//
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    62
class MyApp : public Application 
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    63
{
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    64
public:
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    65
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    66
  MyApp ();
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    67
  virtual ~MyApp();
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    68
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    69
  void Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate);
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    70
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    71
private:
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    72
  virtual void StartApplication (void);  
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    73
  virtual void StopApplication (void);
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    74
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    75
  void ScheduleTx (void);
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    76
  void SendPacket (void);
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    77
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    78
  Ptr<Socket>     m_socket;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    79
  Address         m_peer;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    80
  uint32_t        m_packetSize;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    81
  uint32_t        m_nPackets;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    82
  DataRate        m_dataRate;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    83
  EventId         m_sendEvent;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    84
  bool            m_running;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    85
  uint32_t        m_packetsSent;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    86
};
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    87
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    88
MyApp::MyApp ()
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    89
  : m_socket (0), 
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    90
    m_peer (), 
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    91
    m_packetSize (0), 
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    92
    m_nPackets (0), 
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    93
    m_dataRate (0), 
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    94
    m_sendEvent (), 
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    95
    m_running (false), 
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    96
    m_packetsSent (0)
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    97
{
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    98
}
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
    99
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   100
MyApp::~MyApp()
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   101
{
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   102
  m_socket = 0;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   103
}
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   104
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   105
void
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   106
MyApp::Setup (Ptr<Socket> socket, Address address, uint32_t packetSize, uint32_t nPackets, DataRate dataRate)
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   107
{
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   108
  m_socket = socket;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   109
  m_peer = address;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   110
  m_packetSize = packetSize;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   111
  m_nPackets = nPackets;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   112
  m_dataRate = dataRate;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   113
}
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   114
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   115
void
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   116
MyApp::StartApplication (void)
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   117
{
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   118
  m_running = true;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   119
  m_packetsSent = 0;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   120
  m_socket->Bind ();
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   121
  m_socket->Connect (m_peer);
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   122
  SendPacket ();
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   123
}
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   124
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   125
void 
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   126
MyApp::StopApplication (void)
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   127
{
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   128
  m_running = false;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   129
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   130
  if (m_sendEvent.IsRunning ())
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   131
    {
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   132
      Simulator::Cancel (m_sendEvent);
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   133
    }
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   134
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   135
  if (m_socket)
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   136
    {
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   137
      m_socket->Close ();
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   138
    }
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   139
}
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   140
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   141
void 
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   142
MyApp::SendPacket (void)
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   143
{
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   144
  Ptr<Packet> packet = Create<Packet> (m_packetSize);
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   145
  m_socket->Send (packet);
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   146
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   147
  if (++m_packetsSent < m_nPackets)
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   148
    {
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   149
      ScheduleTx ();
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   150
    }
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   151
}
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   152
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   153
void 
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   154
MyApp::ScheduleTx (void)
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   155
{
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   156
  if (m_running)
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   157
    {
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   158
      Time tNext (Seconds (m_packetSize * 8 / static_cast<double> (m_dataRate.GetBitRate ())));
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   159
      m_sendEvent = Simulator::Schedule (tNext, &MyApp::SendPacket, this);
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   160
    }
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   161
}
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   162
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   163
static void
6053
b17ea72acee9 OutputStreamKeeper to OutputStreamWrapper
Craig Dowell <craigdo@ee.washington.edu>
parents: 6052
diff changeset
   164
CwndChange (Ptr<OutputStreamWrapper> stream, uint32_t oldCwnd, uint32_t newCwnd)
6020
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   165
{
6021
fddc1ed290f8 DLT_PPP for sixth dropped packets
Craig Dowell <craigdo@ee.washington.edu>
parents: 6020
diff changeset
   166
  NS_LOG_UNCOND (Simulator::Now ().GetSeconds () << "\t" << newCwnd);
6020
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   167
  *stream->GetStream () << Simulator::Now ().GetSeconds () << "\t" << oldCwnd << "\t" << newCwnd << std::endl;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   168
}
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   169
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   170
static void
6052
e146b9c9c7e7 PcapFileObject to PcapFileWrapper
Craig Dowell <craigdo@ee.washington.edu>
parents: 6049
diff changeset
   171
RxDrop (Ptr<PcapFileWrapper> file, Ptr<const Packet> p)
6020
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   172
{
6021
fddc1ed290f8 DLT_PPP for sixth dropped packets
Craig Dowell <craigdo@ee.washington.edu>
parents: 6020
diff changeset
   173
  NS_LOG_UNCOND ("RxDrop at " << Simulator::Now ().GetSeconds ());
6020
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   174
  file->Write(Simulator::Now(), p);
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   175
}
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   176
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   177
int 
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   178
main (int argc, char *argv[])
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   179
{
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   180
  NodeContainer nodes;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   181
  nodes.Create (2);
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   182
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   183
  PointToPointHelper pointToPoint;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   184
  pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps"));
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   185
  pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms"));
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   186
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   187
  NetDeviceContainer devices;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   188
  devices = pointToPoint.Install (nodes);
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   189
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   190
  Ptr<RateErrorModel> em = CreateObjectWithAttributes<RateErrorModel> (
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   191
    "RanVar", RandomVariableValue (UniformVariable (0., 1.)),
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   192
    "ErrorRate", DoubleValue (0.00001));
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   193
  devices.Get (1)->SetAttribute ("ReceiveErrorModel", PointerValue (em));
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   194
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   195
  InternetStackHelper stack;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   196
  stack.Install (nodes);
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   197
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   198
  Ipv4AddressHelper address;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   199
  address.SetBase ("10.1.1.0", "255.255.255.252");
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   200
  Ipv4InterfaceContainer interfaces = address.Assign (devices);
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   201
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   202
  uint16_t sinkPort = 8080;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   203
  Address sinkAddress (InetSocketAddress(interfaces.GetAddress (1), sinkPort));
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   204
  PacketSinkHelper packetSinkHelper ("ns3::TcpSocketFactory", InetSocketAddress (Ipv4Address::GetAny (), sinkPort));
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   205
  ApplicationContainer sinkApps = packetSinkHelper.Install (nodes.Get (1));
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   206
  sinkApps.Start (Seconds (0.));
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   207
  sinkApps.Stop (Seconds (20.));
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   208
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   209
  Ptr<Socket> ns3TcpSocket = Socket::CreateSocket (nodes.Get (0), TcpSocketFactory::GetTypeId ());
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   210
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   211
  Ptr<MyApp> app = CreateObject<MyApp> ();
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   212
  app->Setup (ns3TcpSocket, sinkAddress, 1040, 1000, DataRate ("1Mbps"));
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   213
  nodes.Get (0)->AddApplication (app);
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   214
  app->SetStartTime (Seconds (1.));
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   215
  app->SetStopTime (Seconds (20.));
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   216
6023
c7fbbfc5a7e8 rearrange trace code in sixth.cc for clarity
Craig Dowell <craigdo@ee.washington.edu>
parents: 6021
diff changeset
   217
  AsciiTraceHelper asciiTraceHelper;
6053
b17ea72acee9 OutputStreamKeeper to OutputStreamWrapper
Craig Dowell <craigdo@ee.washington.edu>
parents: 6052
diff changeset
   218
  Ptr<OutputStreamWrapper> stream = asciiTraceHelper.CreateFileStream ("sixth.cwnd");
6023
c7fbbfc5a7e8 rearrange trace code in sixth.cc for clarity
Craig Dowell <craigdo@ee.washington.edu>
parents: 6021
diff changeset
   219
  ns3TcpSocket->TraceConnectWithoutContext ("CongestionWindow", MakeBoundCallback (&CwndChange, stream));
c7fbbfc5a7e8 rearrange trace code in sixth.cc for clarity
Craig Dowell <craigdo@ee.washington.edu>
parents: 6021
diff changeset
   220
6020
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   221
  PcapHelper pcapHelper;
6206
9787dc9fdd84 bug 872: ns3::PcapFileWrapper::Write explodes stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 6053
diff changeset
   222
  Ptr<PcapFileWrapper> file = pcapHelper.CreateFile ("sixth.pcap", std::ios::out, PcapHelper::DLT_PPP);
6020
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   223
  devices.Get (1)->TraceConnectWithoutContext("PhyRxDrop", MakeBoundCallback (&RxDrop, file));
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   224
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   225
  Simulator::Stop (Seconds(20));
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   226
  Simulator::Run ();
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   227
  Simulator::Destroy ();
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   228
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   229
  return 0;
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   230
}
f8212529e82a add sixth tutorial example for mid-level tracing helpers
Craig Dowell <craigdo@ee.washington.edu>
parents:
diff changeset
   231