15 #ifndef DETECTORGRAPH_INCLUDE_TIMEOUTPUBLISHERSERVICE_HPP_ 16 #define DETECTORGRAPH_INCLUDE_TIMEOUTPUBLISHERSERVICE_HPP_ 21 #if defined(BUILD_FEATURE_DETECTORGRAPH_CONFIG_LITE) 23 #include "detectorgraphliteconfig.hpp" 55 struct DispatcherInterface
57 virtual void Dispatch(
Graph& aGraph) = 0;
58 virtual ~DispatcherInterface() {}
66 struct Dispatcher :
public DispatcherInterface
68 Dispatcher() : mData() {}
69 Dispatcher(
const T& aData) : mData(aData) {}
70 virtual void Dispatch(
Graph& aGraph)
83 struct PeriodicPublishingSeries
85 TimeOffset mPublishingPeriodMsec;
86 TimeOffset mMetronomeAccumulator;
87 DispatcherInterface* mpDispatcher;
89 PeriodicPublishingSeries(TimeOffset aPublishingPeriodMsec,
90 DispatcherInterface* aDispatcher)
91 : mPublishingPeriodMsec(aPublishingPeriodMsec)
92 , mMetronomeAccumulator(0)
93 , mpDispatcher(aDispatcher) {}
97 #if defined(BUILD_FEATURE_DETECTORGRAPH_CONFIG_LITE) 99 DetectorGraphConfig::kMaxNumberOfTimeouts> TimeoutDispatchersContainer;
101 DetectorGraphConfig::kMaxNumberOfPeriodicTimers> PeriodicPublishingSeriesContainer;
102 struct TimeoutCtxt {};
104 struct PeriodicCtxt {};
107 typedef std::vector<DispatcherInterface*> TimeoutDispatchersContainer;
108 typedef std::vector<PeriodicPublishingSeries> PeriodicPublishingSeriesContainer;
156 #if defined(BUILD_FEATURE_DETECTORGRAPH_CONFIG_LITE) 157 SchedulePeriodicPublishingDispatcher(mPeriodicDispatchersAllocator.New<Dispatcher<T>>(), aPeriodInMilliseconds);
159 SchedulePeriodicPublishingDispatcher(
new Dispatcher<T>(), aPeriodInMilliseconds);
161 #if defined(BUILD_FEATURE_DETECTORGRAPH_CONFIG_INSTRUMENT_RESOURCE_USAGE) 162 #if defined(BUILD_FEATURE_DETECTORGRAPH_CONFIG_LITE) 163 DG_LOG(
"Scheduling PeriodicPublishing every %d milliseconds\n", aPeriodInMilliseconds);
165 DG_LOG(
"Scheduling PeriodicPublishing of %s every %d milliseconds\n", T().GetName(), aPeriodInMilliseconds);
184 void ScheduleTimeout(
const T& aData,
const TimeOffset aMillisecondsFromNow,
const TimeoutPublisherHandle aTimerHandle)
188 #if defined(BUILD_FEATURE_DETECTORGRAPH_CONFIG_LITE) 189 ScheduleTimeoutDispatcher(mTimeoutDispatchersAllocator.New<Dispatcher<T>>(aData), aMillisecondsFromNow, aTimerHandle);
191 ScheduleTimeoutDispatcher(
new Dispatcher<T>(aData), aMillisecondsFromNow, aTimerHandle);
193 #if defined(BUILD_FEATURE_DETECTORGRAPH_CONFIG_INSTRUMENT_RESOURCE_USAGE) 194 #if defined(BUILD_FEATURE_DETECTORGRAPH_CONFIG_LITE) 195 DG_LOG(
"Scheduling Timeout in %d milliseconds\n", aMillisecondsFromNow);
197 DG_LOG(
"Scheduling Timeout for %s in %d milliseconds\n", aData.GetName(), aMillisecondsFromNow);
225 virtual TimeOffset
GetTime()
const = 0;
255 virtual void SetTimeout(
const TimeOffset aMillisecondsFromNow,
const TimeoutPublisherHandle) = 0;
262 virtual void Start(
const TimeoutPublisherHandle) = 0;
269 virtual void Cancel(
const TimeoutPublisherHandle) = 0;
285 virtual void StartMetronome(
const TimeOffset aPeriodInMilliseconds) = 0;
298 void ScheduleTimeoutDispatcher(DispatcherInterface* aDispatcher,
const TimeOffset aMillisecondsFromNow,
const TimeoutPublisherHandle aTimerHandle);
303 void SchedulePeriodicPublishingDispatcher(DispatcherInterface* aDispatcher,
const TimeOffset aPeriodInMilliseconds);
308 TimeOffset gcd(TimeOffset lhs, TimeOffset rhs);
319 TimeOffset mMetronomePeriodMsec;
324 TimeoutDispatchersContainer mTimeoutDispatchers;
329 PeriodicPublishingSeriesContainer mPeriodicSeries;
331 #if defined(BUILD_FEATURE_DETECTORGRAPH_CONFIG_LITE) 332 TimeoutDispatchersAllocator mTimeoutDispatchersAllocator;
333 PeriodicDispatchersAllocator mPeriodicDispatchersAllocator;
339 #endif // DETECTORGRAPH_INCLUDE_TIMEOUTPUBLISHERSERVICE_HPP_ 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.
virtual void Start(const TimeoutPublisherHandle)=0
Should start a timer for the given handle.
void SchedulePeriodicPublishing(const TimeOffset aPeriodInMilliseconds)
Schedules a TopicState for publishing periodically.
virtual TimeOffset GetMonotonicTime() const =0
Should return monotonic time since some unspecified starting point.
Implements a graph of Topics & Detectors with Input/Output APIs.
virtual ~TimeoutPublisherService()
Destructor.
TimeoutPublisherHandle GetUniqueTimerHandle()
Returns a unique id/handle for a new timer.
void PushData(const TTopicState &aTopicState)
Push data to a specific topic in the graph.
TimeoutPublisherService(Graph &graph)
Constructor that initializes the service connected to a graph.
virtual void SetTimeout(const TimeOffset aMillisecondsFromNow, const TimeoutPublisherHandle)=0
Should setup a timeout for the given handle.
A service that provides Timer function to DetectorGraph Detectors.
void DG_LOG(const char *aLogString,...)
virtual void StartMetronome(const TimeOffset aPeriodInMilliseconds)=0
Should start the metronome (periodic timer) for the given period.
int TimeoutPublisherHandle
void ScheduleTimeout(const T &aData, const TimeOffset aMillisecondsFromNow, const TimeoutPublisherHandle aTimerHandle)
Schedules a TopicState for Publishing after a timeout.
void StartPeriodicPublishing()
Starts a Metronome to publish scheduled TopicStates.
virtual void CancelMetronome()=0
Should stop the metronome.
void TimeoutExpired(const TimeoutPublisherHandle aTimerHandle)
Fires/Dispatches a TopicState that was pending on a timeout.
void MetronomeFired()
Update metronome counters and Fires/Dispatches TopicStates that was pending on scheduled period...
virtual TimeOffset GetTime() const =0
Should return the time offset to Epoch.
virtual void Cancel(const TimeoutPublisherHandle)=0
Should cancel the timer the given handle.