Merge pull request #244 from stanley-cheung/php_route_guide

PHP route guide
pull/3109/head
Tim Emiola 10 years ago
commit 5f229bfdc0
  1. 4
      php/greeter_client.php
  2. 262
      php/route_guide/README.md
  3. 731
      php/route_guide/route_guide.php
  4. 120
      php/route_guide/route_guide.proto
  5. 205
      php/route_guide/route_guide_client.php
  6. 36
      php/route_guide/run_route_guide_client.sh
  7. 18
      php/run_greeter_client.sh

@ -32,8 +32,8 @@
*
*/
require 'vendor/autoload.php';
require 'helloworld.php';
require dirname(__FILE__) . '/vendor/autoload.php';
require dirname(__FILE__) . '/helloworld.php';
function greet($name) {
$client = new helloworld\GreeterClient(

@ -0,0 +1,262 @@
#gRPC Basics: PHP
This tutorial provides a basic PHP programmer's introduction to working with gRPC. By walking through this example you'll learn how to:
- Define a service in a .proto file.
- Generate client code using the protocol buffer compiler.
- Use the PHP gRPC API to write a simple client for your service.
It assumes a passing familiarity with [protocol buffers](https://developers.google.com/protocol-buffers/docs/overview). Note that the example in this tutorial uses the proto2 version of the protocol buffers language.
Also note that currently you can only create clients in PHP for gRPC services - you can find out how to create gRPC servers in our other tutorials, e.g. [Node.js](https://github.com/grpc/grpc-common/tree/master/node/route_guide).
This isn't a comprehensive guide to using gRPC in PHP: more reference documentation is coming soon.
- [Why use gRPC?](#why-grpc)
- [Example code and setup](#setup)
- [Try it out!](#try)
- [Defining the service](#proto)
- [Generating client code](#protoc)
- [Creating the client](#client)
<a name="why-grpc"></a>
## Why use gRPC?
With gRPC you can define your service once in a .proto file and implement clients and servers in any of gRPC's supported languages, which in turn can be run in environments ranging from servers inside Google to your own tablet - all the complexity of communication between different languages and environments is handled for you by gRPC. You also get all the advantages of working with protocol buffers, including efficient serialization, a simple IDL, and easy interface updating.
<a name="setup"></a>
## Example code and setup
The example code for our tutorial is in [grpc/grpc-common/php/route_guide](https://github.com/grpc/grpc-common/tree/master/php/route_guide). To download the example, clone the `grpc-common` repository by running the following command:
```shell
$ git clone https://github.com/grpc/grpc-common.git
```
Then change your current directory to `grpc-common/php/route_guide`:
```shell
$ cd grpc-common/php/route_guide
```
Our example is a simple route mapping application that lets clients get information about features on their route, create a summary of their route, and exchange route information such as traffic updates with the server and other clients.
You also should have the relevant tools installed to generate the client interface code (and a server in another language, for testing). You can obtain the latter by following [these setup instructions](https://github.com/grpc/homebrew-grpc).
<a name="try"></a>
## Try it out!
To try the sample app, we need a gRPC server running locally. Let's compile and run, for example, the Node.js server in this repository:
```shell
$ cd ../../node
$ npm install
$ cd route_guide
$ nodejs ./route_guide_server.js --db_path=route_guide_db.json
```
Run the PHP client (in a different terminal):
```shell
$ ./run_route_guide_client.sh
```
The next sections guide you step-by-step through how this proto service is defined, how to generate a client library from it, and how to create a client stub that uses that library.
<a name="proto"></a>
## Defining the service
First let's look at how the service we're using is defined. A gRPC *service* and its method *request* and *response* types using [protocol buffers](https://developers.google.com/protocol-buffers/docs/overview). You can see the complete .proto file for our example in [`grpc-common/protos/route_guide.proto`](https://github.com/grpc/grpc-common/blob/master/protos/route_guide.proto).
To define a service, you specify a named `service` in your .proto file:
```protobuf
service RouteGuide {
...
}
```
Then you define `rpc` methods inside your service definition, specifying their request and response types. Protocol buffers let you define four kinds of service method, all of which are used in the `RouteGuide` service:
- A *simple RPC* where the client sends a request to the server and receives a response later, just like a normal remote procedure call.
```protobuf
// Obtains the feature at a given position.
rpc GetFeature(Point) returns (Feature) {}
```
- A *response-streaming RPC* where the client sends a request to the server and gets back a stream of response messages. You specify a response-streaming method by placing the `stream` keyword before the *response* type.
```protobuf
// Obtains the Features available within the given Rectangle. Results are
// streamed rather than returned at once (e.g. in a response message with a
// repeated field), as the rectangle may cover a large area and contain a
// huge number of features.
rpc ListFeatures(Rectangle) returns (stream Feature) {}
```
- A *request-streaming RPC* where the client sends a sequence of messages to the server. Once the client has finished writing the messages, it waits for the server to read them all and return its response. You specify a request-streaming method by placing the `stream` keyword before the *request* type.
```protobuf
// Accepts a stream of Points on a route being traversed, returning a
// RouteSummary when traversal is completed.
rpc RecordRoute(stream Point) returns (RouteSummary) {}
```
- A *bidirectional streaming RPC* where both sides send a sequence of messages to the other. The two streams operate independently, so clients and servers can read and write in whatever order they like: for example, the server could wait to receive all the client messages before writing its responses, or it could alternately read a message then write a message, or some other combination of reads and writes. The order of messages in each stream is preserved. You specify this type of method by placing the `stream` keyword before both the request and the response.
```protobuf
// Accepts a stream of RouteNotes sent while a route is being traversed,
// while receiving other RouteNotes (e.g. from other users).
rpc RouteChat(stream RouteNote) returns (stream RouteNote) {}
```
Our .proto file also contains protocol buffer message type definitions for all the request and response types used in our service methods - for example, here's the `Point` message type:
```protobuf
// Points are represented as latitude-longitude pairs in the E7 representation
// (degrees multiplied by 10**7 and rounded to the nearest integer).
// Latitudes should be in the range +/- 90 degrees and longitude should be in
// the range +/- 180 degrees (inclusive).
message Point {
int32 latitude = 1;
int32 longitude = 2;
}
```
<a name="protoc"></a>
## Generating client code
The PHP client stub implementation of the proto files can be generated by the [`protoc-gen-php`](https://github.com/datto/protobuf-php) tool. To install the tool:
```sh
$ cd grpc-common/php
$ php composer.phar install
$ cd vendor/datto/protobuf-php
$ gem install rake ronn
$ rake pear:package version=1.0
$ sudo pear install Protobuf-1.0.tgz
```
To generate the client stub implementation .php file:
```sh
$ cd php/route_guide
$ protoc-gen-php -i . -o . ./route_guide.proto
```
A `route_guide.php` file will be generated in the `php/route_guide` directory. You do not need to modify the file.
To load the generated client stub file, simply `require` it in your PHP application:
```php
require dirname(__FILE__) . '/route_guide.php';
```
The file contains:
- All the protocol buffer code to populate, serialize, and retrieve our request and response message types.
- A class called `examples\RouteGuideClient` that lets clients call the methods defined in the `RouteGuide` service.
<a name="client"></a>
## Creating the client
In this section, we'll look at creating a PHP client for our `RouteGuide` service. You can see our complete example client code in [grpc-common/php/route_guide/route_guide_client.php](https://github.com/grpc/grpc-common/blob/master/php/route_guide/route_guide_client.php).
### Constructing a client object
To call service methods, we first need to create a client object, an instance of the generated `RouteGuideClient` class. The constructor of the class expects the server address and port we want to connect to:
```php
$client = new examples\RouteGuideClient(new Grpc\BaseStub('localhost:50051', []));
```
### Calling service methods
Now let's look at how we call our service methods.
#### Simple RPC
Calling the simple RPC `GetFeature` is nearly as straightforward as calling a local asynchronous method.
```php
$point = new examples\Point();
$point->setLatitude(409146138);
$point->setLongitude(-746188906);
list($feature, $status) = $client->GetFeature($point)->wait();
```
As you can see, we create and populate a request object, i.e. an `examples\Point` object. Then, we call the method on the stub, passing it the request object. If there is no error, then we can read the response information from the server from our response object, i.e. an `examples\Feature` object.
```php
print sprintf("Found %s \n at %f, %f\n", $feature->getName(),
$feature->getLocation()->getLatitude() / COORD_FACTOR,
$feature->getLocation()->getLongitude() / COORD_FACTOR);
```
#### Streaming RPCs
Now let's look at our streaming methods. Here's where we call the server-side streaming method `ListFeatures`, which returns a stream of geographical `Feature`s:
```php
$lo_point = new examples\Point();
$hi_point = new examples\Point();
$lo_point->setLatitude(400000000);
$lo_point->setLongitude(-750000000);
$hi_point->setLatitude(420000000);
$hi_point->setLongitude(-730000000);
$rectangle = new examples\Rectangle();
$rectangle->setLo($lo_point);
$rectangle->setHi($hi_point);
$call = $client->ListFeatures($rectangle);
// an iterator over the server streaming responses
$features = $call->responses();
foreach ($features as $feature) {
// process each feature
} // the loop will end when the server indicates there is no more responses to be sent.
```
The `$call->responses()` method call returns an iterator. When the server sends a response, a `$feature` object will be returned in the `foreach` loop, until the server indiciates that there will be no more responses to be sent.
The client-side streaming method `RecordRoute` is similar, except there we pass the method an iterator and get back a `examples\RouteSummary`.
```php
$points_iter = function($db) {
for ($i = 0; $i < $num_points; $i++) {
$point = new examples\Point();
$point->setLatitude($lat);
$point->setLongitude($long);
yield $point;
}
};
// $points_iter is an iterator simulating client streaming
list($route_summary, $status) =
$client->RecordRoute($points_iter($db))->wait();
```
Finally, let's look at our bidirectional streaming RPC `routeChat()`. In this case, we just pass a context to the method and get back a `BidiStreamingCall` stream object, which we can use to both write and read messages.
```php
$call = $client->RouteChat();
```
To write messages from the client:
```php
foreach ($notes as $n) {
$route_note = new examples\RouteNote();
$call->write($route_note);
}
$call->writesDone();
```
To read messages from the server:
```php
while ($route_note_reply = $call->read()) {
// process $route_note_reply
}
```
Each side will always get the other's messages in the order they were written, both the client and server can read and write in any order — the streams operate completely independently.

@ -0,0 +1,731 @@
<?php
// DO NOT EDIT! Generated by Protobuf-PHP protoc plugin 1.0
// Source: route_guide.proto
// Date: 2015-06-12 00:32:41
namespace examples {
class Point extends \DrSlump\Protobuf\Message {
/** @var int */
public $latitude = 0;
/** @var int */
public $longitude = 0;
/** @var \Closure[] */
protected static $__extensions = array();
public static function descriptor()
{
$descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'examples.Point');
// OPTIONAL INT32 latitude = 1
$f = new \DrSlump\Protobuf\Field();
$f->number = 1;
$f->name = "latitude";
$f->type = \DrSlump\Protobuf::TYPE_INT32;
$f->rule = \DrSlump\Protobuf::RULE_OPTIONAL;
$f->default = 0;
$descriptor->addField($f);
// OPTIONAL INT32 longitude = 2
$f = new \DrSlump\Protobuf\Field();
$f->number = 2;
$f->name = "longitude";
$f->type = \DrSlump\Protobuf::TYPE_INT32;
$f->rule = \DrSlump\Protobuf::RULE_OPTIONAL;
$f->default = 0;
$descriptor->addField($f);
foreach (self::$__extensions as $cb) {
$descriptor->addField($cb(), true);
}
return $descriptor;
}
/**
* Check if <latitude> has a value
*
* @return boolean
*/
public function hasLatitude(){
return $this->_has(1);
}
/**
* Clear <latitude> value
*
* @return \examples\Point
*/
public function clearLatitude(){
return $this->_clear(1);
}
/**
* Get <latitude> value
*
* @return int
*/
public function getLatitude(){
return $this->_get(1);
}
/**
* Set <latitude> value
*
* @param int $value
* @return \examples\Point
*/
public function setLatitude( $value){
return $this->_set(1, $value);
}
/**
* Check if <longitude> has a value
*
* @return boolean
*/
public function hasLongitude(){
return $this->_has(2);
}
/**
* Clear <longitude> value
*
* @return \examples\Point
*/
public function clearLongitude(){
return $this->_clear(2);
}
/**
* Get <longitude> value
*
* @return int
*/
public function getLongitude(){
return $this->_get(2);
}
/**
* Set <longitude> value
*
* @param int $value
* @return \examples\Point
*/
public function setLongitude( $value){
return $this->_set(2, $value);
}
}
}
namespace examples {
class Rectangle extends \DrSlump\Protobuf\Message {
/** @var \examples\Point */
public $lo = null;
/** @var \examples\Point */
public $hi = null;
/** @var \Closure[] */
protected static $__extensions = array();
public static function descriptor()
{
$descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'examples.Rectangle');
// OPTIONAL MESSAGE lo = 1
$f = new \DrSlump\Protobuf\Field();
$f->number = 1;
$f->name = "lo";
$f->type = \DrSlump\Protobuf::TYPE_MESSAGE;
$f->rule = \DrSlump\Protobuf::RULE_OPTIONAL;
$f->reference = '\examples\Point';
$descriptor->addField($f);
// OPTIONAL MESSAGE hi = 2
$f = new \DrSlump\Protobuf\Field();
$f->number = 2;
$f->name = "hi";
$f->type = \DrSlump\Protobuf::TYPE_MESSAGE;
$f->rule = \DrSlump\Protobuf::RULE_OPTIONAL;
$f->reference = '\examples\Point';
$descriptor->addField($f);
foreach (self::$__extensions as $cb) {
$descriptor->addField($cb(), true);
}
return $descriptor;
}
/**
* Check if <lo> has a value
*
* @return boolean
*/
public function hasLo(){
return $this->_has(1);
}
/**
* Clear <lo> value
*
* @return \examples\Rectangle
*/
public function clearLo(){
return $this->_clear(1);
}
/**
* Get <lo> value
*
* @return \examples\Point
*/
public function getLo(){
return $this->_get(1);
}
/**
* Set <lo> value
*
* @param \examples\Point $value
* @return \examples\Rectangle
*/
public function setLo(\examples\Point $value){
return $this->_set(1, $value);
}
/**
* Check if <hi> has a value
*
* @return boolean
*/
public function hasHi(){
return $this->_has(2);
}
/**
* Clear <hi> value
*
* @return \examples\Rectangle
*/
public function clearHi(){
return $this->_clear(2);
}
/**
* Get <hi> value
*
* @return \examples\Point
*/
public function getHi(){
return $this->_get(2);
}
/**
* Set <hi> value
*
* @param \examples\Point $value
* @return \examples\Rectangle
*/
public function setHi(\examples\Point $value){
return $this->_set(2, $value);
}
}
}
namespace examples {
class Feature extends \DrSlump\Protobuf\Message {
/** @var string */
public $name = null;
/** @var \examples\Point */
public $location = null;
/** @var \Closure[] */
protected static $__extensions = array();
public static function descriptor()
{
$descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'examples.Feature');
// OPTIONAL STRING name = 1
$f = new \DrSlump\Protobuf\Field();
$f->number = 1;
$f->name = "name";
$f->type = \DrSlump\Protobuf::TYPE_STRING;
$f->rule = \DrSlump\Protobuf::RULE_OPTIONAL;
$descriptor->addField($f);
// OPTIONAL MESSAGE location = 2
$f = new \DrSlump\Protobuf\Field();
$f->number = 2;
$f->name = "location";
$f->type = \DrSlump\Protobuf::TYPE_MESSAGE;
$f->rule = \DrSlump\Protobuf::RULE_OPTIONAL;
$f->reference = '\examples\Point';
$descriptor->addField($f);
foreach (self::$__extensions as $cb) {
$descriptor->addField($cb(), true);
}
return $descriptor;
}
/**
* Check if <name> has a value
*
* @return boolean
*/
public function hasName(){
return $this->_has(1);
}
/**
* Clear <name> value
*
* @return \examples\Feature
*/
public function clearName(){
return $this->_clear(1);
}
/**
* Get <name> value
*
* @return string
*/
public function getName(){
return $this->_get(1);
}
/**
* Set <name> value
*
* @param string $value
* @return \examples\Feature
*/
public function setName( $value){
return $this->_set(1, $value);
}
/**
* Check if <location> has a value
*
* @return boolean
*/
public function hasLocation(){
return $this->_has(2);
}
/**
* Clear <location> value
*
* @return \examples\Feature
*/
public function clearLocation(){
return $this->_clear(2);
}
/**
* Get <location> value
*
* @return \examples\Point
*/
public function getLocation(){
return $this->_get(2);
}
/**
* Set <location> value
*
* @param \examples\Point $value
* @return \examples\Feature
*/
public function setLocation(\examples\Point $value){
return $this->_set(2, $value);
}
}
}
namespace examples {
class RouteNote extends \DrSlump\Protobuf\Message {
/** @var \examples\Point */
public $location = null;
/** @var string */
public $message = null;
/** @var \Closure[] */
protected static $__extensions = array();
public static function descriptor()
{
$descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'examples.RouteNote');
// OPTIONAL MESSAGE location = 1
$f = new \DrSlump\Protobuf\Field();
$f->number = 1;
$f->name = "location";
$f->type = \DrSlump\Protobuf::TYPE_MESSAGE;
$f->rule = \DrSlump\Protobuf::RULE_OPTIONAL;
$f->reference = '\examples\Point';
$descriptor->addField($f);
// OPTIONAL STRING message = 2
$f = new \DrSlump\Protobuf\Field();
$f->number = 2;
$f->name = "message";
$f->type = \DrSlump\Protobuf::TYPE_STRING;
$f->rule = \DrSlump\Protobuf::RULE_OPTIONAL;
$descriptor->addField($f);
foreach (self::$__extensions as $cb) {
$descriptor->addField($cb(), true);
}
return $descriptor;
}
/**
* Check if <location> has a value
*
* @return boolean
*/
public function hasLocation(){
return $this->_has(1);
}
/**
* Clear <location> value
*
* @return \examples\RouteNote
*/
public function clearLocation(){
return $this->_clear(1);
}
/**
* Get <location> value
*
* @return \examples\Point
*/
public function getLocation(){
return $this->_get(1);
}
/**
* Set <location> value
*
* @param \examples\Point $value
* @return \examples\RouteNote
*/
public function setLocation(\examples\Point $value){
return $this->_set(1, $value);
}
/**
* Check if <message> has a value
*
* @return boolean
*/
public function hasMessage(){
return $this->_has(2);
}
/**
* Clear <message> value
*
* @return \examples\RouteNote
*/
public function clearMessage(){
return $this->_clear(2);
}
/**
* Get <message> value
*
* @return string
*/
public function getMessage(){
return $this->_get(2);
}
/**
* Set <message> value
*
* @param string $value
* @return \examples\RouteNote
*/
public function setMessage( $value){
return $this->_set(2, $value);
}
}
}
namespace examples {
class RouteSummary extends \DrSlump\Protobuf\Message {
/** @var int */
public $point_count = 0;
/** @var int */
public $feature_count = 0;
/** @var int */
public $distance = 0;
/** @var int */
public $elapsed_time = 0;
/** @var \Closure[] */
protected static $__extensions = array();
public static function descriptor()
{
$descriptor = new \DrSlump\Protobuf\Descriptor(__CLASS__, 'examples.RouteSummary');
// OPTIONAL INT32 point_count = 1
$f = new \DrSlump\Protobuf\Field();
$f->number = 1;
$f->name = "point_count";
$f->type = \DrSlump\Protobuf::TYPE_INT32;
$f->rule = \DrSlump\Protobuf::RULE_OPTIONAL;
$f->default = 0;
$descriptor->addField($f);
// OPTIONAL INT32 feature_count = 2
$f = new \DrSlump\Protobuf\Field();
$f->number = 2;
$f->name = "feature_count";
$f->type = \DrSlump\Protobuf::TYPE_INT32;
$f->rule = \DrSlump\Protobuf::RULE_OPTIONAL;
$f->default = 0;
$descriptor->addField($f);
// OPTIONAL INT32 distance = 3
$f = new \DrSlump\Protobuf\Field();
$f->number = 3;
$f->name = "distance";
$f->type = \DrSlump\Protobuf::TYPE_INT32;
$f->rule = \DrSlump\Protobuf::RULE_OPTIONAL;
$f->default = 0;
$descriptor->addField($f);
// OPTIONAL INT32 elapsed_time = 4
$f = new \DrSlump\Protobuf\Field();
$f->number = 4;
$f->name = "elapsed_time";
$f->type = \DrSlump\Protobuf::TYPE_INT32;
$f->rule = \DrSlump\Protobuf::RULE_OPTIONAL;
$f->default = 0;
$descriptor->addField($f);
foreach (self::$__extensions as $cb) {
$descriptor->addField($cb(), true);
}
return $descriptor;
}
/**
* Check if <point_count> has a value
*
* @return boolean
*/
public function hasPointCount(){
return $this->_has(1);
}
/**
* Clear <point_count> value
*
* @return \examples\RouteSummary
*/
public function clearPointCount(){
return $this->_clear(1);
}
/**
* Get <point_count> value
*
* @return int
*/
public function getPointCount(){
return $this->_get(1);
}
/**
* Set <point_count> value
*
* @param int $value
* @return \examples\RouteSummary
*/
public function setPointCount( $value){
return $this->_set(1, $value);
}
/**
* Check if <feature_count> has a value
*
* @return boolean
*/
public function hasFeatureCount(){
return $this->_has(2);
}
/**
* Clear <feature_count> value
*
* @return \examples\RouteSummary
*/
public function clearFeatureCount(){
return $this->_clear(2);
}
/**
* Get <feature_count> value
*
* @return int
*/
public function getFeatureCount(){
return $this->_get(2);
}
/**
* Set <feature_count> value
*
* @param int $value
* @return \examples\RouteSummary
*/
public function setFeatureCount( $value){
return $this->_set(2, $value);
}
/**
* Check if <distance> has a value
*
* @return boolean
*/
public function hasDistance(){
return $this->_has(3);
}
/**
* Clear <distance> value
*
* @return \examples\RouteSummary
*/
public function clearDistance(){
return $this->_clear(3);
}
/**
* Get <distance> value
*
* @return int
*/
public function getDistance(){
return $this->_get(3);
}
/**
* Set <distance> value
*
* @param int $value
* @return \examples\RouteSummary
*/
public function setDistance( $value){
return $this->_set(3, $value);
}
/**
* Check if <elapsed_time> has a value
*
* @return boolean
*/
public function hasElapsedTime(){
return $this->_has(4);
}
/**
* Clear <elapsed_time> value
*
* @return \examples\RouteSummary
*/
public function clearElapsedTime(){
return $this->_clear(4);
}
/**
* Get <elapsed_time> value
*
* @return int
*/
public function getElapsedTime(){
return $this->_get(4);
}
/**
* Set <elapsed_time> value
*
* @param int $value
* @return \examples\RouteSummary
*/
public function setElapsedTime( $value){
return $this->_set(4, $value);
}
}
}
namespace examples {
class RouteGuideClient{
private $rpc_impl;
public function __construct($rpc_impl) {
$this->rpc_impl = $rpc_impl;
}
/**
* @param examples\Point $input
*/
public function GetFeature(\examples\Point $argument, $metadata = array()) {
return $this->rpc_impl->_simpleRequest('/examples.RouteGuide/GetFeature', $argument, '\examples\Feature::deserialize', $metadata);
}
/**
* @param examples\Rectangle $input
*/
public function ListFeatures($argument, $metadata = array()) {
return $this->rpc_impl->_serverStreamRequest('/examples.RouteGuide/ListFeatures', $argument, '\examples\Feature::deserialize', $metadata);
}
/**
* @param examples\Point $input
*/
public function RecordRoute($arguments, $metadata = array()) {
return $this->rpc_impl->_clientStreamRequest('/examples.RouteGuide/RecordRoute', $arguments, '\examples\RouteSummary::deserialize', $metadata);
}
/**
* @param examples\RouteNote $input
*/
public function RouteChat($metadata = array()) {
return $this->rpc_impl->_bidiRequest('/examples.RouteGuide/RouteChat', '\examples\RouteNote::deserialize', $metadata);
}
}
}

@ -0,0 +1,120 @@
// Copyright 2015, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
syntax = "proto2";
option java_package = "io.grpc.examples";
package examples;
// Interface exported by the server.
service RouteGuide {
// A simple RPC.
//
// Obtains the feature at a given position.
rpc GetFeature(Point) returns (Feature) {}
// A server-to-client streaming RPC.
//
// Obtains the Features available within the given Rectangle. Results are
// streamed rather than returned at once (e.g. in a response message with a
// repeated field), as the rectangle may cover a large area and contain a
// huge number of features.
rpc ListFeatures(Rectangle) returns (stream Feature) {}
// A client-to-server streaming RPC.
//
// Accepts a stream of Points on a route being traversed, returning a
// RouteSummary when traversal is completed.
rpc RecordRoute(stream Point) returns (RouteSummary) {}
// A Bidirectional streaming RPC.
//
// Accepts a stream of RouteNotes sent while a route is being traversed,
// while receiving other RouteNotes (e.g. from other users).
rpc RouteChat(stream RouteNote) returns (stream RouteNote) {}
}
// Points are represented as latitude-longitude pairs in the E7 representation
// (degrees multiplied by 10**7 and rounded to the nearest integer).
// Latitudes should be in the range +/- 90 degrees and longitude should be in
// the range +/- 180 degrees (inclusive).
message Point {
optional int32 latitude = 1 [default = 0];
optional int32 longitude = 2 [default = 0];
}
// A latitude-longitude rectangle, represented as two diagonally opposite
// points "lo" and "hi".
message Rectangle {
// One corner of the rectangle.
optional Point lo = 1;
// The other corner of the rectangle.
optional Point hi = 2;
}
// A feature names something at a given point.
//
// If a feature could not be named, the name is empty.
message Feature {
// The name of the feature.
optional string name = 1;
// The point where the feature is detected.
optional Point location = 2;
}
// A RouteNote is a message sent while at a given point.
message RouteNote {
// The location from which the message is sent.
optional Point location = 1;
// The message to be sent.
optional string message = 2;
}
// A RouteSummary is received in response to a RecordRoute rpc.
//
// It contains the number of individual points received, the number of
// detected features, and the total distance covered as the cumulative sum of
// the distance between each point.
message RouteSummary {
// The number of points received.
optional int32 point_count = 1 [default = 0];
// The number of known features passed while traversing the route.
optional int32 feature_count = 2 [default = 0];
// The distance covered in metres.
optional int32 distance = 3 [default = 0];
// The duration of the traversal in seconds.
optional int32 elapsed_time = 4 [default = 0];
}

@ -0,0 +1,205 @@
<?php
/*
*
* Copyright 2015, Google Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Google Inc. nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
require dirname(__FILE__) . '/../vendor/autoload.php';
require dirname(__FILE__) . '/route_guide.php';
define('COORD_FACTOR', 1e7);
$client = new examples\RouteGuideClient(
new Grpc\BaseStub('localhost:50051', []));
function printFeature($feature) {
$name = $feature->getName();
if (!$name) {
$name_str = "no feature";
} else {
$name_str = "feature called $name";
}
print sprintf("Found %s \n at %f, %f\n", $name_str,
$feature->getLocation()->getLatitude() / COORD_FACTOR,
$feature->getLocation()->getLongitude() / COORD_FACTOR);
}
/**
* Run the getFeature demo. Calls getFeature with a point known to have a
* feature and a point known not to have a feature.
*/
function runGetFeature() {
print "Running GetFeature...\n";
global $client;
$point = new examples\Point();
$points = array(
array(409146138, -746188906),
array(0, 0),
);
foreach ($points as $p) {
$point->setLatitude($p[0]);
$point->setLongitude($p[1]);
// make a unary grpc call
list($feature, $status) = $client->GetFeature($point)->wait();
printFeature($feature);
}
}
/**
* Run the listFeatures demo. Calls listFeatures with a rectangle
* containing all of the features in the pre-generated
* database. Prints each response as it comes in.
*/
function runListFeatures() {
print "Running ListFeatures...\n";
global $client;
$lo_point = new examples\Point();
$hi_point = new examples\Point();
$lo_point->setLatitude(400000000);
$lo_point->setLongitude(-750000000);
$hi_point->setLatitude(420000000);
$hi_point->setLongitude(-730000000);
$rectangle = new examples\Rectangle();
$rectangle->setLo($lo_point);
$rectangle->setHi($hi_point);
$call = $client->ListFeatures($rectangle);
// an iterator over the server streaming responses
$features = $call->responses();
foreach ($features as $feature) {
printFeature($feature);
}
}
/**
* Run the recordRoute demo. Sends several randomly chosen points from the
* pre-generated feature database with a variable delay in between. Prints
* the statistics when they are sent from the server.
*/
function runRecordRoute() {
print "Running RecordRoute...\n";
global $client, $argv;
$db = json_decode(file_get_contents($argv[1]), true);
$points_iter = function($db) {
$num_points_in_db = count($db);
$num_points = 10;
for ($i = 0; $i < $num_points; $i++) {
$point = new examples\Point();
$index = rand(0, $num_points_in_db - 1);
$lat = $db[$index]['location']['latitude'];
$long = $db[$index]['location']['longitude'];
$feature_name = $db[$index]['name'];
$point->setLatitude($lat);
$point->setLongitude($long);
print sprintf("Visiting point %f, %f,\n with feature name: %s\n",
$lat / COORD_FACTOR, $long / COORD_FACTOR,
$feature_name ? $feature_name : '<empty>');
usleep(rand(300000, 800000));
yield $point;
}
};
// $points_iter is an iterator simulating client streaming
list($route_summary, $status) =
$client->RecordRoute($points_iter($db))->wait();
print sprintf("Finished trip with %d points\nPassed %d features\n".
"Travelled %d meters\nIt took %d seconds\n",
$route_summary->getPointCount(),
$route_summary->getFeatureCount(),
$route_summary->getDistance(),
$route_summary->getElapsedTime());
}
/**
* Run the routeChat demo. Send some chat messages, and print any chat
* messages that are sent from the server.
*/
function runRouteChat() {
print "Running RouteChat...\n";
global $client;
// start the bidirectional streaming call
$call = $client->RouteChat();
$notes = array(
array(1, 1, 'first message'),
array(1, 2, 'second message'),
array(2, 1, 'third message'),
array(1, 1, 'fourth message'),
array(1, 1, 'fifth message'),
);
foreach ($notes as $n) {
$point = new examples\Point();
$point->setLatitude($lat = $n[0]);
$point->setLongitude($long = $n[1]);
$route_note = new examples\RouteNote();
$route_note->setLocation($point);
$route_note->setMessage($message = $n[2]);
print sprintf("Sending message: '%s' at (%d, %d)\n",
$message, $lat, $long);
// send a bunch of messages to the server
$call->write($route_note);
}
$call->writesDone();
// read from the server until there's no more
while ($route_note_reply = $call->read()) {
print sprintf("Previous left message at (%d, %d): '%s'\n",
$route_note_reply->getLocation()->getLatitude(),
$route_note_reply->getLocation()->getLongitude(),
$route_note_reply->getMessage());
}
}
/**
* Run all of the demos in order
*/
function main() {
runGetFeature();
runListFeatures();
runRecordRoute();
runRouteChat();
}
if (empty($argv[1])) {
print "Usage: php -d extension=grpc.so route_guide_client.php " .
"<path to route_guide_db.json>\n";
exit(1);
}
main();

@ -0,0 +1,36 @@
#!/bin/bash
# Copyright 2015, Google Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following disclaimer
# in the documentation and/or other materials provided with the
# distribution.
# * Neither the name of Google Inc. nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
set -e
cd $(dirname $0)
command -v brew >/dev/null 2>&1 && \
extension_dir="-d extension_dir="`brew --prefix`/opt/grpc-php
php $extension_dir -d extension=grpc.so \
route_guide_client.php ../../node/route_guide/route_guide_db.json

@ -30,18 +30,6 @@
set -e
cd $(dirname $0)
which brew >> /dev/null || { echo 'please make sure brew is in the PATH'; exit 1; }
default_extension_dir=`php -i 2>/dev/null | grep extension_dir | sed 's/.*=> //g'`
brew_home=`brew --prefix`
module_dir=$brew_home/opt/grpc-php
# sym-link in system supplied extensions
for f in $default_extension_dir/*.so
do
ln -s $f $module_dir/$(basename $f) &> /dev/null || true
done
php \
-d extension_dir=$module_dir \
-d extension=grpc.so \
greeter_client.php
command -v brew >/dev/null 2>&1 && \
extension_dir="-d extension_dir="`brew --prefix`/opt/grpc-php
php $extension_dir -d extension=grpc.so greeter_client.php $1

Loading…
Cancel
Save