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