Merge in pid/ping status.
[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 <cstring>
13 #include <netdb.h>
14 #include <netinet/in.h>
15 #include <string>
16 #include <unistd.h>
17
18 #define SERVERS_TO_CREATE 5
19
20 struct server_st;
21
22 typedef pid_t (test_server_getpid)(server_st &);
23 typedef bool (test_server_ping)(server_st &);
24
25 struct server_st {
26 private:
27 bool _used;
28 pid_t _pid;
29 in_port_t _port;
30 char pid_file[FILENAME_MAX]; // Did we start it, or was it just sitting there?
31 std::string _command;
32 test_server_getpid *__get_pid;
33 test_server_ping *__ping;
34
35 public:
36
37 char hostname[NI_MAXHOST];
38
39 server_st() :
40 _used(false),
41 _pid(-1),
42 _port(0),
43 __get_pid(NULL),
44 __ping(NULL)
45 {
46 pid_file[0]= 0;
47 strncpy(hostname, "localhost", sizeof(hostname));
48 }
49
50 void set_methods(test_server_getpid *get_pid_arg, test_server_ping *ping_arg)
51 {
52 __get_pid= get_pid_arg;
53 __ping= ping_arg;
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 void set_hostname(const char *arg)
117 {
118 strncpy(hostname, arg, sizeof(hostname));
119 }
120
121 bool kill();
122 bool start();
123
124 private:
125 void reset_pid();
126 };
127
128 std::ostream& operator<<(std::ostream& output, const server_st &arg);
129
130 struct server_startup_st
131 {
132 uint8_t count;
133 uint8_t udp;
134 std::string server_list;
135 server_st server[SERVERS_TO_CREATE];
136
137 server_startup_st() :
138 count(SERVERS_TO_CREATE),
139 udp(0)
140 { }
141
142 ~server_startup_st();
143 };
144
145 #ifdef __cplusplus
146 extern "C" {
147 #endif
148
149
150 bool server_startup(server_startup_st *construct);
151 void server_shutdown(server_startup_st *construct);
152
153 #ifdef __cplusplus
154 }
155 #endif