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