bdad612bf1adbdd1fa0f4884a3008758f69fafcc
[senf.git] / senf / Scheduler / TimerEventProxy.ct
1 // $Id$
2 //
3 // Copyright (C) 2010
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 //
6 // The contents of this file are subject to the Fraunhofer FOKUS Public License
7 // Version 1.0 (the "License"); you may not use this file except in compliance
8 // with the License. You may obtain a copy of the License at 
9 // http://senf.berlios.de/license.html
10 //
11 // The Fraunhofer FOKUS Public License Version 1.0 is based on, 
12 // but modifies the Mozilla Public License Version 1.1.
13 // See the full license text for the amendments.
14 //
15 // Software distributed under the License is distributed on an "AS IS" basis, 
16 // WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
17 // for the specific language governing rights and limitations under the License.
18 //
19 // The Original Code is Fraunhofer FOKUS code.
20 //
21 // The Initial Developer of the Original Code is Fraunhofer-Gesellschaft e.V. 
22 // (registered association), Hansastraße 27 c, 80686 Munich, Germany.
23 // All Rights Reserved.
24 //
25 // Contributor(s):
26 //   Mathias Kretschmer <mtk@berlios.de>
27 //   Jens Moedeker <jmo@berlios.de>
28
29 /** \file
30     \brief TimerEventProxy non-inline template implementation */
31
32 // Custom includes
33 #include <senf/Utils/membind.hh>
34
35 #define prefix_
36 //-/////////////////////////////////////////////////////////////////////////////////////////////////
37
38 template<typename IdType>
39 prefix_ senf::scheduler::TimerEventProxy<IdType>::TimerEventProxy(std::string const & description)
40     : entrySetById( entrySet.template get<Id>()),
41       entrySetByTimeout( entrySet.template get<Timeout> ()),
42       timer( "TimerEventProxy " + description,
43               membind(&TimerEventProxy<IdType>::timerEvent, this), SENF_INT2CLOCKTYPE(0), false)
44 { }
45
46 template<typename IdType>
47 prefix_ void senf::scheduler::TimerEventProxy<IdType>::timerEvent()
48 {
49     ClockService::clock_type now = senf::scheduler::now();
50     typename EntrySetByTimeout_t::iterator it = entrySetByTimeout.begin();
51     while (it != entrySetByTimeout.end() && it->timeout <= now) {
52         Entry item (*it);
53         // remove due entry from set
54         entrySetByTimeout.erase(it);
55         // call callback
56         item.cb(now, item.id);
57         it = entrySetByTimeout.begin();
58     }
59     if (entrySet.size() > 0)
60         timer.timeout(entrySetByTimeout.begin()->timeout);
61 }
62
63 template<typename IdType>
64 prefix_ void senf::scheduler::TimerEventProxy<IdType>::add(
65         ClockService::clock_type timeout, IdType const & id, Callback cb)
66 {
67     // insert new entry or replace the timeout of an entry already indexed
68     typename EntrySetById_t::iterator i = entrySetById.find(id);
69     if (i == entrySetById.end())
70         entrySetByTimeout.insert( Entry(timeout, id, cb));
71         else {
72                 Entry tmp = *i;
73                 tmp.timeout = timeout;
74                 entrySetById.replace(i,tmp);
75         }
76     // the scheduler time to the first earliest timeout (ordered index)
77     timer.timeout( entrySetByTimeout.begin()->timeout);
78 }
79
80 template<typename IdType>
81 prefix_ bool senf::scheduler::TimerEventProxy<IdType>::remove(IdType const & id)
82 {
83     bool removed (entrySetById.erase( id) > 0);
84     if (entrySet.size() > 0)
85         timer.timeout(entrySetByTimeout.begin()->timeout);
86     else
87         timer.disable();
88     return removed;
89 }
90
91 template<typename IdType>
92 prefix_ senf::ClockService::clock_type senf::scheduler::TimerEventProxy<IdType>::timeout(IdType const & id)
93     const
94 {
95     typename EntrySetById_t::const_iterator i ( entrySetById.find( id));
96     return i == entrySetById.end() ? SENF_INT2CLOCKTYPE(0) : i->timeout;
97 }
98
99
100 template<typename IdType>
101 prefix_ std::vector<std::pair<senf::ClockService::clock_type, IdType> > senf::scheduler::TimerEventProxy<IdType>::list()
102     const
103 {
104     std::vector<std::pair<ClockService::clock_type, IdType> > tmp;
105
106     typename EntrySetByTimeout_t::const_iterator it;
107     for (it = entrySetByTimeout.begin(); it != entrySetByTimeout.end(); ++it) {
108         tmp.push_back(std::make_pair<ClockService::clock_type, IdType>( it->timeout, it->id));
109     }
110     return tmp;
111 }
112
113 template<typename IdType>
114 prefix_ unsigned senf::scheduler::TimerEventProxy<IdType>::numEvents()
115   const
116 {
117     return entrySetByTimeout.size();
118 }
119
120 template<typename IdType>
121 prefix_ void senf::scheduler::TimerEventProxy<IdType>::clear()
122 {
123     entrySetByTimeout.clear();
124 }
125
126 //-/////////////////////////////////////////////////////////////////////////////////////////////////
127 #undef prefix_
128
129 \f
130 // Local Variables:
131 // mode: c++
132 // fill-column: 100
133 // c-file-style: "senf"
134 // indent-tabs-mode: nil
135 // ispell-local-dictionary: "american"
136 // compile-command: "scons -u test"
137 // comment-column: 40
138 // End:
139
140