Update cursor
[m6w6/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 memcached_string_t& hostname,
45 in_port_t port,
46 uint32_t weight, memcached_connection_t type)
47 {
48 self->options.is_shutting_down= false;
49 self->options.is_dead= false;
50 self->number_of_hosts= 0;
51 self->cursor_active= 0;
52 self->port= port;
53 self->io_bytes_sent= 0;
54 self->request_id= 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 self->io_wait_count.read= 0;
59 self->io_wait_count.write= 0;
60 self->io_wait_count.timeouts= 0;
61 self->io_wait_count._bytes_read= 0;
62 self->major_version= UINT8_MAX;
63 self->micro_version= UINT8_MAX;
64 self->minor_version= UINT8_MAX;
65 self->type= type;
66 self->error_messages= NULL;
67
68 self->state= MEMCACHED_SERVER_STATE_NEW;
69 self->next_retry= 0;
70
71 self->root= root;
72 if (root)
73 {
74 self->version= ++root->server_info.version;
75 }
76 else
77 {
78 self->version= UINT_MAX;
79 }
80 self->limit_maxbytes= 0;
81 memcpy(self->hostname, hostname.c_str, hostname.size);
82 self->hostname[hostname.size]= 0;
83 }
84
85 static memcached_server_st *_server_create(memcached_server_st *self, const memcached_st *memc)
86 {
87 if (self == NULL)
88 {
89 self= libmemcached_xmalloc(memc, struct memcached_server_st);
90
91 if (self == NULL)
92 {
93 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
94 }
95
96 self->options.is_allocated= true;
97 }
98 else
99 {
100 self->options.is_allocated= false;
101 }
102
103 self->options.is_initialized= true;
104
105 return self;
106 }
107
108 memcached_server_st *__server_create_with(memcached_st *memc,
109 memcached_server_st* allocated_instance,
110 const memcached_string_t& hostname,
111 const in_port_t port,
112 uint32_t weight,
113 const memcached_connection_t type)
114 {
115 if (memcached_is_valid_servername(hostname) == false)
116 {
117 memcached_set_error(*memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT, memcached_literal_param("Invalid hostname provided"));
118 return NULL;
119 }
120
121 allocated_instance= _server_create(allocated_instance, memc);
122
123 if (allocated_instance == NULL)
124 {
125 return NULL;
126 }
127
128 _server_init(allocated_instance, const_cast<memcached_st *>(memc), hostname, port, weight, type);
129
130 return allocated_instance;
131 }
132
133 void __server_free(memcached_server_st *self)
134 {
135 memcached_error_free(*self);
136
137 if (memcached_is_allocated(self))
138 {
139 libmemcached_free(self->root, self);
140 }
141 else
142 {
143 self->options.is_initialized= false;
144 }
145 }
146
147 void memcached_server_free(memcached_server_st *self)
148 {
149 if (self == NULL)
150 {
151 return;
152 }
153
154 if (memcached_server_list_count(self))
155 {
156 memcached_server_list_free(self);
157 return;
158 }
159
160 __server_free(self);
161 }
162
163 void memcached_server_error_reset(memcached_server_st *self)
164 {
165 WATCHPOINT_ASSERT(self);
166 if (self == NULL)
167 {
168 return;
169 }
170
171 memcached_error_free(*self);
172 }
173
174 uint32_t memcached_servers_set_count(memcached_server_st *servers, uint32_t count)
175 {
176 WATCHPOINT_ASSERT(servers);
177 if (servers == NULL)
178 {
179 return 0;
180 }
181
182 return servers->number_of_hosts= count;
183 }
184
185 uint32_t memcached_server_count(const memcached_st *self)
186 {
187 WATCHPOINT_ASSERT(self);
188 if (self == NULL)
189 return 0;
190
191 return self->number_of_hosts;
192 }
193
194 const char *memcached_server_name(const memcached_server_instance_st self)
195 {
196 WATCHPOINT_ASSERT(self);
197 if (self == NULL)
198 return NULL;
199
200 return self->hostname;
201 }
202
203 in_port_t memcached_server_port(const memcached_server_instance_st self)
204 {
205 WATCHPOINT_ASSERT(self);
206 if (self == NULL)
207 {
208 return 0;
209 }
210
211 return self->port();
212 }
213
214 uint32_t memcached_server_response_count(const memcached_server_instance_st self)
215 {
216 WATCHPOINT_ASSERT(self);
217 if (self == NULL)
218 {
219 return 0;
220 }
221
222 return self->cursor_active_;
223 }
224
225 const char *memcached_server_type(const memcached_server_instance_st ptr)
226 {
227 if (ptr)
228 {
229 switch (ptr->type)
230 {
231 case MEMCACHED_CONNECTION_TCP:
232 return "TCP";
233
234 case MEMCACHED_CONNECTION_UDP:
235 return "UDP";
236
237 case MEMCACHED_CONNECTION_UNIX_SOCKET:
238 return "SOCKET";
239 }
240 }
241
242 return "UNKNOWN";
243 }
244
245 uint8_t memcached_server_major_version(const memcached_server_instance_st instance)
246 {
247 if (instance)
248 {
249 return instance->major_version;
250 }
251
252 return UINT8_MAX;
253 }
254
255 uint8_t memcached_server_minor_version(const memcached_server_instance_st instance)
256 {
257 if (instance)
258 {
259 return instance->minor_version;
260 }
261
262 return UINT8_MAX;
263 }
264
265 uint8_t memcached_server_micro_version(const memcached_server_instance_st instance)
266 {
267 if (instance)
268 {
269 return instance->micro_version;
270 }
271
272 return UINT8_MAX;
273 }