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.
 
 
 
 
 
 

186 lines
4.9 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. #include "tcp_cache_server.h"
  20. #include "cache_storage.h"
  21. #ifndef CPPCMS_WIN32
  22. #include <signal.h>
  23. #endif
  24. #include <iostream>
  25. #include <stdlib.h>
  26. #include <stdexcept>
  27. struct params {
  28. bool en_cache;
  29. enum { none , files,sqlite3 } en_sessions;
  30. std::string session_backend;
  31. std::string session_file;
  32. std::string session_dir;
  33. int items_limit;
  34. int gc_frequency;
  35. int files_no;
  36. int port;
  37. std::string ip;
  38. int threads;
  39. void help()
  40. {
  41. std::cerr<<
  42. "Usage cppcms_tcp_scale [parameter]\n"
  43. " --bind IP ipv4/ipv6 IPto bind (default 0.0.0.0)\n"
  44. " --port N port to bind -- MANDATORY\n"
  45. " --threads N number of threads, default 1\n"
  46. " --cache Enable cache module\n"
  47. " --limit N maximal Number of items to store\n"
  48. " mandatory if cache enabled\n"
  49. " --session-files Enable files bases session backend\n"
  50. " --dir Directory where files stored\n"
  51. " mandatory if session-files enabled\n"
  52. " --gc N gc frequencty seconds (default 600)\n"
  53. " it is enabled if threads > 1\n"
  54. "\n"
  55. " At least one of --session-files,"
  56. " --cache\n"
  57. " should be defined\n"
  58. "\n";
  59. }
  60. params(int argc,char **argv) :
  61. en_cache(false),
  62. en_sessions(none),
  63. items_limit(-1),
  64. gc_frequency(-1),
  65. files_no(0),
  66. port(-1),
  67. ip("0.0.0.0"),
  68. threads(1)
  69. {
  70. using namespace std;
  71. argv++;
  72. while(*argv) {
  73. string param=*argv;
  74. char *next= *(argv+1);
  75. if(param=="--bind" && next) {
  76. ip=next;
  77. argv++;
  78. }
  79. else if(param=="--port" && next) {
  80. port=atoi(next);
  81. argv++;
  82. }
  83. else if(param=="--threads" && next) {
  84. threads=atoi(next);
  85. argv++;
  86. }
  87. /* else if(param=="--gc" && next) {
  88. gc_frequency=atoi(next);
  89. argv++;
  90. }*/
  91. else if(param=="--limit" && next) {
  92. items_limit=atoi(next);
  93. argv++;
  94. }
  95. else if(param=="--session-files") {
  96. en_sessions=files;
  97. }
  98. else if(param=="--dir" && next) {
  99. session_dir=next;
  100. argv++;
  101. }
  102. else if(param=="--cache") {
  103. en_cache=true;
  104. }
  105. else {
  106. help();
  107. throw runtime_error("Incorrect parameter:"+param);
  108. }
  109. argv++;
  110. }
  111. if(!en_cache && !en_sessions) {
  112. help();
  113. throw runtime_error("Neither cache nor sessions mods are defined");
  114. }
  115. if(en_sessions == files && session_dir.empty()) {
  116. help();
  117. throw runtime_error("parameter --dir undefined");
  118. }
  119. if(en_sessions == sqlite3 && session_file.empty()) {
  120. help();
  121. throw runtime_error("patameter --file undefined");
  122. }
  123. if(files_no == -1) files_no=1;
  124. if(port==-1) {
  125. help();
  126. throw runtime_error("parameter --port undefined");
  127. }
  128. if(en_cache && items_limit == -1) {
  129. help();
  130. throw runtime_error("parameter --limit undefined");
  131. }
  132. if(gc_frequency != -1) {
  133. if(threads == 1) {
  134. throw runtime_error("You have to use more then one thread to enable gc");
  135. }
  136. }
  137. if(threads > 1 && gc_frequency==-1) {
  138. gc_frequency = 600;
  139. }
  140. }
  141. };
  142. int main(int argc,char **argv)
  143. {
  144. try
  145. {
  146. params par(argc,argv);
  147. cppcms::intrusive_ptr<cppcms::impl::base_cache> cache;
  148. //auto_ptr<session_server_storage> storage;
  149. if(par.en_cache)
  150. cache = cppcms::impl::thread_cache_factory(par.items_limit);
  151. cppcms::impl::tcp_cache_service srv(cache,par.threads,par.ip,par.port);
  152. #ifndef CPPCMS_WIN32
  153. // Wait for signlas for exit
  154. sigset_t wait_mask;
  155. sigemptyset(&wait_mask);
  156. sigaddset(&wait_mask, SIGINT);
  157. sigaddset(&wait_mask, SIGQUIT);
  158. sigaddset(&wait_mask, SIGTERM);
  159. pthread_sigmask(SIG_BLOCK, &wait_mask, 0);
  160. int sig = 0;
  161. sigwait(&wait_mask, &sig);
  162. std::cout<<"Catched signal: exiting..."<<std::endl;
  163. #else
  164. std::cout << "Press any key to stop..." << std::flush;
  165. std::cin.get();
  166. #endif
  167. srv.stop();
  168. }
  169. catch(std::exception const &e) {
  170. std::cerr<<"Error:"<<e.what()<<std::endl;
  171. return 1;
  172. }
  173. std::cout<<"Done"<<std::endl;
  174. return 0;
  175. }