class Api::HandlerBase

Attributes

handle_duration_max[R]
handle_duration_min[R]
handle_duration_total[R]
requests_count[R]

Properties

Public Class Methods

new(options = nil) click to toggle source

default constructor

# File lib/mrpin/api/base/handler_base.rb, line 18
def initialize(options = nil)
  @stats_locker = Mutex.new

  reset_handler_stats

  @logger = AppInfo.instance.logger
end

Public Instance Methods

before_handle() click to toggle source
# File lib/mrpin/api/base/handler_base.rb, line 27
def before_handle
end
handle_request(session, request) click to toggle source
# File lib/mrpin/api/base/handler_base.rb, line 31
def handle_request(session, request)
end
post_handle(handle_start_at) click to toggle source
# File lib/mrpin/api/base/handler_base.rb, line 35
def post_handle(handle_start_at)
  return if handle_start_at.nil?

  handle_duration = ((Time.now - handle_start_at).to_f * 1000).to_i

  @stats_locker.synchronize do
    @requests_count        += 1
    @handle_duration_total += handle_duration

    @handle_duration_min = [@handle_duration_min, handle_duration].min
    @handle_duration_max = [@handle_duration_max, handle_duration].max
  end
end
reset_handler_stats() click to toggle source
# File lib/mrpin/api/base/handler_base.rb, line 50
def reset_handler_stats
  @stats_locker.synchronize do
    @requests_count        = 0
    @handle_duration_total = 0

    @handle_duration_max = 0
    @handle_duration_min = 2_147_483_647
  end
end

Protected Instance Methods

player_is_nil?(session, request_id) click to toggle source
# File lib/mrpin/api/base/handler_base.rb, line 61
def player_is_nil?(session, request_id)
  result = session.player.nil?

  if result
    session.send_response_error(request_id)

    AppInfo.instance.on_server_error("player is nil in handler #{self.class}")
  end

  result
end
send_response_ok_unless_error(session, request_id, error = nil) click to toggle source
# File lib/mrpin/api/base/handler_base.rb, line 74
def send_response_ok_unless_error(session, request_id, error = nil)
  if error.nil?
    response = Api::ResponseOk.new(request_id)
  else
    response             = Api::ResponseError.new(request_id)
    response.description = error
  end

  session.send_response(response)
end