From 5253720853c0bf999149b5fa2cbe122030ec6ed8 Mon Sep 17 00:00:00 2001 From: ZhouyihaiDing Date: Sun, 5 Nov 2017 18:25:54 -0800 Subject: php example --- examples/php/README.md | 3 +- examples/php/greeter_client.php | 10 +- examples/php/greeter_proto_gen.sh | 17 ++ examples/php/helloworld.pb.php | 58 ------ examples/php/helloworld_grpc_pb.php | 49 ----- examples/php/route_guide/route_guide.pb.php | 209 ---------------------- examples/php/route_guide/route_guide_client.php | 43 +++-- examples/php/route_guide/route_guide_grpc_pb.php | 101 ----------- examples/php/route_guide/route_guide_proto_gen.sh | 16 ++ 9 files changed, 68 insertions(+), 438 deletions(-) create mode 100755 examples/php/greeter_proto_gen.sh delete mode 100644 examples/php/helloworld.pb.php delete mode 100644 examples/php/helloworld_grpc_pb.php delete mode 100644 examples/php/route_guide/route_guide.pb.php delete mode 100644 examples/php/route_guide/route_guide_grpc_pb.php create mode 100755 examples/php/route_guide/route_guide_proto_gen.sh (limited to 'examples/php') diff --git a/examples/php/README.md b/examples/php/README.md index d30cbdafd0..49703ce172 100644 --- a/examples/php/README.md +++ b/examples/php/README.md @@ -41,10 +41,11 @@ TRY IT! $ node greeter_server.js ``` - - Run the client + - Generate proto files and run the client ``` $ cd examples/php + $ ./greeter_proto_gen.sh $ ./run_greeter_client.sh ``` diff --git a/examples/php/greeter_client.php b/examples/php/greeter_client.php index 981319a451..bb94a08527 100644 --- a/examples/php/greeter_client.php +++ b/examples/php/greeter_client.php @@ -17,12 +17,14 @@ * */ +// php:generate protoc --proto_path=./../protos --php_out=./ --grpc_out=./ --plugin=protoc-gen-grpc=./../../bins/opt/grpc_php_plugin ./../protos/helloworld.proto + require dirname(__FILE__).'/vendor/autoload.php'; -// The following includes are needed when using protobuf 3.1.0 -// and will suppress warnings when using protobuf 3.2.0+ -@include_once dirname(__FILE__).'/helloworld.pb.php'; -@include_once dirname(__FILE__).'/helloworld_grpc_pb.php'; +@include_once dirname(__FILE__).'/Helloworld/GreeterClient.php'; +@include_once dirname(__FILE__).'/Helloworld/HelloReply.php'; +@include_once dirname(__FILE__).'/Helloworld/HelloRequest.php'; +@include_once dirname(__FILE__).'/GPBMetadata/Helloworld.php'; function greet($name) { diff --git a/examples/php/greeter_proto_gen.sh b/examples/php/greeter_proto_gen.sh new file mode 100755 index 0000000000..a0d7b29c86 --- /dev/null +++ b/examples/php/greeter_proto_gen.sh @@ -0,0 +1,17 @@ +#!/bin/bash +# Copyright 2017 gRPC authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +protoc --proto_path=./../protos --php_out=./ --grpc_out=./ --plugin=protoc-gen-grpc=./../../bins/opt/grpc_php_plugin ./../protos/helloworld.proto + diff --git a/examples/php/helloworld.pb.php b/examples/php/helloworld.pb.php deleted file mode 100644 index 757a4a58f5..0000000000 --- a/examples/php/helloworld.pb.php +++ /dev/null @@ -1,58 +0,0 @@ -name; - } - - public function setName($var) - { - GPBUtil::checkString($var, True); - $this->name = $var; - } - -} - -class HelloReply extends \Google\Protobuf\Internal\Message -{ - private $message = ''; - - public function getMessage() - { - return $this->message; - } - - public function setMessage($var) - { - GPBUtil::checkString($var, True); - $this->message = $var; - } - -} - -$pool = DescriptorPool::getGeneratedPool(); - -$pool->internalAddGeneratedFile(hex2bin( - "0ae6010a1068656c6c6f776f726c642e70726f746f120a68656c6c6f776f" . - "726c64221c0a0c48656c6c6f52657175657374120c0a046e616d65180120" . - "012809221d0a0a48656c6c6f5265706c79120f0a076d6573736167651801" . - "2001280932490a0747726565746572123e0a0853617948656c6c6f12182e" . - "68656c6c6f776f726c642e48656c6c6f526571756573741a162e68656c6c" . - "6f776f726c642e48656c6c6f5265706c79220042360a1b696f2e67727063" . - "2e6578616d706c65732e68656c6c6f776f726c64420f48656c6c6f576f72" . - "6c6450726f746f5001a20203484c57620670726f746f33" -)); - diff --git a/examples/php/helloworld_grpc_pb.php b/examples/php/helloworld_grpc_pb.php deleted file mode 100644 index ba83ed2614..0000000000 --- a/examples/php/helloworld_grpc_pb.php +++ /dev/null @@ -1,49 +0,0 @@ -_simpleRequest('/helloworld.Greeter/SayHello', - $argument, - ['\Helloworld\HelloReply', 'decode'], - $metadata, $options); - } - - } - -} diff --git a/examples/php/route_guide/route_guide.pb.php b/examples/php/route_guide/route_guide.pb.php deleted file mode 100644 index 78f3ea6e2a..0000000000 --- a/examples/php/route_guide/route_guide.pb.php +++ /dev/null @@ -1,209 +0,0 @@ -latitude; - } - - public function setLatitude($var) - { - GPBUtil::checkInt32($var); - $this->latitude = $var; - } - - public function getLongitude() - { - return $this->longitude; - } - - public function setLongitude($var) - { - GPBUtil::checkInt32($var); - $this->longitude = $var; - } - -} - -class Rectangle extends \Google\Protobuf\Internal\Message -{ - private $lo = null; - private $hi = null; - - public function getLo() - { - return $this->lo; - } - - public function setLo(&$var) - { - GPBUtil::checkMessage($var, \Routeguide\Point::class); - $this->lo = $var; - } - - public function getHi() - { - return $this->hi; - } - - public function setHi(&$var) - { - GPBUtil::checkMessage($var, \Routeguide\Point::class); - $this->hi = $var; - } - -} - -class Feature extends \Google\Protobuf\Internal\Message -{ - private $name = ''; - private $location = null; - - public function getName() - { - return $this->name; - } - - public function setName($var) - { - GPBUtil::checkString($var, True); - $this->name = $var; - } - - public function getLocation() - { - return $this->location; - } - - public function setLocation(&$var) - { - GPBUtil::checkMessage($var, \Routeguide\Point::class); - $this->location = $var; - } - -} - -class RouteNote extends \Google\Protobuf\Internal\Message -{ - private $location = null; - private $message = ''; - - public function getLocation() - { - return $this->location; - } - - public function setLocation(&$var) - { - GPBUtil::checkMessage($var, \Routeguide\Point::class); - $this->location = $var; - } - - public function getMessage() - { - return $this->message; - } - - public function setMessage($var) - { - GPBUtil::checkString($var, True); - $this->message = $var; - } - -} - -class RouteSummary extends \Google\Protobuf\Internal\Message -{ - private $point_count = 0; - private $feature_count = 0; - private $distance = 0; - private $elapsed_time = 0; - - public function getPointCount() - { - return $this->point_count; - } - - public function setPointCount($var) - { - GPBUtil::checkInt32($var); - $this->point_count = $var; - } - - public function getFeatureCount() - { - return $this->feature_count; - } - - public function setFeatureCount($var) - { - GPBUtil::checkInt32($var); - $this->feature_count = $var; - } - - public function getDistance() - { - return $this->distance; - } - - public function setDistance($var) - { - GPBUtil::checkInt32($var); - $this->distance = $var; - } - - public function getElapsedTime() - { - return $this->elapsed_time; - } - - public function setElapsedTime($var) - { - GPBUtil::checkInt32($var); - $this->elapsed_time = $var; - } - -} - -$pool = DescriptorPool::getGeneratedPool(); - -$pool->internalAddGeneratedFile(hex2bin( - "0ac5050a11726f7574655f67756964652e70726f746f120a726f75746567" . - "75696465222c0a05506f696e7412100a086c617469747564651801200128" . - "0512110a096c6f6e67697475646518022001280522490a0952656374616e" . - "676c65121d0a026c6f18012001280b32112e726f75746567756964652e50" . - "6f696e74121d0a02686918022001280b32112e726f75746567756964652e" . - "506f696e74223c0a0746656174757265120c0a046e616d65180120012809" . - "12230a086c6f636174696f6e18022001280b32112e726f75746567756964" . - "652e506f696e7422410a09526f7574654e6f746512230a086c6f63617469" . - "6f6e18012001280b32112e726f75746567756964652e506f696e74120f0a" . - "076d65737361676518022001280922620a0c526f75746553756d6d617279" . - "12130a0b706f696e745f636f756e7418012001280512150a0d6665617475" . - "72655f636f756e7418022001280512100a0864697374616e636518032001" . - "280512140a0c656c61707365645f74696d651804200128053285020a0a52" . - "6f757465477569646512360a0a4765744665617475726512112e726f7574" . - "6567756964652e506f696e741a132e726f75746567756964652e46656174" . - "7572652200123e0a0c4c697374466561747572657312152e726f75746567" . - "756964652e52656374616e676c651a132e726f75746567756964652e4665" . - "617475726522003001123e0a0b5265636f7264526f75746512112e726f75" . - "746567756964652e506f696e741a182e726f75746567756964652e526f75" . - "746553756d6d61727922002801123f0a09526f7574654368617412152e72" . - "6f75746567756964652e526f7574654e6f74651a152e726f757465677569" . - "64652e526f7574654e6f746522002801300142360a1b696f2e677270632e" . - "6578616d706c65732e726f7574656775696465420f526f75746547756964" . - "6550726f746f5001a20203525447620670726f746f33" -)); - diff --git a/examples/php/route_guide/route_guide_client.php b/examples/php/route_guide/route_guide_client.php index 8ac797722c..92b2bacecd 100644 --- a/examples/php/route_guide/route_guide_client.php +++ b/examples/php/route_guide/route_guide_client.php @@ -17,12 +17,9 @@ * */ -require dirname(__FILE__).'/../vendor/autoload.php'; +// php:generate protoc --proto_path=./../../protos --php_out=./ --grpc_out=./ --plugin=protoc-gen-grpc=./../../../bins/opt/grpc_php_plugin ./../../protos/route_guide.proto -// The following includes are needed when using protobuf 3.1.0 -// and will suppress warnings when using protobuf 3.2.0+ -@include_once dirname(__FILE__).'/route_guide.pb.php'; -@include_once dirname(__FILE__).'/route_guide_grpc_pb.php'; +require dirname(__FILE__).'/../vendor/autoload.php'; define('COORD_FACTOR', 1e7); @@ -38,9 +35,12 @@ function printFeature($feature) } else { $name_str = "feature called $name"; } - echo sprintf("Found %s \n at %f, %f\n", $name_str, + echo sprintf( + "Found %s \n at %f, %f\n", + $name_str, $feature->getLocation()->getLatitude() / COORD_FACTOR, - $feature->getLocation()->getLongitude() / COORD_FACTOR); + $feature->getLocation()->getLongitude() / COORD_FACTOR + ); } /** @@ -122,19 +122,24 @@ function runRecordRoute() $feature_name = $db[$index]['name']; $point->setLatitude($lat); $point->setLongitude($long); - echo sprintf("Visiting point %f, %f,\n with feature name: %s\n", - $lat / COORD_FACTOR, $long / COORD_FACTOR, - $feature_name ? $feature_name : ''); + echo sprintf( + "Visiting point %f, %f,\n with feature name: %s\n", + $lat / COORD_FACTOR, + $long / COORD_FACTOR, + $feature_name ? $feature_name : '' + ); usleep(rand(300000, 800000)); $call->write($point); } list($route_summary, $status) = $call->wait(); - echo sprintf("Finished trip with %d points\nPassed %d features\n". + echo 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()); + $route_summary->getElapsedTime() + ); } /** @@ -166,8 +171,12 @@ function runRouteChat() $route_note->setLocation($point); $route_note->setMessage($message = $n[2]); - echo sprintf("Sending message: '%s' at (%d, %d)\n", - $message, $lat, $long); + echo sprintf( + "Sending message: '%s' at (%d, %d)\n", + $message, + $lat, + $long + ); // send a bunch of messages to the server $call->write($route_note); } @@ -175,10 +184,12 @@ function runRouteChat() // read from the server until there's no more while ($route_note_reply = $call->read()) { - echo sprintf("Previous left message at (%d, %d): '%s'\n", + echo sprintf( + "Previous left message at (%d, %d): '%s'\n", $route_note_reply->getLocation()->getLatitude(), $route_note_reply->getLocation()->getLongitude(), - $route_note_reply->getMessage()); + $route_note_reply->getMessage() + ); } } diff --git a/examples/php/route_guide/route_guide_grpc_pb.php b/examples/php/route_guide/route_guide_grpc_pb.php deleted file mode 100644 index e7a9cd39cd..0000000000 --- a/examples/php/route_guide/route_guide_grpc_pb.php +++ /dev/null @@ -1,101 +0,0 @@ -_simpleRequest('/routeguide.RouteGuide/GetFeature', - $argument, - ['\Routeguide\Feature', 'decode'], - $metadata, $options); - } - - /** - * 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. - * @param \Routeguide\Rectangle $argument input argument - * @param array $metadata metadata - * @param array $options call options - */ - public function ListFeatures(\Routeguide\Rectangle $argument, - $metadata = [], $options = []) { - return $this->_serverStreamRequest('/routeguide.RouteGuide/ListFeatures', - $argument, - ['\Routeguide\Feature', 'decode'], - $metadata, $options); - } - - /** - * A client-to-server streaming RPC. - * - * Accepts a stream of Points on a route being traversed, returning a - * RouteSummary when traversal is completed. - * @param array $metadata metadata - * @param array $options call options - */ - public function RecordRoute($metadata = [], $options = []) { - return $this->_clientStreamRequest('/routeguide.RouteGuide/RecordRoute', - ['\Routeguide\RouteSummary','decode'], - $metadata, $options); - } - - /** - * 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). - * @param array $metadata metadata - * @param array $options call options - */ - public function RouteChat($metadata = [], $options = []) { - return $this->_bidiRequest('/routeguide.RouteGuide/RouteChat', - ['\Routeguide\RouteNote','decode'], - $metadata, $options); - } - - } - -} diff --git a/examples/php/route_guide/route_guide_proto_gen.sh b/examples/php/route_guide/route_guide_proto_gen.sh new file mode 100755 index 0000000000..bfd7cdb725 --- /dev/null +++ b/examples/php/route_guide/route_guide_proto_gen.sh @@ -0,0 +1,16 @@ +#!/bin/bash +# Copyright 2015 gRPC authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +protoc --proto_path=./../../protos --php_out=./ --grpc_out=./ --plugin=protoc-gen-grpc=./../../../bins/opt/grpc_php_plugin ./../../protos/route_guide.proto -- cgit v1.2.3