A Discrete-Event Network Simulator
API
trickle-timer.cc
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2020 Universita' di Firenze, Italy
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation;
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * Author: Tommaso Pecorella <tommaso.pecorella@unifi.it>
19  */
20 
21 #include "trickle-timer.h"
22 #include "log.h"
23 #include <limits>
24 
25 
26 namespace ns3 {
27 
28 NS_LOG_COMPONENT_DEFINE ("TrickleTimer");
29 
31  : m_impl (0),
32  m_timerExpiration (),
33  m_intervalExpiration (),
34  m_currentInterval (Time(0)),
35  m_counter (0),
36  m_uniRand (CreateObject<UniformRandomVariable> ())
37 {
38  NS_LOG_FUNCTION (this);
39 
40  m_minInterval = Time (0);
41  m_ticks = 0;
42  m_maxInterval = Time (0);
43  m_redundancy = 0;
44 }
45 
46 TrickleTimer::TrickleTimer (Time minInterval, uint8_t doublings, uint16_t redundancy)
47  : m_impl (0),
48  m_timerExpiration (),
49  m_intervalExpiration (),
50  m_currentInterval (Time(0)),
51  m_counter (0),
52  m_uniRand (CreateObject<UniformRandomVariable> ())
53 {
54  NS_LOG_FUNCTION (this << minInterval << doublings << redundancy);
55  NS_ASSERT_MSG (doublings < std::numeric_limits<decltype(m_ticks)>::digits, "Doublings value is too large");
56 
57  m_minInterval = minInterval;
58  m_ticks = 1;
59  m_ticks <<= doublings;
60  m_maxInterval = m_ticks * minInterval;
61  m_redundancy = redundancy;
62 }
63 
65 {
66  NS_LOG_FUNCTION (this);
69  delete m_impl;
70 }
71 
72 int64_t
73 TrickleTimer::AssignStreams (int64_t streamNum)
74 {
75  m_uniRand->SetStream (streamNum);
76  return 1;
77 }
78 
79 void
80 TrickleTimer::SetParameters (Time minInterval, uint8_t doublings, uint16_t redundancy)
81 {
82  NS_LOG_FUNCTION (this << minInterval << doublings << redundancy);
83  NS_ASSERT_MSG (doublings < std::numeric_limits<decltype(m_ticks)>::digits, "Doublings value is too large");
84 
85  m_minInterval = minInterval;
86  m_ticks = 1;
87  m_ticks <<= doublings;
88  m_maxInterval = m_ticks * minInterval;
89  m_redundancy = redundancy;
90 }
91 
92 Time
94 {
95  NS_LOG_FUNCTION (this);
96  return m_minInterval;
97 }
98 
99 Time
101 {
102  NS_LOG_FUNCTION (this);
103  return m_maxInterval;
104 }
105 
106 uint8_t
108 {
109  NS_LOG_FUNCTION (this);
110 
111  if (m_ticks == 0)
112  {
113  return 0;
114  }
115 
116  // Here we assume that m_ticks is a power of 2.
117  // This could have been way more elegant by using
118  // std::countl_zero() defined in the <bit> header
119  // which is c++20 - so not yet widely available.
120 
121  uint64_t ticks = m_ticks;
122  uint8_t doublings = 0;
123  while (ticks != 1)
124  {
125  ticks >>= 1;
126  doublings ++;
127  }
128 
129  return doublings;
130 }
131 
132 uint16_t
134 {
135  NS_LOG_FUNCTION (this);
136  return m_redundancy;
137 }
138 
139 Time
141 {
142  NS_LOG_FUNCTION (this);
143 
145  {
147  }
148 
149  return TimeStep (0);
150 }
151 
152 Time
154 {
155  NS_LOG_FUNCTION (this);
156 
158  {
160  }
161 
162  return TimeStep (0);
163 }
164 
165 
166 void
168 {
169  NS_LOG_FUNCTION (this);
170 
171  uint64_t randomInt;
172  double random;
173 
174  NS_ASSERT_MSG (m_minInterval != Time (0), "Timer not initialized");
175 
176  randomInt = m_uniRand->GetInteger (1, m_ticks);
177  random = randomInt;
178  if (randomInt < m_ticks)
179  {
180  random += m_uniRand->GetValue (0, 1);
181  }
182 
183  m_currentInterval = m_minInterval * random;
185 
186  m_counter = 0;
187 
188  Time timerExpitation = m_uniRand->GetValue (0.5, 1) * m_currentInterval;
190 
191  return;
192 }
193 
194 void
196 {
197  NS_LOG_FUNCTION (this);
198  m_counter ++;
199 }
200 
201 void
203 {
204  NS_LOG_FUNCTION (this);
206  {
207  Reset ();
208  }
209 }
210 
211 void
213 {
214  NS_LOG_FUNCTION (this);
215 
219 
221 
222  m_counter = 0;
223 
224  Time timerExpitation = m_uniRand->GetValue (0.5, 1) * m_currentInterval;
226 
227  return;
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 
264 
265  m_counter = 0;
266 
267  Time timerExpitation = m_uniRand->GetValue (0.5, 1) * m_currentInterval;
269 }
270 
271 
272 
273 } // namespace ns3
274 
static Time GetDelayLeft(const EventId &id)
Get the remaining time until this event will execute.
Definition: simulator.cc:204
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:557
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
Time GetIntervalLeft(void) const
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by "...
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
~TrickleTimer()
Destructor.
uint32_t GetInteger(uint32_t min, uint32_t max)
Get the next random value, as an unsigned integer in the specified range .
uint64_t m_ticks
Interval span (i.e., exp2(doublings)).
void ConsistentEvent()
Records a consistent event.
ns3::TrickleTimer timer class declaration.
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
uint8_t GetDoublings(void) const
Get the doublings of the timer.
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:813
uint16_t m_counter
Event counter.
uint16_t m_redundancy
Redundancy constant.
uint16_t GetRedundancy(void) const
Get the Redundancy constant of the timer.
Time GetDelayLeft(void) const
void TimerExpire(void)
Internal callback invoked when the timer expires.
TimerImpl * m_impl
The timer implementation, which contains the bound callback function and arguments.
Time GetMaxInterval(void) const
Get the MaxInterval of the timer.
The uniform distribution Random Number Generator (RNG).
Time m_minInterval
Minimum interval.
void Reset()
Reset the timer.
TrickleTimer()
Constructor.
void SetParameters(Time minInterval, uint8_t doublings, uint16_t redundancy)
Set the timer parameters.
Every class exported by the ns3 library is enclosed in the ns3 namespace.
EventId m_intervalExpiration
The future event scheduled to expire the interval.
Time GetMinInterval(void) const
Get the MinInterval of the timer.
void InconsistentEvent()
Records an inconsistent event.
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
Ptr< UniformRandomVariable > m_uniRand
Object to generate uniform random numbers.
#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:88
int64_t AssignStreams(int64_t streamNum)
Assigns the stream number for the uniform random number generator to use.
void Stop()
Stop the timer.
EventId m_timerExpiration
The future event scheduled to expire the timer.
void IntervalExpire(void)
Internal callback invoked when the interval expires.
Time m_maxInterval
Maximum interval.
bool IsRunning(void) const
This method is syntactic sugar for !IsExpired().
Definition: event-id.cc:71
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:53
virtual void Invoke(void)=0
Invoke the expire function.
Time m_currentInterval
Current interval.
Debug message logging.
void Enable()
Enable the timer.
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition: object.h:576