1 | #include <Poco/ConsoleChannel.h> |
2 | #include <Poco/Logger.h> |
3 | #include <Poco/Event.h> |
4 | #include <Common/ZooKeeper/ZooKeeperImpl.h> |
5 | #include <Common/typeid_cast.h> |
6 | #include <iostream> |
7 | #include <boost/algorithm/string.hpp> |
8 | |
9 | |
10 | using namespace Coordination; |
11 | |
12 | |
13 | int main(int argc, char ** argv) |
14 | try |
15 | { |
16 | if (argc < 2) |
17 | { |
18 | std::cerr << "Usage: ./zkutil_test_commands_new_lib host:port,host:port...\n" ; |
19 | return 1; |
20 | } |
21 | |
22 | Poco::AutoPtr<Poco::ConsoleChannel> channel(new Poco::ConsoleChannel(std::cerr)); |
23 | Poco::Logger::root().setChannel(channel); |
24 | Poco::Logger::root().setLevel("trace" ); |
25 | |
26 | std::string addresses_arg = argv[1]; |
27 | std::vector<std::string> addresses_strings; |
28 | boost::split(addresses_strings, addresses_arg, boost::is_any_of("," )); |
29 | ZooKeeper::Addresses addresses; |
30 | addresses.reserve(addresses_strings.size()); |
31 | for (const auto & address_string : addresses_strings) |
32 | addresses.emplace_back(address_string); |
33 | |
34 | ZooKeeper zk(addresses, {}, {}, {}, {5, 0}, {0, 50000}, {0, 50000}); |
35 | |
36 | Poco::Event event(true); |
37 | |
38 | std::cout << "create\n" ; |
39 | |
40 | zk.create("/test" , "old" , false, false, {}, |
41 | [&](const CreateResponse & response) |
42 | { |
43 | if (response.error) |
44 | std::cerr << "Error (create) " << response.error << ": " << errorMessage(response.error) << '\n'; |
45 | else |
46 | std::cerr << "Created path: " << response.path_created << '\n'; |
47 | |
48 | //event.set(); |
49 | }); |
50 | |
51 | //event.wait(); |
52 | |
53 | std::cout << "get\n" ; |
54 | |
55 | zk.get("/test" , |
56 | [&](const GetResponse & response) |
57 | { |
58 | if (response.error) |
59 | std::cerr << "Error (get) " << response.error << ": " << errorMessage(response.error) << '\n'; |
60 | else |
61 | std::cerr << "Value: " << response.data << '\n'; |
62 | |
63 | //event.set(); |
64 | }, |
65 | [](const WatchResponse & response) |
66 | { |
67 | if (response.error) |
68 | std::cerr << "Watch (get) on /test, Error " << response.error << ": " << errorMessage(response.error) << '\n'; |
69 | else |
70 | std::cerr << "Watch (get) on /test, path: " << response.path << ", type: " << response.type << '\n'; |
71 | }); |
72 | |
73 | //event.wait(); |
74 | |
75 | std::cout << "set\n" ; |
76 | |
77 | zk.set("/test" , "new" , -1, |
78 | [&](const SetResponse & response) |
79 | { |
80 | if (response.error) |
81 | std::cerr << "Error (set) " << response.error << ": " << errorMessage(response.error) << '\n'; |
82 | else |
83 | std::cerr << "Set\n" ; |
84 | |
85 | //event.set(); |
86 | }); |
87 | |
88 | //event.wait(); |
89 | |
90 | std::cout << "list\n" ; |
91 | |
92 | zk.list("/" , |
93 | [&](const ListResponse & response) |
94 | { |
95 | if (response.error) |
96 | std::cerr << "Error (list) " << response.error << ": " << errorMessage(response.error) << '\n'; |
97 | else |
98 | { |
99 | std::cerr << "Children:\n" ; |
100 | for (const auto & name : response.names) |
101 | std::cerr << name << "\n" ; |
102 | } |
103 | |
104 | //event.set(); |
105 | }, |
106 | [](const WatchResponse & response) |
107 | { |
108 | if (response.error) |
109 | std::cerr << "Watch (list) on /, Error " << response.error << ": " << errorMessage(response.error) << '\n'; |
110 | else |
111 | std::cerr << "Watch (list) on /, path: " << response.path << ", type: " << response.type << '\n'; |
112 | }); |
113 | |
114 | //event.wait(); |
115 | |
116 | std::cout << "exists\n" ; |
117 | |
118 | zk.exists("/test" , |
119 | [&](const ExistsResponse & response) |
120 | { |
121 | if (response.error) |
122 | std::cerr << "Error (exists) " << response.error << ": " << errorMessage(response.error) << '\n'; |
123 | else |
124 | std::cerr << "Exists\n" ; |
125 | |
126 | //event.set(); |
127 | }, |
128 | [](const WatchResponse & response) |
129 | { |
130 | if (response.error) |
131 | std::cerr << "Watch (exists) on /test, Error " << response.error << ": " << errorMessage(response.error) << '\n'; |
132 | else |
133 | std::cerr << "Watch (exists) on /test, path: " << response.path << ", type: " << response.type << '\n'; |
134 | }); |
135 | |
136 | //event.wait(); |
137 | |
138 | std::cout << "remove\n" ; |
139 | |
140 | zk.remove("/test" , -1, [&](const RemoveResponse & response) |
141 | { |
142 | if (response.error) |
143 | std::cerr << "Error (remove) " << response.error << ": " << errorMessage(response.error) << '\n'; |
144 | else |
145 | std::cerr << "Removed\n" ; |
146 | |
147 | //event.set(); |
148 | }); |
149 | |
150 | //event.wait(); |
151 | |
152 | std::cout << "multi\n" ; |
153 | |
154 | Requests ops; |
155 | |
156 | { |
157 | CreateRequest create_request; |
158 | create_request.path = "/test" ; |
159 | create_request.data = "multi1" ; |
160 | ops.emplace_back(std::make_shared<CreateRequest>(std::move(create_request))); |
161 | } |
162 | |
163 | { |
164 | SetRequest set_request; |
165 | set_request.path = "/test" ; |
166 | set_request.data = "multi2" ; |
167 | ops.emplace_back(std::make_shared<SetRequest>(std::move(set_request))); |
168 | } |
169 | |
170 | { |
171 | RemoveRequest remove_request; |
172 | remove_request.path = "/test" ; |
173 | ops.emplace_back(std::make_shared<RemoveRequest>(std::move(remove_request))); |
174 | } |
175 | |
176 | zk.multi(ops, [&](const MultiResponse & response) |
177 | { |
178 | if (response.error) |
179 | std::cerr << "Error (multi) " << response.error << ": " << errorMessage(response.error) << '\n'; |
180 | else |
181 | { |
182 | for (const auto & elem : response.responses) |
183 | if (elem->error) |
184 | std::cerr << "Error (elem) " << elem->error << ": " << errorMessage(elem->error) << '\n'; |
185 | |
186 | std::cerr << "Created path: " << dynamic_cast<const CreateResponse &>(*response.responses[0]).path_created << '\n'; |
187 | } |
188 | |
189 | event.set(); |
190 | }); |
191 | |
192 | event.wait(); |
193 | return 0; |
194 | } |
195 | catch (...) |
196 | { |
197 | std::cerr << DB::getCurrentExceptionMessage(__PRETTY_FUNCTION__) << '\n'; |
198 | return 1; |
199 | } |
200 | |