class Tapyrus::RPC::HttpServer

Tapyrusrb RPC server.

Attributes

logger[RW]
node[R]

Public Class Methods

new(node) click to toggle source
# File lib/tapyrus/rpc/http_server.rb, line 14
def initialize(node)
  @node = node
  @logger = Tapyrus::Logger.create(:debug)
end
run(node, port = 8332) click to toggle source
# File lib/tapyrus/rpc/http_server.rb, line 24
def self.run(node, port = 8332)
  EM.start_server('0.0.0.0', port, HttpServer, node)
end

Public Instance Methods

parse_json_params() click to toggle source

parse request parameter. @return [Array] the array of command and args

# File lib/tapyrus/rpc/http_server.rb, line 58
def parse_json_params
  params = JSON.parse(@http_post_content)
  [params['method'], params['params']]
end
post_init() click to toggle source
Calls superclass method
# File lib/tapyrus/rpc/http_server.rb, line 19
def post_init
  super
  logger.debug 'start http server.'
end
process_http_request() click to toggle source

process http request.

# File lib/tapyrus/rpc/http_server.rb, line 29
def process_http_request
  operation =
    proc do
      command, args = parse_json_params
      logger.debug("process http request. command = #{command}")
      begin
        send(command, *args).to_json
      rescue Exception => e
        e
      end
    end
  callback =
    proc do |result|
      response = EM::DelegatedHttpResponse.new(self)
      if result.is_a?(Exception)
        response.status = 500
        response.content = result.message
      else
        response.status = 200
        response.content = result
      end
      response.content_type 'application/json'
      response.send_response
    end
  EM.defer(operation, callback)
end