Merging bzr://gaz.tangent.org/libmemcached/build/ to Build branch
[awesomized/libmemcached] / libtest / gearmand.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 #include <config.h>
24 #include <libtest/common.h>
25
26 #include <libtest/gearmand.h>
27
28 #include "util/instance.hpp"
29 #include "util/operation.hpp"
30
31 using namespace datadifferential;
32 using namespace libtest;
33
34 #include <cassert>
35 #include <cerrno>
36 #include <cstdio>
37 #include <cstdlib>
38 #include <cstring>
39 #include <iostream>
40 #include <signal.h>
41 #include <sstream>
42 #include <sys/types.h>
43 #include <sys/wait.h>
44 #include <unistd.h>
45
46 #include <libgearman/gearman.h>
47
48 #ifndef __INTEL_COMPILER
49 #pragma GCC diagnostic ignored "-Wold-style-cast"
50 #endif
51
52 class GetPid : public util::Instance::Finish
53 {
54 private:
55 pid_t _pid;
56
57 public:
58 GetPid() :
59 _pid(-1)
60 { }
61
62 pid_t pid()
63 {
64 return _pid;
65 }
66
67
68 bool call(const bool success, const std::string &response)
69 {
70 _pid= -1;
71 if (success and response.size())
72 {
73 _pid= atoi(response.c_str());
74 }
75
76 if (is_pid_valid(_pid) == false)
77 {
78 _pid= -1;
79 return false;
80 }
81
82 return true;
83 }
84 };
85
86 using namespace libtest;
87
88 class Gearmand : public libtest::Server
89 {
90 private:
91 public:
92 Gearmand(const std::string& host_arg, in_port_t port_arg) :
93 libtest::Server(host_arg, port_arg, GEARMAND_BINARY, true)
94 {
95 set_pid_file();
96 }
97
98 bool ping()
99 {
100 gearman_client_st *client= gearman_client_create(NULL);
101 if (client == NULL)
102 {
103 Error << "Could not allocate memory for gearman_client_create()";
104 return false;
105 }
106 gearman_client_set_timeout(client, 3000);
107
108 if (gearman_success(gearman_client_add_server(client, hostname().c_str(), port())))
109 {
110 gearman_return_t rc= gearman_client_echo(client, test_literal_param("This is my echo test"));
111
112 if (gearman_success(rc))
113 {
114 gearman_client_free(client);
115 return true;
116 }
117 #if 0
118 Error << hostname().c_str() << ":" << port() << " was " << gearman_strerror(rc) << " extended: " << gearman_client_error(client);
119 #endif
120 }
121 else
122 {
123 Error << "gearman_client_add_server() " << gearman_client_error(client);
124 }
125
126 gearman_client_free(client);
127
128 return false;;
129 }
130
131 const char *name()
132 {
133 return "gearmand";
134 };
135
136 void log_file_option(Application& app, const std::string& arg)
137 {
138 if (arg.empty() == false)
139 {
140 std::string buffer("--log-file=");
141 buffer+= arg;
142 app.add_option("--verbose=DEBUG");
143 app.add_option(buffer);
144 }
145 }
146
147 bool has_log_file_option() const
148 {
149 return true;
150 }
151
152 bool is_libtool()
153 {
154 return true;
155 }
156
157 bool has_syslog() const
158 {
159 return true;
160 }
161
162 bool has_port_option() const
163 {
164 return true;
165 }
166
167 bool build(size_t argc, const char *argv[]);
168 };
169
170 bool Gearmand::build(size_t argc, const char *argv[])
171 {
172 std::stringstream arg_buffer;
173
174 if (getuid() == 0 or geteuid() == 0)
175 {
176 add_option("-u", "root");
177 }
178
179 add_option("--listen=localhost");
180
181 for (size_t x= 0 ; x < argc ; x++)
182 {
183 add_option(argv[x]);
184 }
185
186 return true;
187 }
188
189 namespace libtest {
190
191 libtest::Server *build_gearmand(const char *hostname, in_port_t try_port)
192 {
193 return new Gearmand(hostname, try_port);
194 }
195
196 }