08301ec306efaa8e4f7932d8c9cfa7d1a13354c2
[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 #include <senf/Utils/Console/Console.hh>
38
39 ///////////////////////////////hh.p////////////////////////////////////////
40 namespace senf {
41 namespace scheduler {
42
43     /** \brief Deadline timer proxy
44
45         The TimerEventProxy is meant to host long term deadline timers to reduce the load of the
46         Scheduler with a huge count of TimerEvent items. It registers deadline timer callbacks which
47         will be called when the timer expires.
48
49         The functionality is based on one TimerEvent instance per TimerEventProxy instance and could
50         host a big count of timers.
51      */
52     template<typename IdType>
53     class TimerEventProxy 
54     {
55     public:
56         typedef boost::function<void(ClockService::clock_type, IdType const &)> Callback;
57
58         TimerEventProxy();              ///< Instantiate a TimerEventProxy
59         TimerEventProxy(std::string const & name, console::DirectoryNode & node);
60                                         /**< \brief Instantiate a TimerEventProxy and add the list
61                                                     command to the give DirectoryNode */
62         
63         void add(ClockService::clock_type timeout, IdType const & id, Callback cb);
64                                         ///< Add new deadline timer
65         
66         bool remove(IdType const & id); ///< Remove timer by given \a id.
67         
68         std::vector<std::pair<ClockService::clock_type, IdType> > list() const;
69                                         ///< Returns a vector of all active timers with timeout and id.
70         
71         ClockService::clock_type timeout(IdType const & id) const;
72                                         ///< Returns timeout for given id
73                                         /**< if no timer for this id is registered \a 0 is returned. */ 
74     private:
75 #ifndef DOXYGEN
76         struct Entry {
77             ClockService::clock_type timeout;
78             IdType id;
79             Callback cb;
80
81             Entry(ClockService::clock_type _timeout, IdType _id, Callback _cb)
82                 : timeout(_timeout), id(_id), cb(_cb) { }
83         };
84         struct Timeout {};
85         struct Id {};
86 #endif
87         // data structure to hold active timers
88         typedef boost::multi_index_container<
89             Entry,
90             boost::multi_index::indexed_by<
91                 boost::multi_index::ordered_non_unique<
92                     boost::multi_index::tag<Timeout>,
93                     boost::multi_index::member<Entry, ClockService::clock_type, &Entry::timeout> 
94                 >,
95                 boost::multi_index::ordered_unique<
96                     boost::multi_index::tag<Id>,
97                     boost::multi_index::member<Entry, IdType, &Entry::id> 
98                 >
99             >
100         > EntrySet_t;
101         typedef typename EntrySet_t::template index<Timeout>::type EntrySetByTimeout_t;
102         typedef typename EntrySet_t::template index<Id>::type EntrySetById_t;
103
104         EntrySet_t entrySet;
105         EntrySetById_t & entrySetById;
106         EntrySetByTimeout_t & entrySetByTimeout;
107         
108         scheduler::TimerEvent timer;
109
110         void timerEvent();  // callback for the Scheduler timer event
111     };
112
113 }}
114
115 ///////////////////////////////hh.e////////////////////////////////////////
116 //#include "TimerEventProxy.cci"
117 #include "TimerEventProxy.ct"
118 //#include "TimerEventProxy.cti"
119 #endif
120
121 \f
122 // Local Variables:
123 // mode: c++
124 // fill-column: 100
125 // comment-column: 40
126 // c-file-style: "senf"
127 // indent-tabs-mode: nil
128 // ispell-local-dictionary: "american"
129 // compile-command: "scons -u test"
130 // End: