DetectorGraph  2.0
timeoutpublisher.hpp
Go to the documentation of this file.
1 // Copyright 2017 Nest Labs, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef DETECTORGRAPH_INCLUDE_TIMEOUTPUBLISHER_HPP_
16 #define DETECTORGRAPH_INCLUDE_TIMEOUTPUBLISHER_HPP_
17 
18 #include "graph.hpp"
19 #include "topicstate.hpp"
21 
22 namespace DetectorGraph
23 {
24 
25 /**
26  * @brief Push data to a topic when timer expires
27  *
28  * TimeoutPublisher provides a mechanism to schedule the publishing of a
29  * TopicState to the graph in the future. Similarly to FuturePublisher,
30  * the published TopicState goes into a separate (future) evaluation and so
31  * toposort constraints do not apply; this allows a detector to publish to
32  * itself.
33  *
34  * Additionally the detector can cancel/reset a scheduled job.
35  *
36  * Example:
37  * @code
38 class HeartbeatDetector : public Detector,
39  public SubscriberInterface<HeartBeat>,
40  public TimeoutPublisher<HeartBeat>
41 {
42  HeartbeatDetector(Graph* graph, TimeoutPublisherService* apTimeoutService) : Detector(graph)
43  {
44  Subscribe<HeartBeat>(this);
45  SetupTimeoutPublishing<HeartBeat>(this, apTimeoutService);
46  }
47 
48  void Evaluate(const HeartBeat&)
49  {
50  // This will be evaluated at 1Hz
51  PublishOnTimeout(HeartBeat(), 1000);
52  }
53 }
54  * @endcode
55  */
56 template<class T>
58 {
59 public:
60  /**
61  * @brief Basic Constructor
62  *
63  * This constructor does not fully initialize the TimeoutPublisher as
64  * a TimeoutPublisherService is needed. That's done by SetTimeoutService.
65  */
68  {
69  }
70 
71  /**
72  * @brief Sets the timeout service and acquires a TimerHandle to be used
73  by the default/simple API.
74  */
75  void SetTimeoutService(TimeoutPublisherService* apTimeoutPublisherService)
76  {
77  mpTimeoutPublisherService = apTimeoutPublisherService;
79  }
80 
81  /**
82  * @brief Empty Virtual Destructor
83  */
84  virtual ~TimeoutPublisher() { }
85 
86  /**
87  * @brief Schedules a TopicState for Publishing after a timeout
88  *
89  * This method is analogous to Publish() in the sense that it's a Detector
90  * output but with two big differences:
91  * - @param aData is only published after @param aMillisecondsFromNow
92  * - @param aData is published to the Graph Input queue instead of another
93  * topic so TopoSort constraints do not apply; this allows a detector to
94  * publish and subscribe to the same topic.
95  * @param aTimerId (optional) allows detectors to control multiple concurrent timers.
96  * TimeoutPublisherHandles are vended through TimeoutPublisherService::GetUniqueTimerHandle
97  */
98  void PublishOnTimeout(const T& aData, const uint64_t aMillisecondsFromNow, TimeoutPublisherHandle aTimerId = kInvalidTimeoutPublisherHandle)
99  {
100  if (aTimerId == kInvalidTimeoutPublisherHandle) { aTimerId = mDefaultHandle; }
101 
102  mpTimeoutPublisherService->ScheduleTimeout<T>(aData, aMillisecondsFromNow, aTimerId);
103  }
104 
105  /**
106  * @brief Cancels the Scheduled PublishOnTimeout
107  */
109  {
110  if (aTimerId == kInvalidTimeoutPublisherHandle) { aTimerId = mDefaultHandle; }
111 
113  }
114 
115  /**
116  * @brief Returns weather a timeout has expired or not.
117  */
119  {
120  if (aTimerId == kInvalidTimeoutPublisherHandle) { aTimerId = mDefaultHandle; }
121 
123  }
124 
125 protected:
126  // Keep reference to Graph in order to push data to graph in future
129 };
130 
131 } // namespace DetectorGraph
132 
133 #endif // DETECTORGRAPH_INCLUDE_TIMEOUTPUBLISHER_HPP_
Push data to a topic when timer expires.
bool HasTimeoutExpired(const TimeoutPublisherHandle aTimerHandle) const
Returns weather the timeout for a given handle has expired/fired already.
void CancelPublishOnTimeout(const TimeoutPublisherHandle aTimerHandle)
Cancels a timeout and deletes the stored TopicState.
TimeoutPublisherHandle GetUniqueTimerHandle()
Returns a unique id/handle for a new timer.
A service that provides Timer function to DetectorGraph Detectors.
void PublishOnTimeout(const T &aData, const uint64_t aMillisecondsFromNow, TimeoutPublisherHandle aTimerId=kInvalidTimeoutPublisherHandle)
Schedules a TopicState for Publishing after a timeout.
void ScheduleTimeout(const T &aData, const TimeOffset aMillisecondsFromNow, const TimeoutPublisherHandle aTimerHandle)
Schedules a TopicState for Publishing after a timeout.
void SetTimeoutService(TimeoutPublisherService *apTimeoutPublisherService)
Sets the timeout service and acquires a TimerHandle to be used by the default/simple API...
TimeoutPublisherHandle mDefaultHandle
void CancelPublishOnTimeout(TimeoutPublisherHandle aTimerId=kInvalidTimeoutPublisherHandle)
Cancels the Scheduled PublishOnTimeout.
virtual ~TimeoutPublisher()
Empty Virtual Destructor.
bool HasTimeoutExpired(TimeoutPublisherHandle aTimerId=kInvalidTimeoutPublisherHandle) const
Returns weather a timeout has expired or not.
TimeoutPublisher()
Basic Constructor.
TimeoutPublisherService * mpTimeoutPublisherService