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 non-inline template implementation */
28 #include <senf/Utils/membind.hh>
31 //-/////////////////////////////////////////////////////////////////////////////////////////////////
33 template<typename IdType>
34 prefix_ senf::scheduler::TimerEventProxy<IdType>::TimerEventProxy(std::string const & description)
35 : entrySetById( entrySet.template get<Id>()),
36 entrySetByTimeout( entrySet.template get<Timeout> ()),
37 timer( "TimerEventProxy " + description,
38 membind(&TimerEventProxy<IdType>::timerEvent, this), 0, false)
41 template<typename IdType>
42 prefix_ void senf::scheduler::TimerEventProxy<IdType>::timerEvent()
44 ClockService::clock_type now = senf::scheduler::now();
45 typename EntrySetByTimeout_t::iterator it = entrySetByTimeout.begin();
46 while (it != entrySetByTimeout.end() && it->timeout <= now) {
48 // remove due entry from set
49 entrySetByTimeout.erase(it);
51 item.cb(now, item.id);
52 it = entrySetByTimeout.begin();
54 if (entrySet.size() > 0)
55 timer.timeout(entrySetByTimeout.begin()->timeout);
58 template<typename IdType>
59 prefix_ void senf::scheduler::TimerEventProxy<IdType>::add(
60 ClockService::clock_type timeout, IdType const & id, Callback cb)
62 // insert new entry or replace the timeout of an entry already indexed
63 typename EntrySetById_t::iterator i = entrySetById.find(id);
64 if(i == entrySetById.end())
65 entrySetByTimeout.insert( Entry(timeout, id, cb));
68 tmp.timeout = timeout;
69 entrySetById.replace(i,tmp);
71 // the scheduler time to the first earliest timeout (ordered index)
72 timer.timeout( entrySetByTimeout.begin()->timeout);
75 template<typename IdType>
76 prefix_ bool senf::scheduler::TimerEventProxy<IdType>::remove(IdType const & id)
78 bool removed (entrySetById.erase( id) > 0);
79 if (entrySet.size() > 0)
80 timer.timeout(entrySetByTimeout.begin()->timeout);
86 template<typename IdType>
87 prefix_ senf::ClockService::clock_type senf::scheduler::TimerEventProxy<IdType>::timeout(IdType const & id)
90 typename EntrySetById_t::const_iterator i ( entrySetById.find( id));
91 return i == entrySetById.end() ? 0 : i->timeout;
95 template<typename IdType>
96 prefix_ std::vector<std::pair<senf::ClockService::clock_type, IdType> > senf::scheduler::TimerEventProxy<IdType>::list()
99 std::vector<std::pair<ClockService::clock_type, IdType> > tmp;
101 typename EntrySetByTimeout_t::const_iterator it;
102 for (it = entrySetByTimeout.begin(); it != entrySetByTimeout.end(); ++it) {
103 tmp.push_back(std::make_pair<ClockService::clock_type, IdType>( it->timeout, it->id));
108 template<typename IdType>
109 prefix_ unsigned senf::scheduler::TimerEventProxy<IdType>::numEvents()
112 return entrySetByTimeout.size();
115 //-/////////////////////////////////////////////////////////////////////////////////////////////////
122 // c-file-style: "senf"
123 // indent-tabs-mode: nil
124 // ispell-local-dictionary: "american"
125 // compile-command: "scons -u test"
126 // comment-column: 40