examples/routeguide_server.rb in griffin-0.1.2 vs examples/routeguide_server.rb in griffin-0.1.3

- old
+ new

@@ -1,10 +1,10 @@ # frozen_string_literal: true $LOAD_PATH.unshift File.expand_path('./examples/routeguide') -require 'grpc_kit' +require 'griffin' require 'pry' require 'json' require 'routeguide_services_pb' require 'logger' @@ -15,10 +15,12 @@ @logger = Logger.new(STDOUT) File.open(RESOURCE_PATH) do |f| features = JSON.parse(f.read) @features = Hash[features.map { |x| [x['location'], x['name']] }] end + + @route_notes = Hash.new { |h, k| h[k] = [] } end def get_feature(point, ctx) name = @features.fetch({ 'longitude' => point.longitude, 'latitude' => point.latitude }, '') @logger.info("Point longitude=#{point.longitude}, latitude=#{point.latitude}, metadata=#{ctx.metadata}") @@ -68,10 +70,25 @@ distance: distance, elapsed_time: Time.now.to_i - start_at, ) end + def route_chat(call) + loop do + rn = call.recv + @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 @@ -97,22 +114,29 @@ top = latitudes.max (point['longitude'] >= left) && (point['longitude'] <= right) && (point['latitude'] >= bottom) && (point['latitude'] <= top) end end -sock = TCPServer.new(50051) +# require 'griffin/interceptors/server/payload_interceptor' +require 'griffin/interceptors/server/filtered_payload_interceptor' +require 'griffin/interceptors/server/logging_interceptor' +require 'griffin/interceptors/server/x_request_id_interceptor' -opts = {} +interceptors = [ + Griffin::Interceptors::Server::FilteredPayloadInterceptor.new, + Griffin::Interceptors::Server::LoggingInterceptor.new, + Griffin::Interceptors::Server::XRequestIdInterceptor.new, +] -if ENV['GRPC_INTERCEPTOR'] - require_relative 'interceptors/server_logging_interceptor' - opts[:interceptors] = [LoggingInterceptor.new] -end +Griffin::Server.configure do |c| + c.bind '127.0.0.1' -server = GrpcKit::Server.new(**opts) -server.handle(Server.new) -server.run + c.port 50051 -loop do - conn = sock.accept - server.session_start(conn) + c.services Server.new + + c.interceptors interceptors + + c.workers 2 end + +Griffin::Server.run