Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(304)

Side by Side Diff: third_party/grpc/examples/php/route_guide/route_guide_client.php

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 <?php
2 /*
3 *
4 * Copyright 2015, Google Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following disclaimer
15 * in the documentation and/or other materials provided with the
16 * distribution.
17 * * Neither the name of Google Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35 require dirname(__FILE__) . '/../vendor/autoload.php';
36 require dirname(__FILE__) . '/route_guide.php';
37
38 define('COORD_FACTOR', 1e7);
39
40 $client = new routeguide\RouteGuideClient('localhost:50051', []);
41
42 function printFeature($feature) {
43 $name = $feature->getName();
44 if (!$name) {
45 $name_str = "no feature";
46 } else {
47 $name_str = "feature called $name";
48 }
49 print sprintf("Found %s \n at %f, %f\n", $name_str,
50 $feature->getLocation()->getLatitude() / COORD_FACTOR,
51 $feature->getLocation()->getLongitude() / COORD_FACTOR);
52 }
53
54 /**
55 * Run the getFeature demo. Calls getFeature with a point known to have a
56 * feature and a point known not to have a feature.
57 */
58 function runGetFeature() {
59 print "Running GetFeature...\n";
60 global $client;
61
62 $point = new routeguide\Point();
63 $points = array(
64 array(409146138, -746188906),
65 array(0, 0),
66 );
67
68 foreach ($points as $p) {
69 $point->setLatitude($p[0]);
70 $point->setLongitude($p[1]);
71 // make a unary grpc call
72 list($feature, $status) = $client->GetFeature($point)->wait();
73 printFeature($feature);
74 }
75 }
76
77 /**
78 * Run the listFeatures demo. Calls listFeatures with a rectangle
79 * containing all of the features in the pre-generated
80 * database. Prints each response as it comes in.
81 */
82 function runListFeatures() {
83 print "Running ListFeatures...\n";
84 global $client;
85
86 $lo_point = new routeguide\Point();
87 $hi_point = new routeguide\Point();
88
89 $lo_point->setLatitude(400000000);
90 $lo_point->setLongitude(-750000000);
91 $hi_point->setLatitude(420000000);
92 $hi_point->setLongitude(-730000000);
93
94 $rectangle = new routeguide\Rectangle();
95 $rectangle->setLo($lo_point);
96 $rectangle->setHi($hi_point);
97
98 // start the server streaming call
99 $call = $client->ListFeatures($rectangle);
100 // an iterator over the server streaming responses
101 $features = $call->responses();
102 foreach ($features as $feature) {
103 printFeature($feature);
104 }
105 }
106
107 /**
108 * Run the recordRoute demo. Sends several randomly chosen points from the
109 * pre-generated feature database with a variable delay in between. Prints
110 * the statistics when they are sent from the server.
111 */
112 function runRecordRoute() {
113 print "Running RecordRoute...\n";
114 global $client, $argv;
115
116 // start the client streaming call
117 $call = $client->RecordRoute();
118
119 $db = json_decode(file_get_contents($argv[1]), true);
120 $num_points_in_db = count($db);
121 $num_points = 10;
122 for ($i = 0; $i < $num_points; $i++) {
123 $point = new routeguide\Point();
124 $index = rand(0, $num_points_in_db - 1);
125 $lat = $db[$index]['location']['latitude'];
126 $long = $db[$index]['location']['longitude'];
127 $feature_name = $db[$index]['name'];
128 $point->setLatitude($lat);
129 $point->setLongitude($long);
130 print sprintf("Visiting point %f, %f,\n with feature name: %s\n",
131 $lat / COORD_FACTOR, $long / COORD_FACTOR,
132 $feature_name ? $feature_name : '<empty>');
133 usleep(rand(300000, 800000));
134 $call->write($point);
135 }
136 list($route_summary, $status) = $call->wait();
137 print sprintf("Finished trip with %d points\nPassed %d features\n".
138 "Travelled %d meters\nIt took %d seconds\n",
139 $route_summary->getPointCount(),
140 $route_summary->getFeatureCount(),
141 $route_summary->getDistance(),
142 $route_summary->getElapsedTime());
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 print "Running RouteChat...\n";
151 global $client;
152
153 // start the bidirectional streaming call
154 $call = $client->RouteChat();
155
156 $notes = array(
157 array(1, 1, 'first message'),
158 array(1, 2, 'second message'),
159 array(2, 1, 'third message'),
160 array(1, 1, 'fourth message'),
161 array(1, 1, 'fifth message'),
162 );
163
164 foreach ($notes as $n) {
165 $point = new routeguide\Point();
166 $point->setLatitude($lat = $n[0]);
167 $point->setLongitude($long = $n[1]);
168
169 $route_note = new routeguide\RouteNote();
170 $route_note->setLocation($point);
171 $route_note->setMessage($message = $n[2]);
172
173 print sprintf("Sending message: '%s' at (%d, %d)\n",
174 $message, $lat, $long);
175 // send a bunch of messages to the server
176 $call->write($route_note);
177 }
178 $call->writesDone();
179
180 // read from the server until there's no more
181 while ($route_note_reply = $call->read()) {
182 print sprintf("Previous left message at (%d, %d): '%s'\n",
183 $route_note_reply->getLocation()->getLatitude(),
184 $route_note_reply->getLocation()->getLongitude(),
185 $route_note_reply->getMessage());
186 }
187 }
188
189 /**
190 * Run all of the demos in order
191 */
192 function main() {
193 runGetFeature();
194 runListFeatures();
195 runRecordRoute();
196 runRouteChat();
197 }
198
199 if (empty($argv[1])) {
200 print "Usage: php -d extension=grpc.so route_guide_client.php " .
201 "<path to route_guide_db.json>\n";
202 exit(1);
203 }
204 main();
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698