added a clear() method to the TimerEventProxy to allow the removal of all pending...
[senf.git] / senf / Scheduler / TimerEventProxy.hh
1 // $Id$
2 //
3 // Copyright (C) 2010
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Mathias Kretschmer <mtk@berlios.de>
7 //     Jens Moedeker <jens.moedeker@fokus.fraunhofer.de>
8 //
9 // This program is free software; you can redistribute it and/or modify
10 // it under the terms of the GNU General Public License as published by
11 // the Free Software Foundation; either version 2 of the License, or
12 // (at your option) any later version.
13 //
14 // This program is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 // GNU General Public License for more details.
18 //
19 // You should have received a copy of the GNU General Public License
20 // along with this program; if not, write to the
21 // Free Software Foundation, Inc.,
22 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23
24 /** \file
25     \brief TimerEventProxy public header */
26
27 #ifndef HH_SENF_Scheduler_TimerEventProxy_
28 #define HH_SENF_Scheduler_TimerEventProxy_ 1
29
30 // Custom includes
31 #include <boost/multi_index_container.hpp>
32 #include <boost/multi_index/ordered_index.hpp>
33 #include <boost/multi_index/member.hpp>
34
35 #include <senf/Scheduler/ClockService.hh>
36 #include <senf/Scheduler/TimerEvent.hh>
37
38 //-/////////////////////////////////////////////////////////////////////////////////////////////////
39 namespace senf {
40 namespace scheduler {
41
42     /** \brief Deadline timer proxy
43
44         The TimerEventProxy is meant to host long %term deadline timers to reduce the load of the
45         Scheduler with a huge count of TimerEvent items. It registers deadline timer callbacks which
46         will be called when the timer expires.
47
48         The functionality is based on one TimerEvent instance per TimerEventProxy instance and could
49         host a big count of timers.
50      */
51     template<typename IdType>
52     class TimerEventProxy
53     {
54     public:
55         typedef boost::function<void(ClockService::clock_type, IdType const &)> Callback;
56
57         TimerEventProxy(std::string const & description = "");
58                                         ///< Instantiate a TimerEventProxy
59                                         /**< \param[in] description Descriptive name (purely informational) */
60
61         void add(ClockService::clock_type timeout, IdType const & id, Callback cb);
62                                         ///< Add new deadline timer
63
64         bool remove(IdType const & id); ///< Remove timer by given \a id.
65
66         std::vector<std::pair<ClockService::clock_type, IdType> > list() const;
67                                         ///< Returns a vector of all active timers with timeout and id.
68
69         ClockService::clock_type timeout(IdType const & id) const;
70                                         ///< Returns timeout for given id
71                                         /**< if no timer for this id is registered \a 0 is returned. */
72
73         unsigned numEvents() const;  ///< Returns the number of pending timer events 
74
75         void clear(); ///< Clears all pending timer events
76         
77     private:
78 #ifndef DOXYGEN
79         struct Entry {
80             ClockService::clock_type timeout;
81             IdType id;
82             Callback cb;
83
84             Entry(ClockService::clock_type _timeout, IdType _id, Callback _cb)
85                 : timeout(_timeout), id(_id), cb(_cb) { }
86         };
87         struct Timeout {};
88         struct Id {};
89 #endif
90         // data structure to hold active timers
91         typedef boost::multi_index_container<
92             Entry,
93             boost::multi_index::indexed_by<
94                 boost::multi_index::ordered_non_unique<
95                     boost::multi_index::tag<Timeout>,
96                     boost::multi_index::member<Entry, ClockService::clock_type, &Entry::timeout>
97                 >,
98                 boost::multi_index::ordered_unique<
99                     boost::multi_index::tag<Id>,
100                     boost::multi_index::member<Entry, IdType, &Entry::id>
101                 >
102             >
103         > EntrySet_t;
104         typedef typename EntrySet_t::template index<Timeout>::type EntrySetByTimeout_t;
105         typedef typename EntrySet_t::template index<Id>::type EntrySetById_t;
106
107         EntrySet_t entrySet;
108         EntrySetById_t & entrySetById;
109         EntrySetByTimeout_t & entrySetByTimeout;
110
111         scheduler::TimerEvent timer;
112
113         void timerEvent();  // callback for the Scheduler timer event
114     };
115
116 }}
117
118 //-/////////////////////////////////////////////////////////////////////////////////////////////////
119 //#include "TimerEventProxy.cci"
120 #include "TimerEventProxy.ct"
121 //#include "TimerEventProxy.cti"
122 #endif
123
124 \f
125 // Local Variables:
126 // mode: c++
127 // fill-column: 100
128 // comment-column: 40
129 // c-file-style: "senf"
130 // indent-tabs-mode: nil
131 // ispell-local-dictionary: "american"
132 // compile-command: "scons -u test"
133 // End: