|
|
|
@ -40,6 +40,7 @@ |
|
|
|
|
#include <grpc++/client_context.h> |
|
|
|
|
#include <grpc++/create_channel.h> |
|
|
|
|
#include <grpc++/credentials.h> |
|
|
|
|
|
|
|
|
|
#include "helloworld.grpc.pb.h" |
|
|
|
|
|
|
|
|
|
using grpc::Channel; |
|
|
|
@ -55,17 +56,28 @@ class GreeterClient { |
|
|
|
|
GreeterClient(std::shared_ptr<Channel> channel) |
|
|
|
|
: stub_(Greeter::NewStub(channel)) {} |
|
|
|
|
|
|
|
|
|
// Assambles the client's payload, sends it and presents the response back
|
|
|
|
|
// from the server.
|
|
|
|
|
std::string SayHello(const std::string& user) { |
|
|
|
|
// Data we are sending to the server.
|
|
|
|
|
HelloRequest request; |
|
|
|
|
request.set_name(user); |
|
|
|
|
|
|
|
|
|
// Container for the data we expect from the server.
|
|
|
|
|
HelloReply reply; |
|
|
|
|
|
|
|
|
|
// Context for the client. It could be used to convey extra information to
|
|
|
|
|
// the server and/or tweak certain RPC behaviors.
|
|
|
|
|
ClientContext context; |
|
|
|
|
|
|
|
|
|
// The actual RPC.
|
|
|
|
|
Status status = stub_->SayHello(&context, request, &reply); |
|
|
|
|
|
|
|
|
|
// Act upon its status.
|
|
|
|
|
if (status.ok()) { |
|
|
|
|
return reply.message(); |
|
|
|
|
} else { |
|
|
|
|
return "Rpc failed"; |
|
|
|
|
return "RPC failed"; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -74,6 +86,11 @@ class GreeterClient { |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
int main(int argc, char** argv) { |
|
|
|
|
// Instantiate the client. It requires a channel, out of which the actual RPCs
|
|
|
|
|
// are created. This channel models a connection to an endpoint (in this case,
|
|
|
|
|
// localhost at port 50051). We indicate that the channel isn't authenticated
|
|
|
|
|
// (use of InsecureCredentials()) and we don't pass any special channel
|
|
|
|
|
// arguments (that could enable extra channel features, such as compression).
|
|
|
|
|
GreeterClient greeter( |
|
|
|
|
grpc::CreateChannel("localhost:50051", grpc::InsecureCredentials(), |
|
|
|
|
ChannelArguments())); |
|
|
|
|