4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at
9 // http://senf.berlios.de/license.html
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on,
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
15 // Software distributed under the License is distributed on an "AS IS" basis,
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
17 // for the specific language governing rights and limitations under the License.
19 // The Original Code is Fraunhofer FOKUS code.
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V.
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
26 // Mathias Kretschmer <mtk@berlios.de>
27 // Jens Moedeker <jmo@berlios.de>
30 \brief TimerEventProxy public header */
32 #ifndef HH_SENF_Scheduler_TimerEventProxy_
33 #define HH_SENF_Scheduler_TimerEventProxy_ 1
36 #include <boost/multi_index_container.hpp>
37 #include <boost/multi_index/ordered_index.hpp>
38 #include <boost/multi_index/member.hpp>
40 #include <senf/Scheduler/ClockService.hh>
41 #include <senf/Scheduler/TimerEvent.hh>
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
47 /** \brief Deadline timer proxy
49 The TimerEventProxy is meant to host long %term deadline timers to reduce the load of the
50 Scheduler with a huge count of TimerEvent items. It registers deadline timer callbacks which
51 will be called when the timer expires.
53 The functionality is based on one TimerEvent instance per TimerEventProxy instance and could
54 host a big count of timers.
56 template<typename IdType>
60 typedef boost::function<void(ClockService::clock_type, IdType const &)> Callback;
62 TimerEventProxy(std::string const & description = "");
63 ///< Instantiate a TimerEventProxy
64 /**< \param[in] description Descriptive name (purely informational) */
66 void add(ClockService::clock_type timeout, IdType const & id, Callback cb);
67 ///< Add new deadline timer
69 bool remove(IdType const & id); ///< Remove timer by given \a id.
71 std::vector<std::pair<ClockService::clock_type, IdType> > list() const;
72 ///< Returns a vector of all active timers with timeout and id.
74 ClockService::clock_type timeout(IdType const & id) const;
75 ///< Returns timeout for given id
76 /**< if no timer for this id is registered \a 0 is returned. */
78 unsigned numEvents() const; ///< Returns the number of pending timer events
80 void clear(); ///< Clears all pending timer events
85 ClockService::clock_type timeout;
89 Entry(ClockService::clock_type _timeout, IdType _id, Callback _cb)
90 : timeout(_timeout), id(_id), cb(_cb) { }
95 // data structure to hold active timers
96 typedef boost::multi_index_container<
98 boost::multi_index::indexed_by<
99 boost::multi_index::ordered_non_unique<
100 boost::multi_index::tag<Timeout>,
101 boost::multi_index::member<Entry, ClockService::clock_type, &Entry::timeout>
103 boost::multi_index::ordered_unique<
104 boost::multi_index::tag<Id>,
105 boost::multi_index::member<Entry, IdType, &Entry::id>
109 typedef typename EntrySet_t::template index<Timeout>::type EntrySetByTimeout_t;
110 typedef typename EntrySet_t::template index<Id>::type EntrySetById_t;
113 EntrySetById_t & entrySetById;
114 EntrySetByTimeout_t & entrySetByTimeout;
116 scheduler::TimerEvent timer;
118 void timerEvent(); // callback for the Scheduler timer event
123 //-/////////////////////////////////////////////////////////////////////////////////////////////////
124 //#include "TimerEventProxy.cci"
125 #include "TimerEventProxy.ct"
126 //#include "TimerEventProxy.cti"
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"