-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
244 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package(default_visibility = ["//visibility:public"]) | ||
|
||
load("@rules_proto//proto:defs.bzl", "proto_library") | ||
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_proto_library") | ||
load("@com_github_grpc_grpc//bazel:cc_grpc_library.bzl", "cc_grpc_library") | ||
|
||
proto_library( | ||
name = "hello_proto", | ||
srcs = ["hello.proto"], | ||
) | ||
|
||
cc_proto_library( | ||
name = "hello_cc_proto", | ||
deps = [":hello_proto"], | ||
) | ||
|
||
cc_grpc_library( | ||
name = "hello_cc_grpc", | ||
srcs = [":hello_proto"], | ||
grpc_only = True, | ||
deps = [":hello_cc_proto"], | ||
) | ||
|
||
cc_binary( | ||
name = "greeter_client", | ||
srcs = ["greeter_client.cc"], | ||
defines = ["BAZEL_BUILD"], | ||
deps = [ | ||
":hello_cc_grpc", | ||
"@com_github_grpc_grpc//:grpc++", | ||
], | ||
) | ||
|
||
cc_binary( | ||
name = "greeter_server", | ||
srcs = ["greeter_server.cc"], | ||
deps = [ | ||
":hello_cc_grpc", | ||
"@com_github_grpc_grpc//:grpc++", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#include <iostream> | ||
#include <memory> | ||
#include <string> | ||
|
||
#include <grpcpp/grpcpp.h> | ||
|
||
#include "experimental/grpc_example/hello.grpc.pb.h" | ||
|
||
using grpc::Channel; | ||
using grpc::ClientContext; | ||
using grpc::Status; | ||
using helloworld::Greeter; | ||
using helloworld::HelloReply; | ||
using helloworld::HelloRequest; | ||
|
||
class GreeterClient { | ||
public: | ||
GreeterClient(std::shared_ptr<Channel> channel) | ||
: stub_(Greeter::NewStub(channel)) {} | ||
|
||
// Assembles 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 { | ||
std::cout << status.error_code() << ": " << status.error_message() | ||
<< std::endl; | ||
return "RPC failed"; | ||
} | ||
} | ||
|
||
std::string SayHelloAgain(const std::string& user) { | ||
// Follows the same pattern as SayHello. | ||
HelloRequest request; | ||
request.set_name(user); | ||
HelloReply reply; | ||
ClientContext context; | ||
|
||
// Here we can use the stub's newly available method we just added. | ||
Status status = stub_->SayHelloAgain(&context, request, &reply); | ||
if (status.ok()) { | ||
return reply.message(); | ||
} else { | ||
std::cout << status.error_code() << ": " << status.error_message() | ||
<< std::endl; | ||
return "RPC failed"; | ||
} | ||
} | ||
|
||
private: | ||
std::unique_ptr<Greeter::Stub> stub_; | ||
}; | ||
|
||
int main(int argc, char** argv) { | ||
std::string address = "localhost"; | ||
std::string port = "50051"; | ||
std::string server_address = address + ":" + port; | ||
std::cout << "Client querying server address: " << server_address << std::endl; | ||
|
||
|
||
// 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 InsecureChannelCredentials()). | ||
GreeterClient greeter(grpc::CreateChannel( | ||
server_address, grpc::InsecureChannelCredentials())); | ||
std::string user("world"); | ||
|
||
std::string reply = greeter.SayHello(user); | ||
std::cout << "Greeter received: " << reply << std::endl; | ||
|
||
reply = greeter.SayHelloAgain(user); | ||
std::cout << "Greeter received: " << reply << std::endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include <iostream> | ||
#include <memory> | ||
#include <string> | ||
|
||
#include <grpcpp/grpcpp.h> | ||
|
||
#include "experimental/grpc_example/hello.grpc.pb.h" | ||
|
||
using grpc::Server; | ||
using grpc::ServerBuilder; | ||
using grpc::ServerContext; | ||
using grpc::Status; | ||
using helloworld::HelloRequest; | ||
using helloworld::HelloReply; | ||
using helloworld::Greeter; | ||
|
||
// Logic and data behind the server's behavior. | ||
class GreeterServiceImpl final : public Greeter::Service { | ||
Status SayHello(ServerContext* context, const HelloRequest* request, | ||
HelloReply* reply) override { | ||
std::string prefix("Hello "); | ||
reply->set_message(prefix + request->name()); | ||
return Status::OK; | ||
} | ||
|
||
Status SayHelloAgain(ServerContext* context, const HelloRequest* request, | ||
HelloReply* reply) override { | ||
std::string prefix("Hello again "); | ||
reply->set_message(prefix + request->name()); | ||
return Status::OK; | ||
} | ||
}; | ||
|
||
void RunServer() { | ||
std::string address = "0.0.0.0"; | ||
std::string port = "50051"; | ||
std::string server_address = address + ":" + port; | ||
GreeterServiceImpl service; | ||
|
||
ServerBuilder builder; | ||
// Listen on the given address without any authentication mechanism. | ||
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); | ||
// Finally assemble the server. | ||
std::unique_ptr<Server> server(builder.BuildAndStart()); | ||
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(); | ||
} | ||
|
||
int main(int argc, char** argv) { | ||
RunServer(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
syntax = "proto3"; | ||
|
||
option java_multiple_files = true; | ||
option java_package = "io.grpc.examples.helloworld"; | ||
option java_outer_classname = "HelloWorldProto"; | ||
option objc_class_prefix = "HLW"; | ||
|
||
package helloworld; | ||
|
||
// The greeting service definition. | ||
service Greeter { | ||
// Sends a greeting | ||
rpc SayHello (HelloRequest) returns (HelloReply) {} | ||
|
||
// Sends another greeting | ||
rpc SayHelloAgain (HelloRequest) returns (HelloReply) {} | ||
} | ||
|
||
// The request message containing the user's name. | ||
message HelloRequest { | ||
string name = 1; | ||
} | ||
|
||
// The response message containing the greetings | ||
message HelloReply { | ||
string message = 1; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
## GRPC | ||
|
||
### References | ||
- [gRPC](https://grpc.io/) | ||
- https://github.com/grpc/grpc/tree/master/examples/cpp | ||
- https://github.com/grpc/grpc-java |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters