blob: 832f4400d2e67258fd325ca5a44497e0ac39ee7e [file] [log] [blame]
Yang Gaod9569372015-02-19 14:22:52 -08001/*
2 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003 * Copyright 2015 gRPC authors.
Yang Gaod9569372015-02-19 14:22:52 -08004 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005 * 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 Gaod9569372015-02-19 14:22:52 -08008 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009 * http://www.apache.org/licenses/LICENSE-2.0
Yang Gaod9569372015-02-19 14:22:52 -080010 *
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011 * 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 Gaod9569372015-02-19 14:22:52 -080016 *
17 */
18
19#include <iostream>
20#include <memory>
21#include <string>
Yang Gaod9569372015-02-19 14:22:52 -080022
Craig Tillerd6599a32015-09-03 09:37:02 -070023#include <grpc++/grpc++.h>
24
Nicolas "Pixel" Noble24263c32017-01-11 01:03:09 +010025#ifdef BAZEL_BUILD
26#include "examples/protos/helloworld.grpc.pb.h"
27#else
Nicolas "Pixel" Noble9efc0832015-04-10 00:15:08 +020028#include "helloworld.grpc.pb.h"
Nicolas "Pixel" Noble24263c32017-01-11 01:03:09 +010029#endif
Yang Gaod9569372015-02-19 14:22:52 -080030
31using grpc::Server;
32using grpc::ServerBuilder;
33using grpc::ServerContext;
34using grpc::Status;
35using helloworld::HelloRequest;
36using helloworld::HelloReply;
37using helloworld::Greeter;
38
Craig Tillerd6599a32015-09-03 09:37:02 -070039// Logic and data behind the server's behavior.
Yang Gaod9569372015-02-19 14:22:52 -080040class GreeterServiceImpl final : public Greeter::Service {
Yang Gao600d70c2015-02-20 10:50:46 -080041 Status SayHello(ServerContext* context, const HelloRequest* request,
Yang Gaod9569372015-02-19 14:22:52 -080042 HelloReply* reply) override {
43 std::string prefix("Hello ");
44 reply->set_message(prefix + request->name());
45 return Status::OK;
46 }
47};
48
49void RunServer() {
50 std::string server_address("0.0.0.0:50051");
51 GreeterServiceImpl service;
52
53 ServerBuilder builder;
Craig Tillerd6599a32015-09-03 09:37:02 -070054 // Listen on the given address without any authentication mechanism.
Nicolas "Pixel" Noble2afb2702015-03-23 23:44:31 +010055 builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
Craig Tillerd6599a32015-09-03 09:37:02 -070056 // Register "service" as the instance through which we'll communicate with
57 // clients. In this case it corresponds to an *synchronous* service.
Yang Gaod9569372015-02-19 14:22:52 -080058 builder.RegisterService(&service);
Craig Tillerd6599a32015-09-03 09:37:02 -070059 // Finally assemble the server.
Yang Gaod9569372015-02-19 14:22:52 -080060 std::unique_ptr<Server> server(builder.BuildAndStart());
61 std::cout << "Server listening on " << server_address << std::endl;
Craig Tillerd6599a32015-09-03 09:37:02 -070062
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 Gao2b4be222015-02-24 15:59:43 -080065 server->Wait();
Yang Gaod9569372015-02-19 14:22:52 -080066}
67
68int main(int argc, char** argv) {
Yang Gaod9569372015-02-19 14:22:52 -080069 RunServer();
70
Yang Gaod9569372015-02-19 14:22:52 -080071 return 0;
72}