Documentation for all the rest of the greeter examples.

pull/3074/head
David Garcia Quintas 9 years ago
parent 2ea2e31158
commit fb72e1e487
  1. 1
      examples/cpp/helloworld/greeter_async_client.cc
  2. 19
      examples/cpp/helloworld/greeter_client.cc
  3. 9
      examples/cpp/helloworld/greeter_server.cc

@ -42,6 +42,7 @@
#include <grpc++/completion_queue.h> #include <grpc++/completion_queue.h>
#include <grpc++/create_channel.h> #include <grpc++/create_channel.h>
#include <grpc++/credentials.h> #include <grpc++/credentials.h>
#include "helloworld.grpc.pb.h" #include "helloworld.grpc.pb.h"
using grpc::Channel; using grpc::Channel;

@ -40,6 +40,7 @@
#include <grpc++/client_context.h> #include <grpc++/client_context.h>
#include <grpc++/create_channel.h> #include <grpc++/create_channel.h>
#include <grpc++/credentials.h> #include <grpc++/credentials.h>
#include "helloworld.grpc.pb.h" #include "helloworld.grpc.pb.h"
using grpc::Channel; using grpc::Channel;
@ -55,17 +56,28 @@ class GreeterClient {
GreeterClient(std::shared_ptr<Channel> channel) GreeterClient(std::shared_ptr<Channel> channel)
: stub_(Greeter::NewStub(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) { std::string SayHello(const std::string& user) {
// Data we are sending to the server.
HelloRequest request; HelloRequest request;
request.set_name(user); request.set_name(user);
// Container for the data we expect from the server.
HelloReply reply; 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; ClientContext context;
// The actual RPC.
Status status = stub_->SayHello(&context, request, &reply); Status status = stub_->SayHello(&context, request, &reply);
// Act upon its status.
if (status.ok()) { if (status.ok()) {
return reply.message(); return reply.message();
} else { } else {
return "Rpc failed"; return "RPC failed";
} }
} }
@ -74,6 +86,11 @@ class GreeterClient {
}; };
int main(int argc, char** argv) { 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( GreeterClient greeter(
grpc::CreateChannel("localhost:50051", grpc::InsecureCredentials(), grpc::CreateChannel("localhost:50051", grpc::InsecureCredentials(),
ChannelArguments())); ChannelArguments()));

@ -40,6 +40,7 @@
#include <grpc++/server_builder.h> #include <grpc++/server_builder.h>
#include <grpc++/server_context.h> #include <grpc++/server_context.h>
#include <grpc++/server_credentials.h> #include <grpc++/server_credentials.h>
#include "helloworld.grpc.pb.h" #include "helloworld.grpc.pb.h"
using grpc::Server; using grpc::Server;
@ -50,6 +51,7 @@ using helloworld::HelloRequest;
using helloworld::HelloReply; using helloworld::HelloReply;
using helloworld::Greeter; using helloworld::Greeter;
// Logic and data behind the server's behavior.
class GreeterServiceImpl final : public Greeter::Service { class GreeterServiceImpl final : public Greeter::Service {
Status SayHello(ServerContext* context, const HelloRequest* request, Status SayHello(ServerContext* context, const HelloRequest* request,
HelloReply* reply) override { HelloReply* reply) override {
@ -64,10 +66,17 @@ void RunServer() {
GreeterServiceImpl service; GreeterServiceImpl service;
ServerBuilder builder; ServerBuilder builder;
// Listen on the given address without any authentication mechanism.
builder.AddListeningPort(server_address, grpc::InsecureServerCredentials()); builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
// Register "service" as the instance through which we'll communicate with
// clients. In this case it corresponds to an *synchronous* service.
builder.RegisterService(&service); builder.RegisterService(&service);
// Finally assemble the server.
std::unique_ptr<Server> server(builder.BuildAndStart()); std::unique_ptr<Server> server(builder.BuildAndStart());
std::cout << "Server listening on " << server_address << std::endl; std::cout << "Server listening on " << server_address << std::endl;
// Wait for the server to shutdown. Note that some other thread must be
// responsible for shutting down the server for this call to ever return.
server->Wait(); server->Wait();
} }

Loading…
Cancel
Save