1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
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.
39 This is a partial implementation for fetching/creating memcached_server_st objects.
41 #include <libmemcached/common.h>
43 static inline void _server_init(memcached_server_st
*self
, memcached_st
*root
,
44 const memcached_string_t
& hostname
,
46 uint32_t weight
, memcached_connection_t type
)
48 self
->options
.is_shutting_down
= false;
49 self
->options
.is_dead
= false;
50 self
->number_of_hosts
= 0;
51 self
->cursor_active
= 0;
54 self
->io_bytes_sent
= 0;
55 self
->server_failure_counter
= 0;
56 self
->server_failure_counter_query_id
= 0;
57 self
->weight
= weight
? weight
: 1; // 1 is the default weight value
58 WATCHPOINT_SET(self
->io_wait_count
.read
= 0);
59 WATCHPOINT_SET(self
->io_wait_count
.write
= 0);
60 self
->major_version
= UINT8_MAX
;
61 self
->micro_version
= UINT8_MAX
;
62 self
->minor_version
= UINT8_MAX
;
64 self
->error_messages
= NULL
;
65 self
->read_ptr
= self
->read_buffer
;
66 self
->read_buffer_length
= 0;
67 self
->read_data_length
= 0;
68 self
->write_buffer_offset
= 0;
69 self
->address_info
= NULL
;
70 self
->address_info_next
= NULL
;
72 self
->state
= MEMCACHED_SERVER_STATE_NEW
;
78 self
->version
= ++root
->server_info
.version
;
82 self
->version
= UINT_MAX
;
84 self
->limit_maxbytes
= 0;
85 memcpy(self
->hostname
, hostname
.c_str
, hostname
.size
);
86 self
->hostname
[hostname
.size
]= 0;
89 static memcached_server_st
*_server_create(memcached_server_st
*self
, const memcached_st
*memc
)
93 self
= (memcached_server_st
*)libmemcached_malloc(memc
, sizeof(memcached_server_st
));
97 return NULL
; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
100 self
->options
.is_allocated
= true;
104 self
->options
.is_allocated
= false;
107 self
->options
.is_initialized
= true;
112 memcached_server_st
*__server_create_with(memcached_st
*memc
,
113 memcached_server_write_instance_st self
,
114 const memcached_string_t
& hostname
,
115 const in_port_t port
,
117 const memcached_connection_t type
)
119 if (memcached_is_valid_servername(hostname
) == false)
121 memcached_set_error(*memc
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
, memcached_literal_param("Invalid hostname provided"));
125 self
= _server_create(self
, memc
);
132 _server_init(self
, const_cast<memcached_st
*>(memc
), hostname
, port
, weight
, type
);
135 if (memc
and memcached_is_udp(memc
))
137 self
->write_buffer_offset
= UDP_DATAGRAM_HEADER_LENGTH
;
138 memcached_io_init_udp_header(self
, 0);
144 void __server_free(memcached_server_st
*self
)
146 memcached_quit_server(self
, false);
148 if (self
->address_info
)
150 freeaddrinfo(self
->address_info
);
151 self
->address_info
= NULL
;
152 self
->address_info_next
= NULL
;
155 memcached_error_free(*self
);
157 if (memcached_is_allocated(self
))
159 libmemcached_free(self
->root
, self
);
163 self
->options
.is_initialized
= false;
167 void memcached_server_free(memcached_server_st
*self
)
174 if (memcached_server_list_count(self
))
176 memcached_server_list_free(self
);
184 If we do not have a valid object to clone from, we toss an error.
186 memcached_server_st
*memcached_server_clone(memcached_server_st
*destination
,
187 memcached_server_st
*source
)
189 /* We just do a normal create if source is missing */
195 memcached_string_t hostname
= { memcached_string_make_from_cstr(source
->hostname
) };
196 destination
= __server_create_with(source
->root
, destination
,
198 source
->port
, source
->weight
,
202 if (source
->error_messages
)
204 destination
->error_messages
= memcached_error_copy(*source
);
212 memcached_return_t
memcached_server_cursor(const memcached_st
*ptr
,
213 const memcached_server_fn
*callback
,
215 uint32_t number_of_callbacks
)
217 memcached_return_t rc
;
218 if (memcached_failed(rc
= initialize_const_query(ptr
)))
224 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
226 memcached_server_instance_st instance
=
227 memcached_server_instance_by_position(ptr
, x
);
229 for (uint32_t y
= 0; y
< number_of_callbacks
; y
++)
231 memcached_return_t ret
= (*callback
[y
])(ptr
, instance
, context
);
233 if (memcached_failed(ret
))
241 return errors
? MEMCACHED_SOME_ERRORS
: MEMCACHED_SUCCESS
;
244 memcached_return_t
memcached_server_execute(memcached_st
*ptr
,
245 memcached_server_execute_fn callback
,
248 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
250 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, x
);
252 unsigned int iferror
= (*callback
)(ptr
, instance
, context
);
260 return MEMCACHED_SUCCESS
;
263 memcached_server_instance_st
memcached_server_by_key(memcached_st
*ptr
,
266 memcached_return_t
*error
)
268 memcached_return_t unused
;
275 memcached_return_t rc
;
276 if (memcached_failed(rc
= initialize_const_query(ptr
)))
282 if (memcached_failed(rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
)))
288 if (memcached_failed((memcached_key_test(*ptr
, (const char **)&key
, &key_length
, 1))))
290 *error
= MEMCACHED_BAD_KEY_PROVIDED
;
294 uint32_t server_key
= memcached_generate_hash(ptr
, key
, key_length
);
295 return memcached_server_instance_by_position(ptr
, server_key
);
299 void memcached_server_error_reset(memcached_server_st
*self
)
301 WATCHPOINT_ASSERT(self
);
307 memcached_error_free(*self
);
310 memcached_server_instance_st
memcached_server_get_last_disconnect(const memcached_st
*self
)
312 WATCHPOINT_ASSERT(self
);
316 return self
->last_disconnected_server
;
319 uint32_t memcached_servers_set_count(memcached_server_st
*servers
, uint32_t count
)
321 WATCHPOINT_ASSERT(servers
);
325 return servers
->number_of_hosts
= count
;
328 uint32_t memcached_server_count(const memcached_st
*self
)
330 WATCHPOINT_ASSERT(self
);
334 return self
->number_of_hosts
;
337 const char *memcached_server_name(const memcached_server_instance_st self
)
339 WATCHPOINT_ASSERT(self
);
343 return self
->hostname
;
346 in_port_t
memcached_server_port(const memcached_server_instance_st self
)
348 WATCHPOINT_ASSERT(self
);
355 uint32_t memcached_server_response_count(const memcached_server_instance_st self
)
357 WATCHPOINT_ASSERT(self
);
361 return self
->cursor_active
;
364 const char *memcached_server_type(const memcached_server_instance_st ptr
)
370 case MEMCACHED_CONNECTION_TCP
:
373 case MEMCACHED_CONNECTION_UDP
:
376 case MEMCACHED_CONNECTION_UNIX_SOCKET
: