Socket: Ignore ECONNREFUSED on write to datagram socket
[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     struct itimerval itimer;
129     itimer.it_interval.tv_sec = CheckInterval;
130     itimer.it_interval.tv_usec = 0;
131     itimer.it_value.tv_sec = CheckInterval;
132     itimer.it_value.tv_usec = 0;
133     CheckError( setitimer, (ITIMER_REAL, &itimer, restart ? 0 : &impl_->olditimer) );
134     
135     impl_->unblock();
136 }
137
138 prefix_ void senf::ClockService::updateSkew(boost::posix_time::ptime time)
139 {
140     Impl::Blocker alrmBlocker (impl_.get());
141     
142     // Make a second 'checkSkew' test, this time with SIGALRM blocked. See
143     // senf::ClockService::now_i()
144
145     if (checkSkew(time)) {
146         struct itimerval itimer;
147         CheckError( getitimer, (ITIMER_REAL, &itimer) );
148         clockSkew(time, (heartbeat_ 
149                          + boost::posix_time::seconds(CheckInterval) 
150                          - boost::posix_time::seconds(itimer.it_value.tv_sec)
151                          - boost::posix_time::microseconds(itimer.it_value.tv_usec)));
152     }
153 }
154
155 ///////////////////////////////cc.e////////////////////////////////////////
156 #undef prefix_
157 //#include "ClockService.mpp"
158
159 \f
160 // Local Variables:
161 // mode: c++
162 // fill-column: 100
163 // comment-column: 40
164 // c-file-style: "senf"
165 // indent-tabs-mode: nil
166 // ispell-local-dictionary: "american"
167 // compile-command: "scons -u test"
168 // End: