-
Notifications
You must be signed in to change notification settings - Fork 0
/
zktest.cpp
70 lines (60 loc) · 1.87 KB
/
zktest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*************************************************************************
> File Name: zktest.cpp
> Author: Feng
> Created Time: Wed 05 Sep 2018 11:23:44 AM CST
> Content:
************************************************************************/
#include <string.h>
#include <iostream>
#include <string>
#include <vector>
#include "ZooClient.h"
using namespace std;
int main(int argc, char *argv[])
{
string host = "192.168.32.107:2181";
ZooClient *zk = new ZooClient;
bool ret = zk->start(host);
if (!ret) {
cout << "ZooClient start host(" << host << ") failed!" << endl;
return 0;
}
string root = "/feng";
if (!zk->nodeExist(root)) {
ret = zk->createNode(root, ZNODE_TYPE_PERSIST);
if (!ret) {
cout << "ZooClient create node(" << root << ") failed!" << endl;
return 0;
}
}
string child = "/feng/child";
for (int i = 0; i < 3; ++i) {
ret = zk->createNode(child, ZNODE_TYPE_PERSIST_SEQUENCE);
if (!ret) {
cout << "ZooClient create node(" << child << ") failed!" << endl;
}
}
vector<string> children;
ret = zk->getNodeChildren(root, children);
if (!ret) {
cout << "ZooClient get node(" << root << ") children failed!" << endl;
return 0;
}
vector<string>::iterator it = children.begin();
for (; it != children.end(); ++it) {
cout << *it << endl;
if (argc > 1 && strcmp(argv[1], "delete") == 0) {
ret = zk->deleteNode(*it);
if (!ret) {
cout << "ZooClient delete node(" << *it << ") failed!" << endl;
}
}
}
if (argc > 1 && strcmp(argv[1], "delete") == 0) {
ret = zk->deleteNode(root);
if (!ret) {
cout << "ZooClient delete node(" << root << ") failed!" << endl;
}
}
return 0;
}