new timer event proxy to reduce scheduler load
[senf.git] / senf / Scheduler / TimerEventProxy.ct
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 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24  \brief TimerEventProxy public header */
25
26 #define prefix_ 
27
28 template<class T>
29 prefix_ senf::scheduler::TimerEventProxy<T>::TimerEventProxy() :
30     timer("timer", senf::membind(&TimerEventProxy<T>::timerEvent, this), 0,
31             false), entrySetById(entrySet.template get<Id> ()),
32             entrySetByTimeout(entrySet.template get<Timeout> ())
33 {
34
35 }
36
37 template<class T>
38 prefix_ senf::scheduler::TimerEventProxy<T>::TimerEventProxy( std::string const & name,
39         senf::console::DirectoryNode & node) :
40     timer("timer", senf::membind(&TimerEventProxy<T>::timerEvent, this), 0,
41             false), entrySetById(entrySet.template get<Id> ()),
42             entrySetByTimeout(entrySet.template get<Timeout> ())
43 {
44     node.add(name, senf::console::factory::Command(
45             &TimerEventProxy<T>::listTimers, this) .doc("List active Timers"));
46 }
47
48 template<class T>
49 prefix_ void senf::scheduler::TimerEventProxy<T>::timerEvent() {
50
51     senf::ClockService::clock_type actual = senf::ClockService::now();
52     typename EntrySetByTimeout_t::iterator it;
53
54     // execute the timer callbacks first
55
56     it = entrySetByTimeout.begin();
57     while (it != entrySetByTimeout.end() && it->timeout <= actual) {
58         Entry<T> item(*it);
59         // remove due entry from set
60         entrySetByTimeout.erase(it);
61         // call callback
62         item.fkt(actual, item.id);
63
64         it = entrySetByTimeout.begin();
65     }
66
67     if (entrySet.size() > 0) {
68         timer.timeout(entrySetByTimeout.begin()->timeout);
69     }
70 }
71
72 template<class T>
73 prefix_ void senf::scheduler::TimerEventProxy<T>::add(
74         senf::ClockService::clock_type timeout, T const & id, Callback fkt)
75 {
76     // insert new entry
77     entrySetByTimeout.insert(Entry<T> (timeout, id, fkt));
78
79     // the scheduler time to the first earliest timeout (ordered index)
80     timer.timeout(entrySetByTimeout.begin()->timeout);
81
82     //  // if map was empty before, hence we need to activate the time event object
83     //  if( entrySetByTimeout.size() >= 1){
84     //      timer.enable();
85     //  }
86 }
87
88 template<class T>
89 prefix_ bool senf::scheduler::TimerEventProxy<T>::del(T const & id)
90 {
91     typename EntrySetById_t::iterator it(entrySetById.find(Entry<T> (0, id, NULL)));
92
93     if (it != entrySetById.end()) {
94         entrySetById.erase(it);
95         return true;
96     }
97     return false;
98 }
99
100 template<class T>
101 prefix_ std::vector<std::pair<senf::ClockService::clock_type, T> > senf::scheduler::TimerEventProxy<T>::list()
102 {
103     std::vector<std::pair<senf::ClockService::clock_type, T> > tmp;
104
105     typename EntrySetByTimeout_t::iterator it;
106     for (it = entrySetByTimeout.begin(); it != entrySetByTimeout.end(); ++it) {
107         tmp.push_back(std::make_pair<senf::ClockService::clock_type, T>( it->timeout, it->id));
108     }
109
110     return tmp;
111 }
112
113 #undef prefix_
114