zookeeper-cpp
ZooKeeper Client for C++
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Groups
connection.hpp
1 #pragma once
2 
3 #include <zk/config.hpp>
4 
5 #include <chrono>
6 #include <iosfwd>
7 #include <memory>
8 #include <mutex>
9 #include <string>
10 #include <vector>
11 
12 #include "buffer.hpp"
13 #include "forwards.hpp"
14 #include "future.hpp"
15 #include "string_view.hpp"
16 
17 namespace zk
18 {
19 
22 
27 {
28 public:
29  static std::shared_ptr<connection> connect(const connection_params&);
30 
31  static std::shared_ptr<connection> connect(string_view conn_string);
32 
33  virtual ~connection() noexcept;
34 
35  virtual void close() = 0;
36 
37  virtual future<get_result> get(string_view path) = 0;
38 
39  virtual future<watch_result> watch(string_view path) = 0;
40 
41  virtual future<get_children_result> get_children(string_view path) = 0;
42 
43  virtual future<watch_children_result> watch_children(string_view path) = 0;
44 
45  virtual future<exists_result> exists(string_view path) = 0;
46 
47  virtual future<watch_exists_result> watch_exists(string_view path) = 0;
48 
49  virtual future<create_result> create(string_view path,
50  const buffer& data,
51  const acl& rules,
52  create_mode mode
53  ) = 0;
54 
55  virtual future<set_result> set(string_view path, const buffer& data, version check) = 0;
56 
57  virtual future<void> erase(string_view path, version check) = 0;
58 
59  virtual future<get_acl_result> get_acl(string_view path) const = 0;
60 
61  virtual future<void> set_acl(string_view path, const acl& rules, acl_version check) = 0;
62 
63  virtual future<multi_result> commit(multi_op&& txn) = 0;
64 
65  virtual future<void> load_fence() = 0;
66 
67  virtual zk::state state() const = 0;
68 
70  virtual future<zk::state> watch_state();
71 
72 protected:
75  virtual void on_session_event(zk::state new_state);
76 
77 private:
78  mutable std::mutex _state_change_promises_protect;
79  std::vector<promise<zk::state>> _state_change_promises;
80 };
81 
84 class connection_params final
85 {
86 public:
87  using host_list = std::vector<std::string>;
88 
89 public:
90  static constexpr std::chrono::milliseconds default_timeout = std::chrono::seconds(10);
91 
92 public:
94  connection_params() noexcept;
95 
96  ~connection_params() noexcept;
97 
121  static connection_params parse(string_view conn_string);
122 
132  const std::string& connection_schema() const { return _connection_schema; }
133  std::string& connection_schema() { return _connection_schema; }
135 
142  const host_list& hosts() const { return _hosts; }
143  host_list& hosts() { return _hosts; }
145 
151  const std::string& chroot() const { return _chroot; }
152  std::string& chroot() { return _chroot; }
154 
158  bool randomize_hosts() const { return _randomize_hosts; }
159  bool& randomize_hosts() { return _randomize_hosts; }
161 
164  bool read_only() const { return _read_only; }
165  bool& read_only() { return _read_only; }
167 
175  std::chrono::milliseconds timeout() const { return _timeout; }
176  std::chrono::milliseconds& timeout() { return _timeout; }
178 
179 private:
180  std::string _connection_schema;
181  host_list _hosts;
182  std::string _chroot;
183  bool _randomize_hosts;
184  bool _read_only;
185  std::chrono::milliseconds _timeout;
186 };
187 
188 bool operator==(const connection_params& lhs, const connection_params& rhs);
189 bool operator!=(const connection_params& lhs, const connection_params& rhs);
190 
191 std::string to_string(const connection_params&);
192 std::ostream& operator<<(std::ostream&, const connection_params&);
193 
195 
196 }
*bool read_only() const
Definition: connection.hpp:164
std::chrono::milliseconds timeout() const
Definition: connection.hpp:175
static connection_params parse(string_view conn_string)
Create an instance from a connection string.
Definition: connection.cpp:186
state
Enumeration of states the client may be at when a watch triggers.
Definition: types.hpp:385
connection_params() noexcept
Create an instance with default values.
Definition: connection.cpp:65
Represents a version of the ACL of an entry.
Definition: types.hpp:171
const std::string & connection_schema() const
Definition: connection.hpp:132
create_mode
When used in client::set, this value determines how the entry is created on the server.
Definition: types.hpp:283
const host_list & hosts() const
Definition: connection.hpp:142
Used to specify parameters for a connection.
Definition: connection.hpp:84
const std::string & chroot() const
Definition: connection.hpp:151
A collection of operations that will be performed atomically.
Definition: multi.hpp:162
Controls the import of future and promise types.
virtual future< zk::state > watch_state()
Watch for a state change.
Definition: connection.cpp:35
Imports the string_view type as std::string_view.
An access control list is a wrapper around acl_rule instances.
Definition: acl.hpp:139
bool randomize_hosts() const
Definition: connection.hpp:158
Controls the buffer type.
virtual void on_session_event(zk::state new_state)
Call this from derived classes when a session event happens.
Definition: connection.cpp:42
Represents a version of the data.
Definition: types.hpp:158
ZKPP_BUFFER_TYPE buffer
The buffer type.
Definition: buffer.hpp:79
An actual connection to the server.
Definition: connection.hpp:26