Cleans up the assert and some other things that Fedora 15 found.
[awesomized/libmemcached] / libmemcached / server.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38 /*
39 This is a partial implementation for fetching/creating memcached_server_st objects.
40 */
41 #include <libmemcached/common.h>
42
43 static inline void _server_init(memcached_server_st *self, memcached_st *root,
44 const char *hostname, in_port_t port,
45 uint32_t weight, memcached_connection_t type)
46 {
47 self->options.is_shutting_down= false;
48 self->options.is_dead= false;
49 self->number_of_hosts= 0;
50 self->cursor_active= 0;
51 self->port= port;
52 self->fd= -1;
53 self->io_bytes_sent= 0;
54 self->server_failure_counter= 0;
55 self->weight= weight ? weight : 1; // 1 is the default weight value
56 WATCHPOINT_SET(self->io_wait_count.read= 0);
57 WATCHPOINT_SET(self->io_wait_count.write= 0);
58 self->major_version= UINT8_MAX;
59 self->micro_version= UINT8_MAX;
60 self->minor_version= UINT8_MAX;
61 self->type= type;
62 self->error_messages= NULL;
63 self->read_ptr= self->read_buffer;
64 self->read_buffer_length= 0;
65 self->read_data_length= 0;
66 self->write_buffer_offset= 0;
67 self->address_info= NULL;
68 self->address_info_next= NULL;
69
70 self->state= MEMCACHED_SERVER_STATE_NEW;
71
72 if (root)
73 {
74 self->next_retry= root->retry_timeout;
75 }
76 else
77 {
78 self->next_retry= 0;
79 }
80
81 self->root= root;
82 self->limit_maxbytes= 0;
83 if (hostname)
84 {
85 strncpy(self->hostname, hostname, NI_MAXHOST - 1);
86 }
87 else
88 {
89 self->hostname[0]= 0;
90 }
91 }
92
93 static memcached_server_st *_server_create(memcached_server_st *self, const memcached_st *memc)
94 {
95 if (not self)
96 {
97 self= (memcached_server_st *)libmemcached_malloc(memc, sizeof(memcached_server_st));
98
99 if (not self)
100 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
101
102 self->options.is_allocated= true;
103 }
104 else
105 {
106 self->options.is_allocated= false;
107 }
108
109 self->options.is_initialized= true;
110
111 return self;
112 }
113
114 memcached_server_st *__server_create_with(const memcached_st *memc,
115 memcached_server_write_instance_st self,
116 const char *hostname, in_port_t port,
117 uint32_t weight, memcached_connection_t type)
118 {
119 self= _server_create(self, memc);
120
121 if (not self)
122 return NULL;
123
124 _server_init(self, const_cast<memcached_st *>(memc), hostname, port, weight, type);
125
126
127 if (type == MEMCACHED_CONNECTION_UDP)
128 {
129 self->write_buffer_offset= UDP_DATAGRAM_HEADER_LENGTH;
130 memcached_io_init_udp_header(self, 0);
131 }
132
133 return self;
134 }
135
136 void __server_free(memcached_server_st *self)
137 {
138 memcached_quit_server(self, false);
139
140 if (self->address_info)
141 {
142 freeaddrinfo(self->address_info);
143 }
144
145 memcached_error_free(*self);
146
147 if (memcached_is_allocated(self))
148 {
149 libmemcached_free(self->root, self);
150 }
151 else
152 {
153 self->options.is_initialized= false;
154 }
155 }
156
157 void memcached_server_free(memcached_server_st *self)
158 {
159 if (not self)
160 return;
161
162 if (memcached_server_list_count(self))
163 {
164 memcached_server_list_free(self);
165 return;
166 }
167
168 __server_free(self);
169 }
170
171 /*
172 If we do not have a valid object to clone from, we toss an error.
173 */
174 memcached_server_st *memcached_server_clone(memcached_server_st *destination,
175 const memcached_server_st *source)
176 {
177 /* We just do a normal create if source is missing */
178 if (not source)
179 return NULL;
180
181 destination= __server_create_with(source->root, destination,
182 source->hostname, source->port, source->weight,
183 source->type);
184 if (not destination)
185 {
186 if (source->error_messages)
187 {
188 destination->error_messages= memcached_error_copy(*source);
189 }
190 }
191
192 return destination;
193
194 }
195
196 memcached_return_t memcached_server_cursor(const memcached_st *ptr,
197 const memcached_server_fn *callback,
198 void *context,
199 uint32_t number_of_callbacks)
200 {
201 memcached_return_t rc;
202 if (memcached_failed(rc= initialize_const_query(ptr)))
203 {
204 return rc;
205 }
206
207 size_t errors= 0;
208 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
209 {
210 memcached_server_instance_st instance=
211 memcached_server_instance_by_position(ptr, x);
212
213 for (uint32_t y= 0; y < number_of_callbacks; y++)
214 {
215 memcached_return_t ret= (*callback[y])(ptr, instance, context);
216
217 if (memcached_failed(ret))
218 {
219 errors++;
220 continue;
221 }
222 }
223 }
224
225 return errors ? MEMCACHED_SOME_ERRORS : MEMCACHED_SUCCESS;
226 }
227
228 memcached_return_t memcached_server_execute(memcached_st *ptr,
229 memcached_server_execute_fn callback,
230 void *context)
231 {
232 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
233 {
234 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
235
236 unsigned int iferror= (*callback)(ptr, instance, context);
237
238 if (iferror)
239 continue;
240 }
241
242 return MEMCACHED_SUCCESS;
243 }
244
245 memcached_server_instance_st memcached_server_by_key(const memcached_st *ptr,
246 const char *key,
247 size_t key_length,
248 memcached_return_t *error)
249 {
250 memcached_return_t rc;
251 memcached_return_t unused;
252
253 if (not error)
254 error= &unused;
255
256 if (memcached_failed(rc= initialize_const_query(ptr)))
257 {
258 *error= rc;
259 return NULL;
260 }
261
262 if (memcached_failed(rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol)))
263 {
264 *error= rc;
265 return NULL;
266 }
267
268 if (memcached_failed((memcached_key_test(*ptr, (const char **)&key, &key_length, 1))))
269 {
270 *error= MEMCACHED_BAD_KEY_PROVIDED;
271 return NULL;
272 }
273
274 uint32_t server_key= memcached_generate_hash(ptr, key, key_length);
275 return memcached_server_instance_by_position(ptr, server_key);
276
277 }
278
279 void memcached_server_error_reset(memcached_server_st *self)
280 {
281 WATCHPOINT_ASSERT(self);
282 if (not self)
283 return;
284
285 memcached_error_free(*self);
286 }
287
288 memcached_server_instance_st memcached_server_get_last_disconnect(const memcached_st *self)
289 {
290 WATCHPOINT_ASSERT(self);
291 if (not self)
292 return 0;
293
294 return self->last_disconnected_server;
295 }
296
297 uint32_t memcached_servers_set_count(memcached_server_st *servers, uint32_t count)
298 {
299 WATCHPOINT_ASSERT(servers);
300 if (not servers)
301 return 0;
302
303 return servers->number_of_hosts= count;
304 }
305
306 uint32_t memcached_server_count(const memcached_st *self)
307 {
308 WATCHPOINT_ASSERT(self);
309 if (not self)
310 return 0;
311
312 return self->number_of_hosts;
313 }
314
315 const char *memcached_server_name(const memcached_server_instance_st self)
316 {
317 WATCHPOINT_ASSERT(self);
318 if (not self)
319 return NULL;
320
321 return self->hostname;
322 }
323
324 in_port_t memcached_server_port(const memcached_server_instance_st self)
325 {
326 WATCHPOINT_ASSERT(self);
327 if (not self)
328 return 0;
329
330 return self->port;
331 }
332
333 uint32_t memcached_server_response_count(const memcached_server_instance_st self)
334 {
335 WATCHPOINT_ASSERT(self);
336 if (not self)
337 return 0;
338
339 return self->cursor_active;
340 }
341
342 const char *memcached_server_type(const memcached_server_instance_st ptr)
343 {
344 if (ptr)
345 {
346 switch (ptr->type)
347 {
348 case MEMCACHED_CONNECTION_TCP:
349 return "TCP";
350
351 case MEMCACHED_CONNECTION_UDP:
352 return "UDP";
353
354 case MEMCACHED_CONNECTION_UNIX_SOCKET:
355 return "SOCKET";
356
357 case MEMCACHED_CONNECTION_MAX:
358 case MEMCACHED_CONNECTION_UNKNOWN:
359 break;
360 }
361 }
362
363 return "UNKNOWN";
364 }