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