diff --git a/etcd/Client.hpp b/etcd/Client.hpp index 6ece300..776fc61 100644 --- a/etcd/Client.hpp +++ b/etcd/Client.hpp @@ -156,6 +156,8 @@ namespace etcd pplx::task send_get(std::string const & key); pplx::task send_asyncmodify_if(std::string const & key, std::string const & value, std::string const & old_value); pplx::task send_asyncdelete(std::string const & key, bool recursive); + + etcdv3::grpcClient grpcClient; private: diff --git a/src/Client.cpp b/src/Client.cpp index 4450ca2..ce8d8b0 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -20,7 +20,7 @@ using etcdserverpb::WatchResponse; using etcdserverpb::WatchCreateRequest; etcd::Client::Client(std::string const & address) - : client(address) + : client(address), grpcClient(address) { std::string stripped_address(address); std::string substr("http://"); diff --git a/v3/include/Utils.hpp b/v3/include/Utils.hpp new file mode 100644 index 0000000..587c6ee --- /dev/null +++ b/v3/include/Utils.hpp @@ -0,0 +1,15 @@ +#ifndef __UTILS_HPP__ +#define __UTILS_HPP__ + +#include "v3/include/AsyncRangeResponse.hpp" +#include "v3/include/grpcClient.hpp" + +namespace etcdv3 +{ + namespace Utils + { + etcdv3::AsyncRangeResponse* getKey(std::string const & key, etcdv3::grpcClient& client); + } +} +#endif + diff --git a/v3/include/grpcClient.hpp b/v3/include/grpcClient.hpp new file mode 100644 index 0000000..9795b11 --- /dev/null +++ b/v3/include/grpcClient.hpp @@ -0,0 +1,25 @@ +#ifndef __GRPC_CLIENT_HPP__ +#define __GRPC_CLIENT_HPP__ + +#include +#include "proto/rpc.grpc.pb.h" +#include "v3/include/AsyncRangeResponse.hpp" + + +using grpc::Channel; +using etcdserverpb::PutRequest; +using etcdserverpb::RangeRequest; +using etcdserverpb::KV; + +namespace etcdv3 +{ + + class grpcClient + { + public: + grpcClient(std::string const & address); + std::unique_ptr stub_; + }; +} + +#endif diff --git a/v3/src/Utils.cpp b/v3/src/Utils.cpp new file mode 100644 index 0000000..a769d0b --- /dev/null +++ b/v3/src/Utils.cpp @@ -0,0 +1,24 @@ +#include "v3/include/AsyncRangeResponse.hpp" +#include "proto/rpc.grpc.pb.h" +using etcdserverpb::RangeRequest; + +#include "v3/include/Utils.hpp" + + +etcdv3::AsyncRangeResponse* etcdv3::Utils::getKey(std::string const & key, etcdv3::grpcClient& client) +{ + RangeRequest get_request; + get_request.set_key(key); + etcdv3::AsyncRangeResponse* resp= new etcdv3::AsyncRangeResponse(); + + resp->status = client.stub_->Range(&resp->context, get_request, &resp->reply); + + if(resp->status.ok()) + { + return resp; + } + else + { + throw std::runtime_error(resp->status.error_message()); + } +} diff --git a/v3/src/grpcClient.cpp b/v3/src/grpcClient.cpp new file mode 100644 index 0000000..7fd37d9 --- /dev/null +++ b/v3/src/grpcClient.cpp @@ -0,0 +1,14 @@ +#include "v3/include/grpcClient.hpp" + +etcdv3::grpcClient::grpcClient(std::string const & address) +{ + std::string stripped_address(address); + std::string substr("http://"); + std::string::size_type i = stripped_address.find(substr); + if(i != std::string::npos) + { + stripped_address.erase(i,substr.length()); + } + std::shared_ptr channel = grpc::CreateChannel(stripped_address, grpc::InsecureChannelCredentials()); + stub_= KV::NewStub(channel); +}