route_guide_client.js 7.1 KB

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