1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 */
18
19 #include <iostream>
20 #include <memory>
21 #include <string>
22
23 #include <grpcpp/grpcpp.h>
24
25 #ifdef BAZEL_BUILD
26 #include "examples/protos/helloworld.grpc.pb.h"
27 #else
28 #include "helloworld.grpc.pb.h"
29 #endif
30
31 using grpc::Channel;
32 using grpc::ClientContext;
33 using grpc::Status;
34 using helloworld::Greeter;
35 using helloworld::HelloReply;
36 using helloworld::HelloRequest;
37
38 class CustomHeaderClient {
39 public:
CustomHeaderClient(std::shared_ptr<Channel> channel)40 CustomHeaderClient(std::shared_ptr<Channel> channel)
41 : stub_(Greeter::NewStub(channel)) {}
42
43 // Assembles the client's payload, sends it and presents the response back
44 // from the server.
SayHello(const std::string & user)45 std::string SayHello(const std::string& user) {
46 // Data we are sending to the server.
47 HelloRequest request;
48 request.set_name(user);
49
50 // Container for the data we expect from the server.
51 HelloReply reply;
52
53 // Context for the client. It could be used to convey extra information to
54 // the server and/or tweak certain RPC behaviors.
55 ClientContext context;
56
57 // Setting custom metadata to be sent to the server
58 context.AddMetadata("custom-header", "Custom Value");
59
60 // Setting custom binary metadata
61 char bytes[8] = {'\0', '\1', '\2', '\3', '\4', '\5', '\6', '\7'};
62 context.AddMetadata("custom-bin", std::string(bytes, 8));
63
64 // The actual RPC.
65 Status status = stub_->SayHello(&context, request, &reply);
66
67 // Act upon its status.
68 if (status.ok()) {
69 std::cout << "Client received initial metadata from server: "
70 << context.GetServerInitialMetadata()
71 .find("custom-server-metadata")
72 ->second
73 << std::endl;
74 std::cout << "Client received trailing metadata from server: "
75 << context.GetServerTrailingMetadata()
76 .find("custom-trailing-metadata")
77 ->second
78 << std::endl;
79 return reply.message();
80 } else {
81 std::cout << status.error_code() << ": " << status.error_message()
82 << std::endl;
83 return "RPC failed";
84 }
85 }
86
87 private:
88 std::unique_ptr<Greeter::Stub> stub_;
89 };
90
main(int argc,char ** argv)91 int main(int argc, char** argv) {
92 // Instantiate the client. It requires a channel, out of which the actual RPCs
93 // are created. This channel models a connection to an endpoint (in this case,
94 // localhost at port 50051). We indicate that the channel isn't authenticated
95 // (use of InsecureChannelCredentials()).
96 CustomHeaderClient greeter(grpc::CreateChannel(
97 "localhost:50051", grpc::InsecureChannelCredentials()));
98 std::string user("world");
99 std::string reply = greeter.SayHello(user);
100 std::cout << "Client received message: " << reply << std::endl;
101 return 0;
102 }
103