Home | History | Annotate | Download | only in route_guide
      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 
     20 // php:generate protoc --proto_path=./../../protos  --php_out=./  --grpc_out=./ --plugin=protoc-gen-grpc=./../../../bins/opt/grpc_php_plugin ./../../protos/route_guide.proto
     21 
     22 require dirname(__FILE__).'/../vendor/autoload.php';
     23 
     24 define('COORD_FACTOR', 1e7);
     25 
     26 $client = new Routeguide\RouteGuideClient('localhost:50051', [
     27     'credentials' => Grpc\ChannelCredentials::createInsecure(),
     28 ]);
     29 
     30 function printFeature($feature)
     31 {
     32     $name = $feature->getName();
     33     if (!$name) {
     34         $name_str = 'no feature';
     35     } else {
     36         $name_str = "feature called $name";
     37     }
     38     echo sprintf(
     39         "Found %s \n  at %f, %f\n",
     40         $name_str,
     41                  $feature->getLocation()->getLatitude() / COORD_FACTOR,
     42                  $feature->getLocation()->getLongitude() / COORD_FACTOR
     43     );
     44 }
     45 
     46 /**
     47  * Run the getFeature demo. Calls getFeature with a point known to have a
     48  * feature and a point known not to have a feature.
     49  */
     50 function runGetFeature()
     51 {
     52     echo "Running GetFeature...\n";
     53     global $client;
     54 
     55     $point = new Routeguide\Point();
     56     $points = array(
     57         array(409146138, -746188906),
     58         array(0, 0),
     59     );
     60 
     61     foreach ($points as $p) {
     62         $point->setLatitude($p[0]);
     63         $point->setLongitude($p[1]);
     64         // make a unary grpc call
     65         list($feature, $status) = $client->GetFeature($point)->wait();
     66         printFeature($feature);
     67     }
     68 }
     69 
     70 /**
     71  * Run the listFeatures demo. Calls listFeatures with a rectangle
     72  * containing all of the features in the pre-generated
     73  * database. Prints each response as it comes in.
     74  */
     75 function runListFeatures()
     76 {
     77     echo "Running ListFeatures...\n";
     78     global $client;
     79 
     80     $lo_point = new Routeguide\Point();
     81     $hi_point = new Routeguide\Point();
     82 
     83     $lo_point->setLatitude(400000000);
     84     $lo_point->setLongitude(-750000000);
     85     $hi_point->setLatitude(420000000);
     86     $hi_point->setLongitude(-730000000);
     87 
     88     $rectangle = new Routeguide\Rectangle();
     89     $rectangle->setLo($lo_point);
     90     $rectangle->setHi($hi_point);
     91 
     92     // start the server streaming call
     93     $call = $client->ListFeatures($rectangle);
     94     // an iterator over the server streaming responses
     95     $features = $call->responses();
     96     foreach ($features as $feature) {
     97         printFeature($feature);
     98     }
     99 }
    100 
    101 /**
    102  * Run the recordRoute demo. Sends several randomly chosen points from the
    103  * pre-generated feature database with a variable delay in between. Prints
    104  * the statistics when they are sent from the server.
    105  */
    106 function runRecordRoute()
    107 {
    108     echo "Running RecordRoute...\n";
    109     global $client, $argv;
    110 
    111     // start the client streaming call
    112     $call = $client->RecordRoute();
    113 
    114     $db = json_decode(file_get_contents($argv[1]), true);
    115     $num_points_in_db = count($db);
    116     $num_points = 10;
    117     for ($i = 0; $i < $num_points; ++$i) {
    118         $point = new Routeguide\Point();
    119         $index = rand(0, $num_points_in_db - 1);
    120         $lat = $db[$index]['location']['latitude'];
    121         $long = $db[$index]['location']['longitude'];
    122         $feature_name = $db[$index]['name'];
    123         $point->setLatitude($lat);
    124         $point->setLongitude($long);
    125         echo sprintf(
    126             "Visiting point %f, %f,\n  with feature name: %s\n",
    127                      $lat / COORD_FACTOR,
    128             $long / COORD_FACTOR,
    129                      $feature_name ? $feature_name : '<empty>'
    130         );
    131         usleep(rand(300000, 800000));
    132         $call->write($point);
    133     }
    134     list($route_summary, $status) = $call->wait();
    135     echo sprintf(
    136         "Finished trip with %d points\nPassed %d features\n".
    137                  "Travelled %d meters\nIt took %d seconds\n",
    138                  $route_summary->getPointCount(),
    139                  $route_summary->getFeatureCount(),
    140                  $route_summary->getDistance(),
    141                  $route_summary->getElapsedTime()
    142     );
    143 }
    144 
    145 /**
    146  * Run the routeChat demo. Send some chat messages, and print any chat
    147  * messages that are sent from the server.
    148  */
    149 function runRouteChat()
    150 {
    151     echo "Running RouteChat...\n";
    152     global $client;
    153 
    154     // start the bidirectional streaming call
    155     $call = $client->RouteChat();
    156 
    157     $notes = array(
    158         array(1, 1, 'first message'),
    159         array(1, 2, 'second message'),
    160         array(2, 1, 'third message'),
    161         array(1, 1, 'fourth message'),
    162         array(1, 1, 'fifth message'),
    163     );
    164 
    165     foreach ($notes as $n) {
    166         $point = new Routeguide\Point();
    167         $point->setLatitude($lat = $n[0]);
    168         $point->setLongitude($long = $n[1]);
    169 
    170         $route_note = new Routeguide\RouteNote();
    171         $route_note->setLocation($point);
    172         $route_note->setMessage($message = $n[2]);
    173 
    174         echo sprintf(
    175             "Sending message: '%s' at (%d, %d)\n",
    176                      $message,
    177             $lat,
    178             $long
    179         );
    180         // send a bunch of messages to the server
    181         $call->write($route_note);
    182     }
    183     $call->writesDone();
    184 
    185     // read from the server until there's no more
    186     while ($route_note_reply = $call->read()) {
    187         echo sprintf(
    188             "Previous left message at (%d, %d): '%s'\n",
    189                      $route_note_reply->getLocation()->getLatitude(),
    190                      $route_note_reply->getLocation()->getLongitude(),
    191                      $route_note_reply->getMessage()
    192         );
    193     }
    194 }
    195 
    196 /**
    197  * Run all of the demos in order.
    198  */
    199 function main()
    200 {
    201     runGetFeature();
    202     runListFeatures();
    203     runRecordRoute();
    204     runRouteChat();
    205 }
    206 
    207 if (empty($argv[1])) {
    208     echo 'Usage: php -d extension=grpc.so route_guide_client.php '.
    209         "<path to route_guide_db.json>\n";
    210     exit(1);
    211 }
    212 main();
    213