A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
trickle-timer.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2020 Universita' di Firenze, Italy
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: Tommaso Pecorella <tommaso.pecorella@unifi.it>
18 */
19
20#ifndef TRICKLE_TIMER_H
21#define TRICKLE_TIMER_H
22
23#include "event-id.h"
24#include "nstime.h"
26
27/**
28 * \file
29 * \ingroup timer
30 * ns3::TrickleTimer timer class declaration.
31 */
32
33namespace ns3
34{
35
36namespace internal
37{
38
39class TimerImpl;
40
41} // namespace internal
42
43/**
44 * \ingroup timer
45 * \brief A Trickle Timer following \RFC{6206}.
46 *
47 * A Trickle Timer is a timer that varies its frequency between a minimum
48 * and a maximum, depending on events. It is typically used to exchange
49 * information in a highly robust, energy efficient, simple, and scalable manner.
50 *
51 * The Trickle Timer has three parameters:
52 * - minInterval Minimum interval.
53 * - doublings Number of doublings to reach the maximum interval.
54 * - redundancy Redundancy constant.
55 *
56 * The timer *period* is variable. It starts at minInterval, and it doubles
57 * the period length up to maxInterval = std::exp2 (doublings) * minInterval.
58 *
59 * The period is reset to minInterval when an *inconsistent* event is detected
60 * (see `TrickleTimer::InconsistentEvent`).
61 *
62 * The actual function fired by the timer is *not* called when a period expires.
63 * Rather, it is called in random moment between half of the actual period,
64 * and the end of actual the period.
65 * Moreover, the function is *not* fired if the timer did detect in the actual
66 * period a number of *consistent* events (see `TrickleTimer::ConsistentEvent`)
67 * greater than the redundancy constant. Setting the redundancy constant to zero
68 * disables this feature.
69 *
70 * The Trickle Timer is mainly used to self-regulate the transmission of periodic
71 * information (e.g., Router Advertisements) in wireless networks - and
72 * particularly in LLNs. In these contexts the frequency of the timer is adjusted
73 * according to, e.g., RS multicast messages. Moreover, the redundancy constant
74 * can be used to avoid congestion in high density networks.
75 *
76 * Please refer to \RFC{6206} for a full description and discussion of the Trickle Timer.
77 */
79{
80 public:
81 /** Constructor. */
83
84 /**
85 * Constructor.
86 *
87 * The maximum interval is set to std::exp2 (doublings) * minInterval.
88 *
89 * \param minInterval Minimum interval.
90 * \param doublings Number of doublings to reach the maximum interval.
91 * \param redundancy Redundancy constant.
92 *
93 * A zero value in the redundancy constant means that the suppression
94 * algorithm is disabled.
95 *
96 */
97 TrickleTimer(Time minInterval, uint8_t doublings, uint16_t redundancy);
98
99 /** Destructor. */
101
102 /**
103 * Assigns the stream number for the uniform random number generator to use
104 *
105 * \param streamNum first stream index to use
106 * \return the number of stream indices assigned by this helper
107 */
108 int64_t AssignStreams(int64_t streamNum);
109
110 /**
111 * \brief Set the timer parameters.
112 *
113 * The maximum interval is set to std::exp2 (doublings) * minInterval.
114 *
115 * \param minInterval Minimum interval.
116 * \param doublings Number of doublings to reach the maximum interval.
117 * \param redundancy Redundancy constant.
118 *
119 * A zero value in the redundancy constant means that the suppression
120 * algorithm is disabled.
121 *
122 */
123 void SetParameters(Time minInterval, uint8_t doublings, uint16_t redundancy);
124
125 /**
126 * \brief Get the MinInterval of the timer.
127 * \return The MinInterval
128 */
129 Time GetMinInterval() const;
130
131 /**
132 * \brief Get the MaxInterval of the timer.
133 *
134 * The timer MaxInterval is always std::exp2 (doublings) * minInterval
135 * \return The MaxInterval
136 */
137 Time GetMaxInterval() const;
138
139 /**
140 * \brief Get the doublings of the timer.
141 * \return The doublings
142 */
143 uint8_t GetDoublings() const;
144
145 /**
146 * \brief Get the Redundancy constant of the timer.
147 * \return The Redundancy
148 */
149 uint16_t GetRedundancy() const;
150
151 /**
152 * \returns The amount of time left until this timer expires.
153 *
154 * This method returns zero if the timer has never been started.
155 */
156 Time GetDelayLeft() const;
157
158 /**
159 * \returns The amount of time left until this timer interval expires.
160 *
161 * This method returns zero if the timer has never been started.
162 */
163 Time GetIntervalLeft() const;
164
165 /**
166 * \brief Enable the timer.
167 */
168 void Enable();
169
170 /**
171 * \brief Records a consistent event.
172 */
173 void ConsistentEvent();
174
175 /**
176 * \brief Records an inconsistent event.
177 */
178 void InconsistentEvent();
179
180 /**
181 * \brief Reset the timer.
182 */
183 void Reset();
184
185 /**
186 * \brief Stop the timer.
187 *
188 * This will reset the timer and cancel all the pending events.
189 */
190 void Stop();
191
192 /**
193 * Set the function to execute when the timer expires.
194 *
195 * \tparam FN \deduced The type of the function.
196 * \param [in] fn The function
197 *
198 * Store this function in this Timer for later use by Timer::Schedule.
199 */
200 template <typename FN>
201 void SetFunction(FN fn);
202
203 /**
204 * Set the function to execute when the timer expires.
205 *
206 * \tparam MEM_PTR \deduced Class method function type.
207 * \tparam OBJ_PTR \deduced Class type containing the function.
208 * \param [in] memPtr The member function pointer
209 * \param [in] objPtr The pointer to object
210 *
211 * Store this function and object in this Timer for later use by Timer::Schedule.
212 */
213 template <typename MEM_PTR, typename OBJ_PTR>
214 void SetFunction(MEM_PTR memPtr, OBJ_PTR objPtr);
215
216 /**
217 * Set the arguments to be used when invoking the expire function.
218 */
219 /**@{*/
220 /**
221 * \tparam Ts \deduced Argument types.
222 * \param [in] args arguments
223 */
224 template <typename... Ts>
225 void SetArguments(Ts&&... args);
226 /**@}*/
227
228 private:
229 /** Internal callback invoked when the timer expires. */
230 void TimerExpire();
231 /** Internal callback invoked when the interval expires. */
232 void IntervalExpire();
233
234 /**
235 * The timer implementation, which contains the bound callback
236 * function and arguments.
237 */
239
240 /** The future event scheduled to expire the timer. */
242
243 /** The future event scheduled to expire the interval. */
245
246 Time m_minInterval; //!< Minimum interval
247 Time m_maxInterval; //!< Maximum interval
248 uint16_t m_redundancy; //!< Redundancy constant.
249
250 uint64_t m_ticks; //!< Interval span (i.e., exp2(doublings)).
251 Time m_currentInterval; //!< Current interval.
252 uint16_t m_counter; //!< Event counter.
253
254 Ptr<UniformRandomVariable> m_uniRand; //!< Object to generate uniform random numbers
255};
256
257} // namespace ns3
258
259/********************************************************************
260 * Implementation of the templates declared above.
261 ********************************************************************/
262
263#include "timer-impl.h"
264
265namespace ns3
266{
267
268template <typename FN>
269void
271{
272 delete m_impl;
274}
275
276template <typename MEM_PTR, typename OBJ_PTR>
277void
278TrickleTimer::SetFunction(MEM_PTR memPtr, OBJ_PTR objPtr)
279{
280 delete m_impl;
281 m_impl = internal::MakeTimerImpl(memPtr, objPtr);
282}
283
284template <typename... Ts>
285void
287{
288 if (m_impl == nullptr)
289 {
291 "You cannot set the arguments of a TrickleTimer before setting its function.");
292 return;
293 }
294 m_impl->SetArgs(std::forward<Ts>(args)...);
295}
296
297} // namespace ns3
298
299#endif /* TRICKLE_TIMER_H */
An identifier for simulation events.
Definition: event-id.h:55
Smart pointer class similar to boost::intrusive_ptr.
Definition: ptr.h:77
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
A Trickle Timer following RFC 6206.
Definition: trickle-timer.h:79
int64_t AssignStreams(int64_t streamNum)
Assigns the stream number for the uniform random number generator to use.
uint8_t GetDoublings() const
Get the doublings of the timer.
Time m_minInterval
Minimum interval.
EventId m_timerExpiration
The future event scheduled to expire the timer.
void Stop()
Stop the timer.
Time m_currentInterval
Current interval.
Time m_maxInterval
Maximum interval.
void SetParameters(Time minInterval, uint8_t doublings, uint16_t redundancy)
Set the timer parameters.
internal::TimerImpl * m_impl
The timer implementation, which contains the bound callback function and arguments.
uint64_t m_ticks
Interval span (i.e., exp2(doublings)).
Time GetMinInterval() const
Get the MinInterval of the timer.
void InconsistentEvent()
Records an inconsistent event.
void Reset()
Reset the timer.
void TimerExpire()
Internal callback invoked when the timer expires.
EventId m_intervalExpiration
The future event scheduled to expire the interval.
Time GetDelayLeft() const
uint16_t GetRedundancy() const
Get the Redundancy constant of the timer.
void SetFunction(FN fn)
Set the function to execute when the timer expires.
void Enable()
Enable the timer.
Time GetMaxInterval() const
Get the MaxInterval of the timer.
Ptr< UniformRandomVariable > m_uniRand
Object to generate uniform random numbers.
~TrickleTimer()
Destructor.
void ConsistentEvent()
Records a consistent event.
void SetArguments(Ts &&... args)
Set the arguments to be used when invoking the expire function.
Time GetIntervalLeft() const
TrickleTimer()
Constructor.
uint16_t m_counter
Event counter.
void IntervalExpire()
Internal callback invoked when the interval expires.
uint16_t m_redundancy
Redundancy constant.
The timer implementation underlying Timer and Watchdog.
Definition: timer-impl.h:46
void SetArgs(Args... args)
Set the arguments to be used when invoking the expire function.
Definition: timer-impl.h:193
ns3::EventId declarations.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
TimerImpl * MakeTimerImpl(U(fn)(Ts...))
Make a TimerImpl from a function pointer taking varying numbers of arguments.
Definition: timer-impl.h:103
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Declaration of classes ns3::Time and ns3::TimeWithUnit, and the TimeValue implementation classes.
ns3::RandomVariableStream declaration, and related classes.
ns3::TimerImpl declaration and implementation.