Utils/Console: Fix singleton instantiation order (ServerManager / Scheduler)
[senf.git] / Scheduler / SignalDispatcher.hh
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 SignalDispatcher public header */
25
26 #ifndef HH_SignalDispatcher_
27 #define HH_SignalDispatcher_ 1
28
29 // Custom includes
30 #include <signal.h>
31 #include <map>
32 #include "FdManager.hh"
33 #include "FIFORunner.hh"
34
35 //#include "SignalDispatcher.mpp"
36 ///////////////////////////////hh.p////////////////////////////////////////
37
38 namespace senf {
39 namespace scheduler {
40
41     /** \brief Scheduler dispatcher managing UNIX signals
42
43         This dispatcher supports registering UNIX signals with the Scheduler.
44
45         \implementation SignalDispatcher provides a single signal handler which all registered
46             signals are assigned to. When a signal is received, data is written to a pipe which has
47             been added to the FdManager and this signals the event.
48
49         \todo Add signal block/unblock management to the FdManager to reduce the number of
50             setprocmask() calls
51       */
52     class SignalDispatcher
53         : public FdManager::Event
54     {
55     public:
56         ///////////////////////////////////////////////////////////////////////////
57         // Types
58
59         typedef boost::function<void (siginfo_t const &)> Callback;
60
61         ///////////////////////////////////////////////////////////////////////////
62         ///\name Structors and default members
63         ///@{
64
65         SignalDispatcher(FdManager & manager, FIFORunner & runner);
66         ~SignalDispatcher();
67
68         ///@}
69         ///////////////////////////////////////////////////////////////////////////
70
71         void add(int signal, Callback const & cb); ///< Add signal event
72                                         /**< \param[in] signal signal number
73                                              \param[in] cb Callback */
74
75         void remove(int signal); ///< Unregister signal event
76
77         void unblockSignals();          ///< Unblock registered signals
78                                         /**< Must be called before waiting for an event */
79         void blockSignals();            ///< Block registered signals
80                                         /**< Must be called directly after FdManager returns */
81
82         bool empty() const;             ///< \c true, if no signal is registered.
83
84     protected:
85
86     private:
87         ///< Internal: UNIX signal event
88         struct SignalEvent
89             : public FIFORunner::TaskInfo
90         {
91             SignalEvent(int signal, Callback cb_);
92             virtual void run();
93
94             siginfo_t siginfo;
95             Callback cb;
96         };
97
98         virtual void signal(int events);
99         static void sigHandler(int signal, ::siginfo_t * siginfo, void *);
100
101         FdManager & manager_;
102         FIFORunner & runner_;
103
104         typedef std::map<int, SignalEvent> HandlerMap;
105         HandlerMap handlers_;
106
107         int sigPipe_[2];
108
109         bool blocked_;
110         sigset_t sigSet_;
111
112         static SignalDispatcher * instance_;
113     };
114
115
116 }}
117
118 ///////////////////////////////hh.e////////////////////////////////////////
119 #include "SignalDispatcher.cci"
120 //#include "SignalDispatcher.ct"
121 //#include "SignalDispatcher.cti"
122 #endif
123
124 \f
125 // Local Variables:
126 // mode: c++
127 // fill-column: 100
128 // comment-column: 40
129 // c-file-style: "senf"
130 // indent-tabs-mode: nil
131 // ispell-local-dictionary: "american"
132 // compile-command: "scons -u test"
133 // End: