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