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.
 
 
 
 
 
 

239 lines
6.2 KiB

  1. #define CPPCMS_SOURCE
  2. #include "applications_pool.h"
  3. #include "application.h"
  4. #include "service.h"
  5. #include "cppcms_error.h"
  6. #include "json.h"
  7. #include <set>
  8. #include <vector>
  9. #include <boost/regex.hpp>
  10. #include <boost/shared_ptr.hpp>
  11. #include <boost/thread.hpp>
  12. namespace cppcms {
  13. struct applications_pool::basic_app_data : public util::noncopyable {
  14. basic_app_data(std::string script) :
  15. script_name(script),
  16. match(0),
  17. use_regex(0)
  18. {
  19. }
  20. basic_app_data(std::string script,std::string pat,int select) :
  21. script_name(script),
  22. expr(pat),
  23. match(select),
  24. use_regex(1)
  25. {
  26. }
  27. std::string script_name;
  28. boost::regex expr;
  29. int match;
  30. bool use_regex;
  31. private:
  32. void check() const
  33. {
  34. if( !script_name.empty()
  35. && script_name[0]!='/'
  36. && script_name[0]!='.'
  37. && script_name!="*")
  38. {
  39. throw cppcms_error("Scipt name should be either '*', start with '.' or '/' or be empty");
  40. }
  41. }
  42. };
  43. struct applications_pool::app_data : public applications_pool::basic_app_data {
  44. app_data(std::string script,std::auto_ptr<applications_pool::factory> f) :
  45. basic_app_data(script),
  46. factory(f),
  47. size(0)
  48. {
  49. }
  50. app_data(std::string script,std::string pat,int select,std::auto_ptr<applications_pool::factory> f) :
  51. basic_app_data(script,pat,select),
  52. factory(f),
  53. size(0)
  54. {
  55. }
  56. std::auto_ptr<applications_pool::factory> factory;
  57. int size;
  58. std::set<application *> pool;
  59. ~app_data()
  60. {
  61. std::set<application *>::iterator p;
  62. for(p=pool.begin();p!=pool.end();++p) {
  63. delete *p;
  64. }
  65. }
  66. };
  67. struct applications_pool::long_running_app_data : public applications_pool::basic_app_data
  68. {
  69. long_running_app_data(std::string script) :
  70. basic_app_data(script)
  71. {
  72. }
  73. long_running_app_data(std::string script,std::string pat,int select) :
  74. basic_app_data(script,pat,select)
  75. {
  76. }
  77. };
  78. struct applications_pool::data {
  79. std::vector<boost::shared_ptr<app_data> > apps;
  80. typedef std::map<application *,boost::shared_ptr<long_running_app_data> > long_running_aps_type;
  81. long_running_aps_type long_running_aps;
  82. int limit;
  83. boost::recursive_mutex mutex;
  84. };
  85. typedef boost::unique_lock<boost::recursive_mutex> lock_it;
  86. applications_pool::applications_pool(service &srv,int limit) :
  87. srv_(&srv),
  88. d(new applications_pool::data())
  89. {
  90. d->limit=limit;
  91. }
  92. applications_pool::~applications_pool()
  93. {
  94. }
  95. std::string applications_pool::script_name()
  96. {
  97. return srv_->settings().get("service.default_script_name","*");
  98. }
  99. void applications_pool::mount(std::auto_ptr<factory> aps)
  100. {
  101. lock_it lock(d->mutex);
  102. d->apps.push_back(boost::shared_ptr<app_data>(new app_data(script_name(),aps)));
  103. }
  104. void applications_pool::mount(std::auto_ptr<factory> aps,std::string path_info,int select)
  105. {
  106. lock_it lock(d->mutex);
  107. d->apps.push_back(boost::shared_ptr<app_data>(new app_data(script_name(),path_info,select,aps)));
  108. }
  109. void applications_pool::mount(std::auto_ptr<factory> aps,std::string script_name)
  110. {
  111. lock_it lock(d->mutex);
  112. d->apps.push_back(boost::shared_ptr<app_data>(new app_data(script_name,aps)));
  113. }
  114. void applications_pool::mount(std::auto_ptr<factory> aps,std::string script_name,std::string path_info,int select)
  115. {
  116. lock_it lock(d->mutex);
  117. d->apps.push_back(boost::shared_ptr<app_data>(new app_data(script_name,path_info,select,aps)));
  118. }
  119. void applications_pool::mount(intrusive_ptr<application> app)
  120. {
  121. lock_it lock(d->mutex);
  122. d->long_running_aps[app.get()]=
  123. boost::shared_ptr<long_running_app_data>(new long_running_app_data(script_name()));
  124. }
  125. void applications_pool::mount(intrusive_ptr<application> app,std::string path_info,int select)
  126. {
  127. lock_it lock(d->mutex);
  128. d->long_running_aps[app.get()]=
  129. boost::shared_ptr<long_running_app_data>(new long_running_app_data(script_name(),path_info,select));
  130. }
  131. void applications_pool::mount(intrusive_ptr<application> app,std::string script_name)
  132. {
  133. lock_it lock(d->mutex);
  134. d->long_running_aps[app.get()]=
  135. boost::shared_ptr<long_running_app_data>(new long_running_app_data(script_name));
  136. }
  137. void applications_pool::mount(intrusive_ptr<application> app,std::string script_name,std::string path_info,int select)
  138. {
  139. lock_it lock(d->mutex);
  140. d->long_running_aps[app.get()]=
  141. boost::shared_ptr<long_running_app_data>(new long_running_app_data(script_name,path_info,select));
  142. }
  143. bool applications_pool::matched(basic_app_data &data,std::string script_name,std::string path_info,std::string &matched)
  144. {
  145. std::string const expected_name=data.script_name;
  146. if(expected_name!="*" && !expected_name.empty()) {
  147. if(expected_name[0]=='/') {
  148. if(script_name!=expected_name)
  149. return false;
  150. }
  151. else { // if(sn[0]=='.')
  152. if( script_name.size() <= expected_name.size()
  153. || script_name.substr(script_name.size() - expected_name.size())!=expected_name)
  154. {
  155. return false;
  156. }
  157. }
  158. }
  159. else if(expected_name=="*" && script_name.empty())
  160. return false;
  161. else if(expected_name.empty() && !script_name.empty())
  162. return false;
  163. boost::cmatch match;
  164. if(!data.use_regex) {
  165. matched=path_info;
  166. return true;
  167. }
  168. else if(boost::regex_match(path_info.c_str(),match,data.expr)) {
  169. matched=match[data.match];
  170. return true;
  171. }
  172. else {
  173. return false;
  174. }
  175. }
  176. intrusive_ptr<application> applications_pool::get(std::string script_name,std::string path_info,std::string &m)
  177. {
  178. lock_it lock(d->mutex);
  179. for(unsigned i=0;i<d->apps.size();i++) {
  180. if(!matched(*d->apps[i],script_name,path_info,m))
  181. continue;
  182. if(d->apps[i]->pool.empty()) {
  183. intrusive_ptr<application> app=(*d->apps[i]->factory)(*srv_).release();
  184. app->pool_id(i);
  185. return app;
  186. }
  187. d->apps[i]->size--;
  188. intrusive_ptr<application> app(*(d->apps[i]->pool.begin()));
  189. d->apps[i]->pool.erase(app.get());
  190. return app;
  191. }
  192. for(data::long_running_aps_type::iterator p=d->long_running_aps.begin();p!=d->long_running_aps.end();++p){
  193. if(!matched(*p->second,script_name,path_info,m))
  194. continue;
  195. intrusive_ptr<application> app=p->first;
  196. return app;
  197. }
  198. return 0;
  199. }
  200. void applications_pool::put(application *app)
  201. {
  202. lock_it lock(d->mutex);
  203. if(!app) return;
  204. int id=app->pool_id();
  205. if(id < 0) {
  206. d->long_running_aps.erase(app);
  207. delete app;
  208. return;
  209. }
  210. if(unsigned(id) >= d->apps.size() || d->apps[id]->size >= d->limit) {
  211. delete app;
  212. return;
  213. }
  214. d->apps[id]->pool.insert(app);
  215. d->apps[id]->size++;
  216. }
  217. } //cppcms