Scheduler/TimerEventProxy: include fix for last commit :(
[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 #ifdef SENF_DEBUG
31 #   define BOOST_MULTI_INDEX_ENABLE_INVARIANT_CHECKING
32 #   define BOOST_MULTI_INDEX_ENABLE_SAFE_MODE
33 #endif
34
35 #include <boost/range/iterator_range.hpp>
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 #include <senf/Utils/Console/Console.hh>
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         ///////////////////////////////////////////////////////////////////////////
61         // Types
62         typedef boost::function<void(senf::ClockService::clock_type, IdType const &)> Callback;
63
64         TimerEventProxy();
65         ///< Instantiate a TimerEventProxy
66
67         TimerEventProxy(std::string const & name, senf::console::DirectoryNode & node);
68         ///< Instantiate a TimerEventProxy and add the list command to the give DirectoryNode
69
70         void add(senf::ClockService::clock_type timeout, IdType const &id, Callback cb);
71         ///< Add new deadline timer
72         bool remove(IdType const & id);
73         ///< Remove timer by given \a id.
74         std::vector<std::pair<senf::ClockService::clock_type, IdType> > list();
75         ///< Returns a vector of all active timers with timeout and id.
76         
77     private:
78 #ifndef DOXYGEN
79         struct Entry {
80             senf::ClockService::clock_type timeout;
81             IdType id;
82             Callback cb;
83
84             Entry(senf::ClockService::clock_type _timeout, IdType _id, Callback _cb)
85                 : timeout(_timeout), id(_id), cb(_cb) { }
86         };
87
88         senf::scheduler::TimerEvent timer;
89
90         //
91         // data structure to hold active timers
92         //
93         struct Timeout {};
94         struct Id {};
95 #endif
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, senf::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
110         typedef typename EntrySet_t::template index<Timeout>::type EntrySetByTimeout_t;
111         typedef typename EntrySet_t::template index<Id>::type EntrySetById_t;
112
113         EntrySet_t entrySet;
114         EntrySetById_t & entrySetById;
115         EntrySetByTimeout_t & entrySetByTimeout;
116
117         // callback for the Scheduler timer event
118         void timerEvent();
119     };
120 }
121 }
122
123 #include "TimerEventProxy.ct"
124
125 #endif