Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 1 | /* |
| 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 Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 37 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 38 | #include <grpc++/grpc++.h> |
| 39 | |
Nicolas "Pixel" Noble | 24263c3 | 2017-01-11 01:03:09 +0100 | [diff] [blame] | 40 | #ifdef BAZEL_BUILD |
| 41 | #include "examples/protos/helloworld.grpc.pb.h" |
| 42 | #else |
Nicolas "Pixel" Noble | 9efc083 | 2015-04-10 00:15:08 +0200 | [diff] [blame] | 43 | #include "helloworld.grpc.pb.h" |
Nicolas "Pixel" Noble | 24263c3 | 2017-01-11 01:03:09 +0100 | [diff] [blame] | 44 | #endif |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 45 | |
| 46 | using grpc::Server; |
| 47 | using grpc::ServerBuilder; |
| 48 | using grpc::ServerContext; |
| 49 | using grpc::Status; |
| 50 | using helloworld::HelloRequest; |
| 51 | using helloworld::HelloReply; |
| 52 | using helloworld::Greeter; |
| 53 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 54 | // Logic and data behind the server's behavior. |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 55 | class GreeterServiceImpl final : public Greeter::Service { |
Yang Gao | 600d70c | 2015-02-20 10:50:46 -0800 | [diff] [blame] | 56 | Status SayHello(ServerContext* context, const HelloRequest* request, |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 57 | HelloReply* reply) override { |
| 58 | std::string prefix("Hello "); |
| 59 | reply->set_message(prefix + request->name()); |
| 60 | return Status::OK; |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | void RunServer() { |
| 65 | std::string server_address("0.0.0.0:50051"); |
| 66 | GreeterServiceImpl service; |
| 67 | |
| 68 | ServerBuilder builder; |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 69 | // Listen on the given address without any authentication mechanism. |
Nicolas "Pixel" Noble | 2afb270 | 2015-03-23 23:44:31 +0100 | [diff] [blame] | 70 | builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 71 | // Register "service" as the instance through which we'll communicate with |
| 72 | // clients. In this case it corresponds to an *synchronous* service. |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 73 | builder.RegisterService(&service); |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 74 | // Finally assemble the server. |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 75 | std::unique_ptr<Server> server(builder.BuildAndStart()); |
| 76 | std::cout << "Server listening on " << server_address << std::endl; |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 77 | |
| 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 Gao | 2b4be22 | 2015-02-24 15:59:43 -0800 | [diff] [blame] | 80 | server->Wait(); |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | int main(int argc, char** argv) { |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 84 | RunServer(); |
| 85 | |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 86 | return 0; |
| 87 | } |