Class: Bitcoin::Network::CommandClient

Inherits:
EM::Connection
  • Object
show all
Defined in:
lib/bitcoin/network/command_client.rb,
lib/bitcoin/gui/connection.rb

Overview

Client to connect to CommandHandler and issue requests or register for events

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port, block, *args) ⇒ CommandClient

create new client connecting to host:port and executing callbacks from block, passing args in.

CommandClient.connect(host, port) do
  on_connected { request("info") }
  on_info {|i| p i}
end


14
15
16
17
18
19
20
21
22
23
24
# File 'lib/bitcoin/network/command_client.rb', line 14

def initialize host, port, block, *args
  @host, @port = host, port
  @args = args
  @callbacks = {}
  @block = block
  instance_eval &block  if block
  @buffer = BufferedTokenizer.new("\x00")
  @connection_attempts = 0
  @requests = {}
  @i = 0
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

register callback methods



87
88
89
90
91
92
93
94
# File 'lib/bitcoin/network/command_client.rb', line 87

def method_missing(name, *args, &block)
  if name =~ /^on_/
    @callbacks[name.to_s.split("on_")[1].to_sym] = block
    log.debug { "callback #{name} registered" }
  else
    super(name, *args)
  end
end

Class Method Details

.connect(host, port, *args, &block) ⇒ Object



30
31
32
# File 'lib/bitcoin/network/command_client.rb', line 30

def self.connect host, port, *args, &block
  client = EM.connect(host, port.to_i, self, host, port.to_i, block, *args)
end

Instance Method Details

#callback(name, *args) ⇒ Object

call the callback specified by name passing in args



79
80
81
82
83
84
# File 'lib/bitcoin/network/command_client.rb', line 79

def callback name, *args
  cb = @callbacks[name.to_sym]
  return  unless cb
  log.debug { "callback: #{name}" }
  cb.call(*args)
end

#gui(&block) ⇒ Object



6
7
8
9
10
# File 'lib/bitcoin/gui/connection.rb', line 6

def gui &block
  EM.next_tick do
    @args[0].instance_eval &block
  end
end

#logObject



26
27
28
# File 'lib/bitcoin/network/command_client.rb', line 26

def log;
  @log ||= Bitcoin::Logger.create(:client)
end

#post_initObject

call connected callback



35
36
37
38
# File 'lib/bitcoin/network/command_client.rb', line 35

def post_init
  log.debug { "Connected" }
  request(:connected) { callback(:connected) }
end

#receive_data(data) ⇒ Object

receive response from server



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/bitcoin/network/command_client.rb', line 63

def receive_data data
  @connection_attempts = 0
  @buffer.extract(data).each do |packet|
    response = JSON.parse(packet)
    log.debug { d = response['result'].inspect
      "response: #{response['method']} #{d[0...50]}#{d.size > 50 ? '...' : ''}" }
    if cb = @requests[response['id']]
      cb.call(response['result'])
    else
      callback(:response, response['method'], response['result'])
      callback(response['method'].to_sym, response['result'])
    end
  end
end

#register_monitor_callbacks(params) ⇒ Object

register callbacks for monitor



97
98
99
100
101
102
# File 'lib/bitcoin/network/command_client.rb', line 97

def register_monitor_callbacks params
  on_monitor do |data|
    next  if data.is_a?(Hash) && data.keys == ["id"]
    callback(params["channel"], data)
  end
end

#request(cmd, params = nil, &block) ⇒ Object

request command cmd with args from the server



52
53
54
55
56
57
58
59
60
# File 'lib/bitcoin/network/command_client.rb', line 52

def request cmd, params = nil, &block
  id = @i += 1
  @requests[id] = block  if block
  log.debug { "request: #{cmd} #{args.inspect}" }
  register_monitor_callbacks(params)  if cmd.to_sym == :monitor
  request = { id: id, method: cmd }
  request[:params] = params  if params
  send_data(request.to_json + "\x00")
end

#unbindObject

call disconnected callback and try to reconnect



41
42
43
44
45
46
47
48
49
# File 'lib/bitcoin/network/command_client.rb', line 41

def unbind
  log.debug { "Disconnected." }
  callback :disconnected
  EM.add_timer(@connection_attempts) do
    @connection_attempts += 1
    reconnect(@host, @port)
    post_init
  end
end