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