1 /*
2 *
3 * Copyright 2023 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 <map>
21 #include <memory>
22 #include <string>
23
24 #include "absl/flags/flag.h"
25 #include "absl/flags/parse.h"
26 #include "absl/strings/str_format.h"
27
28 #include <grpcpp/ext/proto_server_reflection_plugin.h>
29 #include <grpcpp/grpcpp.h>
30 #include <grpcpp/health_check_service_interface.h>
31
32 #ifdef BAZEL_BUILD
33 #include "examples/protos/helloworld.grpc.pb.h"
34 #include "examples/protos/route_guide.grpc.pb.h"
35 #else
36 #include "helloworld.grpc.pb.h"
37 #include "route_guide.grpc.pb.h"
38 #endif
39
40 ABSL_FLAG(uint16_t, port, 50051, "Server port for the service");
41
42 using grpc::CallbackServerContext;
43 using grpc::Server;
44 using grpc::ServerBuilder;
45 using grpc::ServerUnaryReactor;
46 using grpc::Status;
47 using helloworld::Greeter;
48 using helloworld::HelloReply;
49 using helloworld::HelloRequest;
50 using routeguide::RouteGuide;
51
52 // Logic and data behind the server's behavior.
53 class GreeterServiceImpl final : public Greeter::CallbackService {
SayHello(CallbackServerContext * context,const HelloRequest * request,HelloReply * reply)54 ServerUnaryReactor* SayHello(CallbackServerContext* context,
55 const HelloRequest* request,
56 HelloReply* reply) override {
57 std::string prefix("Hello ");
58 reply->set_message(prefix + request->name());
59
60 ServerUnaryReactor* reactor = context->DefaultReactor();
61 reactor->Finish(Status::OK);
62 return reactor;
63 }
64 };
65
66 class RouteGuideImpl final : public RouteGuide::CallbackService {
67 public:
GetFeature(CallbackServerContext * context,const routeguide::Point * request,routeguide::Feature * response)68 ServerUnaryReactor* GetFeature(CallbackServerContext* context,
69 const routeguide::Point* request,
70 routeguide::Feature* response) override {
71 std::string key =
72 absl::StrFormat("%d:%d", request->latitude(), request->longitude());
73 response->set_name(absl::StrFormat("Feature: latitude: %d, longitude: %d",
74 request->latitude(),
75 request->longitude()));
76 *response->mutable_location() = *request;
77 ServerUnaryReactor* reactor = context->DefaultReactor();
78 reactor->Finish(Status::OK);
79 return reactor;
80 }
81
82 private:
83 std::map<std::string, routeguide::Feature> features_db_;
84 };
85
RunServer(uint16_t port)86 void RunServer(uint16_t port) {
87 std::string server_address = absl::StrFormat("0.0.0.0:%d", port);
88 GreeterServiceImpl greeter;
89 RouteGuideImpl route_guide;
90
91 grpc::EnableDefaultHealthCheckService(true);
92 grpc::reflection::InitProtoReflectionServerBuilderPlugin();
93 ServerBuilder builder;
94 // Listen on the given address without any authentication mechanism.
95 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
96 // Register "greeter" as the instance through which we'll communicate with
97 // clients. In this case it corresponds to an *synchronous* greeter.
98 builder.RegisterService(&greeter);
99 builder.RegisterService(&route_guide);
100 // Finally assemble the server.
101 std::unique_ptr<Server> server(builder.BuildAndStart());
102 std::cout << "Server listening on " << server_address << std::endl;
103
104 // Wait for the server to shutdown. Note that some other thread must be
105 // responsible for shutting down the server for this call to ever return.
106 server->Wait();
107 }
108
main(int argc,char ** argv)109 int main(int argc, char** argv) {
110 absl::ParseCommandLine(argc, argv);
111 RunServer(absl::GetFlag(FLAGS_port));
112 return 0;
113 }
114