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 */
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:103
Time GetIntervalLeft(void) const
~TrickleTimer()
Destructor.
TimerImpl * MakeTimerImpl(FN fn)
Make a TimerImpl from a function pointer taking varying numbers of arguments.
Definition: timer-impl.h:252
uint64_t m_ticks
Interval span (i.e., exp2(doublings)).
void ConsistentEvent()
Records a consistent event.
uint8_t GetDoublings(void) const
Get the doublings of the timer.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:165
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.
Declaration of classes ns3::Time and ns3::TimeWithUnit, and the TimeValue implementation classes...
Time m_minInterval
Minimum interval.
void SetArguments(Ts &&... args)
Set the arguments to be used when invoking the expire function.
void Reset()
Reset the timer.
TrickleTimer()
Constructor.
void SetParameters(Time minInterval, uint8_t doublings, uint16_t redundancy)
Set the timer parameters.
void SetFunction(FN fn)
Set the function to execute when the timer expires.
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.
The timer implementation underlying Timer and Watchdog.
Definition: timer-impl.h:42
Ptr< UniformRandomVariable > m_uniRand
Object to generate uniform random numbers.
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.
An identifier for simulation events.
Definition: event-id.h:53
void IntervalExpire(void)
Internal callback invoked when the interval expires.
ns3::RandomVariableStream declaration, and related classes.
Time m_maxInterval
Maximum interval.
void SetArgs(T1 a1)
Set the arguments to be used when invoking the expire function.
Definition: timer-impl.h:959
Time m_currentInterval
Current interval.
void Enable()
Enable the timer.
ns3::TimerImpl declaration and implementation.
ns3::EventId declarations.
A Trickle Timer following RFC 6206.
Definition: trickle-timer.h:73