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