Author: breed
Date: Thu Mar 17 01:00:44 2011
New Revision: 1082362
URL: http://svn.apache.org/viewvc?rev=1082362&view=rev
Log:
ZOOKEEPER-1020. Implement function in C client to determine which host you're
currently connected to.
Modified:
zookeeper/trunk/CHANGES.txt
zookeeper/trunk/src/c/include/zookeeper.h
zookeeper/trunk/src/c/src/zookeeper.c
zookeeper/trunk/src/c/tests/TestClient.cc
Modified: zookeeper/trunk/CHANGES.txt
URL:
http://svn.apache.org/viewvc/zookeeper/trunk/CHANGES.txt?rev=1082362&r1=1082361&r2=1082362&view=diff
==============================================================================
--- zookeeper/trunk/CHANGES.txt (original)
+++ zookeeper/trunk/CHANGES.txt Thu Mar 17 01:00:44 2011
@@ -282,6 +282,8 @@ NEW FEATURES:
ZOOKEEPER-775. A large scale pub/sub system (Erwin, Ivan and Ben via
mahadev)
+ ZOOKEEPER-1020. Implement function in C client to determine which host
you're currently connected to. (stephen tyree via breed)
+
Release 3.3.0 - 2010-03-24
Non-backward compatible changes:
Modified: zookeeper/trunk/src/c/include/zookeeper.h
URL:
http://svn.apache.org/viewvc/zookeeper/trunk/src/c/include/zookeeper.h?rev=1082362&r1=1082361&r2=1082362&view=diff
==============================================================================
--- zookeeper/trunk/src/c/include/zookeeper.h (original)
+++ zookeeper/trunk/src/c/include/zookeeper.h Thu Mar 17 01:00:44 2011
@@ -20,6 +20,7 @@
#define ZOOKEEPER_H_
#include <stdlib.h>
+#include <sys/socket.h>
#include <sys/time.h>
#include <stdio.h>
#include <ctype.h>
@@ -366,6 +367,14 @@ ZOOAPI void zoo_set_context(zhandle_t *z
*/
ZOOAPI watcher_fn zoo_set_watcher(zhandle_t *zh,watcher_fn newFn);
+/**
+ * \brief returns the socket address for the current connection
+ * \return socket address of the connected host or NULL on failure, only valid
if the
+ * connection is current connected
+ */
+ZOOAPI struct sockaddr* zookeeper_get_connected_host(zhandle_t *zh,
+ struct sockaddr *addr, socklen_t *addr_len);
+
#ifndef THREADED
/**
* \brief Returns the events that zookeeper is interested in.
Modified: zookeeper/trunk/src/c/src/zookeeper.c
URL:
http://svn.apache.org/viewvc/zookeeper/trunk/src/c/src/zookeeper.c?rev=1082362&r1=1082361&r2=1082362&view=diff
==============================================================================
--- zookeeper/trunk/src/c/src/zookeeper.c (original)
+++ zookeeper/trunk/src/c/src/zookeeper.c Thu Mar 17 01:00:44 2011
@@ -645,6 +645,18 @@ watcher_fn zoo_set_watcher(zhandle_t *zh
return oldWatcher;
}
+struct sockaddr* zookeeper_get_connected_host(zhandle_t *zh,
+ struct sockaddr *addr, socklen_t *addr_len)
+{
+ if (zh->state!=ZOO_CONNECTED_STATE) {
+ return NULL;
+ }
+ if (getpeername(zh->fd, addr, addr_len)==-1) {
+ return NULL;
+ }
+ return addr;
+}
+
static void log_env() {
char buf[2048];
#ifdef HAVE_SYS_UTSNAME_H
Modified: zookeeper/trunk/src/c/tests/TestClient.cc
URL:
http://svn.apache.org/viewvc/zookeeper/trunk/src/c/tests/TestClient.cc?rev=1082362&r1=1082361&r2=1082362&view=diff
==============================================================================
--- zookeeper/trunk/src/c/tests/TestClient.cc (original)
+++ zookeeper/trunk/src/c/tests/TestClient.cc Thu Mar 17 01:00:44 2011
@@ -503,7 +503,7 @@ public:
void testAuth() {
int rc;
count = 0;
- watchctx_t ctx1, ctx2, ctx3, ctx4;
+ watchctx_t ctx1, ctx2, ctx3, ctx4, ctx5;
zhandle_t *zk = createClient(&ctx1);
struct ACL_vector nodeAcl;
struct ACL acl_val;
@@ -600,6 +600,17 @@ public:
CPPUNIT_ASSERT_EQUAL(0, zoo_state(zk2)); // 0 ==> ZOO_CLOSED_STATE
rc = zoo_add_auth(zk2, "digest", "pat:passwd", 10, voidCompletion,
(void*)ZOK);
CPPUNIT_ASSERT_EQUAL((int) ZINVALIDSTATE, rc);
+
+ struct sockaddr addr;
+ socklen_t addr_len = sizeof(addr);
+ zk = createClient(&ctx5);
+ stopServer();
+ CPPUNIT_ASSERT(ctx5.waitForDisconnected(zk));
+ CPPUNIT_ASSERT(zookeeper_get_connected_host(zk, &addr, &addr_len) ==
NULL);
+ addr_len = sizeof(addr);
+ startServer();
+ CPPUNIT_ASSERT(ctx5.waitForConnected(zk));
+ CPPUNIT_ASSERT(zookeeper_get_connected_host(zk, &addr, &addr_len) !=
NULL);
}
void testGetChildren2() {