A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
trickle-timer.cc
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#include "trickle-timer.h"
21
22#include "log.h"
23
24#include <bit>
25#include <limits>
26
27namespace ns3
28{
29
30NS_LOG_COMPONENT_DEFINE("TrickleTimer");
31
33 : m_impl(nullptr),
34 m_timerExpiration(),
35 m_intervalExpiration(),
36 m_currentInterval(Time(0)),
37 m_counter(0),
39{
40 NS_LOG_FUNCTION(this);
41
43 m_ticks = 0;
45 m_redundancy = 0;
46}
47
48TrickleTimer::TrickleTimer(Time minInterval, uint8_t doublings, uint16_t redundancy)
49 : m_impl(nullptr),
50 m_timerExpiration(),
51 m_intervalExpiration(),
52 m_currentInterval(Time(0)),
53 m_counter(0),
55{
56 NS_LOG_FUNCTION(this << minInterval << doublings << redundancy);
57 NS_ASSERT_MSG(doublings < std::numeric_limits<decltype(m_ticks)>::digits,
58 "Doublings value is too large");
59
60 m_minInterval = minInterval;
61 m_ticks = 1;
62 m_ticks <<= doublings;
63 m_maxInterval = m_ticks * minInterval;
64 m_redundancy = redundancy;
65}
66
68{
69 NS_LOG_FUNCTION(this);
72 delete m_impl;
73}
74
75int64_t
77{
78 m_uniRand->SetStream(streamNum);
79 return 1;
80}
81
82void
83TrickleTimer::SetParameters(Time minInterval, uint8_t doublings, uint16_t redundancy)
84{
85 NS_LOG_FUNCTION(this << minInterval << doublings << redundancy);
86 NS_ASSERT_MSG(doublings < std::numeric_limits<decltype(m_ticks)>::digits,
87 "Doublings value is too large");
88
89 m_minInterval = minInterval;
90 m_ticks = 1;
91 m_ticks <<= doublings;
92 m_maxInterval = m_ticks * minInterval;
93 m_redundancy = redundancy;
94}
95
96Time
98{
99 NS_LOG_FUNCTION(this);
100 return m_minInterval;
101}
102
103Time
105{
106 NS_LOG_FUNCTION(this);
107 return m_maxInterval;
108}
109
110uint8_t
112{
113 NS_LOG_FUNCTION(this);
114
115 if (m_ticks == 0)
116 {
117 return 0;
118 }
119
120 return std::countr_zero(m_ticks);
121}
122
123uint16_t
125{
126 NS_LOG_FUNCTION(this);
127 return m_redundancy;
128}
129
130Time
132{
133 NS_LOG_FUNCTION(this);
134
136 {
138 }
139
140 return TimeStep(0);
141}
142
143Time
145{
146 NS_LOG_FUNCTION(this);
147
149 {
151 }
152
153 return TimeStep(0);
154}
155
156void
158{
159 NS_LOG_FUNCTION(this);
160
161 uint64_t randomInt;
162 double random;
163
164 NS_ASSERT_MSG(m_minInterval != Time(0), "Timer not initialized");
165
166 randomInt = m_uniRand->GetInteger(1, m_ticks);
167 random = randomInt;
168 if (randomInt < m_ticks)
169 {
170 random += m_uniRand->GetValue(0, 1);
171 }
172
176
177 m_counter = 0;
178
179 Time timerExpitation = m_uniRand->GetValue(0.5, 1) * m_currentInterval;
181}
182
183void
185{
186 NS_LOG_FUNCTION(this);
187 m_counter++;
188}
189
190void
192{
193 NS_LOG_FUNCTION(this);
195 {
196 Reset();
197 }
198}
199
200void
202{
203 NS_LOG_FUNCTION(this);
204
208
211
212 m_counter = 0;
213
214 Time timerExpitation = m_uniRand->GetValue(0.5, 1) * m_currentInterval;
216}
217
218void
220{
221 NS_LOG_FUNCTION(this);
222
226 m_counter = 0;
227}
228
229void
231{
232 NS_LOG_FUNCTION(this);
233
234 if (m_counter < m_redundancy || m_redundancy == 0)
235 {
236 m_impl->Invoke();
237 }
238}
239
240void
242{
243 NS_LOG_FUNCTION(this);
244
247 {
249 }
250
253
254 m_counter = 0;
255
256 Time timerExpitation = m_uniRand->GetValue(0.5, 1) * m_currentInterval;
258}
259
260} // namespace ns3
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:55
bool IsRunning() const
This method is syntactic sugar for !IsExpired().
Definition: event-id.cc:76
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:571
static Time GetDelayLeft(const EventId &id)
Get the remaining time until this event will execute.
Definition: simulator.cc:217
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
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 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.
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 uniform distribution Random Number Generator (RNG).
double GetValue(double min, double max)
Get the next random value drawn from the distribution.
uint32_t GetInteger(uint32_t min, uint32_t max)
Get the next random value drawn from the distribution.
virtual void Invoke()=0
Invoke the expire function.
#define NS_ASSERT_MSG(condition, message)
At runtime, in debugging builds, if this condition is not true, the program prints the message to out...
Definition: assert.h:86
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition: object.h:630
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::TrickleTimer timer class declaration.