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:2181", watcher, 10000, 0, 0, 0); if (!zh) { fprintf(stderr, "Error1: %s\n", strerror(errno)); return errno; } if (zoo_add_auth(zh, "foo", p, strlen(p), 0, 0) != ZOK) { fprintf(stderr, "Error2: %s\n", strerror(errno)); 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, (char*)&buffer, sizeof(buffer) - 1); if (!rc) { fprintf(stderr, "Error3: %s\n", strerror(errno)); return errno; } /** 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, "Error4: %d for %s\n", rc, __LINE__); } else { fprintf(stdout, "Got %s\n", (char*)&buffer); } zookeeper_close(zh); return 0; }
int ZookeeperClient::Auth(const std::string& scheme, const std::string& cert) { if (NULL == m_zk) { return -1; } std::vector<void*>* cb = new std::vector<void*>; cb->push_back(new std::string(scheme)); cb->push_back(new std::string(cert)); cb->push_back(this); return zoo_add_auth(m_zk, scheme.c_str(), cert.data(), cert.size(), ZookeeperClient::AuthCB, cb); }
static VALUE method_add_auth(VALUE self, VALUE scheme, VALUE cert, VALUE completion, VALUE completion_data) { struct zk_rb_data* zk; Data_Get_Struct(rb_iv_get(self, "@data"), struct zk_rb_data, zk); Check_Type(scheme, T_STRING); Check_Type(cert, T_STRING); // Check_Type(completion, T_OBJECT); // ??? check_errors(zoo_add_auth(zk->zh, RSTRING(scheme)->ptr, RSTRING(cert)->ptr, RSTRING(cert)->len, void_completion_callback, DATA_PTR(completion_data))); return Qtrue; }
int main(int argc, char** argv) { char buffer[512]; char p[2048]; char *cert=0; char appId[64]; strcpy(p, "dummy"); zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG); zh = zookeeper_init("localhost:2181", watcher, 10000,0,0,0); while(zoo_state(zh) == 0); if(zoo_add_auth(zh,"digest",p,strlen(p),0,0) != ZOK) { printf("auto failed\n"); return 2; } printf("auto success\n"); 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, "/zookeeper/xyz","value",5,&ZOO_OPEN_ACL_UNSAFE, ZOO_EPHEMERAL,buffer, sizeof(buffer) -1); int buflen = sizeof(buffer); struct Stat stat; while(true) { rc = zoo_get(zh, "/zookeeper/xyz", 0, buffer, &buflen, &stat); if(rc) { fprintf(stderr, "Error %d, for %d\n",rc, __LINE__); } printf("%s", buffer); sleep(5); } zookeeper_close(zh); return 0; }
int main(int argc, char **argv) { #ifndef THREADED fd_set rfds, wfds, efds; int processed=0; #endif char buffer[4096]; char p[2048]; #ifdef YCA char *cert=0; char appId[64]; #endif int bufoff = 0; FILE *fh; if (argc < 2) { fprintf(stderr, "USAGE %s zookeeper_host_list [clientid_file|cmd:(ls|ls2|create|od|...)]\n", argv[0]); fprintf(stderr, "Version: ZooKeeper cli (c client) version %d.%d.%d\n", ZOO_MAJOR_VERSION, ZOO_MINOR_VERSION, ZOO_PATCH_VERSION); return 2; } if (argc > 2) { if(strncmp("cmd:",argv[2],4)==0){ size_t cmdlen = strlen(argv[2]); if (cmdlen > sizeof(cmd)) { fprintf(stderr, "Command length %zu exceeds max length of %zu\n", cmdlen, sizeof(cmd)); return 2; } strncpy(cmd, argv[2]+4, sizeof(cmd)); batchMode=1; fprintf(stderr,"Batch mode: %s\n",cmd); }else{ clientIdFile = argv[2]; fh = fopen(clientIdFile, "r"); if (fh) { if (fread(&myid, sizeof(myid), 1, fh) != sizeof(myid)) { memset(&myid, 0, sizeof(myid)); } fclose(fh); } } } #ifdef YCA strcpy(appId,"yahoo.example.yca_test"); cert = yca_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"); } #else strcpy(p, "dummy"); #endif verbose = 0; zoo_set_debug_level(ZOO_LOG_LEVEL_WARN); zoo_deterministic_conn_order(1); // enable deterministic order hostPort = argv[1]; zh = zookeeper_init(hostPort, watcher, 30000, &myid, 0, 0); if (!zh) { return errno; } #ifdef YCA if(zoo_add_auth(zh,"yca",p,strlen(p),0,0)!=ZOK) return 2; #endif #ifdef THREADED while(!shutdownThisThing) { int rc; int len = sizeof(buffer) - bufoff -1; if (len <= 0) { fprintf(stderr, "Can't handle lines that long!\n"); exit(2); } rc = read(0, buffer+bufoff, len); if (rc <= 0) { fprintf(stderr, "bye\n"); shutdownThisThing=1; break; } bufoff += rc; buffer[bufoff] = '\0'; while (strchr(buffer, '\n')) { char *ptr = strchr(buffer, '\n'); *ptr = '\0'; processline(buffer); ptr++; memmove(buffer, ptr, strlen(ptr)+1); bufoff = 0; } } #else FD_ZERO(&rfds); FD_ZERO(&wfds); FD_ZERO(&efds); while (!shutdownThisThing) { int fd; int interest; int events; struct timeval tv; int rc; zookeeper_interest(zh, &fd, &interest, &tv); if (fd != -1) { if (interest&ZOOKEEPER_READ) { FD_SET(fd, &rfds); } else { FD_CLR(fd, &rfds); } if (interest&ZOOKEEPER_WRITE) { FD_SET(fd, &wfds); } else { FD_CLR(fd, &wfds); } } else { fd = 0; } FD_SET(0, &rfds); rc = select(fd+1, &rfds, &wfds, &efds, &tv); events = 0; if (rc > 0) { if (FD_ISSET(fd, &rfds)) { events |= ZOOKEEPER_READ; } if (FD_ISSET(fd, &wfds)) { events |= ZOOKEEPER_WRITE; } } if(batchMode && processed==0){ //batch mode processline(cmd); processed=1; } if (FD_ISSET(0, &rfds)) { int rc; int len = sizeof(buffer) - bufoff -1; if (len <= 0) { fprintf(stderr, "Can't handle lines that long!\n"); exit(2); } rc = read(0, buffer+bufoff, len); if (rc <= 0) { fprintf(stderr, "bye\n"); break; } bufoff += rc; buffer[bufoff] = '\0'; while (strchr(buffer, '\n')) { char *ptr = strchr(buffer, '\n'); *ptr = '\0'; processline(buffer); ptr++; memmove(buffer, ptr, strlen(ptr)+1); bufoff = 0; } } zookeeper_process(zh, events); } #endif if (to_send!=0) fprintf(stderr,"Recvd %d responses for %d requests sent\n",recvd,sent); zookeeper_close(zh); return 0; }
void processline(char *line) { int rc; int async = ((line[0] == 'a') && !(startsWith(line, "addauth "))); if (async) { line++; } if (startsWith(line, "help")) { fprintf(stderr, " create [+[e|s]] <path>\n"); fprintf(stderr, " delete <path>\n"); fprintf(stderr, " set <path> <data>\n"); fprintf(stderr, " get <path>\n"); fprintf(stderr, " ls <path>\n"); fprintf(stderr, " ls2 <path>\n"); fprintf(stderr, " sync <path>\n"); fprintf(stderr, " exists <path>\n"); fprintf(stderr, " wexists <path>\n"); fprintf(stderr, " myid\n"); fprintf(stderr, " verbose\n"); fprintf(stderr, " addauth <id> <scheme>\n"); fprintf(stderr, " quit\n"); fprintf(stderr, "\n"); fprintf(stderr, " prefix the command with the character 'a' to run the command asynchronously.\n"); fprintf(stderr, " run the 'verbose' command to toggle verbose logging.\n"); fprintf(stderr, " i.e. 'aget /foo' to get /foo asynchronously\n"); } else if (startsWith(line, "verbose")) { if (verbose) { verbose = 0; zoo_set_debug_level(ZOO_LOG_LEVEL_WARN); fprintf(stderr, "logging level set to WARN\n"); } else { verbose = 1; zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG); fprintf(stderr, "logging level set to DEBUG\n"); } } else if (startsWith(line, "get ")) { line += 4; if (line[0] != '/') { fprintf(stderr, "Path must start with /, found: %s\n", line); return; } rc = zoo_aget(zh, line, 1, my_data_completion, strdup(line)); if (rc) { fprintf(stderr, "Error %d for %s\n", rc, line); } } else if (startsWith(line, "set ")) { char *ptr; line += 4; if (line[0] != '/') { fprintf(stderr, "Path must start with /, found: %s\n", line); return; } ptr = strchr(line, ' '); if (!ptr) { fprintf(stderr, "No data found after path\n"); return; } *ptr = '\0'; ptr++; if (async) { rc = zoo_aset(zh, line, ptr, strlen(ptr), -1, my_stat_completion, strdup(line)); } else { struct Stat stat; rc = zoo_set2(zh, line, ptr, strlen(ptr), -1, &stat); } if (rc) { fprintf(stderr, "Error %d for %s\n", rc, line); } } else if (startsWith(line, "ls ")) { line += 3; if (line[0] != '/') { fprintf(stderr, "Path must start with /, found: %s\n", line); return; } gettimeofday(&startTime, 0); rc= zoo_aget_children(zh, line, 1, my_strings_completion, strdup(line)); if (rc) { fprintf(stderr, "Error %d for %s\n", rc, line); } } else if (startsWith(line, "ls2 ")) { line += 4; if (line[0] != '/') { fprintf(stderr, "Path must start with /, found: %s\n", line); return; } gettimeofday(&startTime, 0); rc= zoo_aget_children2(zh, line, 1, my_strings_stat_completion, strdup(line)); if (rc) { fprintf(stderr, "Error %d for %s\n", rc, line); } } else if (startsWith(line, "create ")) { int flags = 0; line += 7; if (line[0] == '+') { line++; if (line[0] == 'e') { flags |= ZOO_EPHEMERAL; line++; } if (line[0] == 's') { flags |= ZOO_SEQUENCE; line++; } line++; } if (line[0] != '/') { fprintf(stderr, "Path must start with /, found: %s\n", line); return; } fprintf(stderr, "Creating [%s] node\n", line); // { // struct ACL _CREATE_ONLY_ACL_ACL[] = {{ZOO_PERM_CREATE, ZOO_ANYONE_ID_UNSAFE}}; // struct ACL_vector CREATE_ONLY_ACL = {1,_CREATE_ONLY_ACL_ACL}; // rc = zoo_acreate(zh, line, "new", 3, &CREATE_ONLY_ACL, flags, // my_string_completion, strdup(line)); // } rc = zoo_acreate(zh, line, "new", 3, &ZOO_OPEN_ACL_UNSAFE, flags, my_string_completion_free_data, strdup(line)); if (rc) { fprintf(stderr, "Error %d for %s\n", rc, line); } } else if (startsWith(line, "delete ")) { line += 7; if (line[0] != '/') { fprintf(stderr, "Path must start with /, found: %s\n", line); return; } if (async) { rc = zoo_adelete(zh, line, -1, my_void_completion, strdup(line)); } else { rc = zoo_delete(zh, line, -1); } if (rc) { fprintf(stderr, "Error %d for %s\n", rc, line); } } else if (startsWith(line, "sync ")) { line += 5; if (line[0] != '/') { fprintf(stderr, "Path must start with /, found: %s\n", line); return; } rc = zoo_async(zh, line, my_string_completion_free_data, strdup(line)); if (rc) { fprintf(stderr, "Error %d for %s\n", rc, line); } } else if (startsWith(line, "wexists ")) { #ifdef THREADED struct Stat stat; #endif line += 8; if (line[0] != '/') { fprintf(stderr, "Path must start with /, found: %s\n", line); return; } #ifndef THREADED rc = zoo_awexists(zh, line, watcher, (void*) 0, my_stat_completion, strdup(line)); #else rc = zoo_wexists(zh, line, watcher, (void*) 0, &stat); #endif if (rc) { fprintf(stderr, "Error %d for %s\n", rc, line); } } else if (startsWith(line, "exists ")) { #ifdef THREADED struct Stat stat; #endif line += 7; if (line[0] != '/') { fprintf(stderr, "Path must start with /, found: %s\n", line); return; } #ifndef THREADED rc = zoo_aexists(zh, line, 1, my_stat_completion, strdup(line)); #else rc = zoo_exists(zh, line, 1, &stat); #endif if (rc) { fprintf(stderr, "Error %d for %s\n", rc, line); } } else if (strcmp(line, "myid") == 0) { printf("session Id = %llx\n", _LL_CAST_ zoo_client_id(zh)->client_id); } else if (strcmp(line, "reinit") == 0) { zookeeper_close(zh); // we can't send myid to the server here -- zookeeper_close() removes // the session on the server. We must start anew. zh = zookeeper_init(hostPort, watcher, 30000, 0, 0, 0); } else if (startsWith(line, "quit")) { fprintf(stderr, "Quitting...\n"); shutdownThisThing=1; } else if (startsWith(line, "od")) { const char val[]="fire off"; fprintf(stderr, "Overdosing...\n"); rc = zoo_aset(zh, "/od", val, sizeof(val)-1, -1, od_completion, 0); if (rc) fprintf(stderr, "od command failed: %d\n", rc); } else if (startsWith(line, "addauth ")) { char *ptr; line += 8; ptr = strchr(line, ' '); if (ptr) { *ptr = '\0'; ptr++; } zoo_add_auth(zh, line, ptr, ptr ? strlen(ptr) : 0, NULL, NULL); } }
void processline(char *line) { int rc; int async = ((line[0] == 'a') && !(startsWith(line, "addauth "))); if (async) { line++; } if (startsWith(line, "help")) { fprintf(stderr, " create [+[e|s]] <path>\n"); fprintf(stderr, " create2 [+[e|s]] <path>\n"); fprintf(stderr, " delete <path>\n"); fprintf(stderr, " set <path> <data>\n"); fprintf(stderr, " get <path>\n"); fprintf(stderr, " ls <path>\n"); fprintf(stderr, " ls2 <path>\n"); fprintf(stderr, " sync <path>\n"); fprintf(stderr, " exists <path>\n"); fprintf(stderr, " wexists <path>\n"); fprintf(stderr, " myid\n"); fprintf(stderr, " verbose\n"); fprintf(stderr, " addauth <id> <scheme>\n"); fprintf(stderr, " config\n"); fprintf(stderr, " reconfig [-file <path> | -members <serverId=host:port1:port2;port3>,... | " " -add <serverId=host:port1:port2;port3>,... | -remove <serverId>,...] [-version <version>]\n"); fprintf(stderr, " quit\n"); fprintf(stderr, "\n"); fprintf(stderr, " prefix the command with the character 'a' to run the command asynchronously.\n"); fprintf(stderr, " run the 'verbose' command to toggle verbose logging.\n"); fprintf(stderr, " i.e. 'aget /foo' to get /foo asynchronously\n"); } else if (startsWith(line, "verbose")) { if (verbose) { verbose = 0; zoo_set_debug_level(ZOO_LOG_LEVEL_WARN); fprintf(stderr, "logging level set to WARN\n"); } else { verbose = 1; zoo_set_debug_level(ZOO_LOG_LEVEL_DEBUG); fprintf(stderr, "logging level set to DEBUG\n"); } } else if (startsWith(line, "get ")) { line += 4; if (line[0] != '/') { fprintf(stderr, "Path must start with /, found: %s\n", line); return; } rc = zoo_aget(zh, line, 1, my_data_completion, strdup(line)); if (rc) { fprintf(stderr, "Error %d for %s\n", rc, line); } } else if (strcmp(line, "config") == 0) { gettimeofday(&startTime, 0); rc = zoo_agetconfig(zh, 1, my_data_completion, strdup(ZOO_CONFIG_NODE)); if (rc) { fprintf(stderr, "Error %d for %s\n", rc, line); } } else if (startsWith(line, "reconfig ")) { int syntaxError = 0; char* p = NULL; char* joining = NULL; char* leaving = NULL; char* members = NULL; size_t members_size = 0; int mode = 0; // 0 = not set, 1 = incremental, 2 = non-incremental int64_t version = -1; line += 9; p = strtok (strdup(line)," "); while (p != NULL) { if (strcmp(p, "-add")==0) { p = strtok (NULL," "); if (mode == 2 || p == NULL) { syntaxError = 1; break; } mode = 1; joining = strdup(p); } else if (strcmp(p, "-remove")==0){ p = strtok (NULL," "); if (mode == 2 || p == NULL) { syntaxError = 1; break; } mode = 1; leaving = strdup(p); } else if (strcmp(p, "-members")==0) { p = strtok (NULL," "); if (mode == 1 || p == NULL) { syntaxError = 1; break; } mode = 2; members = strdup(p); } else if (strcmp(p, "-file")==0){ FILE *fp = NULL; p = strtok (NULL," "); if (mode == 1 || p == NULL) { syntaxError = 1; break; } mode = 2; fp = fopen(p, "r"); if (fp == NULL) { fprintf(stderr, "Error reading file: %s\n", p); syntaxError = 1; break; } fseek(fp, 0L, SEEK_END); /* Position to end of file */ members_size = ftell(fp); /* Get file length */ rewind(fp); /* Back to start of file */ members = calloc(members_size + 1, sizeof(char)); if(members == NULL ) { fprintf(stderr, "\nInsufficient memory to read file: %s\n", p); syntaxError = 1; fclose(fp); break; } /* Read the entire file into members * NOTE: -- fread returns number of items successfully read * not the number of bytes. We're requesting one item of * members_size bytes. So we expect the return value here * to be 1. */ if (fread(members, members_size, 1, fp) != 1){ fprintf(stderr, "Error reading file: %s\n", p); syntaxError = 1; fclose(fp); break; } fclose(fp); } else if (strcmp(p, "-version")==0){ p = strtok (NULL," "); if (version != -1 || p == NULL){ syntaxError = 1; break; } #ifdef WIN32 version = _strtoui64(p, NULL, 16); #else version = strtoull(p, NULL, 16); #endif if (version < 0) { syntaxError = 1; break; } } else { syntaxError = 1; break; } p = strtok (NULL," "); } if (syntaxError) return; rc = zoo_areconfig(zh, joining, leaving, members, version, my_data_completion, strdup(line)); free(joining); free(leaving); free(members); if (rc) { fprintf(stderr, "Error %d for %s\n", rc, line); } } else if (startsWith(line, "set ")) { char *ptr; line += 4; if (line[0] != '/') { fprintf(stderr, "Path must start with /, found: %s\n", line); return; } ptr = strchr(line, ' '); if (!ptr) { fprintf(stderr, "No data found after path\n"); return; } *ptr = '\0'; ptr++; if (async) { rc = zoo_aset(zh, line, ptr, strlen(ptr), -1, my_stat_completion, strdup(line)); } else { struct Stat stat; rc = zoo_set2(zh, line, ptr, strlen(ptr), -1, &stat); } if (rc) { fprintf(stderr, "Error %d for %s\n", rc, line); } } else if (startsWith(line, "ls ")) { line += 3; if (line[0] != '/') { fprintf(stderr, "Path must start with /, found: %s\n", line); return; } gettimeofday(&startTime, 0); rc= zoo_aget_children(zh, line, 1, my_strings_completion, strdup(line)); if (rc) { fprintf(stderr, "Error %d for %s\n", rc, line); } } else if (startsWith(line, "ls2 ")) { line += 4; if (line[0] != '/') { fprintf(stderr, "Path must start with /, found: %s\n", line); return; } gettimeofday(&startTime, 0); rc= zoo_aget_children2(zh, line, 1, my_strings_stat_completion, strdup(line)); if (rc) { fprintf(stderr, "Error %d for %s\n", rc, line); } } else if (startsWith(line, "create ") || startsWith(line, "create2 ")) { int flags = 0; int is_create2 = startsWith(line, "create2 "); line += is_create2 ? 8 : 7; if (line[0] == '+') { line++; if (line[0] == 'e') { flags |= ZOO_EPHEMERAL; line++; } if (line[0] == 's') { flags |= ZOO_SEQUENCE; line++; } line++; } if (line[0] != '/') { fprintf(stderr, "Path must start with /, found: %s\n", line); return; } fprintf(stderr, "Creating [%s] node\n", line); // { // struct ACL _CREATE_ONLY_ACL_ACL[] = {{ZOO_PERM_CREATE, ZOO_ANYONE_ID_UNSAFE}}; // struct ACL_vector CREATE_ONLY_ACL = {1,_CREATE_ONLY_ACL_ACL}; // rc = zoo_acreate(zh, line, "new", 3, &CREATE_ONLY_ACL, flags, // my_string_completion, strdup(line)); // } if (is_create2) { rc = zoo_acreate2(zh, line, "new", 3, &ZOO_OPEN_ACL_UNSAFE, flags, my_string_stat_completion_free_data, strdup(line)); } else { rc = zoo_acreate(zh, line, "new", 3, &ZOO_OPEN_ACL_UNSAFE, flags, my_string_completion_free_data, strdup(line)); } if (rc) { fprintf(stderr, "Error %d for %s\n", rc, line); } } else if (startsWith(line, "delete ")) { line += 7; if (line[0] != '/') { fprintf(stderr, "Path must start with /, found: %s\n", line); return; } if (async) { rc = zoo_adelete(zh, line, -1, my_void_completion, strdup(line)); } else { rc = zoo_delete(zh, line, -1); } if (rc) { fprintf(stderr, "Error %d for %s\n", rc, line); } } else if (startsWith(line, "sync ")) { line += 5; if (line[0] != '/') { fprintf(stderr, "Path must start with /, found: %s\n", line); return; } rc = zoo_async(zh, line, my_string_completion_free_data, strdup(line)); if (rc) { fprintf(stderr, "Error %d for %s\n", rc, line); } } else if (startsWith(line, "wexists ")) { #ifdef THREADED struct Stat stat; #endif line += 8; if (line[0] != '/') { fprintf(stderr, "Path must start with /, found: %s\n", line); return; } #ifndef THREADED rc = zoo_awexists(zh, line, watcher, (void*) 0, my_stat_completion, strdup(line)); #else rc = zoo_wexists(zh, line, watcher, (void*) 0, &stat); #endif if (rc) { fprintf(stderr, "Error %d for %s\n", rc, line); } } else if (startsWith(line, "exists ")) { #ifdef THREADED struct Stat stat; #endif line += 7; if (line[0] != '/') { fprintf(stderr, "Path must start with /, found: %s\n", line); return; } #ifndef THREADED rc = zoo_aexists(zh, line, 1, my_stat_completion, strdup(line)); #else rc = zoo_exists(zh, line, 1, &stat); #endif if (rc) { fprintf(stderr, "Error %d for %s\n", rc, line); } } else if (strcmp(line, "myid") == 0) { printf("session Id = %llx\n", _LL_CAST_ zoo_client_id(zh)->client_id); } else if (strcmp(line, "reinit") == 0) { zookeeper_close(zh); // we can't send myid to the server here -- zookeeper_close() removes // the session on the server. We must start anew. zh = zookeeper_init(hostPort, watcher, 30000, 0, 0, 0); } else if (startsWith(line, "quit")) { fprintf(stderr, "Quitting...\n"); shutdownThisThing=1; } else if (startsWith(line, "od")) { const char val[]="fire off"; fprintf(stderr, "Overdosing...\n"); rc = zoo_aset(zh, "/od", val, sizeof(val)-1, -1, od_completion, 0); if (rc) fprintf(stderr, "od command failed: %d\n", rc); } else if (startsWith(line, "addauth ")) { char *ptr; line += 8; ptr = strchr(line, ' '); if (ptr) { *ptr = '\0'; ptr++; } zoo_add_auth(zh, line, ptr, ptr ? strlen(ptr) : 0, NULL, NULL); } }
int main() { 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_ERROR); zh = zookeeper_init("127.0.0.1:2181", watcher, 10000, 0, 0, 0); zh2 = zookeeper_init("127.0.0.1:2181", watcher, 10000, 0, 0, 0); // zookeeper_init是一个异步函数需要等待zh->state非0 while(zoo_state(zh) == 0) {} while(zoo_state(zh2) == 0) {} // struct Id id_tmp = {(char*)"digest", (char*)"user:tpUq/4Pn5A64fVZyQ0gOJ8ZWqkY="}; int rc = zoo_add_auth(zh,"digest", "dbscale:dbscale",strlen("dbscale:dbscale"),0,0); if (rc == ZOK){ printf("zoo add auth sucess\n"); } else { printf("zoo add auth failed\n"); } // struct ACL CREATE_ONLY_ACL[] = {{ZOO_PERM_ALL, id_tmp}};//, {ZOO_PERM_READ, ZOO_AUTH_IDS}, {ZOO_PERM_WRITE, ZOO_AUTH_IDS}}; // struct ACL_vector CREATE_ONLY = {1, CREATE_ONLY_ACL}; rc = zoo_create(zh,"/xyz","value", 5, &ZOO_CREATOR_ALL_ACL/*&CREATE_ONLY*/, ZOO_EPHEMERAL, buffer, sizeof(buffer)-1); if (rc == ZOK) { printf("create ok \n"); } else { printf("create faild%d\n" ,rc); } struct ACL_vector ret; printf("zoo set acl %d\n", zoo_get_acl(zh, "/xyz", &ret, 0)); rc = zoo_exists(zh, "/xyz", 0, 0); if (rc != ZOK) { printf("%d exist fail\n", rc); } else { printf("/xyz is exist\n"); } /** this operation will fail with a ZNOAUTH error */ int buflen= sizeof(buffer); struct Stat stat; rc = zoo_exists(zh2, "/xyz", 0, 0); if (rc == ZNOAUTH) { fprintf(stderr, "Error %d for %d\n", rc, __LINE__); } else { printf("zh2 exist %s\n", buffer); } rc = zoo_get(zh2, "/xyz", 0, buffer, &buflen, &stat); if (rc == ZNOAUTH) { fprintf(stderr, "Error %d for %d\n", rc, __LINE__); } else { printf("get data %s\n", buffer); } if (rc == ZOK){ printf("zoo create xyz sucess\n"); } else { printf("zoo create xyz failed %s\n", zerror(rc)); } rc = zoo_add_auth(zh2,"digest", "dbscale:dbscale",strlen("dbscale:dbscale"),0,0); if (rc == ZOK){ printf("zoo add auth sucess\n"); } else { printf("zoo add auth failed\n"); } rc = zoo_get(zh2, "/xyz", 0, buffer, &buflen, &stat); if (rc == ZNOAUTH) { fprintf(stderr, "Error %d for %d\n", rc, __LINE__); } else { printf("get data %s\n", buffer); } rc = zoo_wget(zh2, "/xyz", watcher, NULL, buffer, &buflen, &stat); if (rc == ZNOAUTH) { fprintf(stderr, "Error %d for %d\n", rc, __LINE__); } else { printf("wget sucess %s\n", buffer); } zookeeper_close(zh); return 0; }