Class: Tapyrus::RPC::HttpServer

Inherits:
EM::Connection
  • Object
show all
Includes:
EM::HttpServer, RequestHandler
Defined in:
lib/tapyrus/rpc/http_server.rb

Overview

Tapyrusrb RPC server.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RequestHandler

#createwallet, #decoderawtransaction, #decodescript, #encryptwallet, #getblockchaininfo, #getblockheader, #getnewaddress, #getpeerinfo, #getwalletinfo, #listaccounts, #listwallets, #sendrawtransaction, #stop

Constructor Details

#initialize(node) ⇒ HttpServer

Returns a new instance of HttpServer.



14
15
16
17
# File 'lib/tapyrus/rpc/http_server.rb', line 14

def initialize(node)
  @node = node
  @logger = Tapyrus::Logger.create(:debug)
end

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



12
13
14
# File 'lib/tapyrus/rpc/http_server.rb', line 12

def logger
  @logger
end

#nodeObject (readonly)

Returns the value of attribute node.



11
12
13
# File 'lib/tapyrus/rpc/http_server.rb', line 11

def node
  @node
end

Class Method Details

.run(node, port = 8332) ⇒ Object



24
25
26
# 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

Instance Method Details

#parse_json_paramsArray

parse request parameter.

Returns:

  • (Array)

    the array of command and args



58
59
60
61
# 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_initObject



19
20
21
22
# File 'lib/tapyrus/rpc/http_server.rb', line 19

def post_init
  super
  logger.debug "start http server."
end

#process_http_requestObject

process http request.



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
# 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