route_guide_client.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  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
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  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.
  16. *
  17. */
  18. var PROTO_PATH = __dirname + '/../../../protos/route_guide.proto';
  19. var async = require('async');
  20. var fs = require('fs');
  21. var parseArgs = require('minimist');
  22. var path = require('path');
  23. var _ = require('lodash');
  24. var grpc = require('@grpc/grpc-js');
  25. var protoLoader = require('@grpc/proto-loader');
  26. var packageDefinition = protoLoader.loadSync(
  27. PROTO_PATH,
  28. {keepCase: true,
  29. longs: String,
  30. enums: String,
  31. defaults: true,
  32. oneofs: true
  33. });
  34. var routeguide = grpc.loadPackageDefinition(packageDefinition).routeguide;
  35. var client = new routeguide.RouteGuide('localhost:50051',
  36. grpc.credentials.createInsecure());
  37. var COORD_FACTOR = 1e7;
  38. /**
  39. * Run the getFeature demo. Calls getFeature with a point known to have a
  40. * feature and a point known not to have a feature.
  41. * @param {function} callback Called when this demo is complete
  42. */
  43. function runGetFeature(callback) {
  44. var next = _.after(2, callback);
  45. function featureCallback(error, feature) {
  46. if (error) {
  47. callback(error);
  48. return;
  49. }
  50. if (feature.name === '') {
  51. console.log('Found no feature at ' +
  52. feature.location.latitude/COORD_FACTOR + ', ' +
  53. feature.location.longitude/COORD_FACTOR);
  54. } else {
  55. console.log('Found feature called "' + feature.name + '" at ' +
  56. feature.location.latitude/COORD_FACTOR + ', ' +
  57. feature.location.longitude/COORD_FACTOR);
  58. }
  59. next();
  60. }
  61. var point1 = {
  62. latitude: 409146138,
  63. longitude: -746188906
  64. };
  65. var point2 = {
  66. latitude: 0,
  67. longitude: 0
  68. };
  69. client.getFeature(point1, featureCallback);
  70. client.getFeature(point2, featureCallback);
  71. }
  72. /**
  73. * Run the listFeatures demo. Calls listFeatures with a rectangle containing all
  74. * of the features in the pre-generated database. Prints each response as it
  75. * comes in.
  76. * @param {function} callback Called when this demo is complete
  77. */
  78. function runListFeatures(callback) {
  79. var rectangle = {
  80. lo: {
  81. latitude: 400000000,
  82. longitude: -750000000
  83. },
  84. hi: {
  85. latitude: 420000000,
  86. longitude: -730000000
  87. }
  88. };
  89. console.log('Looking for features between 40, -75 and 42, -73');
  90. var call = client.listFeatures(rectangle);
  91. call.on('data', function(feature) {
  92. console.log('Found feature called "' + feature.name + '" at ' +
  93. feature.location.latitude/COORD_FACTOR + ', ' +
  94. feature.location.longitude/COORD_FACTOR);
  95. });
  96. call.on('end', callback);
  97. }
  98. /**
  99. * Run the recordRoute demo. Sends several randomly chosen points from the
  100. * pre-generated feature database with a variable delay in between. Prints the
  101. * statistics when they are sent from the server.
  102. * @param {function} callback Called when this demo is complete
  103. */
  104. function runRecordRoute(callback) {
  105. var argv = parseArgs(process.argv, {
  106. string: 'db_path'
  107. });
  108. fs.readFile(path.resolve(argv.db_path), function(err, data) {
  109. if (err) {
  110. callback(err);
  111. return;
  112. }
  113. var feature_list = JSON.parse(data);
  114. var num_points = 10;
  115. var call = client.recordRoute(function(error, stats) {
  116. if (error) {
  117. callback(error);
  118. return;
  119. }
  120. console.log('Finished trip with', stats.point_count, 'points');
  121. console.log('Passed', stats.feature_count, 'features');
  122. console.log('Travelled', stats.distance, 'meters');
  123. console.log('It took', stats.elapsed_time, 'seconds');
  124. callback();
  125. });
  126. /**
  127. * Constructs a function that asynchronously sends the given point and then
  128. * delays sending its callback
  129. * @param {number} lat The latitude to send
  130. * @param {number} lng The longitude to send
  131. * @return {function(function)} The function that sends the point
  132. */
  133. function pointSender(lat, lng) {
  134. /**
  135. * Sends the point, then calls the callback after a delay
  136. * @param {function} callback Called when complete
  137. */
  138. return function(callback) {
  139. console.log('Visiting point ' + lat/COORD_FACTOR + ', ' +
  140. lng/COORD_FACTOR);
  141. call.write({
  142. latitude: lat,
  143. longitude: lng
  144. });
  145. _.delay(callback, _.random(500, 1500));
  146. };
  147. }
  148. var point_senders = [];
  149. for (var i = 0; i < num_points; i++) {
  150. var rand_point = feature_list[_.random(0, feature_list.length - 1)];
  151. point_senders[i] = pointSender(rand_point.location.latitude,
  152. rand_point.location.longitude);
  153. }
  154. async.series(point_senders, function() {
  155. call.end();
  156. });
  157. });
  158. }
  159. /**
  160. * Run the routeChat demo. Send some chat messages, and print any chat messages
  161. * that are sent from the server.
  162. * @param {function} callback Called when the demo is complete
  163. */
  164. function runRouteChat(callback) {
  165. var call = client.routeChat();
  166. call.on('data', function(note) {
  167. console.log('Got message "' + note.message + '" at ' +
  168. note.location.latitude + ', ' + note.location.longitude);
  169. });
  170. call.on('end', callback);
  171. var notes = [{
  172. location: {
  173. latitude: 0,
  174. longitude: 0
  175. },
  176. message: 'First message'
  177. }, {
  178. location: {
  179. latitude: 0,
  180. longitude: 1
  181. },
  182. message: 'Second message'
  183. }, {
  184. location: {
  185. latitude: 1,
  186. longitude: 0
  187. },
  188. message: 'Third message'
  189. }, {
  190. location: {
  191. latitude: 0,
  192. longitude: 0
  193. },
  194. message: 'Fourth message'
  195. }];
  196. for (var i = 0; i < notes.length; i++) {
  197. var note = notes[i];
  198. console.log('Sending message "' + note.message + '" at ' +
  199. note.location.latitude + ', ' + note.location.longitude);
  200. call.write(note);
  201. }
  202. call.end();
  203. }
  204. /**
  205. * Run all of the demos in order
  206. */
  207. function main() {
  208. async.series([
  209. runGetFeature,
  210. runListFeatures,
  211. runRecordRoute,
  212. runRouteChat
  213. ]);
  214. }
  215. if (require.main === module) {
  216. main();
  217. }
  218. exports.runGetFeature = runGetFeature;
  219. exports.runListFeatures = runListFeatures;
  220. exports.runRecordRoute = runRecordRoute;
  221. exports.runRouteChat = runRouteChat;