Scheduler/TimerEventProxy: some code clean ups
[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 //     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 #define prefix_ 
28
29 template<class T>
30 prefix_ senf::scheduler::TimerEventProxy<T>::TimerEventProxy() 
31     : timer("TimerEventProxy", senf::membind(&TimerEventProxy<T>::timerEvent, this), 0, false), 
32       entrySetById(entrySet.template get<Id> ()),
33       entrySetByTimeout(entrySet.template get<Timeout> ())
34 { }
35
36 template<class T>
37 prefix_ senf::scheduler::TimerEventProxy<T>::TimerEventProxy( std::string const & name,
38         senf::console::DirectoryNode & node) 
39     : timer("TimerEventProxy", senf::membind(&TimerEventProxy<T>::timerEvent, this), 0, false),
40       entrySetById(entrySet.template get<Id> ()),
41       entrySetByTimeout(entrySet.template get<Timeout> ())
42 {
43     node.add(name, senf::console::factory::Command(
44             &TimerEventProxy<T>::listTimers, this) .doc("List active Timers"));
45 }
46
47 template<class T>
48 prefix_ void senf::scheduler::TimerEventProxy<T>::timerEvent() {
49
50     senf::ClockService::clock_type actual = senf::ClockService::now();
51     typename EntrySetByTimeout_t::iterator it;
52
53     // execute the timer callbacks first
54
55     it = entrySetByTimeout.begin();
56     while (it != entrySetByTimeout.end() && it->timeout <= actual) {
57         Entry item (*it);
58         // remove due entry from set
59         entrySetByTimeout.erase(it);
60         // call callback
61         item.cb(actual, item.id);
62
63         it = entrySetByTimeout.begin();
64     }
65
66     if (entrySet.size() > 0) {
67         timer.timeout(entrySetByTimeout.begin()->timeout);
68     }
69 }
70
71 template<class IdType>
72 prefix_ void senf::scheduler::TimerEventProxy<IdType>::add(
73         senf::ClockService::clock_type timeout, IdType const & id, Callback cb)
74 {
75     // insert new entry
76     entrySetByTimeout.insert( Entry(timeout, id, cb));
77
78     // the scheduler time to the first earliest timeout (ordered index)
79     timer.timeout( entrySetByTimeout.begin()->timeout);
80 }
81
82 template<class IdType>
83 prefix_ bool senf::scheduler::TimerEventProxy<IdType>::remove(IdType const & id)
84 {
85     return entrySetById.erase( id) > 0;
86 }
87
88 template<class IdType>
89 prefix_ std::vector<std::pair<senf::ClockService::clock_type, IdType> > senf::scheduler::TimerEventProxy<IdType>::list()
90 {
91     std::vector<std::pair<senf::ClockService::clock_type, IdType> > tmp;
92
93     typename EntrySetByTimeout_t::iterator it;
94     for (it = entrySetByTimeout.begin(); it != entrySetByTimeout.end(); ++it) {
95         tmp.push_back(std::make_pair<senf::ClockService::clock_type, IdType>( it->timeout, it->id));
96     }
97
98     return tmp;
99 }
100
101 #undef prefix_
102