A Discrete-Event Network Simulator
API
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 <limits>
25 
26 namespace ns3
27 {
28 
29 NS_LOG_COMPONENT_DEFINE("TrickleTimer");
30 
32  : m_impl(nullptr),
33  m_timerExpiration(),
34  m_intervalExpiration(),
35  m_currentInterval(Time(0)),
36  m_counter(0),
38 {
39  NS_LOG_FUNCTION(this);
40 
41  m_minInterval = Time(0);
42  m_ticks = 0;
43  m_maxInterval = Time(0);
44  m_redundancy = 0;
45 }
46 
47 TrickleTimer::TrickleTimer(Time minInterval, uint8_t doublings, uint16_t redundancy)
48  : m_impl(nullptr),
49  m_timerExpiration(),
50  m_intervalExpiration(),
51  m_currentInterval(Time(0)),
52  m_counter(0),
54 {
55  NS_LOG_FUNCTION(this << minInterval << doublings << redundancy);
56  NS_ASSERT_MSG(doublings < std::numeric_limits<decltype(m_ticks)>::digits,
57  "Doublings value is too large");
58 
59  m_minInterval = minInterval;
60  m_ticks = 1;
61  m_ticks <<= doublings;
62  m_maxInterval = m_ticks * minInterval;
63  m_redundancy = redundancy;
64 }
65 
67 {
68  NS_LOG_FUNCTION(this);
71  delete m_impl;
72 }
73 
74 int64_t
75 TrickleTimer::AssignStreams(int64_t streamNum)
76 {
77  m_uniRand->SetStream(streamNum);
78  return 1;
79 }
80 
81 void
82 TrickleTimer::SetParameters(Time minInterval, uint8_t doublings, uint16_t redundancy)
83 {
84  NS_LOG_FUNCTION(this << minInterval << doublings << redundancy);
85  NS_ASSERT_MSG(doublings < std::numeric_limits<decltype(m_ticks)>::digits,
86  "Doublings value is too large");
87 
88  m_minInterval = minInterval;
89  m_ticks = 1;
90  m_ticks <<= doublings;
91  m_maxInterval = m_ticks * minInterval;
92  m_redundancy = redundancy;
93 }
94 
95 Time
97 {
98  NS_LOG_FUNCTION(this);
99  return m_minInterval;
100 }
101 
102 Time
104 {
105  NS_LOG_FUNCTION(this);
106  return m_maxInterval;
107 }
108 
109 uint8_t
111 {
112  NS_LOG_FUNCTION(this);
113 
114  if (m_ticks == 0)
115  {
116  return 0;
117  }
118 
119  // Here we assume that m_ticks is a power of 2.
120  // This could have been way more elegant by using
121  // std::countl_zero() defined in the <bit> header
122  // which is c++20 - so not yet widely available.
123 
124  uint64_t ticks = m_ticks;
125  uint8_t doublings = 0;
126  while (ticks != 1)
127  {
128  ticks >>= 1;
129  doublings++;
130  }
131 
132  return doublings;
133 }
134 
135 uint16_t
137 {
138  NS_LOG_FUNCTION(this);
139  return m_redundancy;
140 }
141 
142 Time
144 {
145  NS_LOG_FUNCTION(this);
146 
148  {
150  }
151 
152  return TimeStep(0);
153 }
154 
155 Time
157 {
158  NS_LOG_FUNCTION(this);
159 
161  {
163  }
164 
165  return TimeStep(0);
166 }
167 
168 void
170 {
171  NS_LOG_FUNCTION(this);
172 
173  uint64_t randomInt;
174  double random;
175 
176  NS_ASSERT_MSG(m_minInterval != Time(0), "Timer not initialized");
177 
178  randomInt = m_uniRand->GetInteger(1, m_ticks);
179  random = randomInt;
180  if (randomInt < m_ticks)
181  {
182  random += m_uniRand->GetValue(0, 1);
183  }
184 
185  m_currentInterval = m_minInterval * random;
188 
189  m_counter = 0;
190 
191  Time timerExpitation = m_uniRand->GetValue(0.5, 1) * m_currentInterval;
193 }
194 
195 void
197 {
198  NS_LOG_FUNCTION(this);
199  m_counter++;
200 }
201 
202 void
204 {
205  NS_LOG_FUNCTION(this);
207  {
208  Reset();
209  }
210 }
211 
212 void
214 {
215  NS_LOG_FUNCTION(this);
216 
220 
223 
224  m_counter = 0;
225 
226  Time timerExpitation = m_uniRand->GetValue(0.5, 1) * m_currentInterval;
228 }
229 
230 void
232 {
233  NS_LOG_FUNCTION(this);
234 
238  m_counter = 0;
239 }
240 
241 void
243 {
244  NS_LOG_FUNCTION(this);
245 
246  if (m_counter < m_redundancy || m_redundancy == 0)
247  {
248  m_impl->Invoke();
249  }
250 }
251 
252 void
254 {
255  NS_LOG_FUNCTION(this);
256 
259  {
261  }
262 
265 
266  m_counter = 0;
267 
268  Time timerExpitation = m_uniRand->GetValue(0.5, 1) * m_currentInterval;
270 }
271 
272 } // 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
virtual void Invoke()=0
Invoke the expire function.
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.
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.
TimerImpl * m_impl
The timer implementation, which contains the bound callback function and arguments.
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.
#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:579
Debug message logging.
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:839
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::TrickleTimer timer class declaration.