A Discrete-Event Network Simulator
API
watchdog.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007 INRIA
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: Mathieu Lacage <mathieu.lacage@sophia.inria.fr>
18  */
19 #ifndef WATCHDOG_H
20 #define WATCHDOG_H
21 
22 #include "event-id.h"
23 #include "nstime.h"
24 
31 namespace ns3
32 {
33 
34 class TimerImpl;
35 
56 class Watchdog
57 {
58  public:
60  Watchdog();
62  ~Watchdog();
63 
73  void Ping(Time delay);
74 
83  template <typename FN>
84  void SetFunction(FN fn);
85 
96  template <typename MEM_PTR, typename OBJ_PTR>
97  void SetFunction(MEM_PTR memPtr, OBJ_PTR objPtr);
98 
107  template <typename... Ts>
108  void SetArguments(Ts&&... args);
111  private:
113  void Expire();
123 };
124 
125 } // namespace ns3
126 
127 /********************************************************************
128  * Implementation of the templates declared above.
129  ********************************************************************/
130 
131 #include "timer-impl.h"
132 
133 namespace ns3
134 {
135 
136 template <typename FN>
137 void
139 {
140  delete m_impl;
141  m_impl = MakeTimerImpl(fn);
142 }
143 
144 template <typename MEM_PTR, typename OBJ_PTR>
145 void
146 Watchdog::SetFunction(MEM_PTR memPtr, OBJ_PTR objPtr)
147 {
148  delete m_impl;
149  m_impl = MakeTimerImpl(memPtr, objPtr);
150 }
151 
152 template <typename... Ts>
153 void
155 {
156  if (m_impl == nullptr)
157  {
158  NS_FATAL_ERROR("You cannot set the arguments of a Watchdog before setting its function.");
159  return;
160  }
161  m_impl->SetArgs(std::forward<Ts>(args)...);
162 }
163 
164 } // namespace ns3
165 
166 #endif /* WATCHDOG_H */
An identifier for simulation events.
Definition: event-id.h:55
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
The timer implementation underlying Timer and Watchdog.
Definition: timer-impl.h:43
void SetArgs(T1 a1)
Set the arguments to be used when invoking the expire function.
Definition: timer-impl.h:1085
A very simple watchdog operating in virtual time.
Definition: watchdog.h:57
void Expire()
Internal callback invoked when the timer expires.
Definition: watchdog.cc:63
void SetArguments(Ts &&... args)
Set the arguments to be used when invoking the expire function.
Definition: watchdog.h:154
EventId m_event
The future event scheduled to expire the timer.
Definition: watchdog.h:120
~Watchdog()
Destructor.
Definition: watchdog.cc:42
Watchdog()
Constructor.
Definition: watchdog.cc:34
void Ping(Time delay)
Delay the timer.
Definition: watchdog.cc:50
void SetFunction(FN fn)
Set the function to execute when the timer expires.
Definition: watchdog.h:138
TimerImpl * m_impl
The timer implementation, which contains the bound callback function and arguments.
Definition: watchdog.h:118
Time m_end
The absolute time when the timer will expire.
Definition: watchdog.h:122
ns3::EventId declarations.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
TimerImpl * MakeTimerImpl(FN fn)
Make a TimerImpl from a function pointer taking varying numbers of arguments.
Definition: timer-impl.h:254
Every class exported by the ns3 library is enclosed in the ns3 namespace.
Declaration of classes ns3::Time and ns3::TimeWithUnit, and the TimeValue implementation classes.
ns3::TimerImpl declaration and implementation.