DetectorGraph  2.0
testtimeoutpublisherservice.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_TEST_UTIL_TESTTIMEOUTPUBLISHERSERVICE_HPP_
16 #define DETECTORGRAPH_TEST_UTIL_TESTTIMEOUTPUBLISHERSERVICE_HPP_
17 
18 #include "graph.hpp"
19 #include "topicstate.hpp"
21 
22 #if defined(BUILD_FEATURE_DETECTORGRAPH_CONFIG_LITE)
23 // LITE_BEGIN
24 #include "detectorgraphliteconfig.hpp"
26 // LITE_END
27 #else
28 // FULL_BEGIN
29 #include <map>
30 // FULL_END
31 #endif
32 
33 namespace DetectorGraph
34 {
35 
36 /**
37  * @brief Push data to a topic when timer expires
38  *
39  * TimeoutPublisher provides a mechanism to schedule the publish of data to a topic.
40  * Additionally the detector can cancel/reset a scheduled job.
41  */
43 {
44 #if defined(BUILD_FEATURE_DETECTORGRAPH_CONFIG_LITE)
45  struct TimeOffsetPairType
46  {
48  TimeOffset second;
49  TimeOffsetPairType(TimeoutPublisherHandle aHandle, TimeOffset aOffset) : first(aHandle), second(aOffset) {}
50  };
52 #else
53  typedef std::map<TimeoutPublisherHandle, TimeOffset> TimeOffsetsContainer;
54 #endif
55  typedef TimeOffsetsContainer::iterator TimeOffsetsIterator;
56 
57 public:
58  // Implementing TimeoutPublisherService derived class
60 
61  virtual TimeOffset GetTime() const;
62  virtual TimeOffset GetMonotonicTime() const;
63 
64 protected:
65  virtual void SetTimeout(const TimeOffset aMillisecondsFromNow, const TimeoutPublisherHandle aTimerId);
66  virtual void Start(const TimeoutPublisherHandle aTimerId);
67  virtual void Cancel(const TimeoutPublisherHandle aTimerId);
68  virtual void StartMetronome(const TimeOffset aPeriodInMilliseconds);
69  virtual void CancelMetronome();
70 
71 public:
72  void SetWallClockOffset(int64_t aWallClockOffset);
73  bool FireNextTimeout();
74  bool ForwardTimeAndEvaluate(TimeOffset aFwdTime, Graph& aGraphToEvaluate);
76 
77 private:
78  TimeOffsetsIterator GetNextTimeout();
79 
80 private:
81  static const TimeOffset kInvalidMaxOffset;
82  static const TimeoutPublisherHandle kMetronomeId;
83  // Mock Inspection state
84  // This could be optimized for time with a queue on the next deadline to
85  // remove the O(N) search for next deadline. But ffs, this is a mock class!
86  TimeOffsetsContainer mTimerDeadlines;
87 
88  // Equivalent to monotonic time
89  TimeOffset mElapsedTime;
90  // Offset summed with monotonic time to produce GetTime()
91  int64_t mWallClockOffset;
92 
93  TimeOffset mMetronomeTimerPeriod;
94 };
95 
96 } // namespace DetectorGraph
97 
98 #endif // DETECTORGRAPH_TEST_UTIL_TESTTIMEOUTPUBLISHERSERVICE_HPP_
virtual void SetTimeout(const TimeOffset aMillisecondsFromNow, const TimeoutPublisherHandle aTimerId)
Should setup a timeout for the given handle.
Implements a graph of Topics & Detectors with Input/Output APIs.
Definition: graph.hpp:127
Push data to a topic when timer expires.
virtual TimeOffset GetTime() const
Should return the time offset to Epoch.
virtual void Start(const TimeoutPublisherHandle aTimerId)
Should start a timer for the given handle.
A service that provides Timer function to DetectorGraph Detectors.
virtual TimeOffset GetMonotonicTime() const
Should return monotonic time since some unspecified starting point.
virtual void CancelMetronome()
Should stop the metronome.
virtual void StartMetronome(const TimeOffset aPeriodInMilliseconds)
Should start the metronome (periodic timer) for the given period.
bool ForwardTimeAndEvaluate(TimeOffset aFwdTime, Graph &aGraphToEvaluate)
virtual void Cancel(const TimeoutPublisherHandle aTimerId)
Should cancel the timer the given handle.