b5d37e46acfbc4da4f136266440ed1b2a6b438c8
[senf.git] / senf / Scheduler / TimerEvent.cc
1 // $Id$
2 //
3 // Copyright (C) 2008
4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 //     Stefan Bund <g0dil@berlios.de>
7 //
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 /** \file
24     \brief TimerDispatcher non-inline non-template implementation */
25
26 #include "TimerEvent.hh"
27 #include "TimerEvent.ih"
28
29 // Custom includes
30 #include <sstream>
31
32 //#include "TimerEvent.mpp"
33 #define prefix_
34 //-/////////////////////////////////////////////////////////////////////////////////////////////////
35
36 prefix_ senf::scheduler::detail::TimerDispatcher::TimerDispatcher()
37     : source_ (new PollTimerSource())
38 {}
39
40 prefix_ senf::scheduler::detail::TimerDispatcher::~TimerDispatcher()
41 {
42     TimerSet::iterator i (timers_.begin());
43     TimerSet::iterator const i_end (timers_.end());
44     for (; i != i_end; ++i)
45         FIFORunner::instance().dequeue(&(*i));
46 }
47
48 void senf::scheduler::detail::TimerDispatcher::add(TimerEvent & event)
49 {
50     TimerSet::iterator i (timers_.insert(event));
51     FIFORunner::instance().enqueue(&(*i));
52 }
53
54 prefix_ void senf::scheduler::detail::TimerDispatcher::remove(TimerEvent & event)
55 {
56     TimerSet::iterator i (TimerSet::current(event));
57     if (i == timers_.end())
58         return;
59     FIFORunner::instance().dequeue(&(*i));
60     timers_.erase(i);
61 }
62
63 prefix_ void senf::scheduler::detail::TimerDispatcher::prepareRun()
64 {
65     TimerSet::iterator i (timers_.begin());
66     TimerSet::iterator const i_end (timers_.end());
67     ClockService::clock_type now (FdManager::instance().eventTime());
68     for (; i != i_end && i->timeout_ <= now ; ++i)
69         i->setRunnable();
70 }
71
72 prefix_ void senf::scheduler::detail::TimerDispatcher::reschedule()
73 {
74     if (timers_.empty())
75         source_->notimeout();
76     else
77         source_->timeout(timers_.begin()->timeout_);
78 }
79
80 //-/////////////////////////////////////////////////////////////////////////////////////////////////
81 // senf::scheduler::detail::TimerDispatcher::TimerEvent
82
83 prefix_ void senf::scheduler::TimerEvent::v_run()
84 {
85     disable();
86     cb_();
87 }
88
89 prefix_ char const * senf::scheduler::TimerEvent::v_type()
90     const
91 {
92     return "tm";
93 }
94
95 prefix_ std::string senf::scheduler::TimerEvent::v_info()
96     const
97 {
98     std::stringstream ss;
99     ss.imbue( std::locale(ss.getloc(),
100                           new boost::posix_time::time_facet("%Y-%m-%d %H:%M:%S.%f-0000")) );
101     ss << "expire " << ClockService::abstime(timeout_);
102     return ss.str();
103 }
104
105 //-/////////////////////////////////////////////////////////////////////////////////////////////////
106 #undef prefix_
107 //#include "TimerEvent.mpp"
108
109 \f
110 // Local Variables:
111 // mode: c++
112 // fill-column: 100
113 // comment-column: 40
114 // c-file-style: "senf"
115 // indent-tabs-mode: nil
116 // ispell-local-dictionary: "american"
117 // compile-command: "scons -u test"
118 // End: