A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
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
25/**
26 * \file
27 * \ingroup timer
28 * ns3::Watchdog timer class declaration.
29 */
30
31namespace ns3
32{
33
34namespace internal
35{
36
37class TimerImpl;
38
39} // namespace internal
40
41/**
42 * \ingroup timer
43 * \brief A very simple watchdog operating in virtual time.
44 *
45 * The watchdog timer is started by calling Ping with a delay value.
46 * Once started the timer cannot be suspended, cancelled or shortened.
47 * It _can_ be lengthened (delayed) by calling Ping again: if the new
48 * expire time (current simulation time plus the new delay)
49 * is greater than the old expire time the timer will be extended
50 * to the new expire time.
51 *
52 * Typical usage would be to periodically Ping the Watchdog, extending
53 * it's execution time. If the owning process fails to Ping before
54 * the Watchdog expires, the registered function will be invoked.
55 *
56 * If you don't ping the watchdog sufficiently often, it triggers its
57 * listening function.
58 *
59 * \see Timer for a more sophisticated general purpose timer.
60 */
62{
63 public:
64 /** Constructor. */
65 Watchdog();
66 /** Destructor. */
67 ~Watchdog();
68
69 /**
70 * Delay the timer.
71 *
72 * \param [in] delay The watchdog delay
73 *
74 * After a call to this method, the watchdog will not be triggered
75 * until the delay specified has been expired. This operation is
76 * sometimes named "re-arming" a watchdog in some operating systems.
77 */
78 void Ping(Time delay);
79
80 /**
81 * Set the function to execute when the timer expires.
82 *
83 * \tparam FN \deduced The type of the function.
84 * \param [in] fn The function
85 *
86 * Store this function in this Timer for later use by Timer::Schedule.
87 */
88 template <typename FN>
89 void SetFunction(FN fn);
90
91 /**
92 * Set the function to execute when the timer expires.
93 *
94 * \tparam MEM_PTR \deduced Class method function type.
95 * \tparam OBJ_PTR \deduced Class type containing the function.
96 * \param [in] memPtr The member function pointer
97 * \param [in] objPtr The pointer to object
98 *
99 * Store this function and object in this Timer for later use by Timer::Schedule.
100 */
101 template <typename MEM_PTR, typename OBJ_PTR>
102 void SetFunction(MEM_PTR memPtr, OBJ_PTR objPtr);
103
104 /**
105 * Set the arguments to be used when invoking the expire function.
106 */
107 /**@{*/
108 /**
109 * \tparam Ts \deduced Argument types.
110 * \param [in] args arguments
111 */
112 template <typename... Ts>
113 void SetArguments(Ts&&... args);
114 /**@}*/
115
116 private:
117 /** Internal callback invoked when the timer expires. */
118 void Expire();
119 /**
120 * The timer implementation, which contains the bound callback
121 * function and arguments.
122 */
124 /** The future event scheduled to expire the timer. */
126 /** The absolute time when the timer will expire. */
128};
129
130} // namespace ns3
131
132/********************************************************************
133 * Implementation of the templates declared above.
134 ********************************************************************/
135
136#include "timer-impl.h"
137
138namespace ns3
139{
140
141template <typename FN>
142void
144{
145 delete m_impl;
147}
148
149template <typename MEM_PTR, typename OBJ_PTR>
150void
151Watchdog::SetFunction(MEM_PTR memPtr, OBJ_PTR objPtr)
152{
153 delete m_impl;
154 m_impl = internal::MakeTimerImpl(memPtr, objPtr);
155}
156
157template <typename... Ts>
158void
160{
161 if (m_impl == nullptr)
162 {
163 NS_FATAL_ERROR("You cannot set the arguments of a Watchdog before setting its function.");
164 return;
165 }
166 m_impl->SetArgs(std::forward<Ts>(args)...);
167}
168
169} // namespace ns3
170
171#endif /* WATCHDOG_H */
An identifier for simulation events.
Definition: event-id.h:55
This application behaves similarly to the Unix ping application, although with fewer options supporte...
Definition: ping.h:56
Simulation virtual time values and global simulation resolution.
Definition: nstime.h:105
A very simple watchdog operating in virtual time.
Definition: watchdog.h:62
internal::TimerImpl * m_impl
The timer implementation, which contains the bound callback function and arguments.
Definition: watchdog.h:123
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:159
EventId m_event
The future event scheduled to expire the timer.
Definition: watchdog.h:125
~Watchdog()
Destructor.
Definition: watchdog.cc:42
Watchdog()
Constructor.
Definition: watchdog.cc:34
void SetFunction(FN fn)
Set the function to execute when the timer expires.
Definition: watchdog.h:143
Time m_end
The absolute time when the timer will expire.
Definition: watchdog.h:127
The timer implementation underlying Timer and Watchdog.
Definition: timer-impl.h:46
void SetArgs(Args... args)
Set the arguments to be used when invoking the expire function.
Definition: timer-impl.h:199
ns3::EventId declarations.
#define NS_FATAL_ERROR(msg)
Report a fatal error with a message and terminate.
Definition: fatal-error.h:179
TimerImpl * MakeTimerImpl(U(fn)(Ts...))
Make a TimerImpl from a function pointer taking varying numbers of arguments.
Definition: timer-impl.h:103
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.