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
*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
->server_timeout_counter
= 0;
58 self
->server_timeout_counter_query_id
= 0;
59 self
->weight
= weight
? weight
: 1; // 1 is the default weight value
60 self
->io_wait_count
.read
= 0;
61 self
->io_wait_count
.write
= 0;
62 self
->io_wait_count
.timeouts
= 0;
63 self
->io_wait_count
._bytes_read
= 0;
64 self
->major_version
= UINT8_MAX
;
65 self
->micro_version
= UINT8_MAX
;
66 self
->minor_version
= UINT8_MAX
;
68 self
->error_messages
= NULL
;
70 self
->state
= MEMCACHED_SERVER_STATE_NEW
;
76 self
->version
= ++root
->server_info
.version
;
80 self
->version
= UINT_MAX
;
82 self
->limit_maxbytes
= 0;
83 memcpy(self
->hostname
, hostname
.c_str
, hostname
.size
);
84 self
->hostname
[hostname
.size
]= 0;
87 static memcached_server_st
*_server_create(memcached_server_st
*self
, const Memcached
*memc
)
91 self
= libmemcached_xmalloc(memc
, struct memcached_server_st
);
95 return NULL
; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
98 self
->options
.is_allocated
= true;
102 self
->options
.is_allocated
= false;
105 self
->options
.is_initialized
= true;
110 memcached_server_st
*__server_create_with(Memcached
*memc
,
111 memcached_server_st
* allocated_instance
,
112 const memcached_string_t
& hostname
,
113 const in_port_t port
,
115 const memcached_connection_t type
)
117 if (memcached_is_valid_servername(hostname
) == false)
119 memcached_set_error(*memc
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
, memcached_literal_param("Invalid hostname provided"));
123 allocated_instance
= _server_create(allocated_instance
, memc
);
125 if (allocated_instance
== NULL
)
130 _server_init(allocated_instance
, const_cast<Memcached
*>(memc
), hostname
, port
, weight
, type
);
132 return allocated_instance
;
135 void __server_free(memcached_server_st
*self
)
137 memcached_error_free(*self
);
139 if (memcached_is_allocated(self
))
141 libmemcached_free(self
->root
, self
);
145 self
->options
.is_initialized
= false;
149 void memcached_server_free(memcached_server_st
*self
)
156 if (memcached_server_list_count(self
))
158 memcached_server_list_free(self
);
165 void memcached_server_error_reset(memcached_server_st
*self
)
167 WATCHPOINT_ASSERT(self
);
173 memcached_error_free(*self
);
176 uint32_t memcached_servers_set_count(memcached_server_st
*servers
, uint32_t count
)
178 WATCHPOINT_ASSERT(servers
);
184 return servers
->number_of_hosts
= count
;
187 uint32_t memcached_server_count(const memcached_st
*self
)
189 WATCHPOINT_ASSERT(self
);
193 return self
->number_of_hosts
;
196 const char *memcached_server_name(const memcached_instance_st
* self
)
198 WATCHPOINT_ASSERT(self
);
201 return self
->_hostname
;
207 in_port_t
memcached_server_port(const memcached_instance_st
* self
)
209 WATCHPOINT_ASSERT(self
);
218 in_port_t
memcached_server_srcport(const memcached_instance_st
* self
)
220 WATCHPOINT_ASSERT(self
);
221 if (self
== NULL
|| self
->fd
== INVALID_SOCKET
|| (self
->type
!= MEMCACHED_CONNECTION_TCP
&& self
->type
!= MEMCACHED_CONNECTION_UDP
))
226 struct sockaddr_in sin
;
227 socklen_t addrlen
= sizeof(sin
);
228 if (getsockname(self
->fd
, (struct sockaddr
*)&sin
, &addrlen
) != -1)
230 return ntohs(sin
.sin_port
);
236 uint32_t memcached_server_response_count(const memcached_instance_st
* self
)
238 WATCHPOINT_ASSERT(self
);
244 return self
->cursor_active_
;
247 const char *memcached_server_type(const memcached_instance_st
* ptr
)
253 case MEMCACHED_CONNECTION_TCP
:
256 case MEMCACHED_CONNECTION_UDP
:
259 case MEMCACHED_CONNECTION_UNIX_SOCKET
:
267 uint8_t memcached_server_major_version(const memcached_instance_st
* instance
)
271 return instance
->major_version
;
277 uint8_t memcached_server_minor_version(const memcached_instance_st
* instance
)
281 return instance
->minor_version
;
287 uint8_t memcached_server_micro_version(const memcached_instance_st
* instance
)
291 return instance
->micro_version
;