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