diff options
author | Abhishek Kumar <abhikumar@google.com> | 2015-02-24 16:20:31 -0800 |
---|---|---|
committer | Abhishek Kumar <abhikumar@google.com> | 2015-02-24 16:20:31 -0800 |
commit | 40508e380b38f08997e9b749778f3ea75f9e2967 (patch) | |
tree | ba146f2b3520dd88f4c7f9dd5fbbc9d8d0301246 /cpp | |
parent | ea0dae50f083951aac92d5452ab641adc47ff92b (diff) | |
parent | 9a2ff4f376da3564f197df8b5704e1dda3799468 (diff) |
Merge pull request #65 from yang-g/rgreadme
Update README
Diffstat (limited to 'cpp')
-rw-r--r-- | cpp/cpptutorial.md | 32 | ||||
-rw-r--r-- | cpp/route_guide/route_guide_server.cc | 5 |
2 files changed, 21 insertions, 16 deletions
diff --git a/cpp/cpptutorial.md b/cpp/cpptutorial.md index af6b6b1a65..613af1ae7c 100644 --- a/cpp/cpptutorial.md +++ b/cpp/cpptutorial.md @@ -20,7 +20,7 @@ With gRPC we can define our service once in a .proto file and implement clients ## Example code and setup -The example code for our tutorial is in [grpc/grpc-common/cpp/route_guide](https://github.com/grpc/grpc-common/cpp/route_guide). To download the example, clone the `grpc-common` repository by running the following command: +The example code for our tutorial is in [grpc/grpc-common/cpp/route_guide](https://github.com/grpc/grpc-common/tree/master/cpp/route_guide). To download the example, clone the `grpc-common` repository by running the following command: ```shell $ git clone https://github.com/google/grpc-common.git ``` @@ -49,7 +49,7 @@ Then you define `rpc` methods inside your service definition, specifying their r - A *simple RPC* where the client sends a request to the server using the stub and waits for a response to come back, just like a normal function call. ``` - // Obtains the feature at a given position. + // Obtains the feature at a given position. rpc GetFeature(Point) returns (Feature) {} ``` @@ -101,7 +101,9 @@ $ make route_guide.pb.cc which actually runs: -[actual command] +```shell +$ protoc -I ../../protos --cpp_out=. --grpc_out=. --plugin=protoc-gen-grpc=`which grpc_cpp_plugin` ../../protos/route_guide.proto +``` Running this command generates the following files: - `route_guide.pb.h`, the header which declares your generated classes @@ -123,7 +125,7 @@ There are two parts to making our `RouteGuide` service do its job: - Implementing the service interface generated from our service definition: doing the actual "work" of our service. - Running a gRPC server to listen for requests from clients and return the service responses. -You can find our example `RouteGuide` server in [grpc-common/cpp/route_guide/route_guide_server.cc]((https://github.com/grpc/grpc-common/blob/master/cpp/route_guide/route_guide_server.cc). Let's take a closer look at how it works. +You can find our example `RouteGuide` server in [grpc-common/cpp/route_guide/route_guide_server.cc](https://github.com/grpc/grpc-common/blob/master/cpp/route_guide/route_guide_server.cc). Let's take a closer look at how it works. ### Implementing RouteGuide @@ -218,9 +220,7 @@ void RunServer(const std::string& db_path) { builder.RegisterService(&service); std::unique_ptr<Server> server(builder.BuildAndStart()); std::cout << "Server listening on " << server_address << std::endl; - while (true) { - std::this_thread::sleep_for(std::chrono::seconds(5)); - } + server->Wait(); } ``` As you can see, we build and start our server using a `ServerBuilder`. To do this, we: @@ -230,8 +230,7 @@ As you can see, we build and start our server using a `ServerBuilder`. To do thi 3. Specify the address and port we want to use to listen for client requests using the builder's `AddPort()` method. 4. Register our service implementation with the builder. 5. Call `BuildAndStart()` on the builder to create and start an RPC server for our service. - -_[is there no equivalent of the Stubby4 wait() method, ie do you have to do the while(true) loop to keep the server running?]_ +5. Call `Wait()` on the server to do a blocking wait until process is killed or `Shutdown()` is called. <a name="client"></a> ## Creating the client @@ -351,7 +350,16 @@ The syntax for reading and writing here is exactly the same as for our client-st ## Try it out! -_[need build and run instructions here]_ - - +Build client and server: +```shell +$ make +``` +Run the server, which will listen on port 50051: +```shell +$ ./route_guide_server +``` +Run the client (in a different terminal): +```shell +$ ./route_guide_client +``` diff --git a/cpp/route_guide/route_guide_server.cc b/cpp/route_guide/route_guide_server.cc index 2699330e4c..d0a2ecd5d4 100644 --- a/cpp/route_guide/route_guide_server.cc +++ b/cpp/route_guide/route_guide_server.cc @@ -37,7 +37,6 @@ #include <iostream> #include <memory> #include <string> -#include <thread> #include <grpc/grpc.h> #include <grpc++/server.h> @@ -191,9 +190,7 @@ void RunServer(const std::string& db_path) { builder.RegisterService(&service); std::unique_ptr<Server> server(builder.BuildAndStart()); std::cout << "Server listening on " << server_address << std::endl; - while (true) { - std::this_thread::sleep_for(std::chrono::seconds(5)); - } + server->Wait(); } int main(int argc, char** argv) { |