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;
53 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 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
;
66 self
->error_messages
= NULL
;
68 self
->state
= MEMCACHED_SERVER_STATE_NEW
;
74 self
->version
= ++root
->server_info
.version
;
78 self
->version
= UINT_MAX
;
80 self
->limit_maxbytes
= 0;
81 memcpy(self
->hostname
, hostname
.c_str
, hostname
.size
);
82 self
->hostname
[hostname
.size
]= 0;
85 static memcached_server_st
*_server_create(memcached_server_st
*self
, const memcached_st
*memc
)
89 self
= libmemcached_xmalloc(memc
, struct memcached_server_st
);
93 return NULL
; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
96 self
->options
.is_allocated
= true;
100 self
->options
.is_allocated
= false;
103 self
->options
.is_initialized
= true;
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
,
113 const memcached_connection_t type
)
115 if (memcached_is_valid_servername(hostname
) == false)
117 memcached_set_error(*memc
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
, memcached_literal_param("Invalid hostname provided"));
121 allocated_instance
= _server_create(allocated_instance
, memc
);
123 if (allocated_instance
== NULL
)
128 _server_init(allocated_instance
, const_cast<memcached_st
*>(memc
), hostname
, port
, weight
, type
);
130 return allocated_instance
;
133 void __server_free(memcached_server_st
*self
)
135 memcached_error_free(*self
);
137 if (memcached_is_allocated(self
))
139 libmemcached_free(self
->root
, self
);
143 self
->options
.is_initialized
= false;
147 void memcached_server_free(memcached_server_st
*self
)
154 if (memcached_server_list_count(self
))
156 memcached_server_list_free(self
);
163 void memcached_server_error_reset(memcached_server_st
*self
)
165 WATCHPOINT_ASSERT(self
);
171 memcached_error_free(*self
);
174 uint32_t memcached_servers_set_count(memcached_server_st
*servers
, uint32_t count
)
176 WATCHPOINT_ASSERT(servers
);
182 return servers
->number_of_hosts
= count
;
185 uint32_t memcached_server_count(const memcached_st
*self
)
187 WATCHPOINT_ASSERT(self
);
191 return self
->number_of_hosts
;
194 const char *memcached_server_name(const memcached_server_instance_st self
)
196 WATCHPOINT_ASSERT(self
);
199 return self
->hostname
;
205 in_port_t
memcached_server_port(const memcached_server_instance_st self
)
207 WATCHPOINT_ASSERT(self
);
216 uint32_t memcached_server_response_count(const memcached_server_instance_st self
)
218 WATCHPOINT_ASSERT(self
);
224 return self
->cursor_active_
;
227 const char *memcached_server_type(const memcached_server_instance_st ptr
)
233 case MEMCACHED_CONNECTION_TCP
:
236 case MEMCACHED_CONNECTION_UDP
:
239 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
247 uint8_t memcached_server_major_version(const memcached_server_instance_st instance
)
251 return instance
->major_version
;
257 uint8_t memcached_server_minor_version(const memcached_server_instance_st instance
)
261 return instance
->minor_version
;
267 uint8_t memcached_server_micro_version(const memcached_server_instance_st instance
)
271 return instance
->micro_version
;