blob: 9eab32c62b481ef45135797b64f4619ed3420399 [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" Noble9efc0832015-04-10 00:15:08 +020040#include "helloworld.grpc.pb.h"
Yang Gaod9569372015-02-19 14:22:52 -080041
42using grpc::Server;
43using grpc::ServerBuilder;
44using grpc::ServerContext;
45using grpc::Status;
46using helloworld::HelloRequest;
47using helloworld::HelloReply;
48using helloworld::Greeter;
49
Craig Tillerd6599a32015-09-03 09:37:02 -070050// Logic and data behind the server's behavior.
Yang Gaod9569372015-02-19 14:22:52 -080051class GreeterServiceImpl final : public Greeter::Service {
Yang Gao600d70c2015-02-20 10:50:46 -080052 Status SayHello(ServerContext* context, const HelloRequest* request,
Yang Gaod9569372015-02-19 14:22:52 -080053 HelloReply* reply) override {
54 std::string prefix("Hello ");
55 reply->set_message(prefix + request->name());
56 return Status::OK;
57 }
58};
59
60void RunServer() {
61 std::string server_address("0.0.0.0:50051");
62 GreeterServiceImpl service;
63
64 ServerBuilder builder;
Craig Tillerd6599a32015-09-03 09:37:02 -070065 // Listen on the given address without any authentication mechanism.
Nicolas "Pixel" Noble2afb2702015-03-23 23:44:31 +010066 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
Craig Tillerd6599a32015-09-03 09:37:02 -070067 // Register "service" as the instance through which we'll communicate with
68 // clients. In this case it corresponds to an *synchronous* service.
Yang Gaod9569372015-02-19 14:22:52 -080069 builder.RegisterService(&service);
Craig Tillerd6599a32015-09-03 09:37:02 -070070 // Finally assemble the server.
Yang Gaod9569372015-02-19 14:22:52 -080071 std::unique_ptr<Server> server(builder.BuildAndStart());
72 std::cout << "Server listening on " << server_address << std::endl;
Craig Tillerd6599a32015-09-03 09:37:02 -070073
74 // Wait for the server to shutdown. Note that some other thread must be
75 // responsible for shutting down the server for this call to ever return.
Yang Gao2b4be222015-02-24 15:59:43 -080076 server->Wait();
Yang Gaod9569372015-02-19 14:22:52 -080077}
78
79int main(int argc, char** argv) {
Yang Gaod9569372015-02-19 14:22:52 -080080 RunServer();
81
Yang Gaod9569372015-02-19 14:22:52 -080082 return 0;
83}