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