795008ab1ce7424dc2dc5ca1d1353f467d06249f
[senf.git] / Utils / Daemon / Daemon.ih
1 // $Id$
2 //
3 // Copyright (C) 2007
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 Daemon internal header */
25
26 #ifndef IH_Daemon_
27 #define IH_Daemon_ 1
28
29 // Custom includes
30 #include <deque>
31 #include <list>
32 #include <boost/utility.hpp>
33 #include <boost/function.hpp>
34 #include "../../Scheduler/Scheduler.hh"
35 #include "../../Scheduler/SignalEvent.hh"
36
37 ///////////////////////////////ih.p////////////////////////////////////////
38
39 namespace senf {
40 namespace detail {
41
42     /** \brief Internal: Watch daemon process for successful startup */
43     class DaemonWatcher
44         : boost::noncopyable
45     {
46     public:
47
48         DaemonWatcher(int pid, int coutpipe, int cerrpipe, int stdout, int stderr);
49
50         void run();
51
52     private:
53
54         class Forwarder 
55         {
56         public:
57             typedef boost::function<void ()> Callback;
58
59             Forwarder(int src, Callback cb);
60             ~Forwarder();
61
62             void addTarget(int fd);
63
64         private:
65
66             typedef std::deque<char> Buffer;
67             struct Target
68             {
69                 int fd;
70                 Buffer::size_type offset;
71             };
72             typedef std::list<Target> Targets;
73
74             void readData(int event);
75             void writeData(int event, Targets::iterator target);
76
77             Buffer buffer_;
78             int src_;
79
80             Targets targets_;
81
82             Callback cb_;
83         };
84         
85         void pipeClosed(int id);
86         void sigChld(siginfo_t const &);
87         void childDied();
88         void childOk();
89
90         int childPid_;
91         int coutpipe_;
92         int cerrpipe_;
93         int stdout_;
94         int stderr_;
95         bool sigChld_;
96
97         scheduler::SignalEvent cldSignal_;
98         scheduler::TimerEvent timer_;
99         Forwarder coutForwarder_;
100         Forwarder cerrForwarder_;
101     };
102
103 }}
104
105 ///////////////////////////////ih.e////////////////////////////////////////
106 #endif
107
108 \f
109 // Local Variables:
110 // mode: c++
111 // fill-column: 100
112 // comment-column: 40
113 // c-file-style: "senf"
114 // indent-tabs-mode: nil
115 // ispell-local-dictionary: "american"
116 // compile-command: "scons -u test"
117 // End: