fa3fcbdb1cedbaab974fff5966fbc4bb8fe88b48
[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 #ifndef HAVE_MSG_NOSIGNAL
51 # define MSG_NOSIGNAL 0
52 #endif
53
54 namespace libtest {
55
56 SimpleClient::SimpleClient(const std::string& hostname_, in_port_t port_) :
57 _hostname(hostname_),
58 _port(port_),
59 sock_fd(INVALID_SOCKET),
60 requested_message(1)
61 {
62 }
63
64 bool SimpleClient::ready(int event_)
65 {
66 struct pollfd fds[1];
67 fds[0].fd= sock_fd;
68 fds[0].events= event_;
69 fds[0].revents= 0;
70
71 int ready_fds= poll(fds, 1, 5000);
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 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 char service[NI_MAXSERV];
122 (void)snprintf(service, NI_MAXSERV, "%d", _port);
123
124 int getaddrinfo_error;
125 if ((getaddrinfo_error= getaddrinfo(_hostname.c_str(), service, &hints, &ai)) != 0)
126 {
127 if (getaddrinfo_error != EAI_SYSTEM)
128 {
129 _error= gai_strerror(getaddrinfo_error);
130 return NULL;
131 }
132 else
133 {
134 _error= strerror(getaddrinfo_error);
135 return NULL;
136 }
137 }
138
139 return ai;
140 }
141
142 SimpleClient::~SimpleClient()
143 {
144 close_socket();
145 }
146
147 void SimpleClient::close_socket()
148 {
149 close(sock_fd);
150 sock_fd= INVALID_SOCKET;
151 }
152
153 bool SimpleClient::instance_connect()
154 {
155 struct addrinfo *ai;
156 if ((ai= lookup()))
157 {
158 {
159 struct addrinfo* address_info_next= ai;
160
161 while (address_info_next and sock_fd == INVALID_SOCKET)
162 {
163 if ((sock_fd= socket(address_info_next->ai_family, address_info_next->ai_socktype, address_info_next->ai_protocol)) != SOCKET_ERROR)
164 {
165 if (connect(sock_fd, address_info_next->ai_addr, address_info_next->ai_addrlen) == SOCKET_ERROR)
166 {
167 close_socket();
168 _error= strerror(errno);
169 }
170 }
171 else
172 {
173 fatal_message(strerror(errno));
174 }
175 address_info_next= address_info_next->ai_next;
176 }
177
178 freeaddrinfo(ai);
179 }
180
181 if (sock_fd == INVALID_SOCKET)
182 {
183 fatal_assert(_error.size());
184 }
185
186 return bool(sock_fd != INVALID_SOCKET);
187 }
188
189 return false;
190 }
191
192 bool SimpleClient::is_valid()
193 {
194 _error.clear();
195 if (sock_fd == INVALID_SOCKET)
196 {
197 return instance_connect();
198 }
199
200 return true;
201 }
202
203 bool SimpleClient::message(const std::string& arg)
204 {
205 if (is_valid())
206 {
207 if (ready(POLLOUT))
208 {
209 off_t offset= 0;
210 const char* ptr= arg.c_str();
211 size_t len= arg.size();
212
213 do
214 {
215 ssize_t nw= send(sock_fd, ptr + offset, len - offset, MSG_NOSIGNAL);
216 if (nw == -1)
217 {
218 if (errno != EINTR)
219 {
220 _error= strerror(errno);
221 return false;
222 }
223 }
224 else
225 {
226 offset += nw;
227 }
228 } while (offset < ssize_t(len));
229
230 return true;
231 }
232 }
233
234 fatal_assert(_error.size());
235
236 return false;
237 }
238
239 bool SimpleClient::send_message(const std::string& arg)
240 {
241 if (message(arg) == true)
242 {
243 return message("\r\n");
244 }
245
246 return false;
247 }
248
249 bool SimpleClient::send_message(const std::string& message_, std::string& response_)
250 {
251 requested_message++;
252 if (send_message(message_))
253 {
254 return response(response_);
255 }
256
257 return false;
258 }
259
260 bool SimpleClient::response(std::string& response_)
261 {
262 response_.clear();
263
264 if (is_valid())
265 {
266 if (ready(POLLIN))
267 {
268 bool more= true;
269 char buffer[2];
270 buffer[1]= 0;
271 do
272 {
273 ssize_t nr= recv(sock_fd, buffer, 1, MSG_NOSIGNAL);
274 if (nr == -1)
275 {
276 if (errno != EINTR)
277 {
278 _error= strerror(errno);
279 return false;
280 }
281 }
282 else
283 {
284 fatal_assert(nr == 1);
285 if (buffer[0] == '\n')
286 {
287 more= false;
288 }
289 response_.append(buffer);
290 }
291 } while (more);
292
293 return response_.size();
294 }
295 }
296
297 fatal_assert(_error.size());
298 return false;
299 }
300
301 } // namespace libtest