server.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /*
  2. * Copyright (C) 2011 Data Differential, http://datadifferential.com/
  3. * Copyright (C) 2006-2009 Brian Aker
  4. * All rights reserved.
  5. *
  6. * Use and distribution licensed under the BSD license. See
  7. * the COPYING file in the parent directory for full text.
  8. */
  9. #pragma once
  10. #include <cassert>
  11. #include <cstdio>
  12. #include <cstring>
  13. #include <netdb.h>
  14. #include <netinet/in.h>
  15. #include <string>
  16. #include <unistd.h>
  17. #include <vector>
  18. namespace libtest {
  19. struct Server {
  20. private:
  21. bool _is_socket;
  22. std::string _socket;
  23. std::string _pid_file;
  24. std::string _log_file;
  25. std::string _base_command; // executable command which include libtool, valgrind, gdb, etc
  26. std::string _running; // Current string being used for system()
  27. pid_t _pid;
  28. protected:
  29. in_port_t _port;
  30. std::string _hostname;
  31. std::string _extra_args;
  32. public:
  33. Server(const std::string& hostname, const in_port_t port_arg, const bool is_socket_arg= false);
  34. virtual ~Server();
  35. virtual const char *name()= 0;
  36. virtual const char *executable()= 0;
  37. virtual const char *port_option()= 0;
  38. virtual const char *pid_file_option()= 0;
  39. virtual const char *daemon_file_option()= 0;
  40. virtual const char *log_file_option()= 0;
  41. virtual bool is_libtool()= 0;
  42. virtual const char *socket_file_option() const
  43. {
  44. return NULL;
  45. }
  46. virtual bool broken_pid_file()
  47. {
  48. return false;
  49. }
  50. const std::string& pid_file() const
  51. {
  52. return _pid_file;
  53. }
  54. const std::string& base_command() const
  55. {
  56. return _base_command;
  57. }
  58. const std::string& log_file() const
  59. {
  60. return _log_file;
  61. }
  62. const std::string& hostname() const
  63. {
  64. return _hostname;
  65. }
  66. const std::string& socket() const
  67. {
  68. return _socket;
  69. }
  70. bool has_socket() const
  71. {
  72. return _is_socket;
  73. }
  74. bool cycle();
  75. virtual bool ping()= 0;
  76. virtual pid_t get_pid(bool error_is_ok= false)= 0;
  77. virtual bool build(int argc, const char *argv[])= 0;
  78. in_port_t port() const
  79. {
  80. return _port;
  81. }
  82. bool has_port() const
  83. {
  84. return (_port != 0);
  85. }
  86. // Reset a server if another process has killed the server
  87. void reset()
  88. {
  89. _pid= -1;
  90. _pid_file.clear();
  91. _log_file.clear();
  92. }
  93. void set_extra_args(const std::string &arg);
  94. bool args(std::string& options);
  95. pid_t pid();
  96. pid_t pid() const
  97. {
  98. return _pid;
  99. }
  100. bool has_pid() const
  101. {
  102. return (_pid > 1);
  103. }
  104. bool check_pid(pid_t pid_arg) const
  105. {
  106. return (pid_arg > 1);
  107. }
  108. bool is_socket() const
  109. {
  110. return _hostname[0] == '/';
  111. }
  112. const std::string running() const
  113. {
  114. return _running;
  115. }
  116. std::string log_and_pid();
  117. bool kill(pid_t pid_arg);
  118. bool start();
  119. bool command(std::string& command_arg);
  120. protected:
  121. void nap();
  122. private:
  123. bool is_helgrind() const;
  124. bool is_valgrind() const;
  125. bool is_debug() const;
  126. bool set_log_file();
  127. bool set_pid_file();
  128. bool set_socket_file();
  129. void rebuild_base_command();
  130. void reset_pid();
  131. };
  132. std::ostream& operator<<(std::ostream& output, const libtest::Server &arg);
  133. class server_startup_st
  134. {
  135. private:
  136. std::string server_list;
  137. public:
  138. uint8_t udp;
  139. std::vector<Server *> servers;
  140. server_startup_st() :
  141. udp(0)
  142. { }
  143. bool start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[]);
  144. std::string option_string() const;
  145. size_t count() const
  146. {
  147. return servers.size();
  148. }
  149. bool is_debug() const;
  150. bool is_helgrind() const;
  151. bool is_valgrind() const;
  152. void shutdown(bool remove= false);
  153. void push_server(Server *);
  154. Server *pop_server();
  155. ~server_startup_st();
  156. };
  157. bool server_startup(server_startup_st&, const std::string&, in_port_t try_port, int argc, const char *argv[]);
  158. } // namespace libtest