Merge in updated yatl.
[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 "libtest/yatlcon.h"
38 #include <libtest/common.h>
39
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <unistd.h>
43 #include <string>
44
45
46 namespace libtest {
47
48 SimpleClient::SimpleClient(const std::string& hostname_, in_port_t port_) :
49 _is_connected(false),
50 _hostname(hostname_),
51 _port(port_),
52 sock_fd(INVALID_SOCKET),
53 requested_message(1)
54 {
55 }
56
57 bool SimpleClient::ready(int event_)
58 {
59 struct pollfd fds[1];
60 fds[0].fd= sock_fd;
61 fds[0].events= event_;
62 fds[0].revents= 0;
63
64 int timeout= 5000;
65 if (_is_connected == false)
66 {
67 timeout= timeout * 30;
68 }
69
70 int ready_fds= poll(fds, 1, timeout);
71
72 if (ready_fds == -1)
73 {
74 _error= strerror(errno);
75 return false;
76 }
77 else if (ready_fds == 1)
78 {
79 if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL))
80 {
81 int err;
82 socklen_t len= sizeof (err);
83 // We replace errno with err if getsockopt() passes, but err has been
84 // set.
85 if (getsockopt(fds[0].fd, SOL_SOCKET, SO_ERROR, &err, &len) == 0)
86 {
87 // We check the value to see what happened wth the socket.
88 if (err == 0)
89 {
90 _error= "getsockopt() returned no error but poll() indicated one existed";
91 return false;
92 }
93 errno= err;
94 }
95 _error= strerror(errno);
96
97 return false;
98 }
99
100 _is_connected= true;
101 if (fds[0].revents & event_)
102 {
103 return true;
104 }
105 }
106
107 fatal_assert(ready_fds == 0);
108 _error= "TIMEOUT";
109
110 return false;
111 }
112
113 struct addrinfo* SimpleClient::lookup()
114 {
115 struct addrinfo *ai= NULL;
116 struct addrinfo hints;
117 memset(&hints, 0, sizeof(struct addrinfo));
118 hints.ai_socktype= SOCK_STREAM;
119 hints.ai_protocol= IPPROTO_TCP;
120
121 libtest::vchar_t service;
122 service.resize(NI_MAXSERV);
123 (void)snprintf(&service[0], service.size(), "%d", _port);
124
125 int getaddrinfo_error;
126 if ((getaddrinfo_error= getaddrinfo(_hostname.c_str(), &service[0], &hints, &ai)) != 0)
127 {
128 if (getaddrinfo_error != EAI_SYSTEM)
129 {
130 _error= gai_strerror(getaddrinfo_error);
131 return NULL;
132 }
133 else
134 {
135 _error= strerror(getaddrinfo_error);
136 return NULL;
137 }
138 }
139
140 return ai;
141 }
142
143 SimpleClient::~SimpleClient()
144 {
145 close_socket();
146 }
147
148 void SimpleClient::close_socket()
149 {
150 if (sock_fd != INVALID_SOCKET)
151 {
152 close(sock_fd);
153 sock_fd= INVALID_SOCKET;
154 }
155 }
156
157 bool SimpleClient::instance_connect()
158 {
159 _is_connected= false;
160 struct addrinfo *ai;
161 if ((ai= lookup()))
162 {
163 {
164 struct addrinfo* address_info_next= ai;
165
166 while (address_info_next and sock_fd == INVALID_SOCKET)
167 {
168 if ((sock_fd= socket(address_info_next->ai_family, address_info_next->ai_socktype, address_info_next->ai_protocol)) != SOCKET_ERROR)
169 {
170 if (connect(sock_fd, address_info_next->ai_addr, address_info_next->ai_addrlen) == SOCKET_ERROR)
171 {
172 close_socket();
173 _error= strerror(errno);
174 }
175 }
176 else
177 {
178 fatal_message(strerror(errno));
179 }
180 address_info_next= address_info_next->ai_next;
181 }
182
183 freeaddrinfo(ai);
184 }
185
186 if (sock_fd == INVALID_SOCKET)
187 {
188 fatal_assert(_error.size());
189 }
190
191 return bool(sock_fd != INVALID_SOCKET);
192 }
193
194 return false;
195 }
196
197 bool SimpleClient::is_valid()
198 {
199 _error.clear();
200 if (sock_fd == INVALID_SOCKET)
201 {
202 return instance_connect();
203 }
204
205 return true;
206 }
207
208 bool SimpleClient::message(const char* ptr, const size_t len)
209 {
210 if (is_valid())
211 {
212 if (ready(POLLOUT))
213 {
214 off_t offset= 0;
215 do
216 {
217 ssize_t nw= send(sock_fd, ptr + offset, len - offset, MSG_NOSIGNAL);
218 if (nw == -1)
219 {
220 if (errno != EINTR)
221 {
222 _error= strerror(errno);
223 return false;
224 }
225 }
226 else
227 {
228 offset += nw;
229 }
230 } while (offset < ssize_t(len));
231
232 return true;
233 }
234 }
235
236 fatal_assert(_error.size());
237
238 return false;
239 }
240
241 bool SimpleClient::send_message(const std::string& arg)
242 {
243 if (message(arg.c_str(), arg.size()) == true)
244 {
245 return message("\r\n", 2);
246 }
247
248 return false;
249 }
250
251 bool SimpleClient::send_data(const libtest::vchar_t& message_, libtest::vchar_t& response_)
252 {
253 requested_message++;
254 if (message(&message_[0], message_.size()))
255 {
256 return response(response_);
257 }
258
259 return false;
260 }
261
262 bool SimpleClient::send_message(const std::string& message_, std::string& response_)
263 {
264 requested_message++;
265 if (send_message(message_))
266 {
267 return response(response_);
268 }
269
270 return false;
271 }
272
273 bool SimpleClient::response(libtest::vchar_t& response_)
274 {
275 response_.clear();
276
277 if (is_valid())
278 {
279 if (ready(POLLIN))
280 {
281 bool more= true;
282 char buffer[2];
283 buffer[1]= 0;
284 do
285 {
286 ssize_t nr= recv(sock_fd, buffer, 1, MSG_NOSIGNAL);
287 if (nr == -1)
288 {
289 if (errno != EINTR)
290 {
291 _error= strerror(errno);
292 return false;
293 }
294 }
295 else if (nr == 0)
296 {
297 close_socket();
298 more= false;
299 }
300 else
301 {
302 response_.reserve(response_.size() + nr +1);
303 fatal_assert(nr == 1);
304 if (buffer[0] == '\n')
305 {
306 more= false;
307 }
308 response_.insert(response_.end(), buffer, buffer +nr);
309 }
310 } while (more);
311
312 return response_.size();
313 }
314 }
315
316 fatal_assert(_error.size());
317 return false;
318 }
319
320 bool SimpleClient::response(std::string& response_)
321 {
322 response_.clear();
323
324 if (is_valid())
325 {
326 if (ready(POLLIN))
327 {
328 bool more= true;
329 char buffer[2];
330 buffer[1]= 0;
331 do
332 {
333 ssize_t nr= recv(sock_fd, buffer, 1, MSG_NOSIGNAL);
334 if (nr == -1)
335 {
336 if (errno != EINTR)
337 {
338 _error= strerror(errno);
339 return false;
340 }
341 }
342 else if (nr == 0)
343 {
344 close_socket();
345 more= false;
346 }
347 else
348 {
349 fatal_assert(nr == 1);
350 if (buffer[0] == '\n')
351 {
352 more= false;
353 }
354 response_.append(buffer);
355 }
356 } while (more);
357
358 return response_.size();
359 }
360 }
361
362 fatal_assert(_error.size());
363 return false;
364 }
365
366 } // namespace libtest