forked from owenliang/zkclient
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.cc
More file actions
executable file
·152 lines (130 loc) · 5.42 KB
/
test.cc
File metadata and controls
executable file
·152 lines (130 loc) · 5.42 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* test.cc
*
* Created on: 2015年6月10日
* Author: Administrator
*/
#include <stdio.h>
#include <unistd.h>
#include "zkclient.h"
void TestGetNodeHandler(ZKErrorCode errcode, const std::string& path, const char* value, int value_len, void* context) {
if (errcode == kZKSucceed) {
printf("TestGetNodeHandler-[kZKSucceed] path=%s value=%.*s\n", path.c_str(), value_len, value);
} else if (errcode == kZKDeleted) {
printf("TestGetNodeHandler-[kZKDeleted] path=%s\n", path.c_str());
} else if (errcode == kZKError) {
printf("TestGetNodeHandler-[kZKError] path=%s\n", path.c_str());
} else if (errcode == kZKNotExist) {
printf("TestGetNodeHandler-[kZKNotExist] path=%s\n", path.c_str());
}
}
void TestGetChildrenHandler(ZKErrorCode errcode, const std::string& path, int count, char** data, void* context) {
if (errcode == kZKSucceed) {
printf("TestGetChildrenHandler-[kZKSucceed] path=%s count=%d\n", path.c_str(), count);
} else if (errcode == kZKDeleted) {
printf("TestGetChildrenHandler-[kZKDeleted] path=%s\n", path.c_str());
} else if (errcode == kZKError) {
printf("TestGetChildrenHandler-[kZKError] path=%s\n", path.c_str());
} else if (errcode == kZKNotExist) {
printf("TestGetChildrenHandler-[kZKNotExist] path=%s\n", path.c_str());
}
}
void TestExistHandler(ZKErrorCode errcode, const std::string& path, const struct Stat* stat, void* context) {
if (errcode == kZKSucceed) {
printf("TestExistHandler-[kZKSucceed] path=%s version=%d\n", path.c_str(), stat->version);
} else if (errcode == kZKDeleted) {
printf("TestExistHandler-[kZKDeleted] path=%s\n", path.c_str());
} else if (errcode == kZKError) {
printf("TestExistHandler-[kZKError] path=%s\n", path.c_str());
} else if (errcode == kZKNotExist) {
printf("TestExistHandler-[kZKNotExist] path=%s\n", path.c_str());
}
}
void TestCreateHandler(ZKErrorCode errcode, const std::string& path, const std::string& value, void* context) {
if (errcode == kZKSucceed) {
printf("TestCreateHandler-[kZKSucceed] path=%s value=%s\n", path.c_str(), value.c_str());
} else if (errcode == kZKError) {
printf("TestCreateHandler-[kZKError] path=%s\n", path.c_str());
} else if (errcode == kZKNotExist) {
printf("TestCreateHandler-[kZKNotExist] path=%s\n", path.c_str());
} else if (errcode == kZKExisted) {
printf("TestCreateHandler-[kZKExisted] path=%s\n", path.c_str());
}
}
void TestSetHandler(ZKErrorCode errcode, const std::string& path, const struct Stat* stat, void* context) {
if (errcode == kZKSucceed) {
printf("TestSetHandler-[kZKSucceed] path=%s version=%d\n", path.c_str(), stat->version);
} else if (errcode == kZKError) {
printf("TestSetHandler-[kZKError] path=%s\n", path.c_str());
} else if (errcode == kZKNotExist) {
printf("TestSetHandler-[kZKNotExist] path=%s\n", path.c_str());
}
}
void TestDeleteHandler(ZKErrorCode errcode, const std::string& path, void* context) {
if (errcode == kZKSucceed) {
printf("TestDeleteHandler-[kZKSucceed] path=%s\n", path.c_str());
} else if (errcode == kZKError) {
printf("TestDeleteHandler-[kZKError] path=%s\n", path.c_str());
} else if (errcode == kZKNotExist) {
printf("TestDeleteHandler-[kZKNotExist] path=%s\n", path.c_str());
} else if (errcode == kZKNotEmpty) {
printf("TestDeleteHandler-[kZKNotEmpty] path=%s\n", path.c_str());
}
}
int main(int argc, char** argv) {
ZKClient& zkclient = ZKClient::GetInstance();
if (!zkclient.Init("127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002", 10000)) {
fprintf(stderr, "ZKClient failed to init...\n");
return -1;
}
bool succeed = zkclient.GetNode("/test", TestGetNodeHandler, NULL, true);
if (!succeed) {
fprintf(stderr, "ZKClient failed to GetNode...\n");
return -1;
}
succeed = zkclient.GetChildren("/test", TestGetChildrenHandler, NULL, true);
if (!succeed) {
fprintf(stderr, "ZKClient failed to GetChildren...\n");
return -1;
}
succeed = zkclient.Exist("/test", TestExistHandler, NULL, true);
if (!succeed) {
fprintf(stderr, "ZKClient failed to Exist...\n");
return -1;
}
succeed = zkclient.Create("/test/node-", "HELLO WORLD", ZOO_EPHEMERAL | ZOO_SEQUENCE, TestCreateHandler, NULL);
if (!succeed) {
fprintf(stderr, "ZKClient failed to Create...\n");
return -1;
}
succeed = zkclient.Set("/test", "set by zkclient~~", TestSetHandler, NULL);
if (!succeed) {
fprintf(stderr, "ZKClient failed to Set...\n");
return -1;
}
succeed = zkclient.Delete("/test", TestDeleteHandler, NULL);
if (!succeed) {
fprintf(stderr, "ZKClient failed to Delete...\n");
return -1;
}
char buffer[1024] = {0};
int buffer_len = sizeof(buffer);
ZKErrorCode errcode = zkclient.GetNode("/test", buffer, &buffer_len, TestGetNodeHandler, NULL, true);
printf("Sync GetNode returns %d, value=%.*s\n", errcode, buffer_len, buffer);
std::vector<std::string> children;
errcode = zkclient.GetChildren("/test", &children, TestGetChildrenHandler, NULL, true);
printf("Sync GetChildren returns %d, count=%lu\n", errcode, children.size());
errcode = zkclient.Exist("/test", NULL, TestExistHandler, NULL, true);
printf("Sync Exist returns %d\n", errcode);
buffer[0] = '\0';
errcode = zkclient.Create("/test/node-", "HELLO ZOO", ZOO_EPHEMERAL | ZOO_SEQUENCE, buffer, sizeof(buffer));
printf("Sync Create returns %d, path=%s\n", errcode, buffer);
errcode = zkclient.Set("/test", "sync set by zkclient~~~");
printf("Sync Set returns %d\n", errcode);
errcode = zkclient.Delete("/test");
printf("Sync Delete returns %d\n", errcode);
while (true) {
sleep(1);
}
return 0;
}