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