Move include files in debian packge into 'senf' subdirectory
[senf.git] / Scheduler / ClockService.cc
1 // $Id$
2 //
3 // Copyright (C) 2007 
4 // Fraunhofer Institut fuer offene Kommunikationssysteme (FOKUS)
5 // Kompetenzzentrum fuer Satelitenkommunikation (SatCom)
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 ClockService non-inline non-template implementation */
25
26 #include "ClockService.hh"
27 //#include "ClockService.ih"
28
29 // Custom includes
30 #include <errno.h>
31 #include <signal.h>
32 #include <sys/time.h>
33 #include <pthread.h>
34 #include "../Utils/Exception.hh"
35
36 //#include "ClockService.mpp"
37 #define prefix_
38 ///////////////////////////////cc.p////////////////////////////////////////
39
40 #define CheckError(op,args) if (op args < 0) throw SystemException(# op, errno)
41
42 ///////////////////////////////////////////////////////////////////////////
43 // senf::ClockService::Impl
44
45 struct senf::ClockService::Impl 
46 {
47     Impl();
48
49     void block();
50     void unblock();
51
52     struct Blocker {
53         Blocker(Impl * i) : impl(i) { impl->block(); }
54         ~Blocker() { impl->unblock(); }
55         Impl * impl;
56     };
57
58     static void timer(int);
59
60     struct sigaction oldaction;
61     struct itimerval olditimer;
62     sigset_t alrm_set;
63 };
64
65 prefix_ senf::ClockService::Impl::Impl()
66 {
67     CheckError( sigemptyset, (&alrm_set) );
68     CheckError( sigaddset, (&alrm_set, SIGALRM) );
69 }
70
71 prefix_ void senf::ClockService::Impl::block()
72 {
73     CheckError( sigprocmask, (SIG_BLOCK, &alrm_set, 0) );
74 }
75
76 prefix_ void senf::ClockService::Impl::unblock()
77 {
78     CheckError( sigprocmask, (SIG_UNBLOCK, &alrm_set, 0) );
79 }
80
81 prefix_ void senf::ClockService::Impl::timer(int)
82 {
83     ClockService::instance().timer();
84 }
85
86 ///////////////////////////////////////////////////////////////////////////
87 // senf::ClockService
88
89 prefix_ senf::ClockService::~ClockService()
90 {
91     setitimer(ITIMER_REAL, &impl_->olditimer, 0);
92     sigaction(SIGALRM, &impl_->oldaction, 0);
93 }
94
95 ////////////////////////////////////////
96 // private members
97
98 prefix_ senf::ClockService::ClockService()
99     : impl_(new ClockService::Impl())
100 {
101     restart_m(false);
102 }
103
104 prefix_ void senf::ClockService::timer()
105 {
106     boost::posix_time::ptime time (boost::posix_time::microsec_clock::universal_time());
107     if (checkSkew(time))
108         clockSkew(time, heartbeat_ + boost::posix_time::seconds(
109                       ClockService::CheckInterval));
110     heartbeat_ = time;
111 }
112
113 prefix_ void senf::ClockService::restart_m(bool restart)
114 {
115     if (restart)
116         // if any syscall fails, the alarm signal stays blocked which is correct
117         impl_->block(); 
118
119     base_ = boost::posix_time::microsec_clock::universal_time();
120     heartbeat_ = base_;
121
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) );
127
128     restartTimer(restart);
129     
130     impl_->unblock();
131 }
132
133 prefix_ void senf::ClockService::restartTimer(bool restart)
134 {
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) );
141 }
142
143 prefix_ void senf::ClockService::updateSkew(boost::posix_time::ptime time)
144 {
145     Impl::Blocker alrmBlocker (impl_.get());
146     
147     // Make a second 'checkSkew' test, this time with SIGALRM blocked. See
148     // senf::ClockService::now_i()
149
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)));
157         heartbeat_ = time;
158         restartTimer();
159     }
160 }
161
162 ///////////////////////////////cc.e////////////////////////////////////////
163 #undef prefix_
164 //#include "ClockService.mpp"
165
166 \f
167 // Local Variables:
168 // mode: c++
169 // fill-column: 100
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"
175 // End: