Update client::get to use grpc.

ParseResponse for AsyncRangeResponse is still stubbed
This commit is contained in:
arches 2016-06-01 10:15:34 -04:00
parent c75548d9e3
commit 6246968086
2 changed files with 67 additions and 2 deletions

View File

@ -16,6 +16,8 @@ using grpc::CompletionQueue;
using grpc::Status;
using etcdserverpb::PutRequest;
using etcdserverpb::PutResponse;
using etcdserverpb::RangeRequest;
using etcdserverpb::RangeResponse;
using etcdserverpb::KV;
namespace etcd
@ -147,6 +149,7 @@ namespace etcd
std::unique_ptr<KV::Stub> stub_;
pplx::task<etcd::Response> send_put(const std::string& key, const std::string& value);
pplx::task<etcd::Response> send_get(std::string const & key);
};
class AsyncPutResponse
@ -159,6 +162,17 @@ namespace etcd
std::unique_ptr<ClientAsyncResponseReader<PutResponse>> response_reader;
Response ParseResponse();
};
class AsyncRangeResponse
{
public:
RangeResponse reply;
Status status;
ClientContext context;
CompletionQueue cq_;
std::unique_ptr<ClientAsyncResponseReader<RangeResponse>> response_reader;
Response ParseResponse();
};
}
#endif

View File

@ -33,8 +33,7 @@ pplx::task<etcd::Response> etcd::Client::send_put_request(web::http::uri_builder
pplx::task<etcd::Response> etcd::Client::get(std::string const & key)
{
web::http::uri_builder uri("/v2/keys" + key);
return send_get_request(uri);
return send_get(key);
}
pplx::task<etcd::Response> etcd::Client::set(std::string const & key, std::string const & value)
@ -137,9 +136,61 @@ pplx::task<etcd::Response> etcd::Client::watch(std::string const & key, int from
etcd::Response etcd::AsyncPutResponse::ParseResponse()
{
std::cout << reply.header().revision() << std::endl;
return etcd::Response();
}
etcd::Response etcd::AsyncRangeResponse::ParseResponse()
{
mvccpb::KeyValue kvs;
if(reply.kvs_size())
{
int index=0;
do
{
kvs = reply.kvs(index++);
std::cout<<reply.header().revision() << std::endl;
std::cout << kvs.create_revision() << std::endl;
std::cout << kvs.mod_revision() << std::endl;
std::cout << kvs.version() << std::endl;
}while(reply.more());
}
return etcd::Response();
}
pplx::task<etcd::Response> etcd::Client::send_get(std::string const & key)
{
RangeRequest request;
request.set_key(key);
etcd::AsyncRangeResponse* call= new etcd::AsyncRangeResponse();
call->response_reader = stub_->AsyncRange(&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::AsyncRangeResponse* call = static_cast<etcd::AsyncRangeResponse*>(got_tag);
if(call->status.ok())
{
resp = call->ParseResponse();
}
delete call;
return resp;
});
}
pplx::task<etcd::Response> etcd::Client::send_put(std::string const & key, std::string const & value)
{