zookeeper-cpp
ZooKeeper Client for C++
 All Classes Files Functions Variables Typedefs Enumerations Enumerator 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 
25 {
26 public:
27  static std::shared_ptr<connection> connect(const connection_params&);
28 
29  static std::shared_ptr<connection> connect(string_view conn_string);
30 
31  virtual ~connection() noexcept;
32 
33  virtual void close() = 0;
34 
35  virtual future<get_result> get(string_view path) = 0;
36 
37  virtual future<watch_result> watch(string_view path) = 0;
38 
39  virtual future<get_children_result> get_children(string_view path) = 0;
40 
41  virtual future<watch_children_result> watch_children(string_view path) = 0;
42 
43  virtual future<exists_result> exists(string_view path) = 0;
44 
45  virtual future<watch_exists_result> watch_exists(string_view path) = 0;
46 
47  virtual future<create_result> create(string_view path,
48  const buffer& data,
49  const acl& rules,
50  create_mode mode
51  ) = 0;
52 
53  virtual future<set_result> set(string_view path, const buffer& data, version check) = 0;
54 
55  virtual future<void> erase(string_view path, version check) = 0;
56 
57  virtual future<get_acl_result> get_acl(string_view path) const = 0;
58 
59  virtual future<void> set_acl(string_view path, const acl& rules, acl_version check) = 0;
60 
61  virtual future<multi_result> commit(multi_op&& txn) = 0;
62 
63  virtual future<void> load_fence() = 0;
64 
65  virtual zk::state state() const = 0;
66 
68  virtual future<zk::state> watch_state();
69 
70 protected:
74  virtual void on_session_event(zk::state new_state);
75 
76 private:
77  mutable std::mutex _state_change_promises_protect;
78  std::vector<promise<zk::state>> _state_change_promises;
79 };
80 
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 
122  static connection_params parse(string_view conn_string);
123 
132  const std::string& connection_schema() const { return _connection_schema; }
133  std::string& connection_schema() { return _connection_schema; }
134 
141  const host_list& hosts() const { return _hosts; }
142  host_list& hosts() { return _hosts; }
143 
149  const std::string& chroot() const { return _chroot; }
150  std::string& chroot() { return _chroot; }
151 
155  bool randomize_hosts() const { return _randomize_hosts; }
156  bool& randomize_hosts() { return _randomize_hosts; }
157 
159  bool read_only() const { return _read_only; }
160  bool& read_only() { return _read_only; }
161 
169  std::chrono::milliseconds timeout() const { return _timeout; }
170  std::chrono::milliseconds& timeout() { return _timeout; }
171 
172 private:
173  std::string _connection_schema;
174  host_list _hosts;
175  std::string _chroot;
176  bool _randomize_hosts;
177  bool _read_only;
178  std::chrono::milliseconds _timeout;
179 };
180 
181 bool operator==(const connection_params& lhs, const connection_params& rhs);
182 bool operator!=(const connection_params& lhs, const connection_params& rhs);
183 
184 std::string to_string(const connection_params&);
185 std::ostream& operator<<(std::ostream&, const connection_params&);
186 
189 }
std::chrono::milliseconds timeout() const
The session timeout between this client and the server.
Definition: connection.hpp:169
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:320
bool read_only() const
Allow connections to read-only servers? The default (false) is to disallow.
Definition: connection.hpp:159
connection_params() noexcept
Create an instance with default values.
Definition: connection.cpp:65
Represents a version of the ACL of a ZNode.
Definition: types.hpp:145
const std::string & connection_schema() const
Determines the underlying zk::connection implementation to use.
Definition: connection.hpp:132
create_mode
When used in client::set, this value determines how the znode is created on the server.
Definition: types.hpp:252
const host_list & hosts() const
Addresses for the ensemble to connect to.
Definition: connection.hpp:141
Used to specify parameters for a connection.
Definition: connection.hpp:84
const std::string & chroot() const
Specifying a value for chroot as something aside from "" or "/" will run the client commands while in...
Definition: connection.hpp:149
Controls the import of future and promise types.
virtual future< zk::state > watch_state()
Watch for a state change.
Definition: connection.cpp:35
Controls the import of the string_view type.
An access control list is a wrapper around acl_rule instances.
Definition: acl.hpp:124
bool randomize_hosts() const
Connect to a host at random (as opposed to attempting connections in order)? The default is to random...
Definition: connection.hpp:155
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:131
ZKPP_BUFFER_TYPE buffer
The buffer type.
Definition: buffer.hpp:67