Class: Bitcoin::RPC::HttpServer

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

Overview

Bitcoinrb 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.



15
16
17
18
# File 'lib/bitcoin/rpc/http_server.rb', line 15

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

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



13
14
15
# File 'lib/bitcoin/rpc/http_server.rb', line 13

def logger
  @logger
end

#nodeObject (readonly)

Returns the value of attribute node.



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

def node
  @node
end

Class Method Details

.run(node, port = 8332) ⇒ Object



25
26
27
# File 'lib/bitcoin/rpc/http_server.rb', line 25

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



57
58
59
60
# File 'lib/bitcoin/rpc/http_server.rb', line 57

def parse_json_params
  params = JSON.parse(@http_post_content)
  [params['method'], params['params']]
end

#post_initObject



20
21
22
23
# File 'lib/bitcoin/rpc/http_server.rb', line 20

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

#process_http_requestObject

process http request.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/bitcoin/rpc/http_server.rb', line 30

def process_http_request
  operation = proc {
    command, args = parse_json_params
    logger.debug("process http request. command = #{command}")
    begin
      send(command, *args).to_json
    rescue Exception => e
      e
    end
  }
  callback = proc{ |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
  }
  EM.defer(operation, callback)
end