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 
NS_LOG_COMPONENT_DEFINE
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:205
ns3::TrickleTimer::GetMaxInterval
Time GetMaxInterval(void) const
Get the MaxInterval of the timer.
Definition: trickle-timer.cc:100
ns3::TrickleTimer::Enable
void Enable()
Enable the timer.
Definition: trickle-timer.cc:167
ns3::TrickleTimer::GetMinInterval
Time GetMinInterval(void) const
Get the MinInterval of the timer.
Definition: trickle-timer.cc:93
ns3
Every class exported by the ns3 library is enclosed in the ns3 namespace.
ns3::TrickleTimer::m_ticks
uint64_t m_ticks
Interval span (i.e., exp2(doublings)).
Definition: trickle-timer.h:247
ns3::Simulator::GetDelayLeft
static Time GetDelayLeft(const EventId &id)
Get the remaining time until this event will execute.
Definition: simulator.cc:204
ns3::TrickleTimer::m_currentInterval
Time m_currentInterval
Current interval.
Definition: trickle-timer.h:248
ns3::Simulator::Schedule
static EventId Schedule(Time const &delay, FUNC f, Ts &&... args)
Schedule an event to expire after delay.
Definition: simulator.h:557
ns3::UniformRandomVariable::GetInteger
uint32_t GetInteger(uint32_t min, uint32_t max)
Get the next random value, as an unsigned integer in the specified range .
Definition: random-variable-stream.cc:193
ns3::TrickleTimer::m_counter
uint16_t m_counter
Event counter.
Definition: trickle-timer.h:249
ns3::TrickleTimer::SetParameters
void SetParameters(Time minInterval, uint8_t doublings, uint16_t redundancy)
Set the timer parameters.
Definition: trickle-timer.cc:80
ns3::TrickleTimer::InconsistentEvent
void InconsistentEvent()
Records an inconsistent event.
Definition: trickle-timer.cc:202
ns3::TrickleTimer::GetDelayLeft
Time GetDelayLeft(void) const
Definition: trickle-timer.cc:140
ns3::UniformRandomVariable
The uniform distribution Random Number Generator (RNG).
Definition: random-variable-stream.h:235
ns3::EventId::IsRunning
bool IsRunning(void) const
This method is syntactic sugar for !IsExpired().
Definition: event-id.cc:71
ns3::TrickleTimer::Reset
void Reset()
Reset the timer.
Definition: trickle-timer.cc:212
ns3::TrickleTimer::GetDoublings
uint8_t GetDoublings(void) const
Get the doublings of the timer.
Definition: trickle-timer.cc:107
ns3::TrickleTimer::AssignStreams
int64_t AssignStreams(int64_t streamNum)
Assigns the stream number for the uniform random number generator to use.
Definition: trickle-timer.cc:73
ns3::TrickleTimer::m_uniRand
Ptr< UniformRandomVariable > m_uniRand
Object to generate uniform random numbers.
Definition: trickle-timer.h:251
ns3::TrickleTimer::GetRedundancy
uint16_t GetRedundancy(void) const
Get the Redundancy constant of the timer.
Definition: trickle-timer.cc:133
ns3::TrickleTimer::IntervalExpire
void IntervalExpire(void)
Internal callback invoked when the interval expires.
Definition: trickle-timer.cc:253
ns3::TrickleTimer::~TrickleTimer
~TrickleTimer()
Destructor.
Definition: trickle-timer.cc:64
ns3::EventId::Cancel
void Cancel(void)
This method is syntactic sugar for the ns3::Simulator::Cancel method.
Definition: event-id.cc:53
ns3::TimerImpl::Invoke
virtual void Invoke(void)=0
Invoke the expire function.
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
NS_ASSERT_MSG
#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
ns3::TrickleTimer::TimerExpire
void TimerExpire(void)
Internal callback invoked when the timer expires.
Definition: trickle-timer.cc:242
ns3::TrickleTimer::m_impl
TimerImpl * m_impl
The timer implementation, which contains the bound callback function and arguments.
Definition: trickle-timer.h:234
log.h
Debug message logging.
ns3::TrickleTimer::m_minInterval
Time m_minInterval
Minimum interval.
Definition: trickle-timer.h:243
NS_LOG_FUNCTION
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
Definition: log-macros-enabled.h:244
ns3::TrickleTimer::Stop
void Stop()
Stop the timer.
Definition: trickle-timer.cc:231
ns3::TrickleTimer::m_redundancy
uint16_t m_redundancy
Redundancy constant.
Definition: trickle-timer.h:245
ns3::TrickleTimer::m_intervalExpiration
EventId m_intervalExpiration
The future event scheduled to expire the interval.
Definition: trickle-timer.h:240
ns3::RandomVariableStream::SetStream
void SetStream(int64_t stream)
Specifies the stream number for the RngStream.
Definition: random-variable-stream.cc:100
ns3::TrickleTimer::m_timerExpiration
EventId m_timerExpiration
The future event scheduled to expire the timer.
Definition: trickle-timer.h:237
ns3::TracedValueCallback::Time
void(* Time)(Time oldValue, Time newValue)
TracedValue callback signature for Time.
Definition: nstime.h:813
ns3::TrickleTimer::TrickleTimer
TrickleTimer()
Constructor.
Definition: trickle-timer.cc:30
trickle-timer.h
ns3::TrickleTimer timer class declaration.
ns3::TrickleTimer::m_maxInterval
Time m_maxInterval
Maximum interval.
Definition: trickle-timer.h:244
ns3::CreateObject
Ptr< T > CreateObject(Args &&... args)
Create an object by type, with varying number of constructor parameters.
Definition: object.h:576
ns3::TrickleTimer::GetIntervalLeft
Time GetIntervalLeft(void) const
Definition: trickle-timer.cc:153
ns3::TrickleTimer::ConsistentEvent
void ConsistentEvent()
Records a consistent event.
Definition: trickle-timer.cc:195
ns3::UniformRandomVariable::GetValue
double GetValue(double min, double max)
Get the next random value, as a double in the specified range .
Definition: random-variable-stream.cc:182