Fix from Trond for deprecation
[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->address_info)
57 {
58 freeaddrinfo(ptr->address_info);
59 ptr->address_info= NULL;
60 }
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 /* We just do a normal create if ptr is missing */
74 if (ptr == NULL)
75 return NULL;
76
77 /* TODO We should check return type */
78 return memcached_server_create_with(ptr->root, clone,
79 ptr->hostname, ptr->port, ptr->weight,
80 ptr->type);
81 }
82
83 memcached_return memcached_server_cursor(memcached_st *ptr,
84 memcached_server_function *callback,
85 void *context,
86 unsigned int number_of_callbacks)
87 {
88 unsigned int y;
89
90 for (y= 0; y < ptr->number_of_hosts; y++)
91 {
92 unsigned int x;
93
94 for (x= 0; x < number_of_callbacks; x++)
95 {
96 unsigned int iferror;
97
98 iferror= (*callback[x])(ptr, &ptr->hosts[y], context);
99
100 if (iferror)
101 continue;
102 }
103 }
104
105 return MEMCACHED_SUCCESS;
106 }
107
108 memcached_server_st *memcached_server_by_key(memcached_st *ptr, const char *key, size_t key_length, memcached_return *error)
109 {
110 uint32_t server_key;
111
112 *error= memcached_validate_key_length(key_length,
113 ptr->flags & MEM_BINARY_PROTOCOL);
114 unlikely (*error != MEMCACHED_SUCCESS)
115 return NULL;
116
117 unlikely (ptr->number_of_hosts == 0)
118 {
119 *error= MEMCACHED_NO_SERVERS;
120 return NULL;
121 }
122
123 if ((ptr->flags & MEM_VERIFY_KEY) && (memcached_key_test((char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED))
124 {
125 *error= MEMCACHED_BAD_KEY_PROVIDED;
126 return NULL;
127 }
128
129 server_key= memcached_generate_hash(ptr, key, key_length);
130
131 return memcached_server_clone(NULL, &ptr->hosts[server_key]);
132
133 }