blob: 7885a160a05a71be2b7fde15880df07650e74af1 [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
38#include <grpc/grpc.h>
39#include <grpc++/server.h>
40#include <grpc++/server_builder.h>
41#include <grpc++/server_context.h>
Yang Gaod6319002015-03-06 23:51:23 -080042#include <grpc++/server_credentials.h>
Yang Gaod9569372015-02-19 14:22:52 -080043#include <grpc++/status.h>
44#include "helloworld.pb.h"
45
46using grpc::Server;
47using grpc::ServerBuilder;
48using grpc::ServerContext;
49using grpc::Status;
50using helloworld::HelloRequest;
51using helloworld::HelloReply;
52using helloworld::Greeter;
53
54class GreeterServiceImpl final : public Greeter::Service {
Yang Gao600d70c2015-02-20 10:50:46 -080055 Status SayHello(ServerContext* context, const HelloRequest* request,
Yang Gaod9569372015-02-19 14:22:52 -080056 HelloReply* reply) override {
57 std::string prefix("Hello ");
58 reply->set_message(prefix + request->name());
59 return Status::OK;
60 }
61};
62
63void RunServer() {
64 std::string server_address("0.0.0.0:50051");
65 GreeterServiceImpl service;
66
67 ServerBuilder builder;
Yang Gaod6319002015-03-06 23:51:23 -080068 builder.AddPort(server_address, grpc::InsecureServerCredentials());
Yang Gaod9569372015-02-19 14:22:52 -080069 builder.RegisterService(&service);
70 std::unique_ptr<Server> server(builder.BuildAndStart());
71 std::cout << "Server listening on " << server_address << std::endl;
Yang Gao2b4be222015-02-24 15:59:43 -080072 server->Wait();
Yang Gaod9569372015-02-19 14:22:52 -080073}
74
75int main(int argc, char** argv) {
76 grpc_init();
77
78 RunServer();
79
80 grpc_shutdown();
81 return 0;
82}