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


10
11
12
13
14
15
16
17
18
# File 'lib/bitcoin/network/command_client.rb', line 10

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
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

register callback methods



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

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



24
25
26
# File 'lib/bitcoin/network/command_client.rb', line 24

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



69
70
71
72
73
74
# File 'lib/bitcoin/network/command_client.rb', line 69

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

#gui(&block) ⇒ Object



4
5
6
7
8
# File 'lib/bitcoin/gui/connection.rb', line 4

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

#logObject



20
21
22
# File 'lib/bitcoin/network/command_client.rb', line 20

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

#post_initObject

call connected callback



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

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

#receive_data(data) ⇒ Object

receive response from server



57
58
59
60
61
62
63
64
65
66
# File 'lib/bitcoin/network/command_client.rb', line 57

def receive_data data
  @connection_attempts = 0
  @buffer.extract(data).each do |packet|
    cmd, *data = *JSON.load(packet)
    log.debug { d = data.inspect
      "response: #{cmd} #{d[0...50]}#{d.size > 50 ? '...' : ''}" }
    callback(:response, cmd, *data)
    callback(cmd.to_sym, *data)
  end
end

#register_monitor_callbacksObject

register callbacks for monitor



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

def register_monitor_callbacks
  on_monitor do |type, data|
    callback(type, *data)
  end
end

#request(cmd, *args) ⇒ Object

request command cmd with args from the server



50
51
52
53
54
# File 'lib/bitcoin/network/command_client.rb', line 50

def request cmd, *args
  log.debug { "request: #{cmd} #{args.inspect}" }
  register_monitor_callbacks  if cmd.to_sym == :monitor
  send_data([cmd, args].to_json + "\x00")
end

#unbindObject

call disconnected callback and try to reconnect



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bitcoin/network/command_client.rb', line 35

def unbind
  log.debug { "Disconnected." }
  callback :disconnected
  if @connection_attempts > 1
    log.info { "Trying to start server..." }
    EM.defer { system("bin/bitcoin_node", "--quiet") }
  end
  EM.add_timer(1) do
    @connection_attempts += 1
    reconnect(@host, @port)
    post_init
  end
end