A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
onoff-application.h
Go to the documentation of this file.
1//
2// Copyright (c) 2006 Georgia Tech Research Corporation
3//
4// This program is free software; you can redistribute it and/or modify
5// it under the terms of the GNU General Public License version 2 as
6// published by the Free Software Foundation;
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program; if not, write to the Free Software
15// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16//
17// Author: George F. Riley<riley@ece.gatech.edu>
18//
19
20// ns3 - On/Off Data Source Application class
21// George F. Riley, Georgia Tech, Spring 2007
22// Adapted from ApplicationOnOff in GTNetS.
23
24#ifndef ONOFF_APPLICATION_H
25#define ONOFF_APPLICATION_H
26
27#include "seq-ts-size-header.h"
28
29#include "ns3/address.h"
30#include "ns3/application.h"
31#include "ns3/data-rate.h"
32#include "ns3/event-id.h"
33#include "ns3/ptr.h"
34#include "ns3/traced-callback.h"
35
36namespace ns3
37{
38
39class Address;
40class RandomVariableStream;
41class Socket;
42
43/**
44 * \ingroup applications
45 * \defgroup onoff OnOffApplication
46 *
47 * This traffic generator follows an On/Off pattern: after
48 * Application::StartApplication
49 * is called, "On" and "Off" states alternate. The duration of each of
50 * these states is determined with the onTime and the offTime random
51 * variables. During the "Off" state, no traffic is generated.
52 * During the "On" state, cbr traffic is generated. This cbr traffic is
53 * characterized by the specified "data rate" and "packet size".
54 */
55/**
56 * \ingroup onoff
57 *
58 * \brief Generate traffic to a single destination according to an
59 * OnOff pattern.
60 *
61 * This traffic generator follows an On/Off pattern: after
62 * Application::StartApplication
63 * is called, "On" and "Off" states alternate. The duration of each of
64 * these states is determined with the onTime and the offTime random
65 * variables. During the "Off" state, no traffic is generated.
66 * During the "On" state, cbr traffic is generated. This cbr traffic is
67 * characterized by the specified "data rate" and "packet size".
68 *
69 * Note: When an application is started, the first packet transmission
70 * occurs _after_ a delay equal to (packet size/bit rate). Note also,
71 * when an application transitions into an off state in between packet
72 * transmissions, the remaining time until when the next transmission
73 * would have occurred is cached and is used when the application starts
74 * up again. Example: packet size = 1000 bits, bit rate = 500 bits/sec.
75 * If the application is started at time 3 seconds, the first packet
76 * transmission will be scheduled for time 5 seconds (3 + 1000/500)
77 * and subsequent transmissions at 2 second intervals. If the above
78 * application were instead stopped at time 4 seconds, and restarted at
79 * time 5.5 seconds, then the first packet would be sent at time 6.5 seconds,
80 * because when it was stopped at 4 seconds, there was only 1 second remaining
81 * until the originally scheduled transmission, and this time remaining
82 * information is cached and used to schedule the next transmission
83 * upon restarting.
84 *
85 * If the underlying socket type supports broadcast, this application
86 * will automatically enable the SetAllowBroadcast(true) socket option.
87 *
88 * If the attribute "EnableSeqTsSizeHeader" is enabled, the application will
89 * use some bytes of the payload to store an header with a sequence number,
90 * a timestamp, and the size of the packet sent. Support for extracting
91 * statistics from this header have been added to \c ns3::PacketSink
92 * (enable its "EnableSeqTsSizeHeader" attribute), or users may extract
93 * the header via trace sources. Note that the continuity of the sequence
94 * number may be disrupted across On/Off cycles.
95 */
97{
98 public:
99 /**
100 * \brief Get the type ID.
101 * \return the object TypeId
102 */
103 static TypeId GetTypeId();
104
106
107 ~OnOffApplication() override;
108
109 /**
110 * \brief Set the total number of bytes to send.
111 *
112 * Once these bytes are sent, no packet is sent again, even in on state.
113 * The value zero means that there is no limit.
114 *
115 * \param maxBytes the total number of bytes to send
116 */
117 void SetMaxBytes(uint64_t maxBytes);
118
119 /**
120 * \brief Return a pointer to associated socket.
121 * \return pointer to associated socket
122 */
123 Ptr<Socket> GetSocket() const;
124
125 int64_t AssignStreams(int64_t stream) override;
126
127 protected:
128 void DoDispose() override;
129
130 private:
131 void StartApplication() override;
132 void StopApplication() override;
133
134 // helpers
135 /**
136 * \brief Cancel all pending events.
137 */
138 void CancelEvents();
139
140 // Event handlers
141 /**
142 * \brief Start an On period
143 */
144 void StartSending();
145 /**
146 * \brief Start an Off period
147 */
148 void StopSending();
149 /**
150 * \brief Send a packet
151 */
152 void SendPacket();
153
154 Ptr<Socket> m_socket; //!< Associated socket
155 Address m_peer; //!< Peer address
156 Address m_local; //!< Local address to bind to
157 bool m_connected; //!< True if connected
158 uint8_t m_tos; //!< The packets Type of Service
161 DataRate m_cbrRate; //!< Rate that data is generated
162 DataRate m_cbrRateFailSafe; //!< Rate that data is generated (check copy)
163 uint32_t m_pktSize; //!< Size of packets
164 uint32_t m_residualBits; //!< Number of generated, but not sent, bits
165 Time m_lastStartTime; //!< Time last packet sent
166 uint64_t m_maxBytes; //!< Limit total number of bytes sent
167 uint64_t m_totBytes; //!< Total bytes sent so far
168 EventId m_startStopEvent; //!< Event id for next start or stop event
169 EventId m_sendEvent; //!< Event id of pending "send packet" event
170 TypeId m_tid; //!< Type of the socket used
171 uint32_t m_seq{0}; //!< Sequence
172 Ptr<Packet> m_unsentPacket; //!< Unsent packet cached for future attempt
173 bool m_enableSeqTsSizeHeader{false}; //!< Enable or disable the use of SeqTsSizeHeader
174
175 /// Traced Callback: transmitted packets.
177
178 /// Callbacks for tracing the packet Tx events, includes source and destination addresses
180
181 /// Callback for tracing the packet Tx events, includes source, destination, the packet sent,
182 /// and header
185
186 private:
187 /**
188 * \brief Schedule the next packet transmission
189 */
190 void ScheduleNextTx();
191 /**
192 * \brief Schedule the next On period start
193 */
194 void ScheduleStartEvent();
195 /**
196 * \brief Schedule the next Off period start
197 */
198 void ScheduleStopEvent();
199 /**
200 * \brief Handle a Connection Succeed event
201 * \param socket the connected socket
202 */
203 void ConnectionSucceeded(Ptr<Socket> socket);
204 /**
205 * \brief Handle a Connection Failed event
206 * \param socket the not connected socket
207 */
208 void ConnectionFailed(Ptr<Socket> socket);
209};
210
211} // namespace ns3
212
213#endif /* ONOFF_APPLICATION_H */
a polymophic address class
Definition: address.h:101
The base class for all ns3 applications.
Definition: application.h:62
Class for representing data rates.
Definition: data-rate.h:89
An identifier for simulation events.
Definition: event-id.h:55
Generate traffic to a single destination according to an OnOff pattern.
static TypeId GetTypeId()
Get the type ID.
uint8_t m_tos
The packets Type of Service.
uint32_t m_residualBits
Number of generated, but not sent, bits.
void ConnectionSucceeded(Ptr< Socket > socket)
Handle a Connection Succeed event.
EventId m_sendEvent
Event id of pending "send packet" event.
void ScheduleStartEvent()
Schedule the next On period start.
bool m_connected
True if connected.
uint32_t m_pktSize
Size of packets.
Ptr< Socket > GetSocket() const
Return a pointer to associated socket.
Ptr< Socket > m_socket
Associated socket.
TracedCallback< Ptr< const Packet > > m_txTrace
Traced Callback: transmitted packets.
void SetMaxBytes(uint64_t maxBytes)
Set the total number of bytes to send.
Ptr< RandomVariableStream > m_offTime
rng for Off Time
void StartApplication() override
Application specific startup code.
Ptr< Packet > m_unsentPacket
Unsent packet cached for future attempt.
int64_t AssignStreams(int64_t stream) override
Assign a fixed random variable stream number to the random variables used by this Application object.
TracedCallback< Ptr< const Packet >, const Address &, const Address & > m_txTraceWithAddresses
Callbacks for tracing the packet Tx events, includes source and destination addresses.
void DoDispose() override
Destructor implementation.
EventId m_startStopEvent
Event id for next start or stop event.
void ScheduleNextTx()
Schedule the next packet transmission.
DataRate m_cbrRateFailSafe
Rate that data is generated (check copy)
void ConnectionFailed(Ptr< Socket > socket)
Handle a Connection Failed event.
void ScheduleStopEvent()
Schedule the next Off period start.
TracedCallback< Ptr< const Packet >, const Address &, const Address &, const SeqTsSizeHeader & > m_txTraceWithSeqTsSize
Callback for tracing the packet Tx events, includes source, destination, the packet sent,...
uint64_t m_maxBytes
Limit total number of bytes sent.
Time m_lastStartTime
Time last packet sent.
uint64_t m_totBytes
Total bytes sent so far.
void StopSending()
Start an Off period.
void StartSending()
Start an On period.
Address m_local
Local address to bind to.
Ptr< RandomVariableStream > m_onTime
rng for On Time
bool m_enableSeqTsSizeHeader
Enable or disable the use of SeqTsSizeHeader.
TypeId m_tid
Type of the socket used.
uint32_t m_seq
Sequence.
DataRate m_cbrRate
Rate that data is generated.
void StopApplication() override
Application specific shutdown code.
Address m_peer
Peer address.
void CancelEvents()
Cancel all pending events.
void SendPacket()
Send a packet.
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Header with a sequence, a timestamp, and a "size" attribute.
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
Forward calls to a chain of Callback.
a unique identifier for an interface.
Definition: type-id.h:59
Every class exported by the ns3 library is enclosed in the ns3 namespace.