Merge in merge of libtest
[awesomized/libmemcached] / libtest / server.h
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
10 #pragma once
11
12 #include <cassert>
13 #include <cstdio>
14 #include <cstring>
15 #include <netdb.h>
16 #include <netinet/in.h>
17 #include <string>
18 #include <unistd.h>
19 #include <vector>
20
21 namespace libtest {
22
23 struct Server {
24 private:
25 bool _is_socket;
26 std::string _socket;
27 std::string _pid_file;
28 std::string _log_file;
29 std::string _base_command; // executable command which include libtool, valgrind, gdb, etc
30 std::string _running; // Current string being used for system()
31 pid_t _pid;
32
33 protected:
34 in_port_t _port;
35 std::string _hostname;
36 std::string _extra_args;
37
38 public:
39 Server(const std::string& hostname, const in_port_t port_arg, const bool is_socket_arg= false);
40
41 virtual ~Server();
42
43 virtual const char *name()= 0;
44 virtual const char *executable()= 0;
45 virtual const char *port_option()= 0;
46 virtual const char *pid_file_option()= 0;
47 virtual const char *daemon_file_option()= 0;
48 virtual const char *log_file_option()= 0;
49 virtual bool is_libtool()= 0;
50
51 virtual const char *socket_file_option() const
52 {
53 return NULL;
54 }
55
56 virtual bool broken_pid_file()
57 {
58 return false;
59 }
60
61 const std::string& pid_file() const
62 {
63 return _pid_file;
64 }
65
66 const std::string& base_command() const
67 {
68 return _base_command;
69 }
70
71 const std::string& log_file() const
72 {
73 return _log_file;
74 }
75
76 const std::string& hostname() const
77 {
78 return _hostname;
79 }
80
81 const std::string& socket() const
82 {
83 return _socket;
84 }
85
86 bool has_socket() const
87 {
88 return _is_socket;
89 }
90
91 bool cycle();
92
93 virtual bool ping()= 0;
94
95 virtual pid_t get_pid(bool error_is_ok= false)= 0;
96
97 virtual bool build(int argc, const char *argv[])= 0;
98
99 in_port_t port() const
100 {
101 return _port;
102 }
103
104 bool has_port() const
105 {
106 return (_port != 0);
107 }
108
109 // Reset a server if another process has killed the server
110 void reset()
111 {
112 _pid= -1;
113 _pid_file.clear();
114 _log_file.clear();
115 }
116
117 void set_extra_args(const std::string &arg);
118
119 bool args(std::string& options);
120
121 pid_t pid();
122
123 pid_t pid() const
124 {
125 return _pid;
126 }
127
128 bool has_pid() const
129 {
130 return (_pid > 1);
131 }
132
133 bool check_pid(pid_t pid_arg) const
134 {
135 return (pid_arg > 1);
136 }
137
138 bool is_socket() const
139 {
140 return _hostname[0] == '/';
141 }
142
143 const std::string running() const
144 {
145 return _running;
146 }
147
148 std::string log_and_pid();
149
150 bool kill(pid_t pid_arg);
151 bool start();
152 bool command(std::string& command_arg);
153
154 protected:
155 void nap();
156
157 private:
158 bool is_valgrind() const;
159 bool is_debug() const;
160 bool set_log_file();
161 bool set_pid_file();
162 bool set_socket_file();
163 void rebuild_base_command();
164 void reset_pid();
165 };
166
167 std::ostream& operator<<(std::ostream& output, const libtest::Server &arg);
168
169 class server_startup_st
170 {
171 private:
172 std::string server_list;
173
174 public:
175
176 uint8_t udp;
177 std::vector<Server *> servers;
178
179 server_startup_st() :
180 udp(0)
181 { }
182
183 bool start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[]);
184
185 std::string option_string() const;
186
187 size_t count() const
188 {
189 return servers.size();
190 }
191
192 bool is_debug() const;
193 bool is_valgrind() const;
194
195 void shutdown(bool remove= false);
196 void push_server(Server *);
197 Server *pop_server();
198
199 ~server_startup_st();
200 };
201
202 bool server_startup(server_startup_st&, const std::string&, in_port_t try_port, int argc, const char *argv[]);
203
204 } // namespace libtest
205
206