TimerEventProxy.hh
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 #ifndef HH_SENF_Scheduler_TimerEventProxy_
18 #define HH_SENF_Scheduler_TimerEventProxy_ 1
19 
20 // Custom includes
21 #include <boost/multi_index_container.hpp>
22 #include <boost/multi_index/ordered_index.hpp>
23 #include <boost/multi_index/member.hpp>
27 
28 //-/////////////////////////////////////////////////////////////////////////////////////////////////
29 namespace senf {
30 namespace scheduler {
31 
41  template<typename IdType>
43  {
44  public:
45  typedef boost::function<void(ClockService::clock_type const &, IdType const &)> Callback;
46  typedef boost::function<void(IdType const & id, senf::ClockService::clock_type const & duration, StatisticsData const & durationsStats)> DurationExceededCallback;
47 
48  TimerEventProxy(std::string const & description = "",
49  senf::ClockService::clock_type const & callbackDurationWarningThreshold = senf::ClockService::clock_type(0),
50  DurationExceededCallback _decb = 0);
52 
55  void add(ClockService::clock_type const & timeout, IdType const & id, Callback cb);
57 
58  bool remove(IdType const & id);
59 
60  std::vector<std::pair<ClockService::clock_type, IdType> > list() const;
62 
63  ClockService::clock_type timeout(IdType const & id) const;
65 
67  unsigned numEvents() const;
68 
69  void clear();
70 
72 
73  private:
74 #ifndef DOXYGEN
75  struct Entry {
77  IdType id;
78  Callback cb;
79 
80  Entry(ClockService::clock_type const & _timeout, IdType _id, Callback _cb)
81  : timeout(_timeout), id(_id), cb(_cb) { }
82  };
83  struct ChangeTimeout {
85  ChangeTimeout(senf::ClockService::clock_type const & t);
86  void operator()(Entry & entry);
87  };
88  struct Timeout {};
89  struct Id {};
90 #endif
91  // data structure to hold active timers
92  typedef boost::multi_index_container<
93  Entry,
94  boost::multi_index::indexed_by<
95  boost::multi_index::ordered_non_unique<
96  boost::multi_index::tag<Timeout>,
97  boost::multi_index::member<Entry, ClockService::clock_type, &Entry::timeout>
98  >,
99  boost::multi_index::ordered_unique<
100  boost::multi_index::tag<Id>,
101  boost::multi_index::member<Entry, IdType, &Entry::id>
102  >
103  >
104  > EntrySet_t;
105  typedef typename EntrySet_t::template index<Timeout>::type EntrySetByTimeout_t;
106  typedef typename EntrySet_t::template index<Id>::type EntrySetById_t;
107 
108  EntrySet_t entrySet_;
109  EntrySetById_t & entrySetById_;
110  EntrySetByTimeout_t & entrySetByTimeout_;
111 
112  scheduler::TimerEvent timer_;
113  ClockService::clock_type callbackDurationThreshold_;
114  StatisticAccumulator<std::uint64_t> callbackDurations_;
115  DurationExceededCallback durationExceededCallback_;
116 
117  void timerEvent(); // callback for the Scheduler timer event
118 
119  };
120 
121 }}
122 
123 //-/////////////////////////////////////////////////////////////////////////////////////////////////
124 //#include "TimerEventProxy.cci"
125 #include "TimerEventProxy.ct"
126 //#include "TimerEventProxy.cti"
127 #endif
128 
129 
130 // Local Variables:
131 // mode: c++
132 // fill-column: 100
133 // comment-column: 40
134 // c-file-style: "senf"
135 // indent-tabs-mode: nil
136 // ispell-local-dictionary: "american"
137 // compile-command: "scons -u test"
138 // End:
config::time_type clock_type
ClockService timer data type.
Definition: ClockService.hh:78
TimerDispatcher public header.
Deadline timer event.
Definition: TimerEvent.hh:59
boost::function< void(IdType const &id, senf::ClockService::clock_type const &duration, StatisticsData const &durationsStats)> DurationExceededCallback
boost::function< void(ClockService::clock_type const &, IdType const &)> Callback
void clear()
Clears all pending timer events.
StatisticsData callbackDurationStats()
Returns and clears the timeInCallback statistics.
TimerEventProxy(std::string const &description="", senf::ClockService::clock_type const &callbackDurationWarningThreshold=senf::ClockService::clock_type(0), DurationExceededCallback _decb=0)
Instantiate a TimerEventProxy.
std::vector< std::pair< ClockService::clock_type, IdType > > list() const
Returns a vector of all active timers with timeout and id.
ClockService public header.
void add(ClockService::clock_type const &timeout, IdType const &id, Callback cb)
Add new deadline timer.
unsigned numEvents() const
Returns the number of pending timer events.
ClockService::clock_type timeout(IdType const &id) const
Returns timeout for given id.