From d116653e2920854e127d544b602d1d4c7cbafe2f Mon Sep 17 00:00:00 2001 From: Mike Smith Date: Wed, 26 Nov 2014 15:41:46 +0000 Subject: new program added, the setup for the config files on the server --- ZKTest/.cproject | 4 +- ZKTest/Debug/ZKTest | Bin 191145 -> 191137 bytes ZKTest/Debug/src/ZKTest.o | Bin 365008 -> 364792 bytes ZKTest/src/ZKTest.cpp | 87 +++++++------- zkServerFileSetup/.cproject | 142 +++++++++++++++++++++++ zkServerFileSetup/.gitignore | 2 + zkServerFileSetup/.project | 27 +++++ zkServerFileSetup/src/zkServerFileSetup.cpp | 174 ++++++++++++++++++++++++++++ 8 files changed, 389 insertions(+), 47 deletions(-) create mode 100644 zkServerFileSetup/.cproject create mode 100644 zkServerFileSetup/.gitignore create mode 100644 zkServerFileSetup/.project create mode 100644 zkServerFileSetup/src/zkServerFileSetup.cpp diff --git a/ZKTest/.cproject b/ZKTest/.cproject index ca9760d..de38b07 100644 --- a/ZKTest/.cproject +++ b/ZKTest/.cproject @@ -26,8 +26,8 @@ - diff --git a/ZKTest/Debug/ZKTest b/ZKTest/Debug/ZKTest index 3ffd542..7975212 100755 Binary files a/ZKTest/Debug/ZKTest and b/ZKTest/Debug/ZKTest differ diff --git a/ZKTest/Debug/src/ZKTest.o b/ZKTest/Debug/src/ZKTest.o index eb10575..d38bcf4 100644 Binary files a/ZKTest/Debug/src/ZKTest.o and b/ZKTest/Debug/src/ZKTest.o differ diff --git a/ZKTest/src/ZKTest.cpp b/ZKTest/src/ZKTest.cpp index c4f4017..de2a3e2 100644 --- a/ZKTest/src/ZKTest.cpp +++ b/ZKTest/src/ZKTest.cpp @@ -25,7 +25,7 @@ using namespace std; static zhandle_t *zk; static const clientid_t *session_id; char* nodeType = "default"; -struct String_vector list_of_children = {0}; +struct String_vector list_of_children = { 0 }; int timeout = 3000; int responseCode = 0; std::mutex mutex_lock; @@ -47,26 +47,26 @@ int main(int argc, char **argv) char* hosts = "localhost:2181"; char* check = "localhost"; char* check2 = ":"; - if(argc >1) + if (argc > 1) { - for (int i = 1; i < argc; i++ ) + for (int i = 1; i < argc; i++) { - if(isdigit(argv[i][0])) + if (isdigit(argv[i][0])) { - if(strstr(argv[i],check2) != NULL) + if (strstr(argv[i], check2) != NULL) { hosts = argv[i]; } } - else if(strstr(argv[i],check) !=NULL) + else if (strstr(argv[i], check) != NULL) { - if(strstr(argv[i],check2) != NULL) + if (strstr(argv[i], check2) != NULL) { hosts = argv[1]; } } - else if(isalpha(argv[i][0])) + else if (isalpha(argv[i][0])) { nodeType = argv[i]; } @@ -109,14 +109,12 @@ int main(int argc, char **argv) * 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* config_to_watch = addition_of_strings.c_str(); - std::cout<<"setting watch for config type of "< elements; uint configBegin = 0; - for (uint it = 0; it != strlen(configString); it++ ) + for (uint it = 0; it != strlen(configString); it++) { - if(configString[it] == ';' || NULL) + if (configString[it] == ';') { string tempString; - while(configBegin != it) + while (configBegin != it) { tempString += configString[configBegin]; configBegin++; } - configBegin = it+1; - char* element = new char[tempString.length()+1]; - std::strcpy (element, tempString.c_str()); + configBegin = it + 1; + char* element = new char[tempString.length() + 1]; + std::strcpy(element, tempString.c_str()); elements.push_back(element); } } - std::cout<<"i am a client of type "< + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/zkServerFileSetup/.gitignore b/zkServerFileSetup/.gitignore new file mode 100644 index 0000000..ac01e66 --- /dev/null +++ b/zkServerFileSetup/.gitignore @@ -0,0 +1,2 @@ +/Debug/ +/Release/ diff --git a/zkServerFileSetup/.project b/zkServerFileSetup/.project new file mode 100644 index 0000000..2553369 --- /dev/null +++ b/zkServerFileSetup/.project @@ -0,0 +1,27 @@ + + + zkServerFileSetup + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.core.ccnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + + diff --git a/zkServerFileSetup/src/zkServerFileSetup.cpp b/zkServerFileSetup/src/zkServerFileSetup.cpp new file mode 100644 index 0000000..03f48f0 --- /dev/null +++ b/zkServerFileSetup/src/zkServerFileSetup.cpp @@ -0,0 +1,174 @@ +//============================================================================ +// Name : zkServerFileSetup.cpp +// Author : Michael P Smith +// Version : +// Copyright : +// Description : Hello World in C++, Ansi-style +//============================================================================ + +//============================================================================ +// 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 "zookeeper.h" + +using namespace std; + +static zhandle_t *zk; +static const clientid_t *session_id; +void watcher(zhandle_t *zzh, int type, int state, const char *path, + void *watcherCtx); + +int main(int argc, char **argv) +{ + session_id = NULL; + char* p; + cout << "Initialising Zookeeper" << endl; // prints !!!Hello World!!! + + zk = zookeeper_init("localhost:2181", watcher, 3000, 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) + { + } + cout << "End of setup Program" << std::endl; + return 0; +} // End of Main + +void safeShutdown(zhandle_t *zzh) +{ + if (zzh) + { + cout << "closing Zookeeper connection" << std::endl; + zookeeper_close(zzh); + zk = 0; + } +} + +static const char* state2String(int state) +{ + if (state == 0) + return "CLOSED_STATE"; + if (state == ZOO_CONNECTING_STATE) + return "CONNECTING_STATE"; + if (state == ZOO_ASSOCIATING_STATE) + return "ASSOCIATING_STATE"; + if (state == ZOO_CONNECTED_STATE) + return "CONNECTED_STATE"; + if (state == ZOO_EXPIRED_SESSION_STATE) + return "EXPIRED_SESSION_STATE"; + if (state == ZOO_AUTH_FAILED_STATE) + return "AUTH_FAILED_STATE"; + + return "INVALID_STATE"; +} + +static const char* type2String(int type) +{ + if (type == ZOO_CREATED_EVENT) + return "CREATED_EVENT"; + if (type == ZOO_DELETED_EVENT) + return "DELETED_EVENT"; + if (type == ZOO_CHANGED_EVENT) + return "CHANGED_EVENT"; + if (type == ZOO_CHILD_EVENT) + return "CHILD_EVENT"; + if (type == ZOO_SESSION_EVENT) + return "SESSION_EVENT"; + if (type == ZOO_NOTWATCHING_EVENT) + return "NOTWATCHING_EVENT"; + + return "UNKNOWN_EVENT_TYPE"; +} +void watcher(zhandle_t *zzh, int type, int state, const char *path, + void *watcherCtx) +{ + if (type == ZOO_SESSION_EVENT) + { + std::cout << type2String(type) << std::endl; + if (state == ZOO_CONNECTED_STATE) + { + std::cout << state2String(type) << std::endl; + /* + * create a temporary zookeeper node that will vanish when this client disconnects, + * useful for testing. + */ + zoo_create(zk, "/test", "my_data", 7, &ZOO_OPEN_ACL_UNSAFE, + ZOO_EPHEMERAL, NULL, 0); + + zoo_create(zk, "/configTest", "top level configuration file", 28, &ZOO_OPEN_ACL_UNSAFE, + 0, NULL, 0); + char* znodeData = "/configTest/typeOneNode;/typeOneNodeWatchingTest;/typeOneNodeWatchingTest/youShouldSeeThis;"; + zoo_create(zk, "/configTest/typeOneNode",znodeData, strlen(znodeData), &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + + char* znodeData1 = "/configTest/typeTwoNode;/typeTwoNodeWatchingTest;/typeTwoNodeWatchingTest/youShouldSeeThis;/typeTwoNodeWatchingTest/youShouldSeeThis/AndThisAlso;"; + zoo_create(zk, "/configTest/typeTwoNode",znodeData1, strlen(znodeData1), &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + + char* znodeData2 = "dataPlaceHolder"; + zoo_create(zk, "/typeOneNodeWatchingTest",znodeData2, strlen(znodeData2), &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + zoo_create(zk, "/typeTwoNodeWatchingTest",znodeData2, strlen(znodeData2), &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + zoo_create(zk, "/typeTwoNodeWatchingTest/youShouldSeeThis",znodeData2, strlen(znodeData2), &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + zoo_create(zk, "/typeTwoNodeWatchingTest/youShouldSeeThis/AndThisAlso",znodeData2, strlen(znodeData2), &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + zoo_create(zk, "/typeOneNodeWatchingTest/youShouldSeeThis",znodeData2, strlen(znodeData2), &ZOO_OPEN_ACL_UNSAFE, 0, 0, 0); + + safeShutdown(zk); + session_id = zoo_client_id(zzh); + return; + } + else if (state == ZOO_AUTH_FAILED_STATE) + { + std::cout << state2String(type) << std::endl; + safeShutdown(zzh); + } + else if (state == ZOO_EXPIRED_SESSION_STATE) + { + std::cout << state2String(type) << std::endl; + zk = zookeeper_init("localhost:2181", watcher, 3000, session_id, + NULL, 0); + } + } + else if (type == ZOO_DELETED_EVENT) + { + std::cout << type2String(type) << std::endl; + zoo_exists(zk, path, true, NULL); + } + else if (type == ZOO_CHILD_EVENT) + { + std::cout << type2String(type) << std::endl; + } + else if (type == ZOO_CHANGED_EVENT) + { + zoo_exists(zk, path, true, NULL); + std::cout << type2String(type) << std::endl; + } + else if (type == ZOO_CREATED_EVENT) + { + zoo_exists(zk, path, true, NULL); + std::cout << type2String(type) << std::endl; + cout << "creation of watched node detected" << std::endl; + } +} -- cgit v1.2.1