Merge in all of libtest updates.
[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
32 protected:
33 pid_t _pid;
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= true)= 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 is_socket() const
134 {
135 return _hostname[0] == '/';
136 }
137
138 const std::string running() const
139 {
140 return _running;
141 }
142
143 std::string log_and_pid();
144
145 bool kill();
146 bool start();
147 bool command(std::string& command_arg);
148
149 protected:
150 void nap();
151
152 private:
153 bool is_valgrind() const;
154 bool is_debug() const;
155 bool set_log_file();
156 bool set_pid_file();
157 bool set_socket_file();
158 void rebuild_base_command();
159 void reset_pid();
160 };
161
162 std::ostream& operator<<(std::ostream& output, const libtest::Server &arg);
163
164 class server_startup_st
165 {
166 private:
167 std::string server_list;
168
169 public:
170
171 uint8_t udp;
172 std::vector<Server *> servers;
173
174 server_startup_st() :
175 udp(0)
176 { }
177
178 bool start_socket_server(const std::string& server_type, const in_port_t try_port, int argc, const char *argv[]);
179
180 std::string option_string() const;
181
182 size_t count() const
183 {
184 return servers.size();
185 }
186
187 bool is_debug() const;
188 bool is_valgrind() const;
189
190 void shutdown(bool remove= false);
191 void push_server(Server *);
192 Server *pop_server();
193
194 ~server_startup_st();
195 };
196
197 bool server_startup(server_startup_st&, const std::string&, in_port_t try_port, int argc, const char *argv[]);
198
199 } // namespace libtest
200
201