Updating test framework for startup/shutdown of memcached.
[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 private:
22 bool _used;
23 pid_t _pid;
24 in_port_t _port;
25 char pid_file[FILENAME_MAX]; // Did we start it, or was it just sitting there?
26 public:
27
28 char hostname[NI_MAXHOST];
29
30 server_st() :
31 _used(false),
32 _pid(-1),
33 _port(0)
34 {
35 pid_file[0]= 0;
36 strncpy(hostname, "localhost", sizeof(hostname));
37 }
38
39 void set_port(in_port_t arg)
40 {
41 _port= arg;
42 }
43
44 in_port_t port() const
45 {
46 return _port;
47 }
48
49 bool has_port() const
50 {
51 return not _port == 0;
52 }
53
54 void set_used()
55 {
56 _used= true;
57 }
58
59 void set_pid(pid_t arg)
60 {
61 _pid= arg;
62 }
63
64 pid_t pid() const
65 {
66 return _pid;
67 }
68
69 bool is_used() const
70 {
71 return _used;
72 }
73
74 ~server_st();
75
76 bool has_pid()
77 {
78 return _pid > 0;
79 }
80
81 bool is_socket() const
82 {
83 return hostname[0] == '/';
84 }
85
86 void set_hostname(const char *arg)
87 {
88 strncpy(hostname, arg, sizeof(hostname));
89 }
90
91 bool kill();
92
93 private:
94 void reset_pid();
95 };
96
97 std::ostream& operator<<(std::ostream& output, const server_st &arg);
98
99 struct server_startup_st
100 {
101 uint8_t count;
102 uint8_t udp;
103 std::string server_list;
104 server_st server[SERVERS_TO_CREATE];
105
106 server_startup_st() :
107 count(SERVERS_TO_CREATE),
108 udp(0)
109 { }
110
111 ~server_startup_st()
112 { }
113 };
114
115 #ifdef __cplusplus
116 extern "C" {
117 #endif
118
119
120 bool server_startup(server_startup_st *construct);
121 void server_shutdown(server_startup_st *construct);
122
123 #ifdef __cplusplus
124 }
125 #endif