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