4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
6 // Stefan Bund <g0dil@berlios.de>
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.
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.
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.
24 \brief ClockService non-inline non-template implementation */
26 #include "ClockService.hh"
27 //#include "ClockService.ih"
34 #include "../Utils/Exception.hh"
36 //#include "ClockService.mpp"
38 ///////////////////////////////cc.p////////////////////////////////////////
40 #define CheckError(op,args) if (op args < 0) throw SystemException(# op, errno)
42 ///////////////////////////////////////////////////////////////////////////
43 // senf::ClockService::Impl
45 struct senf::ClockService::Impl
53 Blocker(Impl * i) : impl(i) { impl->block(); }
54 ~Blocker() { impl->unblock(); }
58 static void timer(int);
60 struct sigaction oldaction;
61 struct itimerval olditimer;
65 prefix_ senf::ClockService::Impl::Impl()
67 CheckError( sigemptyset, (&alrm_set) );
68 CheckError( sigaddset, (&alrm_set, SIGALRM) );
71 prefix_ void senf::ClockService::Impl::block()
73 CheckError( sigprocmask, (SIG_BLOCK, &alrm_set, 0) );
76 prefix_ void senf::ClockService::Impl::unblock()
78 CheckError( sigprocmask, (SIG_UNBLOCK, &alrm_set, 0) );
81 prefix_ void senf::ClockService::Impl::timer(int)
83 ClockService::instance().timer();
86 ///////////////////////////////////////////////////////////////////////////
89 prefix_ senf::ClockService::~ClockService()
91 setitimer(ITIMER_REAL, &impl_->olditimer, 0);
92 sigaction(SIGALRM, &impl_->oldaction, 0);
95 ////////////////////////////////////////
98 prefix_ senf::ClockService::ClockService()
99 : impl_(new ClockService::Impl())
104 prefix_ void senf::ClockService::timer()
106 boost::posix_time::ptime time (boost::posix_time::microsec_clock::universal_time());
108 clockSkew(time, heartbeat_ + boost::posix_time::seconds(
109 ClockService::CheckInterval));
113 prefix_ void senf::ClockService::restart_m(bool restart)
116 // if any syscall fails, the alarm signal stays blocked which is correct
119 base_ = boost::posix_time::microsec_clock::universal_time();
122 struct sigaction action;
123 action.sa_handler = & senf::ClockService::Impl::timer;
124 CheckError( sigemptyset, (&action.sa_mask) );
125 action.sa_flags = SA_RESTART;
126 CheckError( sigaction, (SIGALRM, &action, restart ? 0 : &impl_->oldaction) );
128 restartTimer(restart);
133 prefix_ void senf::ClockService::restartTimer(bool restart)
135 struct itimerval itimer;
136 itimer.it_interval.tv_sec = CheckInterval;
137 itimer.it_interval.tv_usec = 0;
138 itimer.it_value.tv_sec = CheckInterval;
139 itimer.it_value.tv_usec = 0;
140 CheckError( setitimer, (ITIMER_REAL, &itimer, restart ? 0 : &impl_->olditimer) );
143 prefix_ void senf::ClockService::updateSkew(boost::posix_time::ptime time)
145 Impl::Blocker alrmBlocker (impl_.get());
147 // Make a second 'checkSkew' test, this time with SIGALRM blocked. See
148 // senf::ClockService::now_i()
150 if (checkSkew(time)) {
151 struct itimerval itimer;
152 CheckError( getitimer, (ITIMER_REAL, &itimer) );
153 clockSkew(time, (heartbeat_
154 + boost::posix_time::seconds(CheckInterval)
155 - boost::posix_time::seconds(itimer.it_value.tv_sec)
156 - boost::posix_time::microseconds(itimer.it_value.tv_usec)));
162 ///////////////////////////////cc.e////////////////////////////////////////
164 //#include "ClockService.mpp"
170 // comment-column: 40
171 // c-file-style: "senf"
172 // indent-tabs-mode: nil
173 // ispell-local-dictionary: "american"
174 // compile-command: "scons -u test"