route_guide_client.rb 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/usr/bin/env ruby
  2. # Copyright 2015 gRPC authors.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. # Sample app that connects to a Route Guide service.
  16. #
  17. # Usage: $ path/to/route_guide_client.rb path/to/route_guide_db.json &
  18. this_dir = File.expand_path(File.dirname(__FILE__))
  19. lib_dir = File.join(File.dirname(this_dir), 'lib')
  20. $LOAD_PATH.unshift(lib_dir) unless $LOAD_PATH.include?(lib_dir)
  21. require 'grpc'
  22. require 'multi_json'
  23. require 'route_guide_services_pb'
  24. include Routeguide
  25. GET_FEATURE_POINTS = [
  26. Point.new(latitude: 409_146_138, longitude: -746_188_906),
  27. Point.new(latitude: 0, longitude: 0)
  28. ]
  29. # runs a GetFeature rpc.
  30. #
  31. # - once with a point known to be present in the sample route database
  32. # - once with a point that is not in the sample database
  33. def run_get_feature(stub)
  34. p 'GetFeature'
  35. p '----------'
  36. GET_FEATURE_POINTS.each do |pt|
  37. resp = stub.get_feature(pt)
  38. if resp.name != ''
  39. p "- found '#{resp.name}' at #{pt.inspect}"
  40. else
  41. p "- found nothing at #{pt.inspect}"
  42. end
  43. end
  44. end
  45. LIST_FEATURES_RECT = Rectangle.new(
  46. lo: Point.new(latitude: 400_000_000, longitude: -750_000_000),
  47. hi: Point.new(latitude: 420_000_000, longitude: -730_000_000))
  48. # runs a ListFeatures rpc.
  49. #
  50. # - the rectangle to chosen to include most of the known features
  51. # in the sample db.
  52. def run_list_features(stub)
  53. p 'ListFeatures'
  54. p '------------'
  55. resps = stub.list_features(LIST_FEATURES_RECT)
  56. resps.each do |r|
  57. p "- found '#{r.name}' at #{r.location.inspect}"
  58. end
  59. end
  60. # RandomRoute provides an Enumerable that yields a random 'route' of points
  61. # from a list of Features.
  62. class RandomRoute
  63. def initialize(features, size)
  64. @features = features
  65. @size = size
  66. end
  67. # yields a point, waiting between 0 and 1 seconds between each yield
  68. #
  69. # @return an Enumerable that yields a random point
  70. def each
  71. return enum_for(:each) unless block_given?
  72. @size.times do
  73. json_feature = @features[rand(0..@features.length)]
  74. next if json_feature.nil?
  75. location = json_feature['location']
  76. pt = Point.new(
  77. Hash[location.each_pair.map { |k, v| [k.to_sym, v] }])
  78. p "- next point is #{pt.inspect}"
  79. yield pt
  80. sleep(rand(0..1))
  81. end
  82. end
  83. end
  84. # runs a RecordRoute rpc.
  85. #
  86. # - the rectangle to chosen to include most of the known features
  87. # in the sample db.
  88. def run_record_route(stub, features)
  89. p 'RecordRoute'
  90. p '-----------'
  91. points_on_route = 10 # arbitrary
  92. reqs = RandomRoute.new(features, points_on_route)
  93. resp = stub.record_route(reqs.each)
  94. p "summary: #{resp.inspect}"
  95. end
  96. ROUTE_CHAT_NOTES = [
  97. RouteNote.new(message: 'doh - a deer',
  98. location: Point.new(latitude: 0, longitude: 0)),
  99. RouteNote.new(message: 'ray - a drop of golden sun',
  100. location: Point.new(latitude: 0, longitude: 1)),
  101. RouteNote.new(message: 'me - the name I call myself',
  102. location: Point.new(latitude: 1, longitude: 0)),
  103. RouteNote.new(message: 'fa - a longer way to run',
  104. location: Point.new(latitude: 1, longitude: 1)),
  105. RouteNote.new(message: 'soh - with needle and a thread',
  106. location: Point.new(latitude: 0, longitude: 1))
  107. ]
  108. # runs a RouteChat rpc.
  109. #
  110. # sends a canned set of route notes and prints out the responses.
  111. def run_route_chat(stub)
  112. p 'Route Chat'
  113. p '----------'
  114. sleeping_enumerator = SleepingEnumerator.new(ROUTE_CHAT_NOTES, 1)
  115. stub.route_chat(sleeping_enumerator.each_item) { |r| p "received #{r.inspect}" }
  116. end
  117. # SleepingEnumerator yields through items, and sleeps between each one
  118. class SleepingEnumerator
  119. def initialize(items, delay)
  120. @items = items
  121. @delay = delay
  122. end
  123. def each_item
  124. return enum_for(:each_item) unless block_given?
  125. @items.each do |item|
  126. sleep @delay
  127. p "next item to send is #{item.inspect}"
  128. yield item
  129. end
  130. end
  131. end
  132. def main
  133. stub = RouteGuide::Stub.new('localhost:50051', :this_channel_is_insecure)
  134. run_get_feature(stub)
  135. run_list_features(stub)
  136. run_route_chat(stub)
  137. if ARGV.length == 0
  138. p 'no feature database; skipping record_route'
  139. exit
  140. end
  141. raw_data = []
  142. File.open(ARGV[0]) do |f|
  143. raw_data = MultiJson.load(f.read)
  144. end
  145. run_record_route(stub, raw_data)
  146. end
  147. main