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