• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <grpcpp/grpcpp.h>
25 
26 #include "caching_interceptor.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;
35 using grpc::ClientContext;
36 using grpc::Status;
37 using keyvaluestore::KeyValueStore;
38 using keyvaluestore::Request;
39 using keyvaluestore::Response;
40 
41 class KeyValueStoreClient {
42  public:
KeyValueStoreClient(std::shared_ptr<Channel> channel)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
GetValues(const std::vector<std::string> & keys)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.
51     ClientContext context;
52     auto stream = stub_->GetValues(&context);
53     for (const auto& key : keys) {
54       // Key we are sending to the server.
55       Request request;
56       request.set_key(key);
57       stream->Write(request);
58 
59       // Get the value for the sent key
60       Response response;
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 
main(int argc,char ** argv)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.
84   grpc::ChannelArguments args;
85   std::vector<
86       std::unique_ptr<grpc::experimental::ClientInterceptorFactoryInterface>>
87       interceptor_creators;
88   interceptor_creators.push_back(std::unique_ptr<CachingInterceptorFactory>(
89       new CachingInterceptorFactory()));
90   auto channel = grpc::experimental::CreateCustomChannelWithInterceptors(
91       "localhost:50051", grpc::InsecureChannelCredentials(), args,
92       std::move(interceptor_creators));
93   KeyValueStoreClient client(channel);
94   std::vector<std::string> keys = {"key1", "key2", "key3", "key4",
95                                    "key5", "key1", "key2", "key4"};
96   client.GetValues(keys);
97 
98   return 0;
99 }
100