Fix CFLAGS in socket.
[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 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)
198 {
199 return self->hostname;
200 }
201
202 return NULL;
203 }
204
205 in_port_t memcached_server_port(const memcached_server_instance_st self)
206 {
207 WATCHPOINT_ASSERT(self);
208 if (self == NULL)
209 {
210 return 0;
211 }
212
213 return self->port();
214 }
215
216 uint32_t memcached_server_response_count(const memcached_server_instance_st self)
217 {
218 WATCHPOINT_ASSERT(self);
219 if (self == NULL)
220 {
221 return 0;
222 }
223
224 return self->cursor_active_;
225 }
226
227 const char *memcached_server_type(const memcached_server_instance_st ptr)
228 {
229 if (ptr)
230 {
231 switch (ptr->type)
232 {
233 case MEMCACHED_CONNECTION_TCP:
234 return "TCP";
235
236 case MEMCACHED_CONNECTION_UDP:
237 return "UDP";
238
239 case MEMCACHED_CONNECTION_UNIX_SOCKET:
240 return "SOCKET";
241 }
242 }
243
244 return "UNKNOWN";
245 }
246
247 uint8_t memcached_server_major_version(const memcached_server_instance_st instance)
248 {
249 if (instance)
250 {
251 return instance->major_version;
252 }
253
254 return UINT8_MAX;
255 }
256
257 uint8_t memcached_server_minor_version(const memcached_server_instance_st instance)
258 {
259 if (instance)
260 {
261 return instance->minor_version;
262 }
263
264 return UINT8_MAX;
265 }
266
267 uint8_t memcached_server_micro_version(const memcached_server_instance_st instance)
268 {
269 if (instance)
270 {
271 return instance->micro_version;
272 }
273
274 return UINT8_MAX;
275 }