localhost.
More error checking on failures (and failures cascading properly).
#include <limits.h>
#include <assert.h>
#include <time.h>
+#include <errno.h>
#ifdef __cplusplus
extern "C" {
typedef struct memcached_st memcached_st;
typedef struct memcached_stat_st memcached_stat_st;
+typedef struct memcached_host_st memcached_host_st;
#define MEMCACHED_DEFAULT_PORT 11211
#define MEMCACHED_DEFAULT_COMMAND_SIZE 350
MEMCACHED_ALLOCATED= 1,
} memcached_allocated;
+struct memcached_host_st {
+ char *hostname;
+ unsigned int port;
+ memcached_host_st *next;
+};
+
struct memcached_stat_st {
unsigned int pid;
unsigned int uptime;
memcached_allocated is_allocated;
int fd;
char connected;
+ memcached_host_st *hosts;
};
/* Public API */
size_t *value_length,
uint16_t *flags,
memcached_return *error);
-void memcached_server_add(memcached_st *ptr, char *server_name, unsigned int port);
+memcached_return memcached_server_add(memcached_st *ptr, char *hostname, unsigned int port);
char *memcached_strerror(memcached_st *ptr, memcached_return rc);
/* These are all private, do not use. */
memcached_response.c \\r
memcached_get.c \\r
memcached_storage.c \\r
+ memcached_delete.c \\r
memcached_stats.c\r
return ptr;
}
-void memcached_server_add(memcached_st *ptr, char *server_name, unsigned int port)
-{
-}
-
-
-memcached_return memcached_delete(memcached_st *ptr, char *key, size_t key_length,
- time_t expiration)
-{
- size_t send_length;
- memcached_return rc;
- char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
-
- rc= memcached_connect(ptr);
-
- if (expiration)
- send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
- "delete %.*s %u\r\n", key_length, key, expiration);
- else
- send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
- "delete %.*s\r\n", key_length, key);
- if ((write(ptr->fd, buffer, send_length) == -1))
- {
- fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key);
-
- return MEMCACHED_WRITE_FAILURE;
- }
-
- return memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE);
-}
-
memcached_return memcached_increment(memcached_st *ptr, char *key, size_t key_length,
unsigned int count)
{
void memcached_deinit(memcached_st *ptr)
{
- if (ptr->fd == -1)
+ memcached_host_st *host_ptr;
+
+ if (ptr->fd > 0)
close(ptr->fd);
+ for (host_ptr= ptr->hosts; host_ptr;)
+ {
+ memcached_host_st *temp;
+
+ temp= host_ptr;
+ host_ptr= host_ptr->next;
+ if (temp->hostname)
+ free(temp->hostname);
+ free(temp);
+ }
+
if (ptr->is_allocated == MEMCACHED_ALLOCATED)
free(ptr);
else
#include <memcached.h>
-memcached_return memcached_connect(memcached_st *ptr)
+memcached_return memcached_server_add(memcached_st *ptr, char *hostname, unsigned int port)
{
- int rc;
- struct sockaddr_in localAddr, servAddr;
- struct hostent *h;
- char *server_name= "localhost";
+ memcached_host_st *host_ptr, *prev_ptr;
- if (ptr->connected)
- return MEMCACHED_SUCCESS;
+ if (!port)
+ port= MEMCACHED_DEFAULT_PORT;
+ if (!hostname)
+ hostname= "localhost";
- if ((h= gethostbyname(server_name)) == NULL)
+ if (ptr->hosts)
+ {
+ for (host_ptr= ptr->hosts; host_ptr; host_ptr= host_ptr->next)
+ prev_ptr= host_ptr;
+ host_ptr= (memcached_host_st *)malloc(sizeof(memcached_host_st));
+ if (!host_ptr)
+ return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
+ prev_ptr->next= host_ptr;
+ }
+ else
{
- fprintf(stderr, "unknown host '%s'\n", server_name);
- return MEMCACHED_HOST_LOCKUP_FAILURE;
+ ptr->hosts=
+ host_ptr= (memcached_host_st *)malloc(sizeof(memcached_host_st));
+ if (!host_ptr)
+ return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
}
- servAddr.sin_family= h->h_addrtype;
- memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
- servAddr.sin_port = htons(MEMCACHED_DEFAULT_PORT);
+ memset(host_ptr, 0, sizeof(memcached_host_st));
+ host_ptr->hostname= (char *)malloc(sizeof(char) * strlen(hostname));
- /* Create the socket */
- if ((ptr->fd= socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ if (!host_ptr->hostname)
{
- fprintf(stderr, "cannot open socket");
- return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
+ free(host_ptr);
+ return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
}
+ memcpy(host_ptr->hostname, hostname, strlen(hostname));
+ host_ptr->port= port;
+
+ return MEMCACHED_SUCCESS;
+}
+memcached_return memcached_connect(memcached_st *ptr)
+{
+ struct sockaddr_in localAddr, servAddr;
+ struct hostent *h;
+ memcached_host_st *host_ptr;
- /* bind any port number */
- localAddr.sin_family = AF_INET;
- localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
- localAddr.sin_port = htons(0);
+ if (ptr->connected)
+ return MEMCACHED_SUCCESS;
- if (bind(ptr->fd, (struct sockaddr *) &localAddr, sizeof(localAddr)) < 0)
+ if (!ptr->hosts)
{
- fprintf(stderr, "cannot bind port TCP %u\n", MEMCACHED_DEFAULT_PORT);
- return(MEMCACHED_CONNECTION_BIND_FAILURE);
+ memcached_return rc;
+ rc= memcached_server_add(ptr, NULL, 0);
+
+ if (rc != MEMCACHED_SUCCESS)
+ return rc;
}
- /* connect to server */
- if (connect(ptr->fd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
+
+ for (host_ptr= ptr->hosts; host_ptr; host_ptr= host_ptr->next)
{
- fprintf(stderr, "cannot connect to host '%s'\n", server_name);
- return MEMCACHED_HOST_LOCKUP_FAILURE;
+ if ((h= gethostbyname(host_ptr->hostname)) == NULL)
+ {
+ fprintf(stderr, "unknown host '%s'\n", host_ptr->hostname);
+ return MEMCACHED_HOST_LOCKUP_FAILURE;
+ }
+
+ servAddr.sin_family= h->h_addrtype;
+ memcpy((char *) &servAddr.sin_addr.s_addr, h->h_addr_list[0], h->h_length);
+ servAddr.sin_port = htons(host_ptr->port);
+
+ /* Create the socket */
+ if ((ptr->fd= socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ {
+ fprintf(stderr, "cannot open socket");
+ return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
+ }
+
+
+ /* bind any port number */
+ localAddr.sin_family = AF_INET;
+ localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
+ localAddr.sin_port = htons(0);
+
+ if (bind(ptr->fd, (struct sockaddr *) &localAddr, sizeof(localAddr)) < 0)
+ {
+ fprintf(stderr, "cannot bind port TCP %u\n", host_ptr->port);
+ return(MEMCACHED_CONNECTION_BIND_FAILURE);
+ }
+
+ /* connect to server */
+ if (connect(ptr->fd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
+ {
+ fprintf(stderr, "cannot connect to host '%s' (%u) (error: %s)\n", host_ptr->hostname,
+ host_ptr->port,
+ strerror(errno));
+ return MEMCACHED_HOST_LOCKUP_FAILURE;
+ }
}
ptr->connected= 1;
--- /dev/null
+#include <memcached.h>
+
+memcached_return memcached_delete(memcached_st *ptr, char *key, size_t key_length,
+ time_t expiration)
+{
+ size_t send_length;
+ memcached_return rc;
+ char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
+
+ rc= memcached_connect(ptr);
+
+ if (expiration)
+ send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
+ "delete %.*s %u\r\n", key_length, key, expiration);
+ else
+ send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
+ "delete %.*s\r\n", key_length, key);
+ if ((write(ptr->fd, buffer, send_length) == -1))
+ {
+ fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key);
+
+ return MEMCACHED_WRITE_FAILURE;
+ }
+
+ return memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE);
+}
*error= memcached_connect(ptr);
+ if (*error != MEMCACHED_SUCCESS)
+ return NULL;
+
send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, "get %.*s\r\n",
key_length, key);
if (*error != MEMCACHED_SUCCESS)
rc= memcached_connect(&memc);
+ if (rc != MEMCACHED_SUCCESS)
+ return rc;
+
if (args)
send_length= snprintf(buffer, HUGE_STRING_LEN,
"stats %s\r\n", args);
for (end_ptr= string_ptr; *end_ptr != ' '; end_ptr++);
key= string_ptr;
key[(size_t)(end_ptr-string_ptr)]= 0;
- printf("Key %s\n", key);
string_ptr= end_ptr + 1;
for (end_ptr= string_ptr; *end_ptr != '\r'; end_ptr++);
value= string_ptr;
value[(size_t)(end_ptr-string_ptr)]= 0;
- printf("Value %s\n", value);
string_ptr= end_ptr + 2;
set_data(stat, key, value);
}
rc= memcached_connect(ptr);
+ if (rc != MEMCACHED_SUCCESS)
+ return rc;
+
send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
"%s %.*s %u %u %u\r\n", verb,
key_length, key, flags, expiration, value_length);