Fixed autoconf macros for cross compile
[awesomized/libmemcached] / libmemcached / server.c
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary: String structure used for libmemcached.
9 *
10 */
11
12 /*
13 This is a partial implementation for fetching/creating memcached_server_st objects.
14 */
15 #include "common.h"
16
17 static inline void _server_init(memcached_server_st *self, const memcached_st *root,
18 const char *hostname, in_port_t port,
19 uint32_t weight, memcached_connection_t type)
20 {
21 self->options.sockaddr_inited= false;
22 self->options.is_shutting_down= false;
23 self->number_of_hosts= 0;
24 self->cursor_active= 0;
25 self->port= port;
26 self->cached_errno= 0;
27 self->fd= -1;
28 self->io_bytes_sent= 0;
29 self->server_failure_counter= 0;
30 self->weight= weight;
31 self->state.is_corked= false;
32 self->state.is_dead= false;
33 WATCHPOINT_SET(self->io_wait_count.read= 0);
34 WATCHPOINT_SET(self->io_wait_count.write= 0);
35 self->major_version= 0;
36 self->micro_version= 0;
37 self->minor_version= 0;
38 self->type= type;
39 self->read_ptr= self->read_buffer;
40 self->cached_server_error= NULL;
41 self->read_buffer_length= 0;
42 self->read_data_length= 0;
43 self->write_buffer_offset= 0;
44 self->address_info= NULL;
45
46 if (root)
47 {
48 self->next_retry= root->retry_timeout;
49 }
50 else
51 {
52 self->next_retry= 0;
53 }
54
55 self->root= root;
56 self->limit_maxbytes= 0;
57 if (hostname == NULL)
58 self->hostname[0]= 0;
59 else
60 strncpy(self->hostname, hostname, NI_MAXHOST - 1);
61 }
62
63 static memcached_server_st *_server_create(memcached_server_st *self, const memcached_st *memc)
64 {
65 if (self == NULL)
66 {
67 self= (memcached_server_st *)libmemcached_malloc(memc, sizeof(memcached_server_st));
68
69 if (! self)
70 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
71
72 self->options.is_allocated= true;
73 }
74 else
75 {
76 self->options.is_allocated= false;
77 }
78
79 self->options.is_initialized= true;
80
81 return self;
82 }
83
84 memcached_server_st *memcached_server_create_with(const memcached_st *memc,
85 memcached_server_write_instance_st self,
86 const char *hostname, in_port_t port,
87 uint32_t weight, memcached_connection_t type)
88 {
89 self= _server_create(self, memc);
90
91 if (self == NULL)
92 return NULL;
93
94 _server_init(self, memc, hostname, port, weight, type);
95
96
97 if (type == MEMCACHED_CONNECTION_UDP)
98 {
99 self->write_buffer_offset= UDP_DATAGRAM_HEADER_LENGTH;
100 memcached_io_init_udp_header(self, 0);
101 }
102
103 return self;
104 }
105
106 void memcached_server_free(memcached_server_st *self)
107 {
108 memcached_quit_server(self, false);
109
110 if (self->cached_server_error)
111 free(self->cached_server_error);
112
113 if (self->address_info)
114 freeaddrinfo(self->address_info);
115
116 if (memcached_is_allocated(self))
117 {
118 libmemcached_free(self->root, self);
119 }
120 else
121 {
122 self->options.is_initialized= false;
123 }
124 }
125
126 /*
127 If we do not have a valid object to clone from, we toss an error.
128 */
129 memcached_server_st *memcached_server_clone(memcached_server_st *destination,
130 const memcached_server_st *source)
131 {
132 /* We just do a normal create if source is missing */
133 if (source == NULL)
134 return NULL;
135
136 destination= memcached_server_create_with(source->root, destination,
137 source->hostname, source->port, source->weight,
138 source->type);
139 if (destination != NULL)
140 {
141 destination->cached_errno= source->cached_errno;
142
143 if (source->cached_server_error)
144 destination->cached_server_error= strdup(source->cached_server_error);
145 }
146
147 return destination;
148
149 }
150
151 memcached_return_t memcached_server_cursor(const memcached_st *ptr,
152 const memcached_server_fn *callback,
153 void *context,
154 uint32_t number_of_callbacks)
155 {
156 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
157 {
158 memcached_server_instance_st instance=
159 memcached_server_instance_by_position(ptr, x);
160
161 for (uint32_t y= 0; y < number_of_callbacks; y++)
162 {
163 unsigned int iferror;
164
165 iferror= (*callback[y])(ptr, instance, context);
166
167 if (iferror)
168 continue;
169 }
170 }
171
172 return MEMCACHED_SUCCESS;
173 }
174
175 memcached_return_t memcached_server_execute(memcached_st *ptr,
176 memcached_server_execute_fn callback,
177 void *context)
178 {
179 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
180 {
181 memcached_server_write_instance_st instance=
182 memcached_server_instance_fetch(ptr, x);
183
184 unsigned int iferror;
185
186 iferror= (*callback)(ptr, instance, context);
187
188 if (iferror)
189 continue;
190 }
191
192 return MEMCACHED_SUCCESS;
193 }
194
195 memcached_server_instance_st memcached_server_by_key(const memcached_st *ptr,
196 const char *key,
197 size_t key_length,
198 memcached_return_t *error)
199 {
200 uint32_t server_key;
201 memcached_server_instance_st instance;
202
203 *error= memcached_validate_key_length(key_length,
204 ptr->flags.binary_protocol);
205 unlikely (*error != MEMCACHED_SUCCESS)
206 return NULL;
207
208 unlikely (memcached_server_count(ptr) == 0)
209 {
210 *error= MEMCACHED_NO_SERVERS;
211 return NULL;
212 }
213
214 if (ptr->flags.verify_key && (memcached_key_test((const char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED))
215 {
216 *error= MEMCACHED_BAD_KEY_PROVIDED;
217 return NULL;
218 }
219
220 server_key= memcached_generate_hash(ptr, key, key_length);
221 instance= memcached_server_instance_by_position(ptr, server_key);
222
223 return instance;
224
225 }
226
227 void memcached_server_error_reset(memcached_server_st *ptr)
228 {
229 ptr->cached_server_error[0]= 0;
230 }
231
232 memcached_server_instance_st memcached_server_get_last_disconnect(const memcached_st *ptr)
233 {
234 return ptr->last_disconnected_server;
235 }
236
237 void memcached_server_list_free(memcached_server_list_st self)
238 {
239 if (self == NULL)
240 return;
241
242 const memcached_st *root= self->root;
243
244 for (uint32_t x= 0; x < memcached_server_list_count(self); x++)
245 {
246 if (self[x].address_info)
247 {
248 freeaddrinfo(self[x].address_info);
249 self[x].address_info= NULL;
250 }
251 }
252
253 if (root)
254 {
255 libmemcached_free(root, self);
256 }
257 else
258 {
259 free(self);
260 }
261 }
262
263 uint32_t memcached_servers_set_count(memcached_server_st *servers, uint32_t count)
264 {
265 return servers->number_of_hosts= count;
266 }
267
268 uint32_t memcached_server_count(const memcached_st *self)
269 {
270 return self->number_of_hosts;
271 }
272
273 const char *memcached_server_name(memcached_server_instance_st self)
274 {
275 return self->hostname;
276 }
277
278 in_port_t memcached_server_port(memcached_server_instance_st self)
279 {
280 return self->port;
281 }
282
283 uint32_t memcached_server_response_count(memcached_server_instance_st self)
284 {
285 return self->cursor_active;
286 }
287
288 const char *memcached_server_error(memcached_server_instance_st ptr)
289 {
290 return ptr
291 ? ptr->cached_server_error
292 : NULL;
293 }
294