blob: c8a6d8ae86aff1f6c1550f8db2ffc88e0ecc6dd0 [file] [log] [blame]
Yang Gaod9569372015-02-19 14:22:52 -08001/*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34#include <iostream>
35#include <memory>
36#include <string>
Yang Gaod9569372015-02-19 14:22:52 -080037
Craig Tillerd6599a32015-09-03 09:37:02 -070038#include <grpc++/grpc++.h>
39
Nicolas "Pixel" Noble24263c32017-01-11 01:03:09 +010040#ifdef BAZEL_BUILD
41#include "examples/protos/helloworld.grpc.pb.h"
42#else
Nicolas "Pixel" Noble9efc0832015-04-10 00:15:08 +020043#include "helloworld.grpc.pb.h"
Nicolas "Pixel" Noble24263c32017-01-11 01:03:09 +010044#endif
Yang Gaod9569372015-02-19 14:22:52 -080045
46using grpc::Server;
47using grpc::ServerBuilder;
48using grpc::ServerContext;
49using grpc::Status;
50using helloworld::HelloRequest;
51using helloworld::HelloReply;
52using helloworld::Greeter;
53
Craig Tillerd6599a32015-09-03 09:37:02 -070054// Logic and data behind the server's behavior.
Yang Gaod9569372015-02-19 14:22:52 -080055class GreeterServiceImpl final : public Greeter::Service {
Yang Gao600d70c2015-02-20 10:50:46 -080056 Status SayHello(ServerContext* context, const HelloRequest* request,
Yang Gaod9569372015-02-19 14:22:52 -080057 HelloReply* reply) override {
58 std::string prefix("Hello ");
59 reply->set_message(prefix + request->name());
60 return Status::OK;
61 }
62};
63
64void RunServer() {
65 std::string server_address("0.0.0.0:50051");
66 GreeterServiceImpl service;
67
68 ServerBuilder builder;
Craig Tillerd6599a32015-09-03 09:37:02 -070069 // Listen on the given address without any authentication mechanism.
Nicolas "Pixel" Noble2afb2702015-03-23 23:44:31 +010070 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
Craig Tillerd6599a32015-09-03 09:37:02 -070071 // Register "service" as the instance through which we'll communicate with
72 // clients. In this case it corresponds to an *synchronous* service.
Yang Gaod9569372015-02-19 14:22:52 -080073 builder.RegisterService(&service);
Craig Tillerd6599a32015-09-03 09:37:02 -070074 // Finally assemble the server.
Yang Gaod9569372015-02-19 14:22:52 -080075 std::unique_ptr<Server> server(builder.BuildAndStart());
76 std::cout << "Server listening on " << server_address << std::endl;
Craig Tillerd6599a32015-09-03 09:37:02 -070077
78 // Wait for the server to shutdown. Note that some other thread must be
79 // responsible for shutting down the server for this call to ever return.
Yang Gao2b4be222015-02-24 15:59:43 -080080 server->Wait();
Yang Gaod9569372015-02-19 14:22:52 -080081}
82
83int main(int argc, char** argv) {
Yang Gaod9569372015-02-19 14:22:52 -080084 RunServer();
85
Yang Gaod9569372015-02-19 14:22:52 -080086 return 0;
87}