client.cc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #include <iostream>
  19. #include <memory>
  20. #include <string>
  21. #include <vector>
  22. #include "caching_interceptor.h"
  23. #include <grpcpp/grpcpp.h>
  24. #ifdef BAZEL_BUILD
  25. #include "examples/protos/keyvaluestore.grpc.pb.h"
  26. #else
  27. #include "keyvaluestore.grpc.pb.h"
  28. #endif
  29. using grpc::Channel;
  30. using grpc::ClientContext;
  31. using grpc::Status;
  32. using keyvaluestore::KeyValueStore;
  33. using keyvaluestore::Request;
  34. using keyvaluestore::Response;
  35. class KeyValueStoreClient {
  36. public:
  37. KeyValueStoreClient(std::shared_ptr<Channel> channel)
  38. : stub_(KeyValueStore::NewStub(channel)) {}
  39. // Requests each key in the vector and displays the key and its corresponding
  40. // value as a pair
  41. void GetValues(const std::vector<std::string>& keys) {
  42. // Context for the client. It could be used to convey extra information to
  43. // the server and/or tweak certain RPC behaviors.
  44. ClientContext context;
  45. auto stream = stub_->GetValues(&context);
  46. for (const auto& key : keys) {
  47. // Key we are sending to the server.
  48. Request request;
  49. request.set_key(key);
  50. stream->Write(request);
  51. // Get the value for the sent key
  52. Response response;
  53. stream->Read(&response);
  54. std::cout << key << " : " << response.value() << "\n";
  55. }
  56. stream->WritesDone();
  57. Status status = stream->Finish();
  58. if (!status.ok()) {
  59. std::cout << status.error_code() << ": " << status.error_message()
  60. << std::endl;
  61. std::cout << "RPC failed";
  62. }
  63. }
  64. private:
  65. std::unique_ptr<KeyValueStore::Stub> stub_;
  66. };
  67. int main(int argc, char** argv) {
  68. // Instantiate the client. It requires a channel, out of which the actual RPCs
  69. // are created. This channel models a connection to an endpoint (in this case,
  70. // localhost at port 50051). We indicate that the channel isn't authenticated
  71. // (use of InsecureChannelCredentials()).
  72. // In this example, we are using a cache which has been added in as an
  73. // interceptor.
  74. grpc::ChannelArguments args;
  75. std::vector<
  76. std::unique_ptr<grpc::experimental::ClientInterceptorFactoryInterface>>
  77. interceptor_creators;
  78. interceptor_creators.push_back(std::unique_ptr<CachingInterceptorFactory>(
  79. new CachingInterceptorFactory()));
  80. auto channel = grpc::experimental::CreateCustomChannelWithInterceptors(
  81. "localhost:50051", grpc::InsecureChannelCredentials(), args,
  82. std::move(interceptor_creators));
  83. KeyValueStoreClient client(channel);
  84. std::vector<std::string> keys = {"key1", "key2", "key3", "key4",
  85. "key5", "key1", "key2", "key4"};
  86. client.GetValues(keys);
  87. return 0;
  88. }