b50b0b2e6fe516a125b679a614e2e14eba9af19c
[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 _sasl;
40 std::string _pid_file;
41 std::string _log_file;
42 std::string _base_command; // executable command which include libtool, valgrind, gdb, etc
43 std::string _running; // Current string being used for system()
44 pid_t _pid;
45
46 protected:
47 in_port_t _port;
48 std::string _hostname;
49 std::string _extra_args;
50
51 public:
52 Server(const std::string& hostname, const in_port_t port_arg, const bool is_socket_arg= false);
53
54 virtual ~Server();
55
56 virtual const char *name()= 0;
57 virtual const char *executable()= 0;
58 virtual const char *port_option()= 0;
59 virtual const char *pid_file_option()= 0;
60 virtual const char *daemon_file_option()= 0;
61 virtual const char *log_file_option()= 0;
62 virtual bool is_libtool()= 0;
63
64 virtual bool broken_socket_cleanup()
65 {
66 return false;
67 }
68
69 virtual const char *socket_file_option() const
70 {
71 return NULL;
72 }
73
74 virtual bool broken_pid_file()
75 {
76 return false;
77 }
78
79 const std::string& pid_file() const
80 {
81 return _pid_file;
82 }
83
84 const std::string& base_command() const
85 {
86 return _base_command;
87 }
88
89 const std::string& log_file() const
90 {
91 return _log_file;
92 }
93
94 const std::string& hostname() const
95 {
96 return _hostname;
97 }
98
99 const std::string& socket() const
100 {
101 return _socket;
102 }
103
104 bool has_socket() const
105 {
106 return _is_socket;
107 }
108
109 bool cycle();
110
111 virtual bool ping()= 0;
112
113 virtual pid_t get_pid(bool error_is_ok= false)= 0;
114
115 virtual bool build(int argc, const char *argv[])= 0;
116
117 in_port_t port() const
118 {
119 return _port;
120 }
121
122 bool has_port() const
123 {
124 return (_port != 0);
125 }
126
127 virtual bool has_syslog() const
128 {
129 return false;
130 }
131
132 // Reset a server if another process has killed the server
133 void reset()
134 {
135 _pid= -1;
136 _pid_file.clear();
137 _log_file.clear();
138 }
139
140 void set_extra_args(const std::string &arg);
141
142 bool args(std::string& options);
143
144 pid_t pid();
145
146 pid_t pid() const
147 {
148 return _pid;
149 }
150
151 bool has_pid() const
152 {
153 return (_pid > 1);
154 }
155
156 bool wait_for_pidfile() const;
157
158 bool check_pid(pid_t pid_arg) const
159 {
160 return (pid_arg > 1);
161 }
162
163 bool is_socket() const
164 {
165 return _hostname[0] == '/';
166 }
167
168 const std::string running() const
169 {
170 return _running;
171 }
172
173 std::string log_and_pid();
174
175 bool kill(pid_t pid_arg);
176 bool start();
177 bool command(std::string& command_arg);
178
179 protected:
180 bool set_pid_file();
181
182 private:
183 bool is_helgrind() const;
184 bool is_valgrind() const;
185 bool is_debug() const;
186 bool set_log_file();
187 bool set_socket_file();
188 void rebuild_base_command();
189 void reset_pid();
190 };
191
192 std::ostream& operator<<(std::ostream& output, const libtest::Server &arg);
193
194 } // namespace libtest
195
196