Change hosts over to realloc array
[awesomized/libmemcached] / lib / memcached.c
1 /*
2 Memcached library
3 */
4 #include <memcached.h>
5
6 memcached_st *memcached_init(memcached_st *ptr)
7 {
8 if (!ptr)
9 {
10 ptr= (memcached_st *)malloc(sizeof(memcached_st));
11
12 if (!ptr)
13 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
14
15 memset(ptr, 0, sizeof(memcached_st));
16 ptr->is_allocated= MEMCACHED_ALLOCATED;
17 }
18 else
19 {
20 memset(ptr, 0, sizeof(memcached_st));
21 }
22
23 return ptr;
24 }
25
26 memcached_return memcached_increment(memcached_st *ptr, char *key, size_t key_length,
27 unsigned int count)
28 {
29 return MEMCACHED_SUCCESS;
30 }
31
32 memcached_return memcached_decrement(memcached_st *ptr, char *key, size_t key_length,
33 unsigned int count)
34 {
35 return MEMCACHED_SUCCESS;
36 }
37
38 memcached_return memcached_flush(memcached_st *ptr, time_t expiration)
39 {
40 size_t send_length;
41 memcached_return rc;
42 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
43
44 rc= memcached_connect(ptr);
45
46 if (expiration)
47 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
48 "flush_all %u\r\n", expiration);
49 else
50 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
51 "flush_all\r\n");
52 if ((send(ptr->hosts[0].fd, buffer, send_length, 0) == -1))
53 {
54 fprintf(stderr, "failed flush_all TCP\n");
55
56 return MEMCACHED_WRITE_FAILURE;
57 }
58
59 return memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE);
60 }
61
62 memcached_return memcached_verbosity(memcached_st *ptr, unsigned int verbosity)
63 {
64 size_t send_length;
65 memcached_return rc;
66 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
67
68 rc= memcached_connect(ptr);
69
70 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
71 "verbosity %u\r\n", verbosity);
72
73 if ((send(ptr->hosts[0].fd, buffer, send_length, 0) == -1))
74 {
75 fprintf(stderr, "failed verbosity\n");
76
77 return MEMCACHED_WRITE_FAILURE;
78 }
79
80 return memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE);
81 }
82
83 /*
84 When this is implemented you will be able to remove single hosts
85 from your current pool of hosts.
86 */
87 memcached_return memcached_quit(memcached_st *ptr, char *hostname, unsigned port)
88 {
89 return MEMCACHED_SUCCESS;
90 }
91
92 void memcached_deinit(memcached_st *ptr)
93 {
94 unsigned int x;
95 memcached_host_st *host_ptr;
96
97 if (ptr->hosts)
98 {
99 for (x= 0; x < ptr->number_of_hosts; x++)
100 {
101 if (ptr->hosts[x].fd > 0)
102 close(ptr->hosts[x].fd);
103
104 if (ptr->hosts[x].hostname)
105 free(ptr->hosts[x].hostname);
106 }
107
108 free(ptr->hosts);
109 }
110
111 if (ptr->is_allocated == MEMCACHED_ALLOCATED)
112 free(ptr);
113 else
114 memset(ptr, 0, sizeof(memcached_st));
115 }
116
117 char *memcached_strerror(memcached_st *ptr, memcached_return rc)
118 {
119 switch (rc)
120 {
121 case MEMCACHED_SUCCESS:
122 return "SUCCESS";
123 case MEMCACHED_FAILURE:
124 return "FAILURE";
125 case MEMCACHED_HOST_LOCKUP_FAILURE:
126 return "HOSTNAME LOOKUP FAILURE";
127 case MEMCACHED_CONNECTION_FAILURE:
128 return "CONNECTION FAILURE";
129 case MEMCACHED_CONNECTION_BIND_FAILURE:
130 return "CONNECTION BIND FAILURE";
131 case MEMCACHED_READ_FAILURE:
132 return "READ FAILURE";
133 case MEMCACHED_UNKNOWN_READ_FAILURE:
134 return "UNKNOWN READ FAILURE";
135 case MEMCACHED_PROTOCOL_ERROR:
136 return "PROTOCOL ERROR";
137 case MEMCACHED_CLIENT_ERROR:
138 return "CLIENT ERROR";
139 case MEMCACHED_SERVER_ERROR:
140 return "SERVER ERROR";
141 case MEMCACHED_WRITE_FAILURE:
142 return "WRITE FAILURE";
143 case MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE:
144 return "CONNECTION SOCKET CREATE FAILURE";
145 case MEMCACHED_DATA_EXISTS:
146 return "CONNECTION DATA EXISTS";
147 case MEMCACHED_DATA_DOES_NOT_EXIST:
148 return "CONNECTION DATA DOES NOT EXIST";
149 case MEMCACHED_NOTSTORED:
150 return "NOT STORED";
151 case MEMCACHED_NOTFOUND:
152 return "NOT FOUND";
153 case MEMCACHED_MEMORY_ALLOCATION_FAILURE:
154 return "MEMORY ALLOCATION FAILURE";
155 case MEMCACHED_PARTIAL_READ:
156 return "PARTIAL READ";
157 };
158 }