3 * Copyright 2015 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 var PROTO_PATH = __dirname + '/../../../protos/route_guide.proto';
21 var async = require('async');
22 var fs = require('fs');
23 var parseArgs = require('minimist');
24 var path = require('path');
25 var _ = require('lodash');
26 var grpc = require('@grpc/grpc-js');
27 var protoLoader = require('@grpc/proto-loader');
28 var packageDefinition = protoLoader.loadSync(
36 var routeguide = grpc.loadPackageDefinition(packageDefinition).routeguide;
37 var client = new routeguide.RouteGuide('localhost:50051',
38 grpc.credentials.createInsecure());
40 var COORD_FACTOR = 1e7;
43 * Run the getFeature demo. Calls getFeature with a point known to have a
44 * feature and a point known not to have a feature.
45 * @param {function} callback Called when this demo is complete
47 function runGetFeature(callback) {
48 var next = _.after(2, callback);
49 function featureCallback(error, feature) {
54 if (feature.name === '') {
55 console.log('Found no feature at ' +
56 feature.location.latitude/COORD_FACTOR + ', ' +
57 feature.location.longitude/COORD_FACTOR);
59 console.log('Found feature called "' + feature.name + '" at ' +
60 feature.location.latitude/COORD_FACTOR + ', ' +
61 feature.location.longitude/COORD_FACTOR);
73 client.getFeature(point1, featureCallback);
74 client.getFeature(point2, featureCallback);
78 * Run the listFeatures demo. Calls listFeatures with a rectangle containing all
79 * of the features in the pre-generated database. Prints each response as it
81 * @param {function} callback Called when this demo is complete
83 function runListFeatures(callback) {
94 console.log('Looking for features between 40, -75 and 42, -73');
95 var call = client.listFeatures(rectangle);
96 call.on('data', function(feature) {
97 console.log('Found feature called "' + feature.name + '" at ' +
98 feature.location.latitude/COORD_FACTOR + ', ' +
99 feature.location.longitude/COORD_FACTOR);
101 call.on('end', callback);
105 * Run the recordRoute demo. Sends several randomly chosen points from the
106 * pre-generated feature database with a variable delay in between. Prints the
107 * statistics when they are sent from the server.
108 * @param {function} callback Called when this demo is complete
110 function runRecordRoute(callback) {
111 var argv = parseArgs(process.argv, {
114 fs.readFile(path.resolve(argv.db_path), function(err, data) {
119 var feature_list = JSON.parse(data);
122 var call = client.recordRoute(function(error, stats) {
127 console.log('Finished trip with', stats.point_count, 'points');
128 console.log('Passed', stats.feature_count, 'features');
129 console.log('Travelled', stats.distance, 'meters');
130 console.log('It took', stats.elapsed_time, 'seconds');
134 * Constructs a function that asynchronously sends the given point and then
135 * delays sending its callback
136 * @param {number} lat The latitude to send
137 * @param {number} lng The longitude to send
138 * @return {function(function)} The function that sends the point
140 function pointSender(lat, lng) {
142 * Sends the point, then calls the callback after a delay
143 * @param {function} callback Called when complete
145 return function(callback) {
146 console.log('Visiting point ' + lat/COORD_FACTOR + ', ' +
152 _.delay(callback, _.random(500, 1500));
155 var point_senders = [];
156 for (var i = 0; i < num_points; i++) {
157 var rand_point = feature_list[_.random(0, feature_list.length - 1)];
158 point_senders[i] = pointSender(rand_point.location.latitude,
159 rand_point.location.longitude);
161 async.series(point_senders, function() {
168 * Run the routeChat demo. Send some chat messages, and print any chat messages
169 * that are sent from the server.
170 * @param {function} callback Called when the demo is complete
172 function runRouteChat(callback) {
173 var call = client.routeChat();
174 call.on('data', function(note) {
175 console.log('Got message "' + note.message + '" at ' +
176 note.location.latitude + ', ' + note.location.longitude);
179 call.on('end', callback);
186 message: 'First message'
192 message: 'Second message'
198 message: 'Third message'
204 message: 'Fourth message'
206 for (var i = 0; i < notes.length; i++) {
208 console.log('Sending message "' + note.message + '" at ' +
209 note.location.latitude + ', ' + note.location.longitude);
216 * Run all of the demos in order
227 if (require.main === module) {
231 exports.runGetFeature = runGetFeature;
233 exports.runListFeatures = runListFeatures;
235 exports.runRecordRoute = runRecordRoute;
237 exports.runRouteChat = runRouteChat;