Updated client::set() function to use grpc.
Parse response for PutRequest is still stubbed.
This commit is contained in:
parent
221cdf39f2
commit
c75548d9e3
|
|
@ -10,6 +10,12 @@
|
||||||
#include "proto/rpc.grpc.pb.h"
|
#include "proto/rpc.grpc.pb.h"
|
||||||
|
|
||||||
using grpc::Channel;
|
using grpc::Channel;
|
||||||
|
using grpc::ClientAsyncResponseReader;
|
||||||
|
using grpc::ClientContext;
|
||||||
|
using grpc::CompletionQueue;
|
||||||
|
using grpc::Status;
|
||||||
|
using etcdserverpb::PutRequest;
|
||||||
|
using etcdserverpb::PutResponse;
|
||||||
using etcdserverpb::KV;
|
using etcdserverpb::KV;
|
||||||
|
|
||||||
namespace etcd
|
namespace etcd
|
||||||
|
|
@ -140,6 +146,18 @@ namespace etcd
|
||||||
web::http::client::http_client client;
|
web::http::client::http_client client;
|
||||||
|
|
||||||
std::unique_ptr<KV::Stub> stub_;
|
std::unique_ptr<KV::Stub> stub_;
|
||||||
|
pplx::task<etcd::Response> send_put(const std::string& key, const std::string& value);
|
||||||
|
};
|
||||||
|
|
||||||
|
class AsyncPutResponse
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
PutResponse reply;
|
||||||
|
Status status;
|
||||||
|
ClientContext context;
|
||||||
|
CompletionQueue cq_;
|
||||||
|
std::unique_ptr<ClientAsyncResponseReader<PutResponse>> response_reader;
|
||||||
|
Response ParseResponse();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -39,8 +39,7 @@ pplx::task<etcd::Response> etcd::Client::get(std::string const & key)
|
||||||
|
|
||||||
pplx::task<etcd::Response> etcd::Client::set(std::string const & key, std::string const & value)
|
pplx::task<etcd::Response> etcd::Client::set(std::string const & key, std::string const & value)
|
||||||
{
|
{
|
||||||
web::http::uri_builder uri("/v2/keys" + key);
|
return send_put(key,value);
|
||||||
return send_put_request(uri, "value", value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pplx::task<etcd::Response> etcd::Client::add(std::string const & key, std::string const & value)
|
pplx::task<etcd::Response> etcd::Client::add(std::string const & key, std::string const & value)
|
||||||
|
|
@ -134,3 +133,45 @@ pplx::task<etcd::Response> etcd::Client::watch(std::string const & key, int from
|
||||||
uri.append_query("recursive=true");
|
uri.append_query("recursive=true");
|
||||||
return send_get_request(uri);
|
return send_get_request(uri);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
etcd::Response etcd::AsyncPutResponse::ParseResponse()
|
||||||
|
{
|
||||||
|
return etcd::Response();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pplx::task<etcd::Response> etcd::Client::send_put(std::string const & key, std::string const & value)
|
||||||
|
{
|
||||||
|
PutRequest request;
|
||||||
|
request.set_key(key);
|
||||||
|
request.set_value(value);
|
||||||
|
|
||||||
|
etcd::AsyncPutResponse* call= new etcd::AsyncPutResponse();
|
||||||
|
|
||||||
|
call->response_reader = stub_->AsyncPut(&call->context,request,&call->cq_);
|
||||||
|
|
||||||
|
call->response_reader->Finish(&call->reply, &call->status, (void*)call);
|
||||||
|
|
||||||
|
|
||||||
|
return pplx::task<etcd::Response>([call]()
|
||||||
|
{
|
||||||
|
void* got_tag;
|
||||||
|
bool ok = false;
|
||||||
|
etcd::Response resp;
|
||||||
|
|
||||||
|
//blocking
|
||||||
|
call->cq_.Next(&got_tag, &ok);
|
||||||
|
GPR_ASSERT(got_tag == (void*)call);
|
||||||
|
GPR_ASSERT(ok);
|
||||||
|
|
||||||
|
etcd::AsyncPutResponse* call = static_cast<etcd::AsyncPutResponse*>(got_tag);
|
||||||
|
|
||||||
|
if(call->status.ok())
|
||||||
|
{
|
||||||
|
resp = call->ParseResponse();
|
||||||
|
}
|
||||||
|
delete call;
|
||||||
|
return resp;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue