Merge Trond.
[m6w6/libmemcached] / libmemcached / memcached_server.c
1 /*
2 This is a partial implementation for fetching/creating memcached_server_st objects.
3 */
4 #include "common.h"
5
6 memcached_server_st *memcached_server_create(memcached_st *memc, memcached_server_st *ptr)
7 {
8 if (ptr == NULL)
9 {
10 ptr= (memcached_server_st *)calloc(1, sizeof(memcached_server_st));
11
12 if (!ptr)
13 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
14
15 ptr->is_allocated= true;
16 }
17 else
18 memset(ptr, 0, sizeof(memcached_server_st));
19
20 ptr->root= memc;
21
22 return ptr;
23 }
24
25 memcached_server_st *memcached_server_create_with(memcached_st *memc, memcached_server_st *host,
26 const char *hostname, unsigned int port,
27 uint32_t weight, memcached_connection type)
28 {
29 host= memcached_server_create(memc, host);
30
31 if (host == NULL)
32 return NULL;
33
34 strncpy(host->hostname, hostname, MEMCACHED_MAX_HOST_LENGTH - 1);
35 host->root= memc ? memc : NULL;
36 host->port= port;
37 host->weight= weight;
38 host->fd= -1;
39 host->type= type;
40 host->read_ptr= host->read_buffer;
41 if (memc)
42 host->next_retry= memc->retry_timeout;
43 if (type == MEMCACHED_CONNECTION_UDP)
44 {
45 host->write_buffer_offset= UDP_DATAGRAM_HEADER_LENGTH;
46 memcached_io_init_udp_header(host, 0);
47 }
48
49 return host;
50 }
51
52 void memcached_server_free(memcached_server_st *ptr)
53 {
54 memcached_quit_server(ptr, 0);
55
56 if (ptr->cached_server_error)
57 free(ptr->cached_server_error);
58
59 if (ptr->address_info)
60 freeaddrinfo(ptr->address_info);
61
62 if (ptr->is_allocated)
63 ptr->root->call_free(ptr->root, ptr);
64 else
65 memset(ptr, 0, sizeof(memcached_server_st));
66 }
67
68 /*
69 If we do not have a valid object to clone from, we toss an error.
70 */
71 memcached_server_st *memcached_server_clone(memcached_server_st *clone, memcached_server_st *ptr)
72 {
73 memcached_server_st *rv= NULL;
74
75 /* We just do a normal create if ptr is missing */
76 if (ptr == NULL)
77 return NULL;
78
79 rv = memcached_server_create_with(ptr->root, clone,
80 ptr->hostname, ptr->port, ptr->weight,
81 ptr->type);
82 if (rv != NULL)
83 {
84 rv->cached_errno= ptr->cached_errno;
85 if (ptr->cached_server_error)
86 rv->cached_server_error= strdup(ptr->cached_server_error);
87 }
88
89 return rv;
90
91 }
92
93 memcached_return memcached_server_cursor(memcached_st *ptr,
94 memcached_server_function *callback,
95 void *context,
96 unsigned int number_of_callbacks)
97 {
98 unsigned int y;
99
100 for (y= 0; y < ptr->number_of_hosts; y++)
101 {
102 unsigned int x;
103
104 for (x= 0; x < number_of_callbacks; x++)
105 {
106 unsigned int iferror;
107
108 iferror= (*callback[x])(ptr, &ptr->hosts[y], context);
109
110 if (iferror)
111 continue;
112 }
113 }
114
115 return MEMCACHED_SUCCESS;
116 }
117
118 memcached_server_st *memcached_server_by_key(memcached_st *ptr, const char *key, size_t key_length, memcached_return *error)
119 {
120 uint32_t server_key;
121
122 *error= memcached_validate_key_length(key_length,
123 ptr->flags & MEM_BINARY_PROTOCOL);
124 unlikely (*error != MEMCACHED_SUCCESS)
125 return NULL;
126
127 unlikely (ptr->number_of_hosts == 0)
128 {
129 *error= MEMCACHED_NO_SERVERS;
130 return NULL;
131 }
132
133 if ((ptr->flags & MEM_VERIFY_KEY) && (memcached_key_test((const char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED))
134 {
135 *error= MEMCACHED_BAD_KEY_PROVIDED;
136 return NULL;
137 }
138
139 server_key= memcached_generate_hash(ptr, key, key_length);
140
141 return memcached_server_clone(NULL, &ptr->hosts[server_key]);
142
143 }
144
145 const char *memcached_server_error(memcached_server_st *ptr)
146 {
147 if (ptr)
148 return ptr->cached_server_error;
149 else
150 return NULL;
151 }
152
153 void memcached_server_error_reset(memcached_server_st *ptr)
154 {
155 ptr->cached_server_error[0]= 0;
156 }