请教zookeeper 客户端c api

pengfangxu8 2012-03-01 05:25:14
各位大侠,现在在做zookeeper,有谁做过zookeeper客户端编程的,c语言哈,实现创建,删除,获取,监控就是api中提供的那几个功能,麻烦给个demo,zookeeper源代码中有c客户端的代码,由于本人不得行,没有提取出来,还请帮帮忙,谢谢了!
...全文
840 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
yfk 2012-04-19
  • 打赏
  • 举报
回复
#include <string.h>
#include <errno.h>

#include "zookeeper.h"

static zhandle_t *zh;

/**
* In this example this method gets the cert for your
* environment -- you must provide
*/
char *foo_get_cert_once(char* id) { return 0; }

/** Watcher function -- empty for this example, not something you should
* do in real code */
void watcher(zhandle_t *zzh, int type, int state, const char *path,
void *watcherCtx) {}

int main(int argc, char argv) {
char buffer[512];
char p[2048];
char *cert=0;
char appId[64];

strcpy(appId, "example.foo_test");
cert = foo_get_cert_once(appId);
if(cert!=0) {
fprintf(stderr,
"Certificate for appid [%s] is [%s]\n",appId,cert);
strncpy(p,cert, sizeof(p)-1);
free(cert);
} else {
fprintf(stderr, "Certificate for appid [%s] not found\n",appId);
strcpy(p, "dummy");
}

zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG);

zh = zookeeper_init("localhost:3181", watcher, 10000, 0, 0, 0);
if (!zh) {
return errno;
}
if(zoo_add_auth(zh,"foo",p,strlen(p),0,0)!=ZOK)
return 2;

struct ACL CREATE_ONLY_ACL[] = {{ZOO_PERM_CREATE, ZOO_AUTH_IDS}};
struct ACL_vector CREATE_ONLY = {1, CREATE_ONLY_ACL};
int rc = zoo_create(zh,"/xyz","value", 5, &CREATE_ONLY, ZOO_EPHEMERAL,
buffer, sizeof(buffer)-1);

/** this operation will fail with a ZNOAUTH error */
int buflen= sizeof(buffer);
struct Stat stat;
rc = zoo_get(zh, "/xyz", 0, buffer, &buflen, &stat);
if (rc) {
fprintf(stderr, "Error %d for %s\n", rc, __LINE__);
}

zookeeper_close(zh);
return 0;
}
yfk 2012-04-19
  • 打赏
  • 举报
回复
编译使用zookeeper提供的c客户端代码:
编译使用C客户端:
$ cd zookeeper-3.4.3/src/c
$ mkdir prefix
$ ./configure --prefix=/home/zxm/zookeeper-3.4.3/src/c/prefix
$ make
$ ./cli_mt 127.0.0.1:2181
Watcher -1 state = CONNECTED_STATE
Got a new session id: 0x136c9d62bdc0004
ls /
time = 4 msec
/: rc = 0
test1
zookeeper
time = 7 msec
get /test1
time = 2438 msec
/test1: rc = 0
value_len = 2
99
Stat:
ctime = Thu Apr 19 02:08:37 2012
czxid=a
mtime=Thu Apr 19 02:08:37 2012
mzxid=a
version=0 aversion=0
ephemeralOwner = 0

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧