//============================================================================ // Name : ZKTest.cpp // Author : Michael P Smith (AKA, Krin), Under Codethink .Ltd // Version : 0.0.1 // Copyright : Your copyright notice // Description : a first attempt at making a zookeeper client in C //============================================================================ #include #include #include #include #include #include #include #include #include #include "zookeeper.h" using namespace std; static zhandle_t *zk; static const clientid_t *session_id; char* nodeType = "default"; struct String_vector list_of_children = {0}; int timeout = 3000; int responseCode = 0; std::mutex mutex_lock; void safeShutdown(zhandle_t *zzh); void configure(); void configurationwatcher(zhandle_t *zzh, int type, int state, const char *path, void *watcherCtx); void watcher(zhandle_t *zzh, int type, int state, const char *path, void *watcherCtx); void discoverChildren(const char* path); int main(int argc, char **argv) { /* * checking to see if the host entered as the second argument is valid * if no host is entered the program will default to local host */ char* hosts; char* check = "localhost"; char* check2 = ":"; if(argc >1) { for (int i = 1; i < argc-1; i++ ) { if(isdigit(argv[i][0])) { if(strstr(argv[i],check2) != NULL) { hosts = argv[i]; } } else if(strstr(argv[i],check) !=NULL) { if(strstr(argv[i],check2) != NULL) { hosts = argv[1]; } } else if(isalpha(argv[i][0])) { nodeType = argv[i]; } } } else { hosts = "localhost:2181"; } session_id = NULL; char* p; cout << "Initialising Zookeeper" << endl; // prints !!!Hello World!!! zk = zookeeper_init(hosts, watcher, timeout, session_id, NULL, 0); while (!zk) { } p = strtok(NULL, " "); std::cout << "starting authentication" << std::endl; zoo_add_auth(zk, "digest", p, p ? strlen(p) : 0, NULL, NULL); while (zoo_state(zk) == 0) { } std::cout << "authentication step done" << std::endl; while (zk) { /* * mutex used to make the main program loop wait until there is feedback * from zookeeper. initially locks, then loops around. thread cannot lock * again until the current lock has been unlocked. */ mutex_lock.lock(); /* * debug test to show that the loop does not continue until watcher * is called */ std::cout<<"loop locked"<> temp; string addition_of_strings = "/configTest/" + temp; const char* node_to_watch = addition_of_strings.c_str(); std::cout<<"setting watch for config type of "<