ChipMaster's trial hacks on C++CMS starting with v1.2.1. Not sure I'll follow on with the v2 since it looks to be breaking and mostly frivolous.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

236 lines
6.9 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // See accompanying file COPYING.TXT file for licensing details.
  6. //
  7. ///////////////////////////////////////////////////////////////////////////////
  8. #ifndef CPPCMS_SERVICE_H
  9. #define CPPCMS_SERVICE_H
  10. #include <cppcms/defs.h>
  11. #include <booster/noncopyable.h>
  12. #include <booster/hold_ptr.h>
  13. #include <booster/function.h>
  14. #include <locale>
  15. #include <booster/auto_ptr_inc.h>
  16. #include <cppcms/locale_fwd.h>
  17. #include <cppcms/json.h>
  18. namespace booster {
  19. namespace aio {
  20. class io_service;
  21. }
  22. }
  23. ///
  24. /// \brief This is the namespace where all CppCMS functionality is placed
  25. ///
  26. namespace cppcms {
  27. namespace plugin {
  28. class scope;
  29. }
  30. namespace impl {
  31. struct cached_settings;
  32. class service;
  33. namespace cgi {
  34. class acceptor;
  35. }
  36. }
  37. class applications_pool;
  38. class thread_pool;
  39. class session_pool;
  40. class cache_pool;
  41. class forwarder;
  42. namespace json {
  43. class value;
  44. }
  45. namespace views {
  46. class manager;
  47. }
  48. ///
  49. /// \brief This class represent the central event loop of the CppCMS applications.
  50. ///
  51. /// This is the central class that is responsible for management of CppCMS services: management of HTTP requests and
  52. /// responses, sessions, cache and much more services. It is the central class that runs during execution of any CppCMS
  53. /// service.
  54. ///
  55. class CPPCMS_API service : public booster::noncopyable
  56. {
  57. public:
  58. ///
  59. /// Load configuration settings from the command line parameters. This object can be passed
  60. /// to service(json::value const &v) constructor allowing to alter these settings before
  61. /// creating the actual service object.
  62. ///
  63. static json::value load_settings(int argc,char *argv[]);
  64. ///
  65. /// Create a new service, passing all configuration settings via json::value instead of parsing command line parameters.
  66. ///
  67. service(json::value const &v);
  68. ///
  69. /// Parse command line parameters, get the configuration file and additional options and create the service.
  70. ///
  71. /// Note for Windows users: argv is assumed to be UTF-8 encoded strings, if you want pass non-ascii or "wide" parameters
  72. /// you need convert them from UTF-16 to UTF-8. For example you can use booster::nowide::convert functions.
  73. ///
  74. service(int argc,char *argv[]);
  75. ///
  76. /// Destroy the service - the last class to go down.
  77. ///
  78. /// Note, if you have asynchronous applications that are not owned the service class they must be destroyed before it
  79. ///
  80. ~service();
  81. ///
  82. /// Start the central even loop of CppCMS service. By default, it also install signal handlers on POSIX platforms
  83. /// to handle SIGINT, SIGTERM and SIGUSR1 signals or uses SetConsoleCtrlHandler on Windows platforms waiting on
  84. /// C, BREAK, CLOSE and SHUTDOWN events.
  85. ///
  86. /// This even call service::shutdown function that stops the main event loop and shutdowns the service properly.
  87. ///
  88. /// If you want to disable these handlers, you may set service.disable_global_exit_handling option to true.
  89. ///
  90. void run();
  91. ///
  92. /// Stop the service event loop. This member function is both thread and signal safe.
  93. ///
  94. void shutdown();
  95. ///
  96. /// Get the configuration setting the service was created with.
  97. ///
  98. json::value const &settings();
  99. ///
  100. /// Get a cppcms::applications_pool instance of this service
  101. ///
  102. cppcms::applications_pool &applications_pool();
  103. ///
  104. /// Get a cppcms::thread_pool instance of this service
  105. ///
  106. cppcms::thread_pool &thread_pool();
  107. ///
  108. /// Get a cppcms::session_pool instance of this service, never used directly by user level applications
  109. ///
  110. cppcms::session_pool &session_pool();
  111. ///
  112. /// Get the cppcms::views::manager instance
  113. ///
  114. cppcms::views::manager &views_pool();
  115. ///
  116. /// Get the cppcms::cache_pool instance of this service, never used directly by user level applications
  117. ///
  118. cppcms::cache_pool &cache_pool();
  119. ///
  120. /// Get cppcms::forwarder instance of this service
  121. ///
  122. cppcms::forwarder &forwarder();
  123. ///
  124. /// Get locale::generator instance of this service
  125. ///
  126. locale::generator const &generator();
  127. ///
  128. /// Get the default locale for this service.
  129. ///
  130. std::locale locale();
  131. ///
  132. /// Get a locale by \a name, shortcut to generator().get(name);
  133. ///
  134. std::locale locale(std::string const &name);
  135. ///
  136. /// Get low level central event loop of the CppCMS service that allows you to connect asynchronous application with generic
  137. /// asynchronous sockets API or create any asynchronous TCP/IP servers over it.
  138. ///
  139. booster::aio::io_service &get_io_service();
  140. ///
  141. /// Post an execution handler \a handler to the event loop queue. This member function is thread safe allowing
  142. /// safe communication between event loop thread (where all asynchronous applications run) and any other threads.
  143. ///
  144. void post(booster::function<void()> const &handler);
  145. ///
  146. /// Execute handler after forking processes (on POSIX platforms). This is useful when you want to start various asynchronous
  147. /// applications or services in separate processes.
  148. ///
  149. void after_fork(booster::function<void()> const &handler);
  150. ///
  151. /// Get the size of thread pool each of CppCMS processes will be running.
  152. ///
  153. int threads_no();
  154. ///
  155. /// Get the number of forked processes used by the service. Always 0 on Windows and Cygwin.
  156. ///
  157. int procs_no();
  158. ///
  159. /// Get current process identification number. Note, this is not getpid() function.
  160. ///
  161. /// When CppCMS service manages preforked process pool, each process gets its own id starting from 1 to procs_no() when
  162. /// the parent supervisor process gets an id 0. If one of the forked processes crash, it is replaced with a new process
  163. /// with same id.
  164. ///
  165. /// So if an application want to run certain activity that can't be executed in multiple processes it can setup an after_fork
  166. /// handle that would check the process id and start this activity.
  167. ///
  168. /// Under Windows and Cygwin it is always 0.
  169. ///
  170. int process_id();
  171. ///
  172. /// Get plugin scope ownd by the cppcms::service
  173. ///
  174. plugin::scope &plugins();
  175. /// \cond INTERNAL
  176. // internal functions never call it directly
  177. cppcms::impl::service &impl();
  178. impl::cached_settings const &cached_settings();
  179. /// \endcond
  180. private:
  181. void setup();
  182. std::auto_ptr<cppcms::impl::cgi::acceptor> setup_acceptor(json::value const &,int,int shift=0);
  183. void stop();
  184. void start_acceptor(bool after_fork=false);
  185. void setup_exit_handling();
  186. bool prefork();
  187. void run_prepare();
  188. void after_fork_exec();
  189. void run_acceptor();
  190. void run_event_loop();
  191. #ifdef CPPCMS_WIN32
  192. void run_win_console();
  193. #endif
  194. #ifdef CPPCMS_WIN_NATIVE
  195. void win_service_prepare();
  196. void win_service_exec();
  197. void run_win_service();
  198. #endif
  199. booster::hold_ptr<impl::service> impl_;
  200. };
  201. } //
  202. #endif