Commit 67b6e2db0d5fbe49cbc9be50306f4d074e11803a

Authored by Marius Hanne
1 parent 1dda506ac0

auto-start node in CommandClient

Showing 3 changed files with 11 additions and 1 deletions Side-by-side Diff

lib/bitcoin/network/command_client.rb
... ... @@ -7,6 +7,7 @@
7 7 @block = block
8 8 instance_eval &block if block
9 9 @buffer = BufferedTokenizer.new("\x00")
  10 + @connection_attempts = 0
10 11 end
11 12  
12 13 def log
... ... @@ -28,6 +29,10 @@
28 29 def unbind
29 30 log.info { "Disconnected" }
30 31 callback :disconnected
  32 + if @connection_attempts > 2
  33 + log.info { "Trying to start server..." }
  34 + EM.defer { system("bin/bitcoin_node", "--quiet") }
  35 + end
31 36 EM.add_timer(1) do
32 37 reconnect(@host, @port)
33 38 post_init
... ... @@ -41,6 +46,7 @@
41 46 end
42 47  
43 48 def receive_data data
  49 + @connection_attempts = 0
44 50 @buffer.extract(data).each do |packet|
45 51 cmd, *data = *JSON.load(packet)
46 52 log.info { d = data.inspect
lib/bitcoin/network/node.rb
... ... @@ -65,6 +65,10 @@
65 65 end
66 66  
67 67 def load_addrs
  68 + unless File.exist?(@config[:addr_file])
  69 + @addrs = []
  70 + return
  71 + end
68 72 @addrs = JSON.load(File.read(@config[:addr_file])).map do |a|
69 73 addr = Bitcoin::P::Addr.new
70 74 addr.time, addr.service, addr.ip, addr.port =
lib/bitcoin/protocol/parser.rb
... ... @@ -44,7 +44,7 @@
44 44 begin
45 45 @h.on_addr( Addr.new(i.pack("C*")) )
46 46 rescue
47   - puts "Error parsing addr: #{i.pack("C*")}"
  47 + puts "Error parsing addr: #{i.inspect}"
48 48 end
49 49 }
50 50 end