From f7508e07e52d2fdb580832a79c33a3d929fc3859 Mon Sep 17 00:00:00 2001 From: Brian Aker Date: Thu, 10 Jun 2010 18:59:25 -0700 Subject: [PATCH] Adding support for libmemcached_ping(). --- libmemcached/util.h | 20 ++++++++++++++++++++ libmemcached/util/ping.c | 38 ++++++++++++++++++++++++++++++++++++++ libmemcached/util/ping.h | 26 ++++++++++++++++++++++++++ tests/server.c | 16 ++++++++++++---- 4 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 libmemcached/util.h create mode 100644 libmemcached/util/ping.c create mode 100644 libmemcached/util/ping.h 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; } -- 2.30.2