1 /*
2 *
3 * Copyright 2015 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
23 #include <grpcpp/grpcpp.h>
24 #include <grpcpp/health_check_service_interface.h>
25 #include <grpcpp/ext/proto_server_reflection_plugin.h>
26
27 #ifdef BAZEL_BUILD
28 #include "examples/protos/helloworld.grpc.pb.h"
29 #else
30 #include "helloworld.grpc.pb.h"
31 #endif
32
33 using grpc::Server;
34 using grpc::ServerBuilder;
35 using grpc::ServerContext;
36 using grpc::Status;
37 using helloworld::HelloRequest;
38 using helloworld::HelloReply;
39 using helloworld::Greeter;
40
41 // Logic and data behind the server's behavior.
42 class GreeterServiceImpl final : public Greeter::Service {
SayHello(ServerContext * context,const HelloRequest * request,HelloReply * reply)43 Status SayHello(ServerContext* context, const HelloRequest* request,
44 HelloReply* reply) override {
45 std::string prefix("Hello ");
46 reply->set_message(prefix + request->name());
47 return Status::OK;
48 }
49 };
50
RunServer()51 void RunServer() {
52 std::string server_address("0.0.0.0:50051");
53 GreeterServiceImpl service;
54
55 grpc::EnableDefaultHealthCheckService(true);
56 grpc::reflection::InitProtoReflectionServerBuilderPlugin();
57 ServerBuilder builder;
58 // Listen on the given address without any authentication mechanism.
59 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
60 // Register "service" as the instance through which we'll communicate with
61 // clients. In this case it corresponds to an *synchronous* service.
62 builder.RegisterService(&service);
63 // Finally assemble the server.
64 std::unique_ptr<Server> server(builder.BuildAndStart());
65 std::cout << "Server listening on " << server_address << std::endl;
66
67 // Wait for the server to shutdown. Note that some other thread must be
68 // responsible for shutting down the server for this call to ever return.
69 server->Wait();
70 }
71
main(int argc,char ** argv)72 int main(int argc, char** argv) {
73 RunServer();
74
75 return 0;
76 }
77