zookeeper-cpp
ZooKeeper Client for C++
connection.cpp
1 #include "connection.hpp"
2 #include "connection_zk.hpp"
3 #include "error.hpp"
4 #include "types.hpp"
5 
6 #include <zookeeper/zookeeper.h>
7 
8 namespace zk
9 {
10 
11 connection::~connection() noexcept
12 { }
13 
14 std::shared_ptr<connection> connection::connect(string_view conn_string)
15 {
16  return std::make_shared<connection_zk>(conn_string);
17 }
18 
19 future<zk::state> connection::watch_state()
20 {
21  std::unique_lock<std::mutex> ax(_state_change_promises_protect);
22  _state_change_promises.emplace_back();
23  return _state_change_promises.rbegin()->get_future();
24 }
25 
27 {
28  std::unique_lock<std::mutex> ax(_state_change_promises_protect);
29  auto l_state_change_promises = std::move(_state_change_promises);
30  ax.unlock();
31 
32  auto ex = new_state == zk::state::expired_session ? get_exception_ptr_of(error_code::session_expired)
33  : new_state == zk::state::authentication_failed ? get_exception_ptr_of(error_code::authentication_failed)
34  : std::exception_ptr();
35 
36  for (auto& p : l_state_change_promises)
37  {
38  if (ex)
39  p.set_exception(ex);
40  else
41  p.set_value(new_state);
42  }
43 }
44 
45 }
state
Enumeration of states the client may be at when a watch triggers.
Definition: types.hpp:320
Definition: acl.cpp:8
Authentication has failed – connection requires a new connection instance with different credentials...
virtual future< zk::state > watch_state()
Watch for a state change.
Definition: connection.cpp:19
The serving cluster has expired this session.
virtual void on_session_event(zk::state new_state)
Call this from derived classes when a session event happens.
Definition: connection.cpp:26