Merge in version/etc libtest
[m6w6/libmemcached] / libtest / server.h
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #pragma once
23
24 #include <cassert>
25 #include <cstdio>
26 #include <cstring>
27 #include <netdb.h>
28 #include <netinet/in.h>
29 #include <string>
30 #include <unistd.h>
31 #include <vector>
32
33 namespace libtest {
34
35 struct Server {
36 private:
37 bool _is_socket;
38 std::string _socket;
39 std::string _pid_file;
40 std::string _log_file;
41 std::string _base_command; // executable command which include libtool, valgrind, gdb, etc
42 std::string _running; // Current string being used for system()
43 pid_t _pid;
44
45 protected:
46 in_port_t _port;
47 std::string _hostname;
48 std::string _extra_args;
49
50 public:
51 Server(const std::string& hostname, const in_port_t port_arg, const bool is_socket_arg= false);
52
53 virtual ~Server();
54
55 virtual const char *name()= 0;
56 virtual const char *executable()= 0;
57 virtual const char *port_option()= 0;
58 virtual const char *pid_file_option()= 0;
59 virtual const char *daemon_file_option()= 0;
60 virtual const char *log_file_option()= 0;
61 virtual bool is_libtool()= 0;
62
63 virtual bool broken_socket_cleanup()
64 {
65 return false;
66 }
67
68 virtual const char *socket_file_option() const
69 {
70 return NULL;
71 }
72
73 virtual bool broken_pid_file()
74 {
75 return false;
76 }
77
78 const std::string& pid_file() const
79 {
80 return _pid_file;
81 }
82
83 const std::string& base_command() const
84 {
85 return _base_command;
86 }
87
88 const std::string& log_file() const
89 {
90 return _log_file;
91 }
92
93 const std::string& hostname() const
94 {
95 return _hostname;
96 }
97
98 const std::string& socket() const
99 {
100 return _socket;
101 }
102
103 bool has_socket() const
104 {
105 return _is_socket;
106 }
107
108 bool cycle();
109
110 virtual bool ping()= 0;
111
112 virtual pid_t get_pid(bool error_is_ok= false)= 0;
113
114 virtual bool build(int argc, const char *argv[])= 0;
115
116 in_port_t port() const
117 {
118 return _port;
119 }
120
121 bool has_port() const
122 {
123 return (_port != 0);
124 }
125
126 // Reset a server if another process has killed the server
127 void reset()
128 {
129 _pid= -1;
130 _pid_file.clear();
131 _log_file.clear();
132 }
133
134 void set_extra_args(const std::string &arg);
135
136 bool args(std::string& options);
137
138 pid_t pid();
139
140 pid_t pid() const
141 {
142 return _pid;
143 }
144
145 bool has_pid() const
146 {
147 return (_pid > 1);
148 }
149
150 bool check_pid(pid_t pid_arg) const
151 {
152 return (pid_arg > 1);
153 }
154
155 bool is_socket() const
156 {
157 return _hostname[0] == '/';
158 }
159
160 const std::string running() const
161 {
162 return _running;
163 }
164
165 std::string log_and_pid();
166
167 bool kill(pid_t pid_arg);
168 bool start();
169 bool command(std::string& command_arg);
170
171 protected:
172 void nap();
173
174 private:
175 bool is_helgrind() const;
176 bool is_valgrind() const;
177 bool is_debug() const;
178 bool set_log_file();
179 bool set_pid_file();
180 bool set_socket_file();
181 void rebuild_base_command();
182 void reset_pid();
183 };
184
185 std::ostream& operator<<(std::ostream& output, const libtest::Server &arg);
186
187 class server_startup_st
188 {
189 private:
190 std::string server_list;
191 bool _socket;
192
193 public:
194
195 uint8_t udp;
196 std::vector<Server *> servers;
197
198 server_startup_st() :
199 _socket(false),
200 udp(0)
201 { }
202
203 bool start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[]);
204
205 std::string option_string() const;
206
207 size_t count() const
208 {
209 return servers.size();
210 }
211
212 bool is_debug() const;
213 bool is_helgrind() const;
214 bool is_valgrind() const;
215
216 bool socket()
217 {
218 return _socket;
219 }
220
221 void set_socket()
222 {
223 _socket= true;
224 }
225
226
227 void shutdown(bool remove= false);
228 void push_server(Server *);
229 Server *pop_server();
230
231 ~server_startup_st();
232 };
233
234 bool server_startup(server_startup_st&, const std::string&, in_port_t try_port, int argc, const char *argv[]);
235
236 } // namespace libtest
237
238