c9a58f6901326c6a58559f2d77b4b4e918c40c58
[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 #include <senf/Utils/membind.hh>
29
30 #define prefix_
31 //-/////////////////////////////////////////////////////////////////////////////////////////////////
32
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)
39 { }
40
41 template<typename IdType>
42 prefix_ void senf::scheduler::TimerEventProxy<IdType>::timerEvent()
43 {
44     ClockService::clock_type now = senf::scheduler::now();
45     typename EntrySetByTimeout_t::iterator it = entrySetByTimeout.begin();
46     while (it != entrySetByTimeout.end() && it->timeout <= now) {
47         Entry item (*it);
48         // remove due entry from set
49         entrySetByTimeout.erase(it);
50         // call callback
51         item.cb(now, item.id);
52         it = entrySetByTimeout.begin();
53     }
54     if (entrySet.size() > 0)
55         timer.timeout(entrySetByTimeout.begin()->timeout);
56 }
57
58 template<typename IdType>
59 prefix_ void senf::scheduler::TimerEventProxy<IdType>::add(
60         ClockService::clock_type timeout, IdType const & id, Callback cb)
61 {
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));
66         else{
67                 Entry tmp = *i;
68                 tmp.timeout = timeout;
69                 entrySetById.replace(i,tmp);
70         }
71     // the scheduler time to the first earliest timeout (ordered index)
72     timer.timeout( entrySetByTimeout.begin()->timeout);
73 }
74
75 template<typename IdType>
76 prefix_ bool senf::scheduler::TimerEventProxy<IdType>::remove(IdType const & id)
77 {
78     bool removed (entrySetById.erase( id) > 0);
79     if (entrySet.size() > 0)
80         timer.timeout(entrySetByTimeout.begin()->timeout);
81     else
82         timer.disable();
83     return removed;
84 }
85
86 template<typename IdType>
87 prefix_ senf::ClockService::clock_type senf::scheduler::TimerEventProxy<IdType>::timeout(IdType const & id)
88     const
89 {
90     typename EntrySetById_t::const_iterator i ( entrySetById.find( id));
91     return i == entrySetById.end() ? 0 : i->timeout;
92 }
93
94
95 template<typename IdType>
96 prefix_ std::vector<std::pair<senf::ClockService::clock_type, IdType> > senf::scheduler::TimerEventProxy<IdType>::list()
97     const
98 {
99     std::vector<std::pair<ClockService::clock_type, IdType> > tmp;
100
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));
104     }
105     return tmp;
106 }
107
108 template<typename IdType>
109 prefix_ unsigned senf::scheduler::TimerEventProxy<IdType>::numEvents()
110   const
111 {
112   return entrySetByTimeout.size();
113 }
114
115 //-/////////////////////////////////////////////////////////////////////////////////////////////////
116 #undef prefix_
117
118 \f
119 // Local Variables:
120 // mode: c++
121 // fill-column: 100
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
127 // End:
128
129