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

Side by Side Diff: third_party/grpc/examples/node/route_guide/route_guide_client.js

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 /*
2 *
3 * Copyright 2015-2016, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34 var PROTO_PATH = __dirname + '/../../protos/route_guide.proto';
35
36 var async = require('async');
37 var fs = require('fs');
38 var parseArgs = require('minimist');
39 var path = require('path');
40 var _ = require('lodash');
41 var grpc = require('grpc');
42 var routeguide = grpc.load(PROTO_PATH).routeguide;
43 var client = new routeguide.RouteGuide('localhost:50051',
44 grpc.credentials.createInsecure());
45
46 var COORD_FACTOR = 1e7;
47
48 /**
49 * Run the getFeature demo. Calls getFeature with a point known to have a
50 * feature and a point known not to have a feature.
51 * @param {function} callback Called when this demo is complete
52 */
53 function runGetFeature(callback) {
54 var next = _.after(2, callback);
55 function featureCallback(error, feature) {
56 if (error) {
57 callback(error);
58 }
59 if (feature.name === '') {
60 console.log('Found no feature at ' +
61 feature.location.latitude/COORD_FACTOR + ', ' +
62 feature.location.longitude/COORD_FACTOR);
63 } else {
64 console.log('Found feature called "' + feature.name + '" at ' +
65 feature.location.latitude/COORD_FACTOR + ', ' +
66 feature.location.longitude/COORD_FACTOR);
67 }
68 next();
69 }
70 var point1 = {
71 latitude: 409146138,
72 longitude: -746188906
73 };
74 var point2 = {
75 latitude: 0,
76 longitude: 0
77 };
78 client.getFeature(point1, featureCallback);
79 client.getFeature(point2, featureCallback);
80 }
81
82 /**
83 * Run the listFeatures demo. Calls listFeatures with a rectangle containing all
84 * of the features in the pre-generated database. Prints each response as it
85 * comes in.
86 * @param {function} callback Called when this demo is complete
87 */
88 function runListFeatures(callback) {
89 var rectangle = {
90 lo: {
91 latitude: 400000000,
92 longitude: -750000000
93 },
94 hi: {
95 latitude: 420000000,
96 longitude: -730000000
97 }
98 };
99 console.log('Looking for features between 40, -75 and 42, -73');
100 var call = client.listFeatures(rectangle);
101 call.on('data', function(feature) {
102 console.log('Found feature called "' + feature.name + '" at ' +
103 feature.location.latitude/COORD_FACTOR + ', ' +
104 feature.location.longitude/COORD_FACTOR);
105 });
106 call.on('end', callback);
107 }
108
109 /**
110 * Run the recordRoute demo. Sends several randomly chosen points from the
111 * pre-generated feature database with a variable delay in between. Prints the
112 * statistics when they are sent from the server.
113 * @param {function} callback Called when this demo is complete
114 */
115 function runRecordRoute(callback) {
116 var argv = parseArgs(process.argv, {
117 string: 'db_path'
118 });
119 fs.readFile(path.resolve(argv.db_path), function(err, data) {
120 if (err) callback(err);
121 var feature_list = JSON.parse(data);
122
123 var num_points = 10;
124 var call = client.recordRoute(function(error, stats) {
125 if (error) {
126 callback(error);
127 }
128 console.log('Finished trip with', stats.point_count, 'points');
129 console.log('Passed', stats.feature_count, 'features');
130 console.log('Travelled', stats.distance, 'meters');
131 console.log('It took', stats.elapsed_time, 'seconds');
132 callback();
133 });
134 /**
135 * Constructs a function that asynchronously sends the given point and then
136 * delays sending its callback
137 * @param {number} lat The latitude to send
138 * @param {number} lng The longitude to send
139 * @return {function(function)} The function that sends the point
140 */
141 function pointSender(lat, lng) {
142 /**
143 * Sends the point, then calls the callback after a delay
144 * @param {function} callback Called when complete
145 */
146 return function(callback) {
147 console.log('Visiting point ' + lat/COORD_FACTOR + ', ' +
148 lng/COORD_FACTOR);
149 call.write({
150 latitude: lat,
151 longitude: lng
152 });
153 _.delay(callback, _.random(500, 1500));
154 };
155 }
156 var point_senders = [];
157 for (var i = 0; i < num_points; i++) {
158 var rand_point = feature_list[_.random(0, feature_list.length - 1)];
159 point_senders[i] = pointSender(rand_point.location.latitude,
160 rand_point.location.longitude);
161 }
162 async.series(point_senders, function() {
163 call.end();
164 });
165 });
166 }
167
168 /**
169 * Run the routeChat demo. Send some chat messages, and print any chat messages
170 * that are sent from the server.
171 * @param {function} callback Called when the demo is complete
172 */
173 function runRouteChat(callback) {
174 var call = client.routeChat();
175 call.on('data', function(note) {
176 console.log('Got message "' + note.message + '" at ' +
177 note.location.latitude + ', ' + note.location.longitude);
178 });
179
180 call.on('end', callback);
181
182 var notes = [{
183 location: {
184 latitude: 0,
185 longitude: 0
186 },
187 message: 'First message'
188 }, {
189 location: {
190 latitude: 0,
191 longitude: 1
192 },
193 message: 'Second message'
194 }, {
195 location: {
196 latitude: 1,
197 longitude: 0
198 },
199 message: 'Third message'
200 }, {
201 location: {
202 latitude: 0,
203 longitude: 0
204 },
205 message: 'Fourth message'
206 }];
207 for (var i = 0; i < notes.length; i++) {
208 var note = notes[i];
209 console.log('Sending message "' + note.message + '" at ' +
210 note.location.latitude + ', ' + note.location.longitude);
211 call.write(note);
212 }
213 call.end();
214 }
215
216 /**
217 * Run all of the demos in order
218 */
219 function main() {
220 async.series([
221 runGetFeature,
222 runListFeatures,
223 runRecordRoute,
224 runRouteChat
225 ]);
226 }
227
228 if (require.main === module) {
229 main();
230 }
231
232 exports.runGetFeature = runGetFeature;
233
234 exports.runListFeatures = runListFeatures;
235
236 exports.runRecordRoute = runRecordRoute;
237
238 exports.runRouteChat = runRouteChat;
OLDNEW
« no previous file with comments | « third_party/grpc/examples/node/route_guide/README.md ('k') | third_party/grpc/examples/node/route_guide/route_guide_db.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698