From: Brian Aker Date: Fri, 11 Jun 2010 01:59:25 +0000 (-0700) Subject: Adding support for libmemcached_ping(). X-Git-Tag: 0.41~26 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=f7508e07e52d2fdb580832a79c33a3d929fc3859;p=awesomized%2Flibmemcached Adding support for libmemcached_ping(). --- diff --git a/libmemcached/util.h b/libmemcached/util.h new file mode 100644 index 00000000..5e1d4f3c --- /dev/null +++ b/libmemcached/util.h @@ -0,0 +1,20 @@ +/* LibMemcached + * Copyright (C) 2006-2009 Brian Aker + * All rights reserved. + * + * Use and distribution licensed under the BSD license. See + * the COPYING file in the parent directory for full text. + * + * Summary: Connection pool library. + * + * Author: Trond Norbye, Brian Aker + * + */ + + +#ifndef __LIBMEMCACHED__UTIL_H__ +#define __LIBMEMCACHED__UTIL_H__ + +#include + +#endif /* __LIBMEMCACHED__UTIL_H__ */ diff --git a/libmemcached/util/ping.c b/libmemcached/util/ping.c new file mode 100644 index 00000000..001cd596 --- /dev/null +++ b/libmemcached/util/ping.c @@ -0,0 +1,38 @@ +/* LibMemcached + * Copyright (C) 2010 Brian Aker + * All rights reserved. + * + * Use and distribution licensed under the BSD license. See + * the COPYING file in the parent directory for full text. + * + * Summary: connects to a host, and makes sure it is alive. + * + */ + +#include "libmemcached/common.h" +#include "libmemcached/memcached_util.h" + + +bool libmemcached_ping(const char *hostname, in_port_t port, memcached_return_t *ret) +{ + memcached_return_t rc; + memcached_st memc, *memc_ptr; + + memc_ptr= memcached_create(&memc); + + rc= memcached_server_add(memc_ptr, hostname, port); + + if (rc == MEMCACHED_SUCCESS) + { + rc= memcached_version(memc_ptr); + } + + memcached_free(memc_ptr); + + if (ret) + { + *ret= rc; + } + + return rc == MEMCACHED_SUCCESS; +} diff --git a/libmemcached/util/ping.h b/libmemcached/util/ping.h new file mode 100644 index 00000000..4ac677ad --- /dev/null +++ b/libmemcached/util/ping.h @@ -0,0 +1,26 @@ +/* LibMemcached + * Copyright (C) 2010 Brian Aker + * All rights reserved. + * + * Use and distribution licensed under the BSD license. See + * the COPYING file in the parent directory for full text. + * + * Summary: connects to a host, and makes sure it is alive. + * + */ + +#ifndef __LIBMEMCACHED_PING_H__ +#define __LIBMEMCACHED_PING_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +LIBMEMCACHED_API +bool libmemcached_ping(const char *hostname, in_port_t port, memcached_return_t *ret); + +#ifdef __cplusplus +} +#endif + +#endif /* __LIBMEMCACHED_PING_H__ */ diff --git a/tests/server.c b/tests/server.c index c96f5972..7c2b91c6 100644 --- a/tests/server.c +++ b/tests/server.c @@ -24,6 +24,7 @@ #include #include #include +#include #include #include "server.h" @@ -73,15 +74,22 @@ void server_startup(server_startup_st *construct) if (x == 0) { sprintf(buffer, "%s -d -P /tmp/%umemc.pid -t 1 -p %u -U %u -m 128", - MEMCACHED_BINARY, x, x + TEST_PORT_BASE, x + TEST_PORT_BASE); + MEMCACHED_BINARY, x, x + TEST_PORT_BASE, x + TEST_PORT_BASE); } else { sprintf(buffer, "%s -d -P /tmp/%umemc.pid -t 1 -p %u -U %u", - MEMCACHED_BINARY, x, x + TEST_PORT_BASE, x + TEST_PORT_BASE); + MEMCACHED_BINARY, x, x + TEST_PORT_BASE, x + TEST_PORT_BASE); } - fprintf(stderr, "STARTING SERVER: %s\n", buffer); - status= system(buffer); + if (libmemcached_ping("localhost", (in_port_t)(x + TEST_PORT_BASE), NULL)) + { + fprintf(stderr, "Server on port %u already exists\n", x + TEST_PORT_BASE); + } + else + { + status= system(buffer); + fprintf(stderr, "STARTING SERVER: %s status:%d\n", buffer, status); + } count= sprintf(end_ptr, "localhost:%u,", x + TEST_PORT_BASE); end_ptr+= count; }