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 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
7 */
8
9#include "trickle-timer.h"
10
11#include "log.h"
12
13#include <bit>
14#include <limits>
15
16namespace ns3
17{
18
19NS_LOG_COMPONENT_DEFINE("TrickleTimer");
20
22 : m_impl(nullptr),
23 m_timerExpiration(),
24 m_intervalExpiration(),
25 m_currentInterval(Time(0)),
26 m_counter(0),
28{
29 NS_LOG_FUNCTION(this);
30
32 m_ticks = 0;
34 m_redundancy = 0;
35}
36
37TrickleTimer::TrickleTimer(Time minInterval, uint8_t doublings, uint16_t redundancy)
38 : m_impl(nullptr),
39 m_timerExpiration(),
40 m_intervalExpiration(),
41 m_currentInterval(Time(0)),
42 m_counter(0),
44{
45 NS_LOG_FUNCTION(this << minInterval << doublings << redundancy);
46 NS_ASSERT_MSG(doublings < std::numeric_limits<decltype(m_ticks)>::digits,
47 "Doublings value is too large");
48
49 m_minInterval = minInterval;
50 m_ticks = 1;
51 m_ticks <<= doublings;
52 m_maxInterval = m_ticks * minInterval;
53 m_redundancy = redundancy;
54}
55
63
64int64_t
66{
67 m_uniRand->SetStream(streamNum);
68 return 1;
69}
70
71void
72TrickleTimer::SetParameters(Time minInterval, uint8_t doublings, uint16_t redundancy)
73{
74 NS_LOG_FUNCTION(this << minInterval << doublings << redundancy);
75 NS_ASSERT_MSG(doublings < std::numeric_limits<decltype(m_ticks)>::digits,
76 "Doublings value is too large");
77
78 m_minInterval = minInterval;
79 m_ticks = 1;
80 m_ticks <<= doublings;
81 m_maxInterval = m_ticks * minInterval;
82 m_redundancy = redundancy;
83}
84
85Time
87{
88 NS_LOG_FUNCTION(this);
89 return m_minInterval;
90}
91
92Time
94{
95 NS_LOG_FUNCTION(this);
96 return m_maxInterval;
97}
98
99uint8_t
101{
102 NS_LOG_FUNCTION(this);
103
104 if (m_ticks == 0)
105 {
106 return 0;
107 }
108
109 return std::countr_zero(m_ticks);
110}
111
112uint16_t
114{
115 NS_LOG_FUNCTION(this);
116 return m_redundancy;
117}
118
119Time
121{
122 NS_LOG_FUNCTION(this);
123
125 {
127 }
128
129 return TimeStep(0);
130}
131
132Time
134{
135 NS_LOG_FUNCTION(this);
136
138 {
140 }
141
142 return TimeStep(0);
143}
144
145void
147{
148 NS_LOG_FUNCTION(this);
149
150 uint64_t randomInt;
151 double random;
152
153 NS_ASSERT_MSG(m_minInterval != Time(0), "Timer not initialized");
154
155 randomInt = m_uniRand->GetInteger(1, m_ticks);
156 random = randomInt;
157 if (randomInt < m_ticks)
158 {
159 random += m_uniRand->GetValue(0, 1);
160 }
161
165
166 m_counter = 0;
167
168 Time timerExpitation = m_uniRand->GetValue(0.5, 1) * m_currentInterval;
170}
171
172void
178
179void
181{
182 NS_LOG_FUNCTION(this);
184 {
185 Reset();
186 }
187}
188
189void
206
207void
217
218void
220{
221 NS_LOG_FUNCTION(this);
222
223 if (m_counter < m_redundancy || m_redundancy == 0)
224 {
225 m_impl->Invoke();
226 }
227}
228
229void
248
249} // namespace ns3
void Cancel()
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition event-id.cc:44
bool IsPending() const
This method is syntactic sugar for !IsExpired().
Definition event-id.cc:65
static EventId Schedule(const Time &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition simulator.h:560
static Time GetDelayLeft(const EventId &id)
Get the remaining time until this event will execute.
Definition simulator.cc:206
Simulation virtual time values and global simulation resolution.
Definition nstime.h:94
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).
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:75
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#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:619
Debug message logging.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::TrickleTimer timer class declaration.