-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathzk_cpp_test.cpp
More file actions
352 lines (297 loc) · 12.9 KB
/
zk_cpp_test.cpp
File metadata and controls
352 lines (297 loc) · 12.9 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#include "zk_cpp/zk_cpp.h"
#include <stdio.h>
#include <iostream>
#include <string>
namespace utils {
static std::string perms_to_string(int32_t perms) {
if (perms == utility::zoo_perm_all) {
return "all";
}
std::string ret;
if (perms & utility::zoo_perm_create) {
ret.append("c");
}
if (perms & utility::zoo_perm_read) {
ret.append("r");
}
if (perms & utility::zoo_perm_delete) {
ret.append("d");
}
if (perms & utility::zoo_perm_write) {
ret.append("w");
}
if (perms & utility::zoo_perm_admin) {
ret.append("a");
}
return ret;
}
static int32_t perms_string_to_int(const std::string& perm_str) {
if (perm_str == "all") {
return utility::zoo_perm_all;
}
int32_t perms = 0;
for (auto c : perm_str) {
if (c == 'c') {
perms |= utility::zoo_perm_create;
}
else if (c == 'r') {
perms |= utility::zoo_perm_read;
}
else if (c == 'd') {
perms |= utility::zoo_perm_delete;
}
else if (c == 'w') {
perms |= utility::zoo_perm_write;
}
else if (c == 'a') {
perms |= utility::zoo_perm_admin;
}
}
return perms;
}
static void string_splits(const char* in_str, const char* sep_str, std::vector<std::string>& out_splits){
std::string in(in_str);
std::string sep(sep_str);
std::size_t start_pos = 0;
while (start_pos < in.size()){
std::size_t pos = in.find(sep, start_pos);
if (pos != std::string::npos){
out_splits.push_back(std::move(in.substr(start_pos, pos - start_pos)));
start_pos = pos + 1;
}
else{
out_splits.push_back(std::move(in.substr(start_pos, in.size() - start_pos)));
break;
}
}
}
}
void print_zk_cpp_usage() {
fprintf(stderr, "usage\n");
fprintf(stderr, " create <path> <value> <flag>\n"
" 0 - persistence\n"
" 1 - ephemeral \n"
" 2 - sequence \n"
" 3 - sequence and ephemeral\n");
fprintf(stderr, " delete <path>\n");
fprintf(stderr, " set <path> <data>\n");
fprintf(stderr, " get <path>\n");
fprintf(stderr, " ls <path>\n");
fprintf(stderr, " exists <path>\n");
fprintf(stderr, " setacl <path> scheme:id:perm\n");
fprintf(stderr, " getacl <path>\n");
fprintf(stderr, " addauth username passwd\n");
fprintf(stderr, " watch_data <path> \n");
fprintf(stderr, " watch_child <path> \n");
}
void data_change_event(const std::string& path, const std::string& new_value) {
printf("data_change_event, path[%s] new_data[%s]\n", path.c_str(), new_value.c_str());
}
void child_change_events(const std::string& path, const std::vector<std::string>& children) {
printf("child_change_events, path[%s] new_child_count[%d]\n", path.c_str(), (int32_t)children.size());
for (int32_t i = 0; i < (int32_t)children.size(); ++i) {
printf("%d, %s\n", i, children[i].c_str());
}
}
int main() {
printf("zk_cpp test\n");
/** format is "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002" */
std::string urls;
printf("url formt is '127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002'\n");
printf("input zk server urls:\n");
std::cin >> urls;
utility::zk_cpp zk;
do {
utility::zoo_rc ret = zk.connect(urls);
if (ret != utility::z_ok) {
printf("try connect zk server failed, code[%d][%s]\n",
ret, utility::zk_cpp::error_string(ret));
break;
}
print_zk_cpp_usage();
std::string cmd;
while (std::cin >> cmd) {
if (cmd == "create") {
std::string path, value;
int32_t flag;
std::cin >> path >> value >> flag;
std::string rpath = path;
utility::zoo_rc ret = utility::z_ok;
if (flag == 0) {
std::vector<utility::zoo_acl_t> acl;
acl.push_back(utility::zk_cpp::create_world_acl(utility::zoo_perm_all));
ret = zk.create_persistent_node(path.c_str(), value, acl);
}
else if (flag == 1) {
std::vector<utility::zoo_acl_t> acl;
acl.push_back(utility::zk_cpp::create_world_acl(utility::zoo_perm_all));
ret = zk.create_ephemeral_node(path.c_str(), value, acl);
}
else if (flag == 2) {
std::vector<utility::zoo_acl_t> acl;
acl.push_back(utility::zk_cpp::create_world_acl(utility::zoo_perm_all));
ret = zk.create_sequence_node(path.c_str(), value, acl, rpath);
}
else if (flag == 3) {
std::vector<utility::zoo_acl_t> acl;
acl.push_back(utility::zk_cpp::create_world_acl(utility::zoo_perm_all));
ret = zk.create_sequance_ephemeral_node(path.c_str(), value, acl, rpath);
}
else {
printf("invalid create path flag[%d]\n", flag);
continue;
}
printf("create path[%s] flag[%d] ret[%d][%s], rpath[%s]\n",
path.c_str(), flag, ret, utility::zk_cpp::error_string(ret), rpath.c_str());
}
else if (cmd == "get") {
std::string path;
std::string value;
std::cin >> path;
utility::zoo_rc ret = zk.get_node(path.c_str(), value, nullptr, true);
printf("try get path[%s]'s value, value[%s] ret[%d][%s]\n",
path.c_str(), value.c_str(), ret, utility::zk_cpp::error_string(ret));
}
else if (cmd == "set") {
std::string path;
std::string value;
std::cin >> path >> value;
utility::zoo_rc ret = zk.set_node(path.c_str(), value, -1);
printf("try set path[%s]'s value to [%s] ret[%d][%s]\n",
path.c_str(), value.c_str(), ret, utility::zk_cpp::error_string(ret));
}
else if (cmd == "exist") {
std::string path;
std::cin >> path;
utility::zoo_rc ret = zk.exists_node(path.c_str(), nullptr, true);
printf("try_check path[%s] exist[%d], ret[%d][%s]\n",
path.c_str(), ret == utility::z_ok, ret, utility::zk_cpp::error_string(ret));
}
else if (cmd == "ls") {
std::string path;
std::vector<std::string> children;
std::cin >> path;
utility::zoo_rc ret = zk.get_children(path.c_str(), children, true);
printf("try get path[%s]'s children's, children count[%d], ret[%d][%s]\n",
path.c_str(), (int32_t)children.size(), ret, utility::zk_cpp::error_string(ret));
std::string list;
list.append("[");
for (int32_t i = 0; i < (int32_t)children.size(); ++i) {
//printf("%s\n", children[i].c_str());
list.append(children[i]).append(", ");
}
list.append("]");
printf("%s\n", list.c_str());
}
else if (cmd == "delete") {
std::string path;
std::cin >> path;
utility::zoo_rc ret = zk.delete_node(path.c_str(), -1);
printf("try delete path[%s], ret[%d][%s]\n",
path.c_str(), ret, utility::zk_cpp::error_string(ret));
}
else if (cmd == "setacl") {
/* setacl <path> scheme:id:perm */
std::string path;
std::string acl_string;
std::cin >> path >> acl_string;
std::vector<std::string> splits;
utils::string_splits(acl_string.c_str(), ":", splits);
if (splits.size() < 3) {
printf("acl formt[%s] error\n", acl_string.c_str());
continue;
}
std::vector<utility::zoo_acl_t> acl;
const std::string& scheme = splits[0];
int32_t perms = 0;
if (scheme == "world") {
const std::string& id = splits[1];
if (id != "anyone") {
printf("acl world formt error, id[%s]\n", id.c_str());
continue;
}
const std::string& perm_str = splits[2];
perms = utils::perms_string_to_int(perm_str);
auto ac = utility::zk_cpp::create_world_acl(perms);
acl.push_back(ac);
}
else if (scheme == "auth") {
// "id" is empty
const std::string& perm_str = splits[2];
perms = utils::perms_string_to_int(perm_str);
auto ac = utility::zk_cpp::create_auth_acl(perms);
acl.push_back(ac);
}
else if (scheme == "digest") {
if (splits.size() < 4) {
printf("acl digest formt error, acl_str[%s]\n", acl_string.c_str());
continue;
}
const std::string& user_name = splits[1];
const std::string& passwd = splits[2];
const std::string& perm_str = splits[3];
perms = utils::perms_string_to_int(perm_str);
auto ac = utility::zk_cpp::create_digest_acl(perms, user_name, passwd);
acl.push_back(ac);
}
else if (scheme == "ip") {
const std::string& id = splits[1];
const std::string& perm_str = splits[2];
perms = utils::perms_string_to_int(perm_str);
auto ac = utility::zk_cpp::create_ip_acl(perms, id);
acl.push_back(ac);
}
else {
printf("unsupported scheme[%s]\n", scheme.c_str());
continue;
}
auto ret = zk.set_acl(path.c_str(), acl, -1);
printf("set acl for path[%s], ret[%d][%s]\n", path.c_str(), ret, utility::zk_cpp::error_string(ret));
}
else if (cmd == "getacl") {
std::string path;
std::cin >> path;
std::vector<utility::zoo_acl_t> acl;
utility::zoo_rc rt = zk.get_acl(path.c_str(), acl);
printf("get acl of path[%s], ret[%d][%s], acl_count[%d]\n",
path.c_str(), ret, utility::zk_cpp::error_string(ret), (int32_t)acl.size());
for (auto& ac : acl) {
printf("%s:%s:%s\n", ac.scheme.c_str(), ac.id.c_str(), utils::perms_to_string(ac.perm).c_str());
}
}
else if (cmd == "addauth") {
std::string user_name;
std::string user_passwd;
std::cin >> user_name >> user_passwd;
utility::zoo_rc rt = zk.add_auth(user_name, user_passwd);
printf("add_auth, ret[%d][%s].\n", ret, utility::zk_cpp::error_string(ret));
}
else if (cmd == "watch_data") {
std::string path;
std::cin >> path;
std::string value;
utility::zoo_rc ret = zk.watch_data_change(path.c_str(), data_change_event, &value);
printf("try watch_data change of path[%s], value[%s] ret[%d][%s]\n",
path.c_str(), value.c_str(), ret, utility::zk_cpp::error_string(ret));
}
else if (cmd == "watch_child") {
std::string path;
std::cin >> path;
std::vector<std::string> children;
utility::zoo_rc ret = zk.watch_children_event(path.c_str(), child_change_events, &children);
printf("try watch_child change of path[%s], child_count[%d] ret[%d][%s]\n",
path.c_str(), (int32_t)children.size(), ret, utility::zk_cpp::error_string(ret));
for (int32_t i = 0; i < (int32_t)children.size(); ++i) {
printf("%d, %s\n", i, children[i].c_str());
}
}
else {
printf("unsupported msg[%s]\n", cmd.c_str());
}
}
} while (0);
#ifdef _WIN32
system("pause");
#endif
return 0;
}