xref: /aosp_15_r20/external/flatbuffers/grpc/samples/greeter/client.cpp (revision 890232f25432b36107d06881e0a25aaa6b473652)
1*890232f2SAndroid Build Coastguard Worker #include "greeter.grpc.fb.h"
2*890232f2SAndroid Build Coastguard Worker #include "greeter_generated.h"
3*890232f2SAndroid Build Coastguard Worker 
4*890232f2SAndroid Build Coastguard Worker #include <grpcpp/grpcpp.h>
5*890232f2SAndroid Build Coastguard Worker 
6*890232f2SAndroid Build Coastguard Worker #include <iostream>
7*890232f2SAndroid Build Coastguard Worker #include <memory>
8*890232f2SAndroid Build Coastguard Worker #include <string>
9*890232f2SAndroid Build Coastguard Worker 
10*890232f2SAndroid Build Coastguard Worker class GreeterClient {
11*890232f2SAndroid Build Coastguard Worker  public:
GreeterClient(std::shared_ptr<grpc::Channel> channel)12*890232f2SAndroid Build Coastguard Worker   GreeterClient(std::shared_ptr<grpc::Channel> channel)
13*890232f2SAndroid Build Coastguard Worker     : stub_(Greeter::NewStub(channel)) {}
14*890232f2SAndroid Build Coastguard Worker 
SayHello(const std::string & name)15*890232f2SAndroid Build Coastguard Worker   std::string SayHello(const std::string &name) {
16*890232f2SAndroid Build Coastguard Worker     flatbuffers::grpc::MessageBuilder mb;
17*890232f2SAndroid Build Coastguard Worker     auto name_offset = mb.CreateString(name);
18*890232f2SAndroid Build Coastguard Worker     auto request_offset = CreateHelloRequest(mb, name_offset);
19*890232f2SAndroid Build Coastguard Worker     mb.Finish(request_offset);
20*890232f2SAndroid Build Coastguard Worker     auto request_msg = mb.ReleaseMessage<HelloRequest>();
21*890232f2SAndroid Build Coastguard Worker 
22*890232f2SAndroid Build Coastguard Worker     flatbuffers::grpc::Message<HelloReply> response_msg;
23*890232f2SAndroid Build Coastguard Worker 
24*890232f2SAndroid Build Coastguard Worker     grpc::ClientContext context;
25*890232f2SAndroid Build Coastguard Worker 
26*890232f2SAndroid Build Coastguard Worker     auto status = stub_->SayHello(&context, request_msg, &response_msg);
27*890232f2SAndroid Build Coastguard Worker     if (status.ok()) {
28*890232f2SAndroid Build Coastguard Worker       const HelloReply *response = response_msg.GetRoot();
29*890232f2SAndroid Build Coastguard Worker       return response->message()->str();
30*890232f2SAndroid Build Coastguard Worker     } else {
31*890232f2SAndroid Build Coastguard Worker       std::cerr << status.error_code() << ": " << status.error_message()
32*890232f2SAndroid Build Coastguard Worker                 << std::endl;
33*890232f2SAndroid Build Coastguard Worker       return "RPC failed";
34*890232f2SAndroid Build Coastguard Worker     }
35*890232f2SAndroid Build Coastguard Worker   }
36*890232f2SAndroid Build Coastguard Worker 
SayManyHellos(const std::string & name,int num_greetings,std::function<void (const std::string &)> callback)37*890232f2SAndroid Build Coastguard Worker   void SayManyHellos(const std::string &name, int num_greetings,
38*890232f2SAndroid Build Coastguard Worker                      std::function<void(const std::string &)> callback) {
39*890232f2SAndroid Build Coastguard Worker     flatbuffers::grpc::MessageBuilder mb;
40*890232f2SAndroid Build Coastguard Worker     auto name_offset = mb.CreateString(name);
41*890232f2SAndroid Build Coastguard Worker     auto request_offset =
42*890232f2SAndroid Build Coastguard Worker         CreateManyHellosRequest(mb, name_offset, num_greetings);
43*890232f2SAndroid Build Coastguard Worker     mb.Finish(request_offset);
44*890232f2SAndroid Build Coastguard Worker     auto request_msg = mb.ReleaseMessage<ManyHellosRequest>();
45*890232f2SAndroid Build Coastguard Worker 
46*890232f2SAndroid Build Coastguard Worker     flatbuffers::grpc::Message<HelloReply> response_msg;
47*890232f2SAndroid Build Coastguard Worker 
48*890232f2SAndroid Build Coastguard Worker     grpc::ClientContext context;
49*890232f2SAndroid Build Coastguard Worker 
50*890232f2SAndroid Build Coastguard Worker     auto stream = stub_->SayManyHellos(&context, request_msg);
51*890232f2SAndroid Build Coastguard Worker     while (stream->Read(&response_msg)) {
52*890232f2SAndroid Build Coastguard Worker       const HelloReply *response = response_msg.GetRoot();
53*890232f2SAndroid Build Coastguard Worker       callback(response->message()->str());
54*890232f2SAndroid Build Coastguard Worker     }
55*890232f2SAndroid Build Coastguard Worker     auto status = stream->Finish();
56*890232f2SAndroid Build Coastguard Worker     if (!status.ok()) {
57*890232f2SAndroid Build Coastguard Worker       std::cerr << status.error_code() << ": " << status.error_message()
58*890232f2SAndroid Build Coastguard Worker                 << std::endl;
59*890232f2SAndroid Build Coastguard Worker       callback("RPC failed");
60*890232f2SAndroid Build Coastguard Worker     }
61*890232f2SAndroid Build Coastguard Worker   }
62*890232f2SAndroid Build Coastguard Worker 
63*890232f2SAndroid Build Coastguard Worker  private:
64*890232f2SAndroid Build Coastguard Worker   std::unique_ptr<Greeter::Stub> stub_;
65*890232f2SAndroid Build Coastguard Worker };
66*890232f2SAndroid Build Coastguard Worker 
main(int argc,char ** argv)67*890232f2SAndroid Build Coastguard Worker int main(int argc, char **argv) {
68*890232f2SAndroid Build Coastguard Worker   std::string server_address("localhost:50051");
69*890232f2SAndroid Build Coastguard Worker 
70*890232f2SAndroid Build Coastguard Worker   auto channel =
71*890232f2SAndroid Build Coastguard Worker       grpc::CreateChannel(server_address, grpc::InsecureChannelCredentials());
72*890232f2SAndroid Build Coastguard Worker   GreeterClient greeter(channel);
73*890232f2SAndroid Build Coastguard Worker 
74*890232f2SAndroid Build Coastguard Worker   std::string name("world");
75*890232f2SAndroid Build Coastguard Worker 
76*890232f2SAndroid Build Coastguard Worker   std::string message = greeter.SayHello(name);
77*890232f2SAndroid Build Coastguard Worker   std::cerr << "Greeter received: " << message << std::endl;
78*890232f2SAndroid Build Coastguard Worker 
79*890232f2SAndroid Build Coastguard Worker   int num_greetings = 10;
80*890232f2SAndroid Build Coastguard Worker   greeter.SayManyHellos(name, num_greetings, [](const std::string &message) {
81*890232f2SAndroid Build Coastguard Worker     std::cerr << "Greeter received: " << message << std::endl;
82*890232f2SAndroid Build Coastguard Worker   });
83*890232f2SAndroid Build Coastguard Worker 
84*890232f2SAndroid Build Coastguard Worker   return 0;
85*890232f2SAndroid Build Coastguard Worker }
86