Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 1 | /* |
| 2 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | * Copyright 2015 gRPC authors. |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 4 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 8 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 10 | * |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <iostream> |
| 20 | #include <memory> |
| 21 | #include <string> |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 22 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 23 | #include <grpc++/grpc++.h> |
| 24 | |
Nicolas "Pixel" Noble | 24263c3 | 2017-01-11 01:03:09 +0100 | [diff] [blame] | 25 | #ifdef BAZEL_BUILD |
| 26 | #include "examples/protos/helloworld.grpc.pb.h" |
| 27 | #else |
Nicolas "Pixel" Noble | 9efc083 | 2015-04-10 00:15:08 +0200 | [diff] [blame] | 28 | #include "helloworld.grpc.pb.h" |
Nicolas "Pixel" Noble | 24263c3 | 2017-01-11 01:03:09 +0100 | [diff] [blame] | 29 | #endif |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 30 | |
| 31 | using grpc::Server; |
| 32 | using grpc::ServerBuilder; |
| 33 | using grpc::ServerContext; |
| 34 | using grpc::Status; |
| 35 | using helloworld::HelloRequest; |
| 36 | using helloworld::HelloReply; |
| 37 | using helloworld::Greeter; |
| 38 | |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 39 | // Logic and data behind the server's behavior. |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 40 | class GreeterServiceImpl final : public Greeter::Service { |
Yang Gao | 600d70c | 2015-02-20 10:50:46 -0800 | [diff] [blame] | 41 | Status SayHello(ServerContext* context, const HelloRequest* request, |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 42 | HelloReply* reply) override { |
| 43 | std::string prefix("Hello "); |
| 44 | reply->set_message(prefix + request->name()); |
| 45 | return Status::OK; |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | void RunServer() { |
| 50 | std::string server_address("0.0.0.0:50051"); |
| 51 | GreeterServiceImpl service; |
| 52 | |
| 53 | ServerBuilder builder; |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 54 | // Listen on the given address without any authentication mechanism. |
Nicolas "Pixel" Noble | 2afb270 | 2015-03-23 23:44:31 +0100 | [diff] [blame] | 55 | builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 56 | // Register "service" as the instance through which we'll communicate with |
| 57 | // clients. In this case it corresponds to an *synchronous* service. |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 58 | builder.RegisterService(&service); |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 59 | // Finally assemble the server. |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 60 | std::unique_ptr<Server> server(builder.BuildAndStart()); |
| 61 | std::cout << "Server listening on " << server_address << std::endl; |
Craig Tiller | d6599a3 | 2015-09-03 09:37:02 -0700 | [diff] [blame] | 62 | |
| 63 | // Wait for the server to shutdown. Note that some other thread must be |
| 64 | // responsible for shutting down the server for this call to ever return. |
Yang Gao | 2b4be22 | 2015-02-24 15:59:43 -0800 | [diff] [blame] | 65 | server->Wait(); |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | int main(int argc, char** argv) { |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 69 | RunServer(); |
| 70 | |
Yang Gao | d956937 | 2015-02-19 14:22:52 -0800 | [diff] [blame] | 71 | return 0; |
| 72 | } |