TimerEvent.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
17 #include "TimerEvent.hh"
18 #include "TimerEvent.ih"
19 
20 // Custom includes
21 #include <sstream>
22 
23 //#include "TimerEvent.mpp"
24 #define prefix_
25 //-/////////////////////////////////////////////////////////////////////////////////////////////////
26 
27 prefix_ senf::scheduler::detail::TimerDispatcher::TimerDispatcher()
28  : source_ (new PollTimerSource())
29 {}
30 
31 prefix_ senf::scheduler::detail::TimerDispatcher::~TimerDispatcher()
32 {
33  TimerSet::iterator i (timers_.begin());
34  TimerSet::iterator const i_end (timers_.end());
35  for (; i != i_end; ++i)
36  FIFORunner::instance().dequeue(&(*i));
37 }
38 
39 void senf::scheduler::detail::TimerDispatcher::add(TimerEvent & event)
40 {
41  TimerSet::iterator i (timers_.insert(event));
42  FIFORunner::instance().enqueue(&(*i));
43 }
44 
45 prefix_ void senf::scheduler::detail::TimerDispatcher::remove(TimerEvent & event)
46 {
47  TimerSet::iterator i (TimerSet::s_iterator_to(event));
48  if (i == timers_.end())
49  return;
50  FIFORunner::instance().dequeue(&(*i));
51  timers_.erase(i);
52 }
53 
54 prefix_ void senf::scheduler::detail::TimerDispatcher::prepareRun()
55 {
56  TimerSet::iterator i (timers_.begin());
57  TimerSet::iterator const i_end (timers_.end());
58  ClockService::clock_type const & now (FdManager::instance().eventTime());
59  for (; i != i_end && i->timeout_ <= now ; ++i)
60  i->setRunnable();
61 }
62 
63 prefix_ void senf::scheduler::detail::TimerDispatcher::reschedule()
64 {
65  if (timers_.empty())
66  source_->notimeout();
67  else
68  source_->timeout(timers_.begin()->timeout_);
69 }
70 
71 //-/////////////////////////////////////////////////////////////////////////////////////////////////
72 // senf::scheduler::detail::TimerDispatcher::TimerEvent
73 
74 prefix_ void senf::scheduler::TimerEvent::v_run()
75 {
76  disable();
77  cb_();
78 }
79 
80 prefix_ char const * senf::scheduler::TimerEvent::v_type()
81  const
82 {
83  return "tm";
84 }
85 
86 prefix_ std::string senf::scheduler::TimerEvent::v_info()
87  const
88 {
89  std::stringstream ss;
90  ss.imbue( std::locale(ss.getloc(),
91  new boost::posix_time::time_facet("%Y-%m-%d %H:%M:%S.%f-0000")) );
92  ss << "expire " << ClockService::abstime(timeout_);
93  return ss.str();
94 }
95 
96 //-/////////////////////////////////////////////////////////////////////////////////////////////////
97 #undef prefix_
98 //#include "TimerEvent.mpp"
99 
100 
101 // Local Variables:
102 // mode: c++
103 // fill-column: 100
104 // comment-column: 40
105 // c-file-style: "senf"
106 // indent-tabs-mode: nil
107 // ispell-local-dictionary: "american"
108 // compile-command: "scons -u test"
109 // End:
TimerDispatcher public header.
ClockService::clock_type const & now()
Return (approximate) current time.
ClockService::clock_type const & eventTime()
Return timestamp of last event.
static abstime_type abstime(clock_type const &clock)
Convert clock to absolute time.
#define prefix_
Definition: TimerEvent.cc:24
void disable()
Disable timer.