Add in missing files.
[m6w6/libmemcached] / libtest / client.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include "mem_config.h"
38
39 #include <libtest/common.h>
40
41 #ifdef HAVE_POLL_H
42 # include <poll.h>
43 #endif
44
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <unistd.h>
48 #include <string>
49
50
51 namespace libtest {
52
53 SimpleClient::SimpleClient(const std::string& hostname_, in_port_t port_) :
54 _hostname(hostname_),
55 _port(port_),
56 sock_fd(INVALID_SOCKET),
57 requested_message(1)
58 {
59 }
60
61 bool SimpleClient::ready(int event_)
62 {
63 struct pollfd fds[1];
64 fds[0].fd= sock_fd;
65 fds[0].events= event_;
66 fds[0].revents= 0;
67
68 int ready_fds= poll(fds, 1, 5000);
69
70 if (ready_fds == -1)
71 {
72 _error= strerror(errno);
73 return false;
74 }
75 else if (ready_fds == 1)
76 {
77 if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL))
78 {
79 int err;
80 socklen_t len= sizeof (err);
81 // We replace errno with err if getsockopt() passes, but err has been
82 // set.
83 if (getsockopt(fds[0].fd, SOL_SOCKET, SO_ERROR, &err, &len) == 0)
84 {
85 // We check the value to see what happened wth the socket.
86 if (err == 0)
87 {
88 _error= "getsockopt() returned no error but poll() indicated one existed";
89 return false;
90 }
91 errno= err;
92 }
93 _error= strerror(errno);
94
95 return false;
96 }
97
98 if (fds[0].revents & event_)
99 {
100 return true;
101 }
102 }
103
104 fatal_assert(ready_fds == 0);
105 _error= "TIMEOUT";
106
107 return false;
108 }
109
110 struct addrinfo* SimpleClient::lookup()
111 {
112 struct addrinfo *ai= NULL;
113 struct addrinfo hints;
114 memset(&hints, 0, sizeof(struct addrinfo));
115 hints.ai_socktype= SOCK_STREAM;
116 hints.ai_protocol= IPPROTO_TCP;
117
118 char service[NI_MAXSERV];
119 (void)snprintf(service, NI_MAXSERV, "%d", _port);
120
121 int getaddrinfo_error;
122 if ((getaddrinfo_error= getaddrinfo(_hostname.c_str(), service, &hints, &ai)) != 0)
123 {
124 if (getaddrinfo_error != EAI_SYSTEM)
125 {
126 _error= gai_strerror(getaddrinfo_error);
127 return NULL;
128 }
129 else
130 {
131 _error= strerror(getaddrinfo_error);
132 return NULL;
133 }
134 }
135
136 return ai;
137 }
138
139 SimpleClient::~SimpleClient()
140 {
141 close_socket();
142 }
143
144 void SimpleClient::close_socket()
145 {
146 close(sock_fd);
147 sock_fd= INVALID_SOCKET;
148 }
149
150 bool SimpleClient::instance_connect()
151 {
152 struct addrinfo *ai;
153 if ((ai= lookup()))
154 {
155 {
156 struct addrinfo* address_info_next= ai;
157
158 while (address_info_next and sock_fd == INVALID_SOCKET)
159 {
160 if ((sock_fd= socket(address_info_next->ai_family, address_info_next->ai_socktype, address_info_next->ai_protocol)) != SOCKET_ERROR)
161 {
162 if (connect(sock_fd, address_info_next->ai_addr, address_info_next->ai_addrlen) == SOCKET_ERROR)
163 {
164 close_socket();
165 _error= strerror(errno);
166 }
167 }
168 else
169 {
170 fatal_message(strerror(errno));
171 }
172 address_info_next= address_info_next->ai_next;
173 }
174
175 freeaddrinfo(ai);
176 }
177
178 if (sock_fd == INVALID_SOCKET)
179 {
180 fatal_assert(_error.size());
181 }
182
183 return bool(sock_fd != INVALID_SOCKET);
184 }
185
186 return false;
187 }
188
189 bool SimpleClient::is_valid()
190 {
191 _error.clear();
192 if (sock_fd == INVALID_SOCKET)
193 {
194 return instance_connect();
195 }
196
197 return true;
198 }
199
200 bool SimpleClient::message(const std::string& arg)
201 {
202 if (is_valid())
203 {
204 if (ready(POLLOUT))
205 {
206 off_t offset= 0;
207 const char* ptr= arg.c_str();
208 size_t len= arg.size();
209
210 do
211 {
212 ssize_t nw= send(sock_fd, ptr + offset, len - offset, MSG_NOSIGNAL);
213 if (nw == -1)
214 {
215 if (errno != EINTR)
216 {
217 _error= strerror(errno);
218 return false;
219 }
220 }
221 else
222 {
223 offset += nw;
224 }
225 } while (offset < ssize_t(len));
226
227 return true;
228 }
229 }
230
231 fatal_assert(_error.size());
232
233 return false;
234 }
235
236 bool SimpleClient::send_message(const std::string& arg)
237 {
238 if (message(arg) == true)
239 {
240 return message("\r\n");
241 }
242
243 return false;
244 }
245
246 bool SimpleClient::send_message(const std::string& message_, std::string& response_)
247 {
248 requested_message++;
249 if (send_message(message_))
250 {
251 return response(response_);
252 }
253
254 return false;
255 }
256
257 bool SimpleClient::response(std::string& response_)
258 {
259 response_.clear();
260
261 if (is_valid())
262 {
263 if (ready(POLLIN))
264 {
265 bool more= true;
266 char buffer[2];
267 buffer[1]= 0;
268 do
269 {
270 ssize_t nr= recv(sock_fd, buffer, 1, MSG_NOSIGNAL);
271 if (nr == -1)
272 {
273 if (errno != EINTR)
274 {
275 _error= strerror(errno);
276 return false;
277 }
278 }
279 else
280 {
281 fatal_assert(nr == 1);
282 if (buffer[0] == '\n')
283 {
284 more= false;
285 }
286 response_.append(buffer);
287 }
288 } while (more);
289
290 return response_.size();
291 }
292 }
293
294 fatal_assert(_error.size());
295 return false;
296 }
297
298 } // namespace libtest