IntervalTimer.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #include "IntervalTimer.hh"
18 //#include "IntervalTimer.ih"
19 
20 // Custom includes
21 #include "EventManager.hh"
22 
23 #define prefix_
24 //-/////////////////////////////////////////////////////////////////////////////////////////////////
25 
26 //-/////////////////////////////////////////////////////////////////////////////////////////////////
27 // senf::ppi::IntervalTimer
28 
29 //-/////////////////////////////////////////////////////////////////////////////////////////////////
30 // private members
31 
32 prefix_ void senf::ppi::IntervalTimer::v_enable()
33 {
34  if (eventsPerInterval_ <= 0)
35  return;
36  info_.intervalStart = manager().now();
37  info_.number = 0;
38  schedule();
39 }
40 
41 prefix_ void senf::ppi::IntervalTimer::v_disable()
42 {
43  timer_.disable();
44 }
45 
46 prefix_ void senf::ppi::IntervalTimer::schedule()
47 {
48  info_.expected = info_.intervalStart + ( interval_ * (info_.number+1) ) / eventsPerInterval_;
49  timer_.timeout(info_.expected);
50 }
51 
52 prefix_ void senf::ppi::IntervalTimer::cb()
53 {
54  callback(info_, info_.expected);
55  if (! enabled())
56  return;
57  ++ info_.number;
58  if (info_.number >= eventsPerInterval_) {
59  info_.number = 0;
60  info_.intervalStart += interval_;
61  }
62  schedule();
63 }
64 
65 //-/////////////////////////////////////////////////////////////////////////////////////////////////
66 #undef prefix_
67 //#include "IntervalTimer.mpp"
68 
69 
70 // Local Variables:
71 // mode: c++
72 // fill-column: 100
73 // comment-column: 40
74 // c-file-style: "senf"
75 // indent-tabs-mode: nil
76 // ispell-local-dictionary: "american"
77 // compile-command: "scons -u test"
78 // End:
void callback(EventArg event, ClockService::clock_type time)
Forward event to user callback.
ClockService::clock_type const & now()
Current time at last event dispatch.
EventManager public header.
void timeout(ClockService::clock_type const &timeout, bool initiallyEnabled=true)
#define prefix_
EventManager & manager() const
EventManager of the event.
unsigned number
Number of the current event within the interval.
bool enabled() const
Check, whether the event is currently enabled.
ClockService::clock_type intervalStart
Start of the current time interval.
IntervalTimer public header.
ClockService::clock_type expected
Scheduled event time.