-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathrouteguide_server.rb
132 lines (104 loc) · 3.57 KB
/
routeguide_server.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# frozen_string_literal: true
$LOAD_PATH.unshift File.expand_path('./examples/routeguide')
require 'griffin'
require 'pry'
require 'json'
require 'routeguide_services_pb'
class Server < Routeguide::RouteGuide::Service
RESOURCE_PATH = './examples/routeguide/routeguide.json'
def initialize # rubocop:disable Lint/MissingSuper
File.open(RESOURCE_PATH) do |f|
features = JSON.parse(f.read)
@features = features.to_h { |x| [x['location'], x['name']] }
end
@route_notes = Hash.new { |h, k| h[k] = [] }
end
def get_feature(point, ctx)
GRPC.logger.info('===== get_feature =====')
name = @features.fetch({ 'longitude' => point.longitude, 'latitude' => point.latitude }, '')
GRPC.logger.info("Point longitude=#{point.longitude}, latitude=#{point.latitude}, metadata=#{ctx.metadata}")
Routeguide::Feature.new(location: point, name: name)
end
def list_features(rect, stream)
GRPC.logger.info('===== list_features =====')
@features.each do |location, name|
if name.nil? || name == '' || !in_range(location, rect)
next
end
pt = Routeguide::Point.new(location)
resp = Routeguide::Feature.new(location: pt, name: name)
GRPC.logger.info(resp)
stream.send_msg(resp)
end
end
def record_route(stream)
GRPC.logger.info('===== record_route =====')
distance = 0
count = 0
features = 0
start_at = Time.now.to_i
last = nil
stream.each do |point|
GRPC.logger.info(point)
count += 1
name = @features.fetch({ 'longitude' => point.longitude, 'latitude' => point.latitude }, '')
unless name == ''
features += 1
end
last = point
distance += calculate_distance(point, last)
end
Routeguide::RouteSummary.new(
point_count: count,
feature_count: features,
distance: distance,
elapsed_time: Time.now.to_i - start_at,
)
end
def route_chat(call)
GRPC.logger.info('===== record_chat =====')
call.each do |rn|
GRPC.logger.info("route_note location=#{rn.location.inspect}, message=#{rn.message}")
key = "#{rn.location.latitude} #{rn.location.longitude}"
saved_msgs = @route_notes[key]
@route_notes[key] << rn.message
saved_msgs.each do |m|
n = Routeguide::RouteNote.new(location: rn.location, message: m)
call.send_msg(n)
end
end
end
private
COORD_FACTOR = 1e7
RADIUS = 637_100
def calculate_distance(point_a, point_b)
lat_a = (point_a.latitude / COORD_FACTOR) * Math::PI / 180
lat_b = (point_b.latitude / COORD_FACTOR) * Math::PI / 180
lon_a = (point_a.longitude / COORD_FACTOR) * Math::PI / 180
lon_b = (point_b.longitude / COORD_FACTOR) * Math::PI / 180
delta_lat = lat_a - lat_b
delta_lon = lon_a - lon_b
a = (Math.sin(delta_lat / 2)**2) + (Math.cos(lat_a) * Math.cos(lat_b)) + (Math.sin(delta_lon / 2)**2)
(2 * RADIUS * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))).to_i
end
def in_range(point, rect)
longitudes = [rect.lo.longitude, rect.hi.longitude]
left = longitudes.min
right = longitudes.max
latitudes = [rect.lo.latitude, rect.hi.latitude]
bottom = latitudes.min
top = latitudes.max
(point['longitude'] >= left) && (point['longitude'] <= right) && (point['latitude'] >= bottom) && (point['latitude'] <= top)
end
end
Griffin::Server.configure do |c|
c.bind '127.0.0.1'
c.port 50051
c.services Server.new
if ENV['GRPC_INTERCEPTOR']
require_relative 'interceptors/server_logging_interceptor'
c.interceptors [LoggingInterceptor.new]
end
c.workers 2
end
Griffin::Server.run