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.
 
 
 
 
 
 

211 lines
3.3 KiB

  1. #define CPPCMS_SOURCE
  2. #include "application.h"
  3. #include "http_context.h"
  4. #include "service.h"
  5. #include "cppcms_error.h"
  6. #include "url_dispatcher.h"
  7. #include "intrusive_ptr.h"
  8. #include "applications_pool.h"
  9. #include "http_response.h"
  10. #include <set>
  11. #include <vector>
  12. #include <boost/bind.hpp>
  13. namespace cppcms {
  14. struct application::data {
  15. data(cppcms::service *s):
  16. service(s),
  17. conn(0),
  18. pool_id(-1)
  19. {
  20. }
  21. cppcms::service *service;
  22. intrusive_ptr<http::context> conn;
  23. int pool_id;
  24. url_dispatcher url;
  25. std::vector<application *> managed_children;
  26. };
  27. application::application(cppcms::service &srv,application *p) :
  28. d(new data(&srv)),
  29. refs_(0)
  30. {
  31. if(p==0)
  32. parent_=root_=this;
  33. else
  34. parent(p);
  35. }
  36. application::~application()
  37. {
  38. for(unsigned i=0;i<d->managed_children.size();i++) {
  39. delete d->managed_children[i];
  40. d->managed_children[i]=0;
  41. }
  42. }
  43. long application::refs()
  44. {
  45. return refs_;
  46. }
  47. cppcms::service &application::service()
  48. {
  49. return *d->service;
  50. }
  51. json::value const &application::settings()
  52. {
  53. return service().settings();
  54. }
  55. http::request &application::request()
  56. {
  57. return context().request();
  58. }
  59. http::response &application::response()
  60. {
  61. return context().response();
  62. }
  63. url_dispatcher &application::dispatcher()
  64. {
  65. return d->url;
  66. }
  67. intrusive_ptr<http::context> application::get_context()
  68. {
  69. return root()->d->conn;
  70. }
  71. http::context &application::context()
  72. {
  73. if(!root()->d->conn)
  74. throw cppcms_error("Access to unassigned context");
  75. return *root()->d->conn;
  76. }
  77. intrusive_ptr<http::context> application::release_context()
  78. {
  79. intrusive_ptr<http::context> ptr=root()->d->conn;
  80. assign_context(0);
  81. return ptr;
  82. }
  83. bool application::is_asynchronous()
  84. {
  85. return pool_id() < 0;
  86. }
  87. void application::assign_context(intrusive_ptr<http::context> conn)
  88. {
  89. root()->d->conn=conn;
  90. }
  91. void application::pool_id(int id)
  92. {
  93. d->pool_id=id;
  94. }
  95. int application::pool_id()
  96. {
  97. return d->pool_id;
  98. }
  99. application *application::parent()
  100. {
  101. return parent_;
  102. }
  103. application *application::root()
  104. {
  105. return root_;
  106. }
  107. void application::parent(application *app)
  108. {
  109. parent_=app;
  110. root_=app->root();
  111. }
  112. void application::add(application &app)
  113. {
  114. if(app.parent()!=this)
  115. app.parent(this);
  116. }
  117. void application::add(application &app,std::string regex,int part)
  118. {
  119. add(app);
  120. url_dispatcher().mount(regex,app,part);
  121. }
  122. void application::assign(application *app)
  123. {
  124. d->managed_children.push_back(app);
  125. add(*app);
  126. }
  127. void application::assign(application *app,std::string regex,int part)
  128. {
  129. d->managed_children.push_back(app);
  130. add(*app,regex,part);
  131. }
  132. void application::recycle()
  133. {
  134. if(root()->d->conn) {
  135. response().out() << std::flush;
  136. response().finalize();
  137. context().async_complete_response();
  138. }
  139. assign_context(0);
  140. }
  141. void intrusive_ptr_add_ref(application *app)
  142. {
  143. ++(app->root()->refs_);
  144. }
  145. // REMEMBER THIS IS CALLED FROM DESTRUCTOR!!!
  146. void intrusive_ptr_release(application *app)
  147. {
  148. // it is called in destructors... So be very careful
  149. try {
  150. app = app->root();
  151. long refs=--(app->refs_);
  152. if(refs > 0)
  153. return;
  154. cppcms::service &service=app->service();
  155. try {
  156. app->recycle();
  157. }
  158. catch(...) {
  159. if(app->pool_id() < 0) {
  160. service.applications_pool().put(app);
  161. }
  162. else
  163. delete app;
  164. throw;
  165. }
  166. service.applications_pool().put(app);
  167. // return the application to pool... or delete it if "pooled"
  168. }
  169. catch(...)
  170. {
  171. // FIXME LOG IT?
  172. }
  173. }
  174. } // cppcms