zookeeper C API
来源:互联网 发布:异星工厂物流网络 编辑:程序博客网 时间:2024/05/21 17:07
#include <stdio.h>#include <stdlib.h>#include <string.h>#include "include/zookeeper/zookeeper.h"#include "include/zookeeper/zookeeper_log.h"//void zktest_watcher_g(zhandle_t* zh, int type, int state,// const char* path, void* watcherCtx)//{// printf("Something happened.\n");// printf("type: %d\n", type);// printf("state: %d\n", state);// printf("path: %s\n", path);// printf("watcherCtx: %s\n", (char *)watcherCtx);//}//void zktest_dump_stat(const struct Stat *stat)//{// char tctimes[40];// char tmtimes[40];// time_t tctime;// time_t tmtime;// if (!stat) {// fprintf(stderr,"null\n");// return;// }// tctime = stat->ctime/1000;// tmtime = stat->mtime/1000;// ctime_r(&tmtime, tmtimes);// ctime_r(&tctime, tctimes);// fprintf(stderr, "\tctime = %s\tczxid=%llx\n"// "\tmtime=%s\tmzxid=%llx\n"// "\tversion=%x\taversion=%x\n"// "\tephemeralOwner = %llx\n",// tctimes, stat->czxid,// tmtimes, stat->mzxid,// (unsigned int)stat->version, (unsigned int)stat->aversion,// stat->ephemeralOwner);//}//void zktest_stat_completion(int rc, const struct Stat *stat, const void *data)//{// fprintf(stderr, "%s: rc = %d Stat:\n", (char*)data, rc);// zktest_dump_stat(stat);//}//void zktest_void_completion(int rc, const void *data)//{// fprintf(stderr, "[%s]: rc = %d\n", (char*)(data==0?"null":data), rc);//}//void zktest_string_completion(int rc, const char *name, const void *data)//{// fprintf(stderr, "[%s]: rc = %d\n", (char*)(data==0?"null":data), rc);// if (!rc) {// fprintf(stderr, "\tname = %s\n", name);// }//}//---------------------------------------------------------------------------------//void zktest_watcher_g(zhandle_t* zh, int type, int state, const char* path, void* watcherCtx){ printf("Something happened.\n"); printf("type: %d\n", type); printf("state: %d\n", state); printf("path: %s\n", path); printf("watcherCtx: %s\n", (char *)watcherCtx);}//自己的监听函数void watcher_myself(zhandle_t *zh,int type,int state,const char *path,void *watcherCtx){ printf("just for testing\n"); printf("path:%s\n",path);}//同步方式创建节点void create(zhandle_t *zkhandle,char *str){ char path_buffer[64]; int bufferlen=sizeof(path_buffer); printf("同步方式创建节点-----------------------\n"); int flag = zoo_create(zkhandle,str,"hahah",5, &ZOO_OPEN_ACL_UNSAFE,0, path_buffer,bufferlen); if (flag!=ZOK) { printf("节点创建失败 \n"); exit(EXIT_FAILURE); } else { printf("创建的节点名称为:%s\n",path_buffer); }}//同步方式获取节点数据void get(zhandle_t* zkhandle){ printf("同步方式获取节点数据-----------------------\n"); char buffer1[64]; int bufferlen1=sizeof(buffer1); int flag1=zoo_get(zkhandle,"/xyz3",0, buffer1,&bufferlen1,NULL); if (flag1 ==ZOK) { printf("节点/xyz3的数据为: %s\n",buffer1); }}//existsvoid exists(zhandle_t *zkhandle,char *str){ int flag = zoo_exists(zkhandle,str,1,NULL);}void wexists(zhandle_t *zkhandle,char *str){ int flag=zoo_wexists(zkhandle,str,watcher_myself,"test",NULL);}//同步方式获得子节点信息void getChildren(zhandle_t *zkhandle,char *str){ struct String_vector strings; struct Stat stat; int flag = zoo_wget_children2(zkhandle,str, watcher_myself,"testgetChildren", &strings,&stat); if (flag==ZOK) { int32_t i=0; for (;i<strings.count;++i) printf("%s\n",strings.data[i]); }}//获取ACL信息void getACL(zhandle_t *zkhandle,char *str){ struct ACL_vector acl; struct Stat stat; int flag = zoo_get_acl(zkhandle,str,&acl,&stat); if (flag==ZOK) { printf("-----------------the ACL of %s:\n------------",str); printf("%d\n",acl.count); printf("%d\n",acl.data->perms); printf("%s\n",acl.data->id.scheme); printf("%s\n",acl.data->id.id); }}//删除节点void my_delete(zhandle_t *zkhandle,char *str){ int flag = zoo_delete(zkhandle,str,-1); if (flag==ZOK) { printf("delete node success\n"); }}int main(int argc, const char *argv[]){ const char* host = "127.0.0.1:2181"; int timeout = 30000; char buffer[512]; int *bufferlen; zoo_set_debug_level(ZOO_LOG_LEVEL_WARN); //设置日志级别,避免出现一些其他信息 zhandle_t* zkhandle = zookeeper_init(host,zktest_watcher_g, timeout, 0, "hello zookeeper.", 0); if (zkhandle ==NULL) { fprintf(stderr, "Error when connecting to zookeeper servers...\n"); exit(EXIT_FAILURE); } char str[]="/xyz30000000014"; // wexists(zkhandle,str); // printf("---------------\n"); //create(zkhandle,str); //get(zkhandle); // getChildren(zkhandle,str); //getACL(zkhandle,str); my_delete(zkhandle,str);}////////////////////////////////////////////////////////////////////////////////////////////int main(int argc, const char *argv[])//{// const char* host = "127.0.0.1:2181,127.0.0.1:2182,"// "127.0.0.1:2183";// int timeout = 30000;// zoo_set_debug_level(ZOO_LOG_LEVEL_WARN);// zhandle_t* zkhandle = zookeeper_init(host,// zktest_watcher_g, timeout, 0, "hello zookeeper.", 0);// if (zkhandle == NULL) {// fprintf(stderr, "Error when connecting to zookeeper servers...\n");// exit(EXIT_FAILURE);// }// // struct ACL ALL_ACL[] = {{ZOO_PERM_ALL, ZOO_ANYONE_ID_UNSAFE}};// // struct ACL_vector ALL_PERMS = {1, ALL_ACL};// int ret = zoo_acreate(zkhandle, "/xyz", "hello", 5,// &ZOO_OPEN_ACL_UNSAFE, 0 /* ZOO_SEQUENCE */,// zktest_string_completion, "acreate");// if (ret) {// fprintf(stderr, "Error %d for %s\n", ret, "acreate");// exit(EXIT_FAILURE);// }// ret = 0;// ret = zoo_aexists(zkhandle, "/xyz", 1, zktest_stat_completion, "aexists");// if (ret) {// fprintf(stderr, "Error %d for %s\n", ret, "aexists");// exit(EXIT_FAILURE);// }// ret = 0;// // Wait for asynchronous zookeeper call done.// getchar();// ret = zoo_adelete(zkhandle, "/xyz", -1, zktest_void_completion, "adelete");// if (ret) {// fprintf(stderr, "Error %d for %s\n", ret, "adelete");// exit(EXIT_FAILURE);// }// // Wait for asynchronous zookeeper call done.// getchar();// zookeeper_close(zkhandle);//}
0 0
- zookeeper C API实例
- zookeeper C API
- Zookeeper C API 指南
- zookeeper C API
- zookeeper C API 编译
- Zookeeper C API学习总结
- zookeeper C API 的安装和使用指南
- zookeeper c api 之Stat结构
- zookeeper c api 常见变量注释
- zookeeper c api 常见变量注释
- zookeeper c api连接管理问题
- zookeeper使用(二) C API 安装
- 摘要: zookeeper C API 的安装和使用指南 "zookeeper C API 的安装和使用指南": 关键词:zookeeper c api 安装 使用指南 1 下载 zookeeper
- ZooKeeper API
- ZooKeeper API
- zookeeper api
- zookeeper API
- ZooKeeper API
- MFC单文档视图拆分窗口
- java的单例、static修饰符及static的继承
- openstack安装(包含离线安装)
- 数据库第四次作业 王小芬 2013212954 理学院
- 安卓学习第十九天:CheckBox与RadioButton
- zookeeper C API
- nyoj_289_苹果
- Android应用性能优化之使用SparseArray替代HashMap
- 求100以内的质数
- matlab求全相应
- RPM 使用
- 使用Java添加系统右键菜单--详解版
- poj2251 Dungeon Master
- Android蓝牙应用程序开发的基本步骤