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>
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.
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.
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.
25 \brief TimerEventProxy public header */
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> ())
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> ())
43 node.add(name, senf::console::factory::Command(
44 &TimerEventProxy<T>::list, this) .doc("List active Timers"));
48 prefix_ void senf::scheduler::TimerEventProxy<T>::timerEvent() {
50 senf::ClockService::clock_type actual = senf::ClockService::now();
51 typename EntrySetByTimeout_t::iterator it;
53 // execute the timer callbacks first
55 it = entrySetByTimeout.begin();
56 while (it != entrySetByTimeout.end() && it->timeout <= actual) {
58 // remove due entry from set
59 entrySetByTimeout.erase(it);
61 item.cb(actual, item.id);
63 it = entrySetByTimeout.begin();
66 if (entrySet.size() > 0) {
67 timer.timeout(entrySetByTimeout.begin()->timeout);
71 template<class IdType>
72 prefix_ void senf::scheduler::TimerEventProxy<IdType>::add(
73 senf::ClockService::clock_type timeout, IdType const & id, Callback cb)
76 entrySetByTimeout.insert( Entry(timeout, id, cb));
78 // the scheduler time to the first earliest timeout (ordered index)
79 timer.timeout( entrySetByTimeout.begin()->timeout);
82 template<class IdType>
83 prefix_ bool senf::scheduler::TimerEventProxy<IdType>::remove(IdType const & id)
85 return entrySetById.erase( id) > 0;
88 template<class IdType>
89 prefix_ std::vector<std::pair<senf::ClockService::clock_type, IdType> > senf::scheduler::TimerEventProxy<IdType>::list()
91 std::vector<std::pair<senf::ClockService::clock_type, IdType> > tmp;
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));