switch to new MPL based Fraunhofer FOKUS Public License
[senf.git] / senf / Scheduler / TimerEventProxy.hh
1 // $Id$
2 //
3 // Copyright (C) 2010
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
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
10 //
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.
14 //
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.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
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.
24 //
25 // Contributor(s):
26 //   Mathias Kretschmer <mtk@berlios.de>
27 //   Jens Moedeker <jmo@berlios.de>
28
29 /** \file
30     \brief TimerEventProxy public header */
31
32 #ifndef HH_SENF_Scheduler_TimerEventProxy_
33 #define HH_SENF_Scheduler_TimerEventProxy_ 1
34
35 // Custom includes
36 #include <boost/multi_index_container.hpp>
37 #include <boost/multi_index/ordered_index.hpp>
38 #include <boost/multi_index/member.hpp>
39
40 #include <senf/Scheduler/ClockService.hh>
41 #include <senf/Scheduler/TimerEvent.hh>
42
43 //-/////////////////////////////////////////////////////////////////////////////////////////////////
44 namespace senf {
45 namespace scheduler {
46
47     /** \brief Deadline timer proxy
48
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.
52
53         The functionality is based on one TimerEvent instance per TimerEventProxy instance and could
54         host a big count of timers.
55      */
56     template<typename IdType>
57     class TimerEventProxy
58     {
59     public:
60         typedef boost::function<void(ClockService::clock_type, IdType const &)> Callback;
61
62         TimerEventProxy(std::string const & description = "");
63                                         ///< Instantiate a TimerEventProxy
64                                         /**< \param[in] description Descriptive name (purely informational) */
65
66         void add(ClockService::clock_type timeout, IdType const & id, Callback cb);
67                                         ///< Add new deadline timer
68
69         bool remove(IdType const & id); ///< Remove timer by given \a id.
70
71         std::vector<std::pair<ClockService::clock_type, IdType> > list() const;
72                                         ///< Returns a vector of all active timers with timeout and id.
73
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. */
77
78         unsigned numEvents() const;  ///< Returns the number of pending timer events 
79
80         void clear(); ///< Clears all pending timer events
81         
82     private:
83 #ifndef DOXYGEN
84         struct Entry {
85             ClockService::clock_type timeout;
86             IdType id;
87             Callback cb;
88
89             Entry(ClockService::clock_type _timeout, IdType _id, Callback _cb)
90                 : timeout(_timeout), id(_id), cb(_cb) { }
91         };
92         struct Timeout {};
93         struct Id {};
94 #endif
95         // data structure to hold active timers
96         typedef boost::multi_index_container<
97             Entry,
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>
102                 >,
103                 boost::multi_index::ordered_unique<
104                     boost::multi_index::tag<Id>,
105                     boost::multi_index::member<Entry, IdType, &Entry::id>
106                 >
107             >
108         > EntrySet_t;
109         typedef typename EntrySet_t::template index<Timeout>::type EntrySetByTimeout_t;
110         typedef typename EntrySet_t::template index<Id>::type EntrySetById_t;
111
112         EntrySet_t entrySet;
113         EntrySetById_t & entrySetById;
114         EntrySetByTimeout_t & entrySetByTimeout;
115
116         scheduler::TimerEvent timer;
117
118         void timerEvent();  // callback for the Scheduler timer event
119     };
120
121 }}
122
123 //-/////////////////////////////////////////////////////////////////////////////////////////////////
124 //#include "TimerEventProxy.cci"
125 #include "TimerEventProxy.ct"
126 //#include "TimerEventProxy.cti"
127 #endif
128
129 \f
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: