src/applications/onoff/onoff-application.cc
author Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
Thu, 13 Mar 2008 12:56:49 -0700
changeset 2602 d9262bff6df2
parent 2503 e667dc0f350e
child 2742 19209c8d963f
permissions -rw-r--r--
add back support for introspected doxygen.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     1
/* -*-  Mode: C++; c-file-style: "gnu"; indent-tabs-mode:nil; -*- */
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     2
//
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     3
// Copyright (c) 2006 Georgia Tech Research Corporation
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     4
//
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     5
// This program is free software; you can redistribute it and/or modify
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     6
// it under the terms of the GNU General Public License version 2 as
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     7
// published by the Free Software Foundation;
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     8
//
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
     9
// This program is distributed in the hope that it will be useful,
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    12
// GNU General Public License for more details.
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    13
//
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    14
// You should have received a copy of the GNU General Public License
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    15
// along with this program; if not, write to the Free Software
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    16
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    17
//
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    18
// Author: George F. Riley<riley@ece.gatech.edu>
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    19
//
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    20
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    21
// ns3 - On/Off Data Source Application class
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    22
// George F. Riley, Georgia Tech, Spring 2007
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    23
// Adapted from ApplicationOnOff in GTNetS.
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    24
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
    25
#include "ns3/log.h"
1162
10f9613bd9be convert Socket to use Address class
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 959
diff changeset
    26
#include "ns3/address.h"
729
b5e744285e92 rename i-node to node
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 728
diff changeset
    27
#include "ns3/node.h"
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    28
#include "ns3/nstime.h"
382
26528ff50d38 Switch over simple-p2p.cc to use OnOffApplication
Tom Henderson <tomh@tomh.org>
parents: 372
diff changeset
    29
#include "ns3/data-rate.h"
524
082ffdd8fbd7 move code around
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 519
diff changeset
    30
#include "ns3/random-variable.h"
082ffdd8fbd7 move code around
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 519
diff changeset
    31
#include "ns3/socket.h"
082ffdd8fbd7 move code around
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 519
diff changeset
    32
#include "ns3/simulator.h"
747
65afca7824d7 i-socket-factory.h -> socket-factory.h
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 729
diff changeset
    33
#include "ns3/socket-factory.h"
1267
0ea5ae44f96b remove uint8_t * from the send path of the socket API
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1162
diff changeset
    34
#include "ns3/packet.h"
2494
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    35
#include "ns3/uinteger.h"
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    36
#include "ns3/trace-source-accessor.h"
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    37
#include "onoff-application.h"
2494
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    38
#include "ns3/udp.h"
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    39
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
    40
NS_LOG_COMPONENT_DEFINE ("OnOffApplication");
1429
31cb0668defd debug prints to make validation easier
Craig Dowell <craigdo@ee.washington.edu>
parents: 1267
diff changeset
    41
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    42
using namespace std;
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    43
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    44
namespace ns3 {
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    45
2494
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    46
NS_OBJECT_ENSURE_REGISTERED (OnOffApplication);
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    47
2494
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    48
TypeId
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    49
OnOffApplication::GetTypeId (void)
613
9679a238439d add dox doc and DefaultValue support to OnOffApplication
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 611
diff changeset
    50
{
2602
d9262bff6df2 add back support for introspected doxygen.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2503
diff changeset
    51
  static TypeId tid = TypeId ("ns3::OnOffApplication")
2494
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    52
    .SetParent<Application> ()
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    53
    .AddConstructor<OnOffApplication> ()
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    54
    .AddAttribute ("DataRate", "The data rate in on state.",
2502
50d0da37f02f introduce the ns3::String class, get rid of the string -> Attribute implicit conversion, and get rid of MakeDataRate, port PointToPointNetDevice to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2494
diff changeset
    55
                   DataRate ("500kb/s"),
2494
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    56
                   MakeDataRateAccessor (&OnOffApplication::m_cbrRate),
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    57
                   MakeDataRateChecker ())
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    58
    .AddAttribute ("PacketSize", "The size of packets sent in on state",
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    59
                   Uinteger (512),
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    60
                   MakeUintegerAccessor (&OnOffApplication::m_pktSize),
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    61
                   MakeUintegerChecker<uint32_t> (1))
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    62
    .AddAttribute ("Remote", "The address of the destination",
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    63
                   Address (),
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    64
                   MakeAddressAccessor (&OnOffApplication::m_peer),
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    65
                   MakeAddressChecker ())
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    66
    .AddAttribute ("OnTime", "A RandomVariable used to pick the duration of the 'On' state.",
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    67
                   ConstantVariable (1.0),
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    68
                   MakeRandomVariableAccessor (&OnOffApplication::m_onTime),
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    69
                   MakeRandomVariableChecker ())
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    70
    .AddAttribute ("OffTime", "A RandomVariable used to pick the duration of the 'Off' state.",
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    71
                   ConstantVariable (1.0),
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    72
                   MakeRandomVariableAccessor (&OnOffApplication::m_offTime),
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    73
                   MakeRandomVariableChecker ())
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    74
    .AddAttribute ("Protocol", "The type of protocol to use.",
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    75
                   Udp::GetTypeId (),
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    76
                   MakeTypeIdAccessor (&OnOffApplication::m_tid),
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    77
                   MakeTypeIdChecker ())
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    78
    .AddTraceSource ("Tx", "A new packet is created and is sent",
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    79
                     MakeTraceSourceAccessor (&OnOffApplication::m_txTrace))
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    80
    ;
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    81
  return tid;
613
9679a238439d add dox doc and DefaultValue support to OnOffApplication
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 611
diff changeset
    82
}
9679a238439d add dox doc and DefaultValue support to OnOffApplication
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 611
diff changeset
    83
2494
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    84
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
    85
OnOffApplication::OnOffApplication ()
613
9679a238439d add dox doc and DefaultValue support to OnOffApplication
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 611
diff changeset
    86
{
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
    87
  NS_LOG_FUNCTION;
613
9679a238439d add dox doc and DefaultValue support to OnOffApplication
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 611
diff changeset
    88
  m_socket = 0;
9679a238439d add dox doc and DefaultValue support to OnOffApplication
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 611
diff changeset
    89
  m_connected = false;
9679a238439d add dox doc and DefaultValue support to OnOffApplication
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 611
diff changeset
    90
  m_residualBits = 0;
9679a238439d add dox doc and DefaultValue support to OnOffApplication
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 611
diff changeset
    91
  m_lastStartTime = Seconds (0);
1859
7f78a53adb33 allow OnOffApplication to send more than 0xffffffff bytes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1828
diff changeset
    92
  m_maxBytes = 0;
613
9679a238439d add dox doc and DefaultValue support to OnOffApplication
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 611
diff changeset
    93
  m_totBytes = 0;
9679a238439d add dox doc and DefaultValue support to OnOffApplication
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 611
diff changeset
    94
}
9679a238439d add dox doc and DefaultValue support to OnOffApplication
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 611
diff changeset
    95
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
    96
OnOffApplication::~OnOffApplication()
1429
31cb0668defd debug prints to make validation easier
Craig Dowell <craigdo@ee.washington.edu>
parents: 1267
diff changeset
    97
{
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
    98
  NS_LOG_FUNCTION;
1429
31cb0668defd debug prints to make validation easier
Craig Dowell <craigdo@ee.washington.edu>
parents: 1267
diff changeset
    99
}
497
34d6f349478f implement Dispose properly throughout the network stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 493
diff changeset
   100
609
9baaee630644 remove dead code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 607
diff changeset
   101
void 
9baaee630644 remove dead code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 607
diff changeset
   102
OnOffApplication::SetMaxBytes(uint32_t maxBytes)
9baaee630644 remove dead code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 607
diff changeset
   103
{
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   104
  NS_LOG_FUNCTION;
1828
6ab68edddf45 nicer logging of parameters (bug 79 patch from Gustavo)
Tom Henderson <tomh@tomh.org>
parents: 1504
diff changeset
   105
  NS_LOG_PARAMS (this << maxBytes);
609
9baaee630644 remove dead code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 607
diff changeset
   106
  m_maxBytes = maxBytes;
9baaee630644 remove dead code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 607
diff changeset
   107
}
9baaee630644 remove dead code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 607
diff changeset
   108
613
9679a238439d add dox doc and DefaultValue support to OnOffApplication
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 611
diff changeset
   109
497
34d6f349478f implement Dispose properly throughout the network stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 493
diff changeset
   110
void
517
702e96b8960f finish Dispose -> DoDispose rework
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 506
diff changeset
   111
OnOffApplication::DoDispose (void)
497
34d6f349478f implement Dispose properly throughout the network stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 493
diff changeset
   112
{
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   113
  NS_LOG_FUNCTION;
1429
31cb0668defd debug prints to make validation easier
Craig Dowell <craigdo@ee.washington.edu>
parents: 1267
diff changeset
   114
568
e1660959ecbb use Ptr<> everywhere Object or NsUnknown are used
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 563
diff changeset
   115
  m_socket = 0;
497
34d6f349478f implement Dispose properly throughout the network stack
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 493
diff changeset
   116
  // chain up
517
702e96b8960f finish Dispose -> DoDispose rework
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 506
diff changeset
   117
  Application::DoDispose ();
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   118
}
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   119
1429
31cb0668defd debug prints to make validation easier
Craig Dowell <craigdo@ee.washington.edu>
parents: 1267
diff changeset
   120
// Application Methods
31cb0668defd debug prints to make validation easier
Craig Dowell <craigdo@ee.washington.edu>
parents: 1267
diff changeset
   121
void OnOffApplication::StartApplication() // Called at time specified by Start
31cb0668defd debug prints to make validation easier
Craig Dowell <craigdo@ee.washington.edu>
parents: 1267
diff changeset
   122
{
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   123
  NS_LOG_FUNCTION;
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   124
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   125
  // Create the socket if not already
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   126
  if (!m_socket)
609
9baaee630644 remove dead code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 607
diff changeset
   127
    {
2494
1c69ea12779c port Applications to Attributes
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2424
diff changeset
   128
      Ptr<SocketFactory> socketFactory = GetNode ()->GetObject<SocketFactory> (m_tid);
619
528f0554b677 make OnOffApplication not depend on IUdp
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 615
diff changeset
   129
      m_socket = socketFactory->CreateSocket ();
609
9baaee630644 remove dead code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 607
diff changeset
   130
      m_socket->Bind ();
1162
10f9613bd9be convert Socket to use Address class
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 959
diff changeset
   131
      m_socket->Connect (m_peer);
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   132
    }
609
9baaee630644 remove dead code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 607
diff changeset
   133
  // Insure no pending event
9baaee630644 remove dead code
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 607
diff changeset
   134
  StopApplication();
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   135
  // If we are not yet connected, there is nothing to do here
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   136
  // The ConnectionComplete upcall will start timers at that time
359
91b7ad7fa784 OnOffApplication functioning; some Node class cleanup
Tom Henderson <tomh@tomh.org>
parents: 349
diff changeset
   137
  //if (!m_connected) return;
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   138
  ScheduleStartEvent();
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   139
}
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   140
1429
31cb0668defd debug prints to make validation easier
Craig Dowell <craigdo@ee.washington.edu>
parents: 1267
diff changeset
   141
void OnOffApplication::StopApplication() // Called at time specified by Stop
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   142
{
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   143
  NS_LOG_FUNCTION;
1429
31cb0668defd debug prints to make validation easier
Craig Dowell <craigdo@ee.washington.edu>
parents: 1267
diff changeset
   144
611
edfeeb67e19f simplify the implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 609
diff changeset
   145
  if (m_sendEvent.IsRunning ())
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   146
    { // Cancel the pending send packet event
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   147
      // Calculate residual bits since last packet sent
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   148
      Time delta(Simulator::Now() - m_lastStartTime);
382
26528ff50d38 Switch over simple-p2p.cc to use OnOffApplication
Tom Henderson <tomh@tomh.org>
parents: 372
diff changeset
   149
      m_residualBits += (uint32_t)(m_cbrRate.GetBitRate() * delta.GetSeconds());
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   150
    }
611
edfeeb67e19f simplify the implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 609
diff changeset
   151
  Simulator::Cancel(m_sendEvent);
edfeeb67e19f simplify the implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 609
diff changeset
   152
  Simulator::Cancel(m_startStopEvent);
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   153
}
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   154
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   155
// Event handlers
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   156
void OnOffApplication::StartSending()
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   157
{
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   158
  NS_LOG_FUNCTION;
1429
31cb0668defd debug prints to make validation easier
Craig Dowell <craigdo@ee.washington.edu>
parents: 1267
diff changeset
   159
478
b067d8ff43df make Application::Stop work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 453
diff changeset
   160
  ScheduleNextTx();  // Schedule the send packet event
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   161
}
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   162
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   163
void OnOffApplication::StopSending()
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   164
{
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   165
  NS_LOG_FUNCTION;
1429
31cb0668defd debug prints to make validation easier
Craig Dowell <craigdo@ee.washington.edu>
parents: 1267
diff changeset
   166
611
edfeeb67e19f simplify the implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 609
diff changeset
   167
  Simulator::Cancel(m_sendEvent);
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   168
}
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   169
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   170
// Private helpers
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   171
void OnOffApplication::ScheduleNextTx()
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   172
{
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   173
  NS_LOG_FUNCTION;
1429
31cb0668defd debug prints to make validation easier
Craig Dowell <craigdo@ee.washington.edu>
parents: 1267
diff changeset
   174
1871
a082b4f3ac92 the intent was to always send data if maxbytes is zero.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1870
diff changeset
   175
  if (m_maxBytes == 0 || m_totBytes < m_maxBytes)
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   176
    {
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   177
      uint32_t bits = m_pktSize * 8 - m_residualBits;
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   178
      NS_LOG_LOGIC ("bits = " << bits);
382
26528ff50d38 Switch over simple-p2p.cc to use OnOffApplication
Tom Henderson <tomh@tomh.org>
parents: 372
diff changeset
   179
      Time nextTime(Seconds (bits / 
26528ff50d38 Switch over simple-p2p.cc to use OnOffApplication
Tom Henderson <tomh@tomh.org>
parents: 372
diff changeset
   180
        static_cast<double>(m_cbrRate.GetBitRate()))); // Time till next packet
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   181
      NS_LOG_LOGIC ("nextTime = " << nextTime);
1432
3aef7d7a71c2 more multicast plumbing
Craig Dowell <craigdo@ee.washington.edu>
parents: 1429
diff changeset
   182
      m_sendEvent = Simulator::Schedule(nextTime, 
3aef7d7a71c2 more multicast plumbing
Craig Dowell <craigdo@ee.washington.edu>
parents: 1429
diff changeset
   183
                                        &OnOffApplication::SendPacket, this);
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   184
    }
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   185
  else
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   186
    { // All done, cancel any pending events
359
91b7ad7fa784 OnOffApplication functioning; some Node class cleanup
Tom Henderson <tomh@tomh.org>
parents: 349
diff changeset
   187
      StopApplication();
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   188
    }
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   189
}
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   190
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   191
void OnOffApplication::ScheduleStartEvent()
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   192
{  // Schedules the event to start sending data (switch to the "On" state)
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   193
  NS_LOG_FUNCTION;
1429
31cb0668defd debug prints to make validation easier
Craig Dowell <craigdo@ee.washington.edu>
parents: 1267
diff changeset
   194
2383
a0e0ec096169 bug 133: automate RandomVariable memory management.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2252
diff changeset
   195
  Time offInterval = Seconds(m_offTime.GetValue());
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   196
  NS_LOG_LOGIC ("start at " << offInterval);
478
b067d8ff43df make Application::Stop work
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 453
diff changeset
   197
  m_startStopEvent = Simulator::Schedule(offInterval, &OnOffApplication::StartSending, this);
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   198
}
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   199
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   200
void OnOffApplication::ScheduleStopEvent()
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   201
{  // Schedules the event to stop sending data (switch to "Off" state)
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   202
  NS_LOG_FUNCTION;
1429
31cb0668defd debug prints to make validation easier
Craig Dowell <craigdo@ee.washington.edu>
parents: 1267
diff changeset
   203
2383
a0e0ec096169 bug 133: automate RandomVariable memory management.
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 2252
diff changeset
   204
  Time onInterval = Seconds(m_onTime.GetValue());
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   205
  Simulator::Schedule(onInterval, &OnOffApplication::StopSending, this);
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   206
}
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   207
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   208
  
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   209
void OnOffApplication::SendPacket()
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   210
{
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   211
  NS_LOG_FUNCTION;
1429
31cb0668defd debug prints to make validation easier
Craig Dowell <craigdo@ee.washington.edu>
parents: 1267
diff changeset
   212
611
edfeeb67e19f simplify the implementation
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 609
diff changeset
   213
  NS_ASSERT (m_sendEvent.IsExpired ());
1866
e7dbcc4df546 do not use Packet objects directly. Use Ptr<Packet> instead
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1504
diff changeset
   214
  Ptr<Packet> packet = Create<Packet> (m_pktSize);
2182
13c8ebd1a19e add application tracing support
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1871
diff changeset
   215
  m_txTrace (packet);
1866
e7dbcc4df546 do not use Packet objects directly. Use Ptr<Packet> instead
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 1504
diff changeset
   216
  m_socket->Send (packet);
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   217
  m_totBytes += m_pktSize;
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   218
  m_lastStartTime = Simulator::Now();
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   219
  m_residualBits = 0;
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   220
  ScheduleNextTx();
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   221
}
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   222
568
e1660959ecbb use Ptr<> everywhere Object or NsUnknown are used
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 563
diff changeset
   223
void OnOffApplication::ConnectionSucceeded(Ptr<Socket>)
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   224
{
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   225
  NS_LOG_FUNCTION;
1429
31cb0668defd debug prints to make validation easier
Craig Dowell <craigdo@ee.washington.edu>
parents: 1267
diff changeset
   226
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   227
  m_connected = true;
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   228
  ScheduleStartEvent();
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   229
}
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   230
  
568
e1660959ecbb use Ptr<> everywhere Object or NsUnknown are used
Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
parents: 563
diff changeset
   231
void OnOffApplication::ConnectionFailed(Ptr<Socket>)
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   232
{
1504
36ecc970ba96 checkpoint debug to log
Craig Dowell <craigdo@ee.washington.edu>
parents: 1496
diff changeset
   233
  NS_LOG_FUNCTION;
349
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   234
  cout << "OnOffApplication, Connection Failed" << endl;
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   235
}
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   236
3c91100689d7 Add base class IPAddr; L4Protocol; Protocol; OnOff Application, and base class Socket
Tom Henderson <tomh@tomh.org>
parents:
diff changeset
   237
} // Namespace ns3