Merge in fix for Ubuntu
[m6w6/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 <cstring>
13 #include <netdb.h>
14 #include <netinet/in.h>
15 #include <string>
16 #include <unistd.h>
17 #include <vector>
18
19 #define SERVERS_TO_CREATE 5
20
21 struct server_st;
22
23 typedef pid_t (test_server_getpid)(server_st &);
24 typedef bool (test_server_ping)(server_st &);
25
26 struct server_st {
27 private:
28 bool _used;
29 pid_t _pid;
30 in_port_t _port;
31 char pid_file[FILENAME_MAX]; // Did we start it, or was it just sitting there?
32 std::string _command;
33 test_server_getpid *__get_pid;
34 test_server_ping *__ping;
35 std::string _hostname;
36
37 public:
38 server_st(in_port_t port_arg, test_server_getpid *, test_server_ping *);
39
40 server_st(const std::string &socket_file, test_server_getpid *, test_server_ping *);
41
42 void set_methods(test_server_getpid *get_pid_arg, test_server_ping *ping_arg)
43 {
44 __get_pid= get_pid_arg;
45 __ping= ping_arg;
46 }
47
48 const char *hostname() const
49 {
50 if (_hostname.empty())
51 return "";
52
53 return _hostname.c_str();
54 }
55
56 bool ping()
57 {
58 if (__ping)
59 return __ping(*this);
60
61 return false;
62 }
63
64 pid_t get_pid()
65 {
66 if (__get_pid)
67 return _pid= __get_pid(*this);
68
69 return -1;
70 }
71
72 void set_port(in_port_t arg)
73 {
74 _port= arg;
75 }
76
77 in_port_t port() const
78 {
79 return _port;
80 }
81
82 bool has_port() const
83 {
84 return (_port != 0);
85 }
86
87 void set_command(const char *arg)
88 {
89 _command= arg;
90 }
91
92 void set_used()
93 {
94 _used= true;
95 }
96
97 pid_t pid();
98
99 bool is_used() const
100 {
101 return _used;
102 }
103
104 ~server_st();
105
106 bool has_pid()
107 {
108 return (_pid > 1);
109 }
110
111 bool is_socket() const
112 {
113 return _hostname[0] == '/';
114 }
115
116 bool kill();
117 bool start();
118
119 private:
120 void reset_pid();
121 };
122
123 std::ostream& operator<<(std::ostream& output, const server_st &arg);
124
125 struct server_startup_st
126 {
127 uint32_t count;
128 uint8_t udp;
129 std::string server_list;
130 std::vector<server_st *> servers;
131
132 server_startup_st() :
133 count(SERVERS_TO_CREATE),
134 udp(0)
135 { }
136
137 void shutdown();
138 void push_server(server_st *);
139
140 ~server_startup_st();
141 };
142
143 #ifdef __cplusplus
144 extern "C" {
145 #endif
146
147
148 bool server_startup(server_startup_st *construct);
149 void server_shutdown(server_startup_st *construct);
150
151 #ifdef __cplusplus
152 }
153 #endif