Daemon.hpp 7.04 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/*******************************************************************************
 *                      castor/server/Daemon.hpp
 *
 * This file is part of the Castor project.
 * See http://castor.web.cern.ch/castor
 *
 * Copyright (C) 2003  CERN
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * @author castor dev team
 ******************************************************************************/

#ifndef CASTOR_SERVER_DAEMON_HPP
#define CASTOR_SERVER_DAEMON_HPP 1

#include "castor/dlf/Message.hpp"
#include "castor/log/Logger.hpp"

namespace castor {
namespace server {

/**
33
 * This class contains the code common to all daemon classes.
34
 *
35
 * The code common to all daemon classes includes daemonization and logging.
36
37
38
39
40
41
42
43
44
45
46
47
48
 */
class Daemon {

public:

  /**
   * Constructor
   *
   * @param logger Object representing the API of the CASTOR logging system.
   */
  Daemon(log::Logger &logger);

  /**
49
   * Destructor.
50
   */
51
  virtual ~Daemon() throw();
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222

  /**
   * Returns this server's name as used by the CASTOR logging system.
   */
  const std::string &getServerName() const throw();

protected:

  /**
   * Sets the runAsStagerSuperuser flag to true.
   *
   * The default value of the runAsStagerSuperuser flag at construction time is
   * false.
   */
  void runAsStagerSuperuser() throw();

  /**
   * Initializes the DLF, both for streaming and regular messages
   * Does not create the DLF thread, this is created after daemonization
   * @param messages the messages to be passed to dlf_init
   */
  void dlfInit(castor::dlf::Message messages[])
    throw (castor::exception::Exception);

  /**
   * Daemonizes the daemon.
   *
   * Please make sure that the setForeground() and runAsStagerSuperuser()
   * methods have been called as appropriate before this method is called.
   * This method takes into account whether the dameon should run in foregreound
   * or background mode (m_foreground) and whether or not the user of daemon
   * should be changed to the stager superuser (m_runAsStagerSuperuser).
   */
  void daemonize() throw(castor::exception::Exception);

  /**
   * Sends a notification message to the given host,port
   * to wake up nbThreads threads to handle pending requests.
   * @param host the destination host
   * @param port the destination port
   * @param tpName the name of the thread pool to be signaled
   * @param nbThreads the number of threads to be signaled
   */
  static void sendNotification(const std::string &host, const int port,
    const char tpName, const int nbThreads = 1) throw();

  /**
   * Writes a message into the CASTOR logging system. Note that no exception
   * will ever be thrown in case of failure. Failures will actually be
   * silently ignored in order to not impact the processing.
   *
   * Note that this version of logMsg() allows the caller to specify the
   * time stamp of the log message.
   *
   * @param priority the priority of the message as defined by the syslog
   * API.
   * @param msg the message.
   * @param numParams the number of parameters in the message.
   * @param params the parameters of the message.
   * @param timeStamp the time stamp of the log message.
   */
  void logMsg(
    const int priority,
    const std::string &msg,
    const int numParams,
    const log::Param params[],
    const struct timeval &timeStamp) throw();

  /**
   * A template function that wraps logMsg in order to get the compiler
   * to automatically determine the size of the params parameter, therefore
   *
   * Note that this version of logMsg() allows the caller to specify the
   * time stamp of the log message.
   *
   * @param priority the priority of the message as defined by the syslog
   * API.
   * @param msg the message.
   * @param params the parameters of the message.
   * @param timeStamp the time stamp of the log message.
   */
  template<int numParams> void logMsg(
    const int priority,
    const std::string &msg,
    castor::log::Param(&params)[numParams],
    const struct timeval &timeStamp) throw() {
    logMsg(priority, msg, numParams, params, timeStamp);
  }

  /**
   * Writes a message into the CASTOR logging system. Note that no exception
   * will ever be thrown in case of failure. Failures will actually be
   * silently ignored in order to not impact the processing.
   *
   * Note that this version of logMsg() implicitly uses the current time as
   * the time stamp of the message.
   *
   * @param priority the priority of the message as defined by the syslog
   * API.
   * @param msg the message.
   * @param numParams the number of parameters in the message.
   * @param params the parameters of the message.
   */
  void logMsg(
    const int priority,
    const std::string &msg,
    const int numParams,
    const castor::log::Param params[]) throw();

  /**
   * A template function that wraps logMsg in order to get the compiler
   * to automatically determine the size of the params parameter, therefore
   * removing the need for the devloper to provide it explicity.
   *
   * Note that this version of logMsg() implicitly uses the current time as
   * the time stamp of the message.
   *
   * @param priority the priority of the message as defined by the syslog
   * API.
   * @param msg the message.
   * @param params the parameters of the message.
   */
  template<int numParams> void logMsg(
    const int priority,
    const std::string &msg,
    castor::log::Param(&params)[numParams]) throw() {
    logMsg(priority, msg, numParams, params);
  }

  /**
   * Writes a message into the CASTOR logging system. Note that no exception
   * will ever be thrown in case of failure. Failures will actually be
   * silently ignored in order to not impact the processing.
   *
   * Note that this version of logMsg() implicitly uses the current time as
   * the time stamp of the message.
   *
   * @param priority the priority of the message as defined by the syslog
   * API.
   * @param msg the message.
   */
  void logMsg(
    const int priority,
    const std::string &msg) throw();

  /**
   * Flag indicating whether the server should run in foreground or background
   * mode.
   */
  bool m_foreground;

private:

  /**
   * Flag indicating whether the server should
   * change identity at startup and run as STAGERSUPERUSER
   * (normally defined as stage:st)
   */
  bool m_runAsStagerSuperuser;

  /**
   * Object representing the API of the CASTOR logging system.
   */
  log::Logger &m_logger;

}; // class Daemon

} // namespace server
} // namespace castor

#endif // CASTOR_SERVER_DAEMON_HPP