route_guide_client.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /*
  3. *
  4. * Copyright 2015 gRPC authors.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. // php:generate protoc --proto_path=./../../protos --php_out=./ --grpc_out=./ --plugin=protoc-gen-grpc=./../../../bins/opt/grpc_php_plugin ./../../protos/route_guide.proto
  20. require dirname(__FILE__).'/../vendor/autoload.php';
  21. define('COORD_FACTOR', 1e7);
  22. $client = new Routeguide\RouteGuideClient('localhost:50051', [
  23. 'credentials' => Grpc\ChannelCredentials::createInsecure(),
  24. ]);
  25. function printFeature($feature)
  26. {
  27. $name = $feature->getName();
  28. if (!$name) {
  29. $name_str = 'no feature';
  30. } else {
  31. $name_str = "feature called $name";
  32. }
  33. echo sprintf(
  34. "Found %s \n at %f, %f\n",
  35. $name_str,
  36. $feature->getLocation()->getLatitude() / COORD_FACTOR,
  37. $feature->getLocation()->getLongitude() / COORD_FACTOR
  38. );
  39. }
  40. /**
  41. * Run the getFeature demo. Calls getFeature with a point known to have a
  42. * feature and a point known not to have a feature.
  43. */
  44. function runGetFeature()
  45. {
  46. echo "Running GetFeature...\n";
  47. global $client;
  48. $point = new Routeguide\Point();
  49. $points = array(
  50. array(409146138, -746188906),
  51. array(0, 0),
  52. );
  53. foreach ($points as $p) {
  54. $point->setLatitude($p[0]);
  55. $point->setLongitude($p[1]);
  56. // make a unary grpc call
  57. list($feature, $status) = $client->GetFeature($point)->wait();
  58. printFeature($feature);
  59. }
  60. }
  61. /**
  62. * Run the listFeatures demo. Calls listFeatures with a rectangle
  63. * containing all of the features in the pre-generated
  64. * database. Prints each response as it comes in.
  65. */
  66. function runListFeatures()
  67. {
  68. echo "Running ListFeatures...\n";
  69. global $client;
  70. $lo_point = new Routeguide\Point();
  71. $hi_point = new Routeguide\Point();
  72. $lo_point->setLatitude(400000000);
  73. $lo_point->setLongitude(-750000000);
  74. $hi_point->setLatitude(420000000);
  75. $hi_point->setLongitude(-730000000);
  76. $rectangle = new Routeguide\Rectangle();
  77. $rectangle->setLo($lo_point);
  78. $rectangle->setHi($hi_point);
  79. // start the server streaming call
  80. $call = $client->ListFeatures($rectangle);
  81. // an iterator over the server streaming responses
  82. $features = $call->responses();
  83. foreach ($features as $feature) {
  84. printFeature($feature);
  85. }
  86. }
  87. /**
  88. * Run the recordRoute demo. Sends several randomly chosen points from the
  89. * pre-generated feature database with a variable delay in between. Prints
  90. * the statistics when they are sent from the server.
  91. */
  92. function runRecordRoute()
  93. {
  94. echo "Running RecordRoute...\n";
  95. global $client, $argv;
  96. // start the client streaming call
  97. $call = $client->RecordRoute();
  98. $db = json_decode(file_get_contents($argv[1]), true);
  99. $num_points_in_db = count($db);
  100. $num_points = 10;
  101. for ($i = 0; $i < $num_points; ++$i) {
  102. $point = new Routeguide\Point();
  103. $index = rand(0, $num_points_in_db - 1);
  104. $lat = $db[$index]['location']['latitude'];
  105. $long = $db[$index]['location']['longitude'];
  106. $feature_name = $db[$index]['name'];
  107. $point->setLatitude($lat);
  108. $point->setLongitude($long);
  109. echo sprintf(
  110. "Visiting point %f, %f,\n with feature name: %s\n",
  111. $lat / COORD_FACTOR,
  112. $long / COORD_FACTOR,
  113. $feature_name ? $feature_name : '<empty>'
  114. );
  115. usleep(rand(300000, 800000));
  116. $call->write($point);
  117. }
  118. list($route_summary, $status) = $call->wait();
  119. echo sprintf(
  120. "Finished trip with %d points\nPassed %d features\n".
  121. "Travelled %d meters\nIt took %d seconds\n",
  122. $route_summary->getPointCount(),
  123. $route_summary->getFeatureCount(),
  124. $route_summary->getDistance(),
  125. $route_summary->getElapsedTime()
  126. );
  127. }
  128. /**
  129. * Run the routeChat demo. Send some chat messages, and print any chat
  130. * messages that are sent from the server.
  131. */
  132. function runRouteChat()
  133. {
  134. echo "Running RouteChat...\n";
  135. global $client;
  136. // start the bidirectional streaming call
  137. $call = $client->RouteChat();
  138. $notes = array(
  139. array(1, 1, 'first message'),
  140. array(1, 2, 'second message'),
  141. array(2, 1, 'third message'),
  142. array(1, 1, 'fourth message'),
  143. array(1, 1, 'fifth message'),
  144. );
  145. foreach ($notes as $n) {
  146. $point = new Routeguide\Point();
  147. $point->setLatitude($lat = $n[0]);
  148. $point->setLongitude($long = $n[1]);
  149. $route_note = new Routeguide\RouteNote();
  150. $route_note->setLocation($point);
  151. $route_note->setMessage($message = $n[2]);
  152. echo sprintf(
  153. "Sending message: '%s' at (%d, %d)\n",
  154. $message,
  155. $lat,
  156. $long
  157. );
  158. // send a bunch of messages to the server
  159. $call->write($route_note);
  160. }
  161. $call->writesDone();
  162. // read from the server until there's no more
  163. while ($route_note_reply = $call->read()) {
  164. echo sprintf(
  165. "Previous left message at (%d, %d): '%s'\n",
  166. $route_note_reply->getLocation()->getLatitude(),
  167. $route_note_reply->getLocation()->getLongitude(),
  168. $route_note_reply->getMessage()
  169. );
  170. }
  171. }
  172. /**
  173. * Run all of the demos in order.
  174. */
  175. function main()
  176. {
  177. runGetFeature();
  178. runListFeatures();
  179. runRecordRoute();
  180. runRouteChat();
  181. }
  182. if (empty($argv[1])) {
  183. echo 'Usage: php -d extension=grpc.so route_guide_client.php '.
  184. "<path to route_guide_db.json>\n";
  185. exit(1);
  186. }
  187. main();