Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions etcd/Response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ class Response {
*/
Value const& prev_value() const;

/**
* Returns the index-th action of the response to an 'recursive watch'
* operation. Equivalent to actions()[index]
*/
std::string const& action(int index) const;

/**
* Returns the vector of actions in a directory in response to an 'recursive
* watch' operation.
*/
std::vector<std::string> const& actions() const;

/**
* Returns the index-th value of the response to an 'ls' operation. Equivalent
* to values()[index]
Expand Down Expand Up @@ -220,6 +232,7 @@ class Response {
std::string _action;
Value _value;
Value _prev_value;
std::vector<std::string> _actions; // for recursive watch
Values _values;
Keys _keys;
int64_t _compact_revision = -1; // for watch
Expand Down
4 changes: 3 additions & 1 deletion etcd/v3/V3Response.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@
namespace etcdv3 {
class V3Response {
public:
V3Response() : error_code(0), index(0){};
V3Response() : error_code(0), index(0) {};
void set_error_code(int code);
int get_error_code() const;
std::string const& get_error_message() const;
void set_error_message(std::string msg);
void set_action(std::string action);
int64_t get_index() const;
std::string const& get_action() const;
std::vector<std::string> const& get_actions() const;
std::vector<etcdv3::KeyValue> const& get_values() const;
std::vector<etcdv3::KeyValue> const& get_prev_values() const;
etcdv3::KeyValue const& get_value() const;
Expand All @@ -44,6 +45,7 @@ class V3Response {
std::string action;
etcdv3::KeyValue value;
etcdv3::KeyValue prev_value;
std::vector<std::string> actions;
std::vector<etcdv3::KeyValue> values;
std::vector<etcdv3::KeyValue> prev_values;
int64_t compact_revision = -1;
Expand Down
6 changes: 6 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,9 @@ else()
install(TARGETS etcd-cpp-api-core etcd-cpp-api
EXPORT etcd-targets)
endif()

target_include_directories(etcd-cpp-api INTERFACE
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}>)

add_executable(main main.cpp)
target_link_libraries(main PRIVATE etcd-cpp-api)
12 changes: 12 additions & 0 deletions src/Response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ etcd::Response::Response(const etcdv3::V3Response& reply,
_error_message = reply.get_error_message();
if (reply.has_values()) {
auto val = reply.get_values();
auto action = reply.get_actions();
for (unsigned int index = 0; index < val.size(); index++) {
if (index < action.size()) {
_actions.push_back(action[index]);
}
_values.push_back(Value(val[index]));
_keys.push_back(val[index].kvs.key());
}
Expand Down Expand Up @@ -96,6 +100,14 @@ etcd::Value const& etcd::Response::value() const { return _value; }

etcd::Value const& etcd::Response::prev_value() const { return _prev_value; }

std::vector<std::string> const& etcd::Response::actions() const {
return _actions;
}

std::string const& etcd::Response::action(int index) const {
return _actions[index];
}

etcd::Values const& etcd::Response::values() const { return _values; }

etcd::Value const& etcd::Response::value(int index) const {
Expand Down
32 changes: 32 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <etcd/SyncClient.hpp>
#include <etcd/Watcher.hpp>

int main() {
auto etcd_client = etcd::SyncClient("http://127.0.0.1:2379");
etcd_client.rmdir("/test", true);
etcd_client.put("/test/0", "val_0");
auto ls_resp = etcd_client.ls("/test");
etcd_client.put("/test/1", "val_1");
etcd_client.put("/test/2", "val_2");
etcd_client.rm("/test/2");

auto watcher = etcd::Watcher(
etcd_client, "/test", ls_resp.index() + 1,
[](const etcd::Response& resp) {
auto actions = resp.actions();
auto values = resp.values();
std::cout << "size of values: " << values.size() << std::endl;
std::cout << "size of keys: " << resp.keys().size() << std::endl;
for (auto i = 0u; i < values.size(); ++i) {
auto action = actions[i];
auto val = values[i];
std::cout << "action: " << action << ", key: " << val.key()
<< ", val: " << val.as_string() << std::endl;
// callback(
// response(resp.action(), val.key(), val.as_string()));
}
},
true /*recursive*/);
std::this_thread::sleep_for(std::chrono::seconds(1));
return 0;
}
6 changes: 6 additions & 0 deletions src/v3/Action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ etcdv3::Action::Action(etcdv3::ActionParameters&& params) {
etcdv3::Action::~Action() {
cq_.Shutdown();

void* tag;
bool ok;
while (cq_.Next(&tag, &ok)) {
// Optionally, handle or log the tag/ok if needed
}

// cancel on-the-fly calls
context.TryCancel();
}
Expand Down
29 changes: 19 additions & 10 deletions src/v3/AsyncGRPC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,24 +283,33 @@ void etcdv3::AsyncWatchResponse::ParseResponse(WatchResponse& reply) {
auto event = reply.events(cnt);
if (mvccpb::Event::EventType::Event_EventType_PUT == event.type()) {
if (event.kv().version() == 1) {
action = etcdv3::CREATE_ACTION;
actions.emplace_back(etcdv3::CREATE_ACTION);
} else {
action = etcdv3::SET_ACTION;
actions.emplace_back(etcdv3::SET_ACTION);
}
value.kvs = event.kv();
etcdv3::KeyValue kv;
kv.kvs.CopyFrom(event.kv());
values.push_back(kv);
} else if (mvccpb::Event::EventType::Event_EventType_DELETE_ ==
event.type()) {
action = etcdv3::DELETE_ACTION;
value.kvs = event.kv();
actions.emplace_back(etcdv3::DELETE_ACTION);
etcdv3::KeyValue kv;
kv.kvs.CopyFrom(event.kv());
values.push_back(kv);
}
if (event.has_prev_kv()) {
prev_value.kvs = event.prev_kv();
etcdv3::KeyValue kv;
kv.kvs.CopyFrom(event.prev_kv());
prev_values.emplace_back(kv);
}
// just store the first occurence of the key in values.
// this is done so tas client will not need to change their behaviour.
// break immediately
break;
}

if (not actions.empty())
action = actions[0];
if (not values.empty())
value = values[0];
if (not prev_values.empty())
prev_value = prev_values[0];
}

etcdv3::AsyncCampaignAction::AsyncCampaignAction(
Expand Down
4 changes: 4 additions & 0 deletions src/v3/V3Response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ void etcdv3::V3Response::set_action(std::string action) {
this->action = action;
}

std::vector<std::string> const& etcdv3::V3Response::get_actions() const {
return actions;
}

std::vector<etcdv3::KeyValue> const& etcdv3::V3Response::get_values() const {
return values;
}
Expand Down