server.cc 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <grpcpp/grpcpp.h>
  23. #ifdef BAZEL_BUILD
  24. #include "examples/protos/keyvaluestore.grpc.pb.h"
  25. #else
  26. #include "keyvaluestore.grpc.pb.h"
  27. #endif
  28. using grpc::Server;
  29. using grpc::ServerBuilder;
  30. using grpc::ServerContext;
  31. using grpc::ServerReaderWriter;
  32. using grpc::Status;
  33. using keyvaluestore::KeyValueStore;
  34. using keyvaluestore::Request;
  35. using keyvaluestore::Response;
  36. struct kv_pair {
  37. const char* key;
  38. const char* value;
  39. };
  40. static const kv_pair kvs_map[] = {
  41. {"key1", "value1"}, {"key2", "value2"}, {"key3", "value3"},
  42. {"key4", "value4"}, {"key5", "value5"},
  43. };
  44. const char* get_value_from_map(const char* key) {
  45. for (size_t i = 0; i < sizeof(kvs_map) / sizeof(kv_pair); ++i) {
  46. if (strcmp(key, kvs_map[i].key) == 0) {
  47. return kvs_map[i].value;
  48. }
  49. }
  50. return "";
  51. }
  52. // Logic and data behind the server's behavior.
  53. class KeyValueStoreServiceImpl final : public KeyValueStore::Service {
  54. Status GetValues(ServerContext* context,
  55. ServerReaderWriter<Response, Request>* stream) override {
  56. Request request;
  57. while (stream->Read(&request)) {
  58. Response response;
  59. response.set_value(get_value_from_map(request.key().c_str()));
  60. stream->Write(response);
  61. }
  62. return Status::OK;
  63. }
  64. };
  65. void RunServer() {
  66. std::string server_address("0.0.0.0:50051");
  67. KeyValueStoreServiceImpl service;
  68. ServerBuilder builder;
  69. // Listen on the given address without any authentication mechanism.
  70. builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  71. // Register "service" as the instance through which we'll communicate with
  72. // clients. In this case, it corresponds to an *synchronous* service.
  73. builder.RegisterService(&service);
  74. // Finally assemble the server.
  75. std::unique_ptr<Server> server(builder.BuildAndStart());
  76. std::cout << "Server listening on " << server_address << std::endl;
  77. // Wait for the server to shutdown. Note that some other thread must be
  78. // responsible for shutting down the server for this call to ever return.
  79. server->Wait();
  80. }
  81. int main(int argc, char** argv) {
  82. RunServer();
  83. return 0;
  84. }