a1c744a6344804af01ae2ad2e613241a829cc30d
[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 non-inline template implementation */
26
27 // Custom includes
28
29 #define prefix_
30 ///////////////////////////////ct.p////////////////////////////////////////
31
32 template<typename IdType>
33 prefix_ senf::scheduler::TimerEventProxy<IdType>::TimerEventProxy(std::string const & description)
34     : entrySetById( entrySet.template get<Id>()),
35       entrySetByTimeout( entrySet.template get<Timeout> ()),
36       timer( "TimerEventProxy " + description,
37               membind(&TimerEventProxy<IdType>::timerEvent, this), 0, false)
38 { }
39
40 template<typename IdType>
41 prefix_ void senf::scheduler::TimerEventProxy<IdType>::timerEvent()
42 {
43     ClockService::clock_type now = senf::scheduler::now();
44     typename EntrySetByTimeout_t::iterator it = entrySetByTimeout.begin();
45     while (it != entrySetByTimeout.end() && it->timeout <= now) {
46         Entry item (*it);
47         // remove due entry from set
48         entrySetByTimeout.erase(it);
49         // call callback
50         item.cb(now, item.id);
51         it = entrySetByTimeout.begin();
52     }
53     if (entrySet.size() > 0)
54         timer.timeout(entrySetByTimeout.begin()->timeout);
55 }
56
57 template<typename IdType>
58 prefix_ void senf::scheduler::TimerEventProxy<IdType>::add(
59         ClockService::clock_type timeout, IdType const & id, Callback cb)
60 {
61     // insert new entry
62     entrySetByTimeout.insert( Entry(timeout, id, cb));
63     // the scheduler time to the first earliest timeout (ordered index)
64     timer.timeout( entrySetByTimeout.begin()->timeout);
65 }
66
67 template<typename IdType>
68 prefix_ bool senf::scheduler::TimerEventProxy<IdType>::remove(IdType const & id)
69 {
70     bool removed (entrySetById.erase( id) > 0);
71     if (entrySet.size() > 0)
72         timer.timeout(entrySetByTimeout.begin()->timeout);
73     else
74         timer.disable();
75     return removed;
76 }
77
78 template<typename IdType>
79 prefix_ senf::ClockService::clock_type senf::scheduler::TimerEventProxy<IdType>::timeout(IdType const & id)
80     const
81 {
82     typename EntrySetById_t::const_iterator i ( entrySetById.find( id));
83     return i == entrySetById.end() ? 0 : i->timeout;
84 }
85
86
87 template<typename IdType>
88 prefix_ std::vector<std::pair<senf::ClockService::clock_type, IdType> > senf::scheduler::TimerEventProxy<IdType>::list()
89     const
90 {
91     std::vector<std::pair<ClockService::clock_type, IdType> > tmp;
92
93     typename EntrySetByTimeout_t::const_iterator it;
94     for (it = entrySetByTimeout.begin(); it != entrySetByTimeout.end(); ++it) {
95         tmp.push_back(std::make_pair<ClockService::clock_type, IdType>( it->timeout, it->id));
96     }
97     return tmp;
98 }
99
100 ///////////////////////////////ct.e////////////////////////////////////////
101 #undef prefix_
102
103 \f
104 // Local Variables:
105 // mode: c++
106 // fill-column: 100
107 // c-file-style: "senf"
108 // indent-tabs-mode: nil
109 // ispell-local-dictionary: "american"
110 // compile-command: "scons -u test"
111 // comment-column: 40
112 // End:
113
114