+#include <brian.h>
/*
* Summary: interface for memcached server
* Description: main include file for libmemcached
char *hostname;
unsigned int port;
int fd;
- memcached_host_st *next;
};
struct memcached_stat_st {
struct memcached_st {
memcached_allocated is_allocated;
- int fd;
- char connected;
memcached_host_st *hosts;
+ unsigned int number_of_hosts;
+ char connected;
};
/* Public API */
{
memset(ptr, 0, sizeof(memcached_st));
}
- ptr->fd= -1;
return ptr;
}
else
send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
"flush_all\r\n");
- if ((send(ptr->fd, buffer, send_length, 0) == -1))
+ if ((send(ptr->hosts[0].fd, buffer, send_length, 0) == -1))
{
fprintf(stderr, "failed flush_all TCP\n");
send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
"verbosity %u\r\n", verbosity);
- if ((send(ptr->fd, buffer, send_length, 0) == -1))
+ if ((send(ptr->hosts[0].fd, buffer, send_length, 0) == -1))
{
fprintf(stderr, "failed verbosity\n");
void memcached_deinit(memcached_st *ptr)
{
+ unsigned int x;
memcached_host_st *host_ptr;
- if (ptr->fd > 0)
- close(ptr->fd);
-
- for (host_ptr= ptr->hosts; host_ptr;)
+ if (ptr->hosts)
{
- memcached_host_st *temp;
+ for (x= 0; x < ptr->number_of_hosts; x++)
+ {
+ if (ptr->hosts[x].fd > 0)
+ close(ptr->hosts[x].fd);
+
+ if (ptr->hosts[x].hostname)
+ free(ptr->hosts[x].hostname);
+ }
- temp= host_ptr;
- host_ptr= host_ptr->next;
- if (temp->hostname)
- free(temp->hostname);
- free(temp);
+ free(ptr->hosts);
}
if (ptr->is_allocated == MEMCACHED_ALLOCATED)
memcached_return memcached_server_add(memcached_st *ptr, char *hostname, unsigned int port)
{
- memcached_host_st *host_ptr, *prev_ptr;
-
if (!port)
port= MEMCACHED_DEFAULT_PORT;
if (!hostname)
hostname= "localhost";
- 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
- {
- ptr->hosts=
- host_ptr= (memcached_host_st *)malloc(sizeof(memcached_host_st));
- if (!host_ptr)
- return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
- }
-
- memset(host_ptr, 0, sizeof(memcached_host_st));
- host_ptr->hostname= (char *)malloc(sizeof(char) * strlen(hostname));
- if (!host_ptr->hostname)
- {
- free(host_ptr);
- return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
- }
- memcpy(host_ptr->hostname, hostname, strlen(hostname));
- host_ptr->port= port;
+ ptr->hosts= (memcached_host_st *)realloc(ptr->hosts, sizeof(memcached_host_st) * (size_t)ptr->number_of_hosts);
+ ptr->hosts[ptr->number_of_hosts].hostname=
+ (char *)malloc(sizeof(char) * strlen(hostname));
+ memcpy(ptr->hosts[ptr->number_of_hosts].hostname, hostname, strlen(hostname));
+ ptr->hosts[ptr->number_of_hosts].port= port;
+ ptr->hosts[ptr->number_of_hosts].fd= -1;
+ ptr->number_of_hosts++;
return MEMCACHED_SUCCESS;
}
memcached_return memcached_connect(memcached_st *ptr)
{
+ unsigned int x;
struct sockaddr_in localAddr, servAddr;
struct hostent *h;
memcached_host_st *host_ptr;
}
- for (host_ptr= ptr->hosts; host_ptr; host_ptr= host_ptr->next)
+ for (x= 0; x < ptr->number_of_hosts; x++)
{
- if ((h= gethostbyname(host_ptr->hostname)) == NULL)
+ if ((h= gethostbyname(ptr->hosts[x].hostname)) == NULL)
{
- fprintf(stderr, "unknown host '%s'\n", host_ptr->hostname);
+ fprintf(stderr, "unknown host '%s'\n", ptr->hosts[x].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);
+ servAddr.sin_port = htons(ptr->hosts[x].port);
/* Create the socket */
- if ((ptr->fd= socket(AF_INET, SOCK_STREAM, 0)) < 0)
+ if ((ptr->hosts[0].fd= socket(AF_INET, SOCK_STREAM, 0)) < 0)
{
fprintf(stderr, "cannot open socket");
return MEMCACHED_CONNECTION_SOCKET_CREATE_FAILURE;
localAddr.sin_addr.s_addr = htonl(INADDR_ANY);
localAddr.sin_port = htons(0);
- if (bind(ptr->fd, (struct sockaddr *) &localAddr, sizeof(localAddr)) < 0)
+ if (bind(ptr->hosts[0].fd, (struct sockaddr *) &localAddr, sizeof(localAddr)) < 0)
{
- fprintf(stderr, "cannot bind port TCP %u\n", host_ptr->port);
+ fprintf(stderr, "cannot bind port TCP %u\n", ptr->hosts[x].port);
return(MEMCACHED_CONNECTION_BIND_FAILURE);
}
/* connect to server */
- if (connect(ptr->fd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
+ if (connect(ptr->hosts[0].fd, (struct sockaddr *) &servAddr, sizeof(servAddr)) < 0)
{
- fprintf(stderr, "cannot connect to host '%s' (%u) (error: %s)\n", host_ptr->hostname,
- host_ptr->port,
+ fprintf(stderr, "cannot connect to host '%s' (%u) (error: %s)\n", ptr->hosts[x].hostname,
+ ptr->hosts[x].port,
strerror(errno));
return MEMCACHED_HOST_LOCKUP_FAILURE;
}
else
send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
"delete %.*s\r\n", key_length, key);
- if ((write(ptr->fd, buffer, send_length) == -1))
+ if ((write(ptr->hosts[0].fd, buffer, send_length) == -1))
{
fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key);
if (*error != MEMCACHED_SUCCESS)
return NULL;
- if ((send(ptr->fd, buffer, send_length, 0) == -1))
+ if ((send(ptr->hosts[0].fd, buffer, send_length, 0) == -1))
{
fprintf(stderr, "failed fetch on %.*s TCP\n", key_length+1, key);
*error= MEMCACHED_WRITE_FAILURE;
need_to_read= *value_length - need_to_copy;
- read_length= read(ptr->fd, pos_ptr, need_to_read);
+ read_length= read(ptr->hosts[0].fd, pos_ptr, need_to_read);
if (read_length != need_to_read)
{
free(value);
size_t send_length;
memset(buffer, 0, buffer_length);
- send_length= read(ptr->fd, buffer, buffer_length);
+ send_length= read(ptr->hosts[0].fd, buffer, buffer_length);
if (send_length)
switch(buffer[0])
send_length= snprintf(buffer, HUGE_STRING_LEN,
"stats\r\n");
- if ((send(memc.fd, buffer, send_length, 0) == -1))
+ if ((send(memc.hosts[0].fd, buffer, send_length, 0) == -1))
{
fprintf(stderr, "failed on stats\n");
send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
"%s %.*s %u %u %u\r\n", verb,
key_length, key, flags, expiration, value_length);
- if ((send(ptr->fd, buffer, send_length, 0) == -1))
+ if ((send(ptr->hosts[0].fd, buffer, send_length, 0) == -1))
{
fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key);
send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
"%.*s\r\n",
value_length, value);
- if ((send(ptr->fd, buffer, send_length, 0) == -1))
+ if ((send(ptr->hosts[0].fd, buffer, send_length, 0) == -1))
{
fprintf(stderr, "failed set on %.*s TCP\n", key_length+1, key);
return MEMCACHED_WRITE_FAILURE;
}
- send_length= read(ptr->fd, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE);
+ send_length= read(ptr->hosts[0].fd, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE);
if (send_length && buffer[0] == 'S') /* STORED */
return MEMCACHED_SUCCESS;