Created
April 26, 2014 16:56
-
-
Save rgs1/11325061 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Local Variables: | |
* compile-command: "gcc -Wall -I/usr/include/zookeeper -lzookeeper_mt states.c -o states" | |
* End: | |
*/ | |
#include <errno.h> | |
#include <pthread.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <time.h> | |
#include <unistd.h> | |
#include "zookeeper.h" | |
void log_msg(const char *msg) | |
{ | |
printf("%s\n", msg); | |
} | |
void error(const char *msg, int rc) | |
{ | |
printf("%s\n", msg); | |
exit(rc); | |
} | |
void watcher(zhandle_t *zzh, int type, int state, const char *path, | |
void *watcherCtx) | |
{ | |
/* printf("watcher has state = %d\n", state); */ | |
} | |
static void void_completion(int rc, const void *data) | |
{ | |
} | |
int main(int argc, char **argv) | |
{ | |
char *host; | |
zhandle_t *zh; | |
int *fd; | |
int rc; | |
if (argc != 2) | |
error("Gimme teh host", 1); | |
host = argv[1]; | |
zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG); | |
/* start the session */ | |
zh = zookeeper_init(host, watcher, 1000, 0, 0, 0); | |
if (!zh) | |
error("Failed to connect", errno); | |
while (zoo_state(zh) != ZOO_CONNECTED_STATE) | |
sleep(1); | |
/* steal the socket's fd */ | |
fd = (int *)zh; | |
/* force reads to fail whilst auth packet is in-flight */ | |
if (shutdown(*fd, SHUT_RD)) | |
log_msg("shutdown failed"); | |
/* send auth packet */ | |
rc = zoo_add_auth(zh, "digest", "yan:yan", 7, void_completion, NULL); | |
if (rc) | |
log_msg("authentication failed"); | |
sleep(2); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment