Menu

[r2044]: / framework / trunk / cppcms / service.h  Maximize  Restore  History

Download this file

238 lines (206 with data), 7.6 kB

  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
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2010 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 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 Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
///////////////////////////////////////////////////////////////////////////////
#ifndef CPPCMS_SERVICE_H
#define CPPCMS_SERVICE_H
#include <cppcms/defs.h>
#include <booster/noncopyable.h>
#include <booster/hold_ptr.h>
#include <booster/function.h>
#include <locale>
#include <memory>
#include <cppcms/locale_fwd.h>
#include <cppcms/json.h>
namespace booster {
namespace aio {
class io_service;
}
}
///
/// \brief This is the namespace where all CppCMS functionality is placed
///
namespace cppcms {
namespace impl {
struct cached_settings;
class service;
namespace cgi {
class acceptor;
}
}
class applications_pool;
class thread_pool;
class session_pool;
class cache_pool;
class forwarder;
namespace json {
class value;
}
namespace views {
class manager;
}
///
/// \brief This class represent the central event loop of the CppCMS applications.
///
/// This is the central class that is responsible for management of CppCMS services: management of HTTP requests and
/// responses, sessions, cache and much more services. It is the central class that runs during execution of any CppCMS
/// service.
///
class CPPCMS_API service : public booster::noncopyable
{
public:
///
/// Load configuration settings from the command line parameters. This object can be passed
/// to service(json::value const &v) constructor allowing to alter these settings before
/// creating the actual service object.
///
static json::value load_settings(int argc,char *argv[]);
///
/// Create a new service, passing all configuration settings via json::value instead of parsing command line parameters.
///
service(json::value const &v);
///
/// Parse command line parameters, get the configuration file and additional options and create the service.
///
/// Note for Windows users: argv is assumed to be UTF-8 encoded strings, if you want pass non-ascii or "wide" parameters
/// you need convert them from UTF-16 to UTF-8. For example you can use booster::nowide::convert functions.
///
service(int argc,char *argv[]);
///
/// Destroy the service - the last class to go down.
///
/// Note, if you have asynchronous applications that are not owned the service class they must be destroyed before it
///
~service();
///
/// Start the central even loop of CppCMS service. By default, it also install signal handlers on POSIX platforms
/// to handle SIGINT, SIGTERM and SIGUSR1 signals or uses SetConsoleCtrlHandler on Windows platforms waiting on
/// C, BREAK, CLOSE and SHUTDOWN events.
///
/// This even call service::shutdown function that stops the main event loop and shutdowns the service properly.
///
/// If you want to disable these handlers, you may set service.disable_global_exit_handling option to true.
///
void run();
///
/// Stop the service event loop. This member function is both thread and signal safe.
///
void shutdown();
///
/// Get the configuration setting the service was created with.
///
json::value const &settings();
///
/// Get a cppcms::applications_pool instance of this service
///
cppcms::applications_pool &applications_pool();
///
/// Get a cppcms::thread_pool instance of this service
///
cppcms::thread_pool &thread_pool();
///
/// Get a cppcms::session_pool instance of this service, never used directly by user level applications
///
cppcms::session_pool &session_pool();
///
/// Get the cppcms::views::manager instance
///
cppcms::views::manager &views_pool();
///
/// Get the cppcms::cache_pool instance of this service, never used directly by user level applications
///
cppcms::cache_pool &cache_pool();
///
/// Get cppcms::forwarder instance of this service
///
cppcms::forwarder &forwarder();
///
/// Get locale::generator instance of this service
///
locale::generator const &generator();
///
/// Get the default locale for this service.
///
std::locale locale();
///
/// Get a locale by \a name, shortcut to generator().get(name);
///
std::locale locale(std::string const &name);
///
/// Get low level central event loop of the CppCMS service that allows you to connect asynchronous application with generic
/// asynchronous sockets API or create any asynchronous TCP/IP servers over it.
///
booster::aio::io_service &get_io_service();
///
/// Post an execution handler \a handler to the event loop queue. This member function is thread safe allowing
/// safe communication between event loop thread (where all asynchronous applications run) and any other threads.
///
void post(booster::function<void()> const &handler);
///
/// Execute handler after forking processes (on POSIX platforms). This is useful when you want to start various asynchronous
/// applications or services in separate processes.
///
void after_fork(booster::function<void()> const &handler);
///
/// Get the size of thread pool each of CppCMS processes will be running.
///
int threads_no();
///
/// Get the number of forked processes used by the service. Always 0 on Windows and Cygwin.
///
int procs_no();
///
/// Get current process identification number. Note, this is not getpid() function.
///
/// When CppCMS service manages preforked process pool, each process gets its own id starting from 1 to procs_no() when
/// the parent supervisor process gets an id 0. If one of the forked processes crash, it is replaced with a new process
/// with same id.
///
/// So if an application want to run certain activity that can't be executed in multiple processes it can setup an after_fork
/// handle that would check the process id and start this activity.
///
/// Under Windows and Cygwin it is always 0.
///
int process_id();
/// \cond INTERNAL
// internal functions never call it directly
cppcms::impl::service &impl();
impl::cached_settings const &cached_settings();
/// \endcond
private:
void setup();
std::auto_ptr<cppcms::impl::cgi::acceptor> setup_acceptor(json::value const &,int,int shift=0);
void stop();
void start_acceptor(bool after_fork=false);
void setup_exit_handling();
bool prefork();
void run_prepare();
void after_fork_exec();
void run_acceptor();
void run_event_loop();
#ifdef CPPCMS_WIN32
void run_win_console();
#endif
#ifdef CPPCMS_WIN_NATIVE
void win_service_prepare();
void win_service_exec();
void run_win_service();
#endif
booster::hold_ptr<impl::service> impl_;
};
} //
#endif
MongoDB Logo MongoDB