examples/cpp/compression/greeter_client.cc
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2018 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;
34 using grpc::Status;
38 
39 class GreeterClient {
40  public:
41  GreeterClient(std::shared_ptr<Channel> channel)
42  : stub_(Greeter::NewStub(channel)) {}
43 
44  // Assembles the client's payload, sends it and presents the response back
45  // from the server.
47  // Data we are sending to the server.
49  request.set_name(user);
50 
51  // Container for the data we expect from the server.
52  HelloReply reply;
53 
54  // Context for the client. It could be used to convey extra information to
55  // the server and/or tweak certain RPC behaviors.
57 
58  // Overwrite the call's compression algorithm to DEFLATE.
60 
61  // The actual RPC.
62  Status status = stub_->SayHello(&context, request, &reply);
63 
64  // Act upon its status.
65  if (status.ok()) {
66  return reply.message();
67  } else {
68  std::cout << status.error_code() << ": " << status.error_message()
69  << std::endl;
70  return "RPC failed";
71  }
72  }
73 
74  private:
75  std::unique_ptr<Greeter::Stub> stub_;
76 };
77 
78 int main(int argc, char** argv) {
79  // Instantiate the client. It requires a channel, out of which the actual RPCs
80  // are created. This channel models a connection to an endpoint (in this case,
81  // localhost at port 50051). We indicate that the channel isn't authenticated
82  // (use of InsecureChannelCredentials()).
84  // Set the default compression algorithm for the channel.
85  args.SetCompressionAlgorithm(GRPC_COMPRESS_GZIP);
87  "localhost:50051", grpc::InsecureChannelCredentials(), args));
88  std::string user("world world world world");
89  std::string reply = greeter.SayHello(user);
90  std::cout << "Greeter received: " << reply << std::endl;
91 
92  return 0;
93 }
GRPC_COMPRESS_DEFLATE
@ GRPC_COMPRESS_DEFLATE
Definition: compression_types.h:62
benchmark.request
request
Definition: benchmark.py:77
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
hellostreamingworld_pb2.HelloRequest
HelloRequest
Definition: hellostreamingworld_pb2.py:102
status
absl::Status status
Definition: rls.cc:251
framework.rpc.grpc_channelz.Channel
Channel
Definition: grpc_channelz.py:32
channel
wrapped_grpc_channel * channel
Definition: src/php/ext/grpc/call.h:33
asyncio_get_stats.args
args
Definition: asyncio_get_stats.py:40
main
int main(int argc, char **argv)
Definition: examples/cpp/compression/greeter_client.cc:78
grpcpp.h
GreeterClient
Definition: grpc-helloworld.cc:73
GreeterClient::GreeterClient
GreeterClient(std::shared_ptr< Channel > channel)
Definition: examples/cpp/compression/greeter_client.cc:41
helloworld.Greeter
Definition: helloworld.py:32
grpc::ClientContext
Definition: grpcpp/impl/codegen/client_context.h:195
hellostreamingworld_pb2.HelloReply
HelloReply
Definition: hellostreamingworld_pb2.py:109
grpc::ChannelArguments
Definition: grpcpp/support/channel_arguments.h:39
GreeterClient::SayHello
std::string SayHello(const std::string &user)
Definition: examples/cpp/compression/greeter_client.cc:46
GRPC_COMPRESS_GZIP
@ GRPC_COMPRESS_GZIP
Definition: compression_types.h:63
grpc::CreateCustomChannel
std::shared_ptr< Channel > CreateCustomChannel(const grpc::string &target, const std::shared_ptr< ChannelCredentials > &creds, const ChannelArguments &args)
grpc::protobuf::util::Status
GRPC_CUSTOM_UTIL_STATUS Status
Definition: include/grpcpp/impl/codegen/config_protobuf.h:93
grpc::Status
Definition: include/grpcpp/impl/codegen/status.h:35
absl::Status::ok
ABSL_MUST_USE_RESULT bool ok() const
Definition: third_party/abseil-cpp/absl/status/status.h:802
context
grpc::ClientContext context
Definition: istio_echo_server_lib.cc:61
grpc::InsecureChannelCredentials
std::shared_ptr< ChannelCredentials > InsecureChannelCredentials()
Credentials for an unencrypted, unauthenticated channel.
Definition: cpp/client/insecure_credentials.cc:69
grpc::ClientContext::set_compression_algorithm
void set_compression_algorithm(grpc_compression_algorithm algorithm)
Definition: client_context.cc:144
GreeterClient::stub_
std::unique_ptr< Greeter::Stub > stub_
Definition: grpc-helloworld.cc:102


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:47