examples/cpp/keyvaluestore/client.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2018 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18 
19 #include <iostream>
20 #include <memory>
21 #include <string>
22 #include <vector>
23 
24 #include "caching_interceptor.h"
25 
26 #include <grpcpp/grpcpp.h>
27 
28 #ifdef BAZEL_BUILD
29 #include "examples/protos/keyvaluestore.grpc.pb.h"
30 #else
31 #include "keyvaluestore.grpc.pb.h"
32 #endif
33 
34 using grpc::Channel;
36 using grpc::Status;
37 using keyvaluestore::KeyValueStore;
40 
42  public:
43  KeyValueStoreClient(std::shared_ptr<Channel> channel)
44  : stub_(KeyValueStore::NewStub(channel)) {}
45 
46  // Requests each key in the vector and displays the key and its corresponding
47  // value as a pair
48  void GetValues(const std::vector<std::string>& keys) {
49  // Context for the client. It could be used to convey extra information to
50  // the server and/or tweak certain RPC behaviors.
52  auto stream = stub_->GetValues(&context);
53  for (const auto& key : keys) {
54  // Key we are sending to the server.
56  request.set_key(key);
57  stream->Write(request);
58 
59  // Get the value for the sent key
61  stream->Read(&response);
62  std::cout << key << " : " << response.value() << "\n";
63  }
64  stream->WritesDone();
65  Status status = stream->Finish();
66  if (!status.ok()) {
67  std::cout << status.error_code() << ": " << status.error_message()
68  << std::endl;
69  std::cout << "RPC failed";
70  }
71  }
72 
73  private:
74  std::unique_ptr<KeyValueStore::Stub> stub_;
75 };
76 
77 int main(int argc, char** argv) {
78  // Instantiate the client. It requires a channel, out of which the actual RPCs
79  // are created. This channel models a connection to an endpoint (in this case,
80  // localhost at port 50051). We indicate that the channel isn't authenticated
81  // (use of InsecureChannelCredentials()).
82  // In this example, we are using a cache which has been added in as an
83  // interceptor.
85  std::vector<
86  std::unique_ptr<grpc::experimental::ClientInterceptorFactoryInterface>>
87  interceptor_creators;
88  interceptor_creators.push_back(std::unique_ptr<CachingInterceptorFactory>(
91  "localhost:50051", grpc::InsecureChannelCredentials(), args,
92  std::move(interceptor_creators));
94  std::vector<std::string> keys = {"key1", "key2", "key3", "key4",
95  "key5", "key1", "key2", "key4"};
96  client.GetValues(keys);
97 
98  return 0;
99 }
CachingInterceptorFactory
Definition: caching_interceptor.h:127
keys
const void * keys
Definition: abseil-cpp/absl/random/internal/randen.cc:49
client
Definition: examples/python/async_streaming/client.py:1
benchmark.request
request
Definition: benchmark.py:77
KeyValueStoreClient::GetValues
void GetValues(const std::vector< std::string > &keys)
Definition: examples/cpp/keyvaluestore/client.cc:48
status
absl::Status status
Definition: rls.cc:251
client
static uv_tcp_t client
Definition: test-callback-stack.c:33
framework.rpc.grpc_channelz.Channel
Channel
Definition: grpc_channelz.py:32
channel
wrapped_grpc_channel * channel
Definition: src/php/ext/grpc/call.h:33
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
grpcpp.h
main
int main(int argc, char **argv)
Definition: examples/cpp/keyvaluestore/client.cc:77
grpc::ClientContext
Definition: grpcpp/impl/codegen/client_context.h:195
KeyValueStoreClient::KeyValueStoreClient
KeyValueStoreClient(std::shared_ptr< Channel > channel)
Definition: examples/cpp/keyvaluestore/client.cc:43
grpc::ChannelArguments
Definition: grpcpp/support/channel_arguments.h:39
KeyValueStoreClient::stub_
std::unique_ptr< KeyValueStore::Stub > stub_
Definition: examples/cpp/keyvaluestore/client.cc:74
caching_interceptor.h
key
const char * key
Definition: hpack_parser_table.cc:164
grpc::experimental::CreateCustomChannelWithInterceptors
std::shared_ptr< Channel > CreateCustomChannelWithInterceptors(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds, const ChannelArguments &args, std::vector< std::unique_ptr< experimental::ClientInterceptorFactoryInterface >> interceptor_creators)
asyncio_get_stats.response
response
Definition: asyncio_get_stats.py:28
grpc::protobuf::util::Status
GRPC_CUSTOM_UTIL_STATUS Status
Definition: include/grpcpp/impl/codegen/config_protobuf.h:93
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
absl::Status::ok
ABSL_MUST_USE_RESULT bool ok() const
Definition: third_party/abseil-cpp/absl/status/status.h:802
demo_pb2.Request
Request
Definition: demo_pb2.py:108
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
grpc::InsecureChannelCredentials
std::shared_ptr< ChannelCredentials > InsecureChannelCredentials()
Credentials for an unencrypted, unauthenticated channel.
Definition: cpp/client/insecure_credentials.cc:69
demo_pb2.Response
Response
Definition: demo_pb2.py:115
KeyValueStoreClient
Definition: examples/cpp/keyvaluestore/client.cc:41
stream
voidpf stream
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:54