A Discrete-Event Network Simulator
API
trickle-timer.h
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 #ifndef TRICKLE_TIMER_H
22 #define TRICKLE_TIMER_H
23 
24 #include "nstime.h"
25 #include "event-id.h"
26 #include "random-variable-stream.h"
27 
34 namespace ns3 {
35 
36 class TimerImpl;
37 
74 {
75 public:
77  TrickleTimer ();
78 
92  TrickleTimer (Time minInterval, uint8_t doublings, uint16_t redundancy);
93 
95  ~TrickleTimer ();
96 
103  int64_t AssignStreams (int64_t streamNum);
104 
118  void SetParameters (Time minInterval, uint8_t doublings, uint16_t redundancy);
119 
124  Time GetMinInterval (void) const;
125 
132  Time GetMaxInterval (void) const;
133 
138  uint8_t GetDoublings (void) const;
139 
144  uint16_t GetRedundancy (void) const;
145 
151  Time GetDelayLeft (void) const;
152 
158  Time GetIntervalLeft (void) const;
159 
163  void Enable ();
164 
168  void ConsistentEvent ();
169 
173  void InconsistentEvent ();
174 
178  void Reset ();
179 
185  void Stop ();
186 
195  template <typename FN>
196  void SetFunction (FN fn);
197 
208  template <typename MEM_PTR, typename OBJ_PTR>
209  void SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr);
210 
211 
220  template <typename... Ts>
221  void SetArguments (Ts&&... args);
224 private:
226  void TimerExpire (void);
228  void IntervalExpire (void);
229 
235 
238 
241 
242 
245  uint16_t m_redundancy;
246 
247  uint64_t m_ticks;
249  uint16_t m_counter;
250 
252 };
253 
254 } // namespace ns3
255 
256 
257 /********************************************************************
258  * Implementation of the templates declared above.
259  ********************************************************************/
260 
261 #include "timer-impl.h"
262 
263 namespace ns3 {
264 
265 
266 template <typename FN>
267 void
269 {
270  delete m_impl;
271  m_impl = MakeTimerImpl (fn);
272 }
273 template <typename MEM_PTR, typename OBJ_PTR>
274 void
275 TrickleTimer::SetFunction (MEM_PTR memPtr, OBJ_PTR objPtr)
276 {
277  delete m_impl;
278  m_impl = MakeTimerImpl (memPtr, objPtr);
279 }
280 
281 template <typename... Ts>
282 void
284 {
285  if (m_impl == 0)
286  {
287  NS_FATAL_ERROR ("You cannot set the arguments of a TrickleTimer before setting its function.");
288  return;
289  }
290  m_impl->SetArgs (std::forward<Ts>(args)...);
291 }
292 
293 } // namespace ns3
294 
295 
296 #endif /* TRICKLE_TIMER_H */
ns3::TrickleTimer::GetMaxInterval
Time GetMaxInterval(void) const
Get the MaxInterval of the timer.
Definition: trickle-timer.cc:100
event-id.h
ns3::EventId declarations.
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::EventId
An identifier for simulation events.
Definition: event-id.h:54
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::TimerImpl::SetArgs
void SetArgs(T1 a1)
Set the arguments to be used when invoking the expire function.
Definition: timer-impl.h:959
timer-impl.h
ns3::TimerImpl declaration and implementation.
ns3::TrickleTimer::m_currentInterval
Time m_currentInterval
Current interval.
Definition: trickle-timer.h:248
random-variable-stream.h
ns3::RandomVariableStream declaration, and related classes.
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::Ptr< UniformRandomVariable >
ns3::MakeTimerImpl
TimerImpl * MakeTimerImpl(FN fn)
Make a TimerImpl from a function pointer taking varying numbers of arguments.
Definition: timer-impl.h:252
NS_FATAL_ERROR
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
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::TrickleTimer::SetArguments
void SetArguments(Ts &&... args)
Set the arguments to be used when invoking the expire function.
Definition: trickle-timer.h:283
ns3::Time
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:104
ns3::TimerImpl
The timer implementation underlying Timer and Watchdog.
Definition: timer-impl.h:43
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
ns3::TrickleTimer::SetFunction
void SetFunction(FN fn)
Set the function to execute when the timer expires.
Definition: trickle-timer.h:268
ns3::TrickleTimer::m_minInterval
Time m_minInterval
Minimum interval.
Definition: trickle-timer.h:243
nstime.h
Declaration of classes ns3::Time and ns3::TimeWithUnit, and the TimeValue implementation classes.
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::TrickleTimer::m_timerExpiration
EventId m_timerExpiration
The future event scheduled to expire the timer.
Definition: trickle-timer.h:237
ns3::TrickleTimer::TrickleTimer
TrickleTimer()
Constructor.
Definition: trickle-timer.cc:30
ns3::TrickleTimer::m_maxInterval
Time m_maxInterval
Maximum interval.
Definition: trickle-timer.h:244
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::TrickleTimer
A Trickle Timer following RFC 6206.
Definition: trickle-timer.h:74