blob: 3c2bbc2074704c4de96ac09069c1ecd47764b943 [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>
37
38#include <grpc/grpc.h>
39#include <grpc++/channel_arguments.h>
40#include <grpc++/channel_interface.h>
41#include <grpc++/client_context.h>
42#include <grpc++/create_channel.h>
43#include <grpc++/status.h>
44#include "helloworld.pb.h"
45
46using grpc::ChannelArguments;
47using grpc::ChannelInterface;
48using grpc::ClientContext;
49using grpc::Status;
50using helloworld::HelloRequest;
51using helloworld::HelloReply;
52using helloworld::Greeter;
53
54class GreeterClient {
55 public:
56 GreeterClient(std::shared_ptr<ChannelInterface> channel)
57 : stub_(Greeter::NewStub(channel)) {}
58
59 std::string SayHello(const std::string& user) {
60 HelloRequest request;
61 request.set_name(user);
62 HelloReply reply;
63 ClientContext context;
64
Yang Gao600d70c2015-02-20 10:50:46 -080065 Status status = stub_->SayHello(&context, request, &reply);
Yang Gaod9569372015-02-19 14:22:52 -080066 if (status.IsOk()) {
67 return reply.message();
68 } else {
69 return "Rpc failed";
70 }
71 }
72
73 void Shutdown() { stub_.reset(); }
74
75 private:
76 std::unique_ptr<Greeter::Stub> stub_;
77};
78
79int main(int argc, char** argv) {
80 grpc_init();
81
82 GreeterClient greeter(
83 grpc::CreateChannel("localhost:50051", ChannelArguments()));
84 std::string user("world");
85 std::string reply = greeter.SayHello(user);
86 std::cout << "Greeter received: " << reply << std::endl;
87
88 greeter.Shutdown();
89
90 grpc_shutdown();
91}