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.
 
 
 
 
 
 

230 lines
7.8 KiB

  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (C) 2008-2010 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Lesser General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. //
  18. ///////////////////////////////////////////////////////////////////////////////
  19. #ifndef CPPCMS_APPLICATIONS_POOL_H
  20. #define CPPCMS_APPLICATIONS_POOL_H
  21. #include "defs.h"
  22. #include <booster/noncopyable.h>
  23. #include <booster/hold_ptr.h>
  24. #include "intrusive_ptr.h"
  25. #include <memory>
  26. #include <string>
  27. namespace cppcms {
  28. class application;
  29. class service;
  30. ///
  31. /// \brief Application pool is the central class that holds user created applications
  32. ///
  33. /// Form the user perspective this class provides an API for mounting user application to the CppCMS service.
  34. ///
  35. /// There are two kind of \a mount member functions, that allow:
  36. ///
  37. /// - Mounting a \a factory of user applications -- for execution of synchronous requests by multiple
  38. /// instances of application.
  39. /// - Mounting single application -- for processing asynchronous requests by single instance of an application
  40. ///
  41. /// The life cycle of synchronous application is defined by application pool itself, and the life cycle
  42. /// of asynchronous depends on its own reference count.
  43. ///
  44. /// This class is thread safe and can be accessed from multiple threads simultaneously.
  45. ///
  46. class CPPCMS_API applications_pool {
  47. public:
  48. ///
  49. /// \brief a base class for user application factories
  50. ///
  51. struct factory : public booster::noncopyable {
  52. ///
  53. /// Returns newly created instance of an application.
  54. ///
  55. virtual std::auto_ptr<application> operator()(service &) const = 0;
  56. virtual ~factory(){}
  57. };
  58. ///
  59. /// Mount an application factory \a aps for processing of any incoming requests. Application
  60. /// would receive PATH_INFO CGI variable for URL matching.
  61. ///
  62. /// This member function is thread safe.
  63. ///
  64. void mount(std::auto_ptr<factory> aps);
  65. ///
  66. /// Mount an application factory \a aps for processing of requests for which CGI PATH_INFO
  67. /// matches the regular expression \a path_info. The marched part of an regular expression \a select would
  68. /// be passed for URL matching.
  69. ///
  70. /// This member function is thread safe.
  71. ///
  72. void mount(std::auto_ptr<factory> aps,std::string path_info,int select);
  73. ///
  74. /// Mount an application factory \a aps for processing of requests for which CGI SCRIPT_NAME exactly
  75. /// matches \a script_name parameter. CGI PATH_INFO is passed to application for URL matching.
  76. ///
  77. /// This member function is thread safe.
  78. ///
  79. void mount(std::auto_ptr<factory> aps,std::string script_name);
  80. ///
  81. /// Mount an application factory \a aps for processing of requests for which CGI SCRIPT_NAME exactly
  82. /// matches \a script_name parameter. And PATH_INFO patches regular expression \a path_info.
  83. /// The matched part \a select is passed to application for URL matching.
  84. ///
  85. /// This member function is thread safe.
  86. ///
  87. void mount(std::auto_ptr<factory> aps,std::string script_name,std::string path_info, int select);
  88. ///
  89. /// Mount an asynchronous application \a app for processing of any incoming requests. Application
  90. /// would receive PATH_INFO CGI variable for URL matching.
  91. ///
  92. /// This member function is thread safe.
  93. ///
  94. void mount(intrusive_ptr<application> app);
  95. ///
  96. /// Mount an asynchronous application \a app for processing of requests for which CGI PATH_INFO
  97. /// matches the regular expression \a path_info. The marched part of an regular expression \a select would
  98. /// be passed for URL matching.
  99. ///
  100. /// This member function is thread safe.
  101. ///
  102. void mount(intrusive_ptr<application> app,std::string path_info,int select);
  103. ///
  104. /// Mount an asynchronous application \a app for processing of requests for which CGI SCRIPT_NAME exactly
  105. /// matches \a script_name parameter. And PATH_INFO patches regular expression \a path_info.
  106. /// The matched part \a select is passed to application for URL matching.
  107. ///
  108. /// This member function is thread safe.
  109. ///
  110. void mount(intrusive_ptr<application> app,std::string script_name);
  111. ///
  112. /// Mount an asynchronous application \a app for processing of requests for which CGI SCRIPT_NAME exactly
  113. /// matches \a script_name parameter. And PATH_INFO patches regular expression \a path_info.
  114. /// The matched part \a select is passed to application for URL matching.
  115. ///
  116. /// This member function is thread safe.
  117. ///
  118. void mount(intrusive_ptr<application> app,std::string script_name,std::string path_info, int select);
  119. ///
  120. /// Internal API - do not use it directly
  121. ///
  122. intrusive_ptr<application> get(std::string script_name,std::string path_info,std::string &match);
  123. ///
  124. /// Internal API - do not use it directly
  125. ///
  126. void put(application *app);
  127. ///
  128. /// Internal API - do not use it directly
  129. ///
  130. applications_pool(service &srv,int pool_size_limit);
  131. ~applications_pool();
  132. private:
  133. struct basic_app_data;
  134. struct app_data;
  135. struct long_running_app_data;
  136. struct data;
  137. std::string script_name();
  138. bool matched(basic_app_data &data,std::string script_name,std::string path_info,std::string &matched);
  139. service *srv_;
  140. booster::hold_ptr<data> d;
  141. };
  142. namespace details {
  143. template<typename T>
  144. struct simple_factory0 : public applications_pool::factory
  145. {
  146. std::auto_ptr<application> operator()(service &s) const
  147. {
  148. std::auto_ptr<application> app(new T(s));
  149. return app;
  150. }
  151. };
  152. template<typename T,typename P1>
  153. struct simple_factory1 : public applications_pool::factory
  154. {
  155. simple_factory1(P1 p1) : p1_(p1) {}
  156. P1 p1_;
  157. std::auto_ptr<application> operator()(service &s) const
  158. {
  159. std::auto_ptr<application> app(new T(s,p1_));
  160. return app;
  161. }
  162. };
  163. template<typename T,typename P1,typename P2>
  164. struct simple_factory2 : public applications_pool::factory
  165. {
  166. simple_factory2(P1 p1,P2 p2) : p1_(p1),p2_(p2) {}
  167. P1 p1_;
  168. P2 p2_;
  169. std::auto_ptr<application> operator()(service &s) const
  170. {
  171. std::auto_ptr<application> app(new T(s,p1_,p2_));
  172. return app;
  173. }
  174. };
  175. } // details
  176. ///
  177. /// Create application factory for application of type T, such as T has a constructor
  178. /// T::T(cppcms::service &s);
  179. ///
  180. template<typename T>
  181. std::auto_ptr<applications_pool::factory> applications_factory()
  182. {
  183. std::auto_ptr<applications_pool::factory> f(new details::simple_factory0<T>);
  184. return f;
  185. }
  186. ///
  187. /// Create application factory for application of type T, such as T has a constructor
  188. /// T::T(cppcms::service &s,P1);
  189. ///
  190. template<typename T,typename P1>
  191. std::auto_ptr<applications_pool::factory> applications_factory(P1 p1)
  192. {
  193. std::auto_ptr<applications_pool::factory> f(new details::simple_factory1<T,P1>(p1));
  194. return f;
  195. }
  196. ///
  197. /// Create application factory for application of type T, such as T has a constructor
  198. /// T::T(cppcms::service &s,P1,P2);
  199. ///
  200. template<typename T,typename P1,typename P2>
  201. std::auto_ptr<applications_pool::factory> applications_factory(P1 p1,P2 p2)
  202. {
  203. std::auto_ptr<applications_pool::factory> f(new details::simple_factory2<T,P1,P2>(p1,p2));
  204. return f;
  205. }
  206. } // cppcms
  207. #endif