Commit 962ae0696c8bc226b87165b7ae0c54d83a61a3d5

Authored by Marius Hanne
1 parent 6e284cb2ba

use CommandClient for gui

Showing 4 changed files with 61 additions and 85 deletions Side-by-side Diff

... ... @@ -73,7 +73,7 @@
73 73  
74 74 EM.run do
75 75 @gui = Bitcoin::Gui::Gui.new(@storage, config['file'])
76   - Bitcoin::Gui::Connection.connect(*options[:command].split(":"), @gui)
  76 + Bitcoin::Gui::Connection.new(*options[:command].split(":"), @gui)
77 77 EM.gtk_main
78 78 end
lib/bitcoin/gui/connection.rb
1 1 module Bitcoin::Gui
2   - class Connection < EM::Connection
3 2  
4   - attr_reader :info
5   -
6   - def initialize gtk
7   - @gtk = gtk
8   - @gtk.node = self
9   - @buf = BufferedTokenizer.new("\x00")
10   - end
11   -
  3 + class Bitcoin::Network::CommandClient
12 4 def gui &block
13 5 EM.next_tick do
14   - @gtk.instance_eval &block
  6 + @args[0].instance_eval &block
15 7 end
16 8 end
  9 + end
17 10  
18   - def post_init
19   - p 'connected'
20   - query("info")
21   - query("monitor", "block connection")
22   - end
  11 + class Connection
23 12  
24   - def query(cmd, args = "")
25   - puts "query: #{cmd}"
26   - send_data([cmd, args].to_json + "\x00")
27   - end
  13 + def initialize host, port, gui
  14 + @gui = gui
  15 + client = Bitcoin::Network::CommandClient.connect(host, port, gui) do
28 16  
29   - def receive_data data
30   - @buf.extract(data).each do |packet|
31   - # p packet
32   - cmd, data = *JSON.load(packet)
33   - puts "data: #{cmd}"
34   - case cmd
35   - when 'info'
36   - text = "connections: #{data['connections']} | " +
37   - "addrs: #{data['addrs']} | uptime: #{data['uptime']}"
  17 + on_connected do
  18 + request :info
  19 + request :monitor, "block", "connection"
  20 + end
  21 +
  22 + on_info do |info|
  23 + text = "connections: #{info['connections']} | " +
  24 + "addrs: #{info['addrs']} | uptime: #{info['uptime']}"
38 25 gui { status_network.push 0, text }
39   - #EM::defer { sleep(1) && query("info") }
  26 + EM::defer { sleep(1) && request(:info) }
  27 + end
40 28  
41   - when 'monitor'
42   - EM.defer do
43   - begin
44   - type, data = *data
45   - case type
46   - when "block"
47   - gui { status_store.push 0, "#{depth}" }
48   - when "tx"
49   - when "connection"
50   - next unless data
51   - conn_type, data = *data
52   - if conn_type == "connected"
53   - row = @gtk.conn_store.append(nil)
54   - row[0] = data['host']
55   - row[1] = data['port']
56   - row[2] = data['state']
57   - row[3] = data['version']
58   - row[4] = data['block']
59   - row[5] = data['started']
60   - row[6] = data['user_agent']
61   - gui { conn_view.model = conn_store }
62   - elsif conn_type == "disconnected"
63   - iter = nil
64   - @gtk.conn_store.each do |model,path,i|
65   - iter = i if i[0] == data[0] && i[1] == data[1].to_s
66   - end
67   - if iter
68   - @gtk.conn_store.remove(iter)
69   - gui { conn_view.model = conn_store}
70   - end
71   - end
72   - when "addr"
73   - else
74   - puts "invalid datatype: #{type.inspect}"
75   - end
76   - rescue
77   - puts "Error reading command: #{cmd}(#{data.inspect})"
78   - p $!
79   - puts *$@
  29 + on_block do |block, depth|
  30 + gui { status_store.push 0, "Blocks: #{depth}" }
  31 + end
  32 +
  33 + on_connection do |state, data|
  34 + if state == "connected"
  35 + row = gui.conn_store.append(nil)
  36 + row[0] = data['host']
  37 + row[1] = data['port']
  38 + row[2] = data['state']
  39 + row[3] = data['version']
  40 + row[4] = data['block']
  41 + row[5] = data['started']
  42 + row[6] = data['user_agent']
  43 + gui { conn_view.model = conn_store }
  44 + elsif state == "disconnected"
  45 + iter = nil
  46 + gui.conn_store.each do |model,path,i|
  47 + iter = i if i[0] == data[0] && i[1] == data[1].to_s
80 48 end
  49 + if iter
  50 + gui.conn_store.remove(iter)
  51 + gui { conn_view.model = conn_store}
  52 + end
81 53 end
  54 + i=0; gui.conn_store.each {i+=1};
  55 + p = gui.notebook.get_nth_page(2)
  56 + l = Gtk::Label.new("Connections (#{i})")
  57 + gui { notebook.set_tab_label(p, l) }
82 58 end
  59 +
  60 + on_disconnected do
  61 + gui { status_network.push 0, "Offline" }
  62 + end
83 63 end
84 64 end
85   -
86   - def unbind
87   - puts "disconnected"
88   - end
89   -
90   - def self.connect host, port, gtk
91   - EM.connect(host, port, self, gtk)
92   - end
93 65 end
  66 +
94 67 end
lib/bitcoin/gui/gui.builder
1 1 <?xml version="1.0" encoding="UTF-8"?>
2 2 <interface>
3 3 <!-- interface-requires gtk+ 3.0 -->
4   - <object class="GtkImage" id="image_new_addr">
  4 + <object class="GtkImage" id="image_new_tx">
5 5 <property name="visible">True</property>
6 6 <property name="can_focus">False</property>
7   - <property name="stock">gtk-add</property>
  7 + <property name="stock">gtk-go-forward</property>
8 8 </object>
9   - <object class="GtkImage" id="image_new_tx">
  9 + <object class="GtkImage" id="image_new_addr">
10 10 <property name="visible">True</property>
11 11 <property name="can_focus">False</property>
12   - <property name="stock">gtk-go-forward</property>
  12 + <property name="stock">gtk-add</property>
13 13 </object>
14 14 <object class="GtkWindow" id="main_window">
15 15 <property name="can_focus">False</property>
... ... @@ -195,7 +195,7 @@
195 195 </packing>
196 196 </child>
197 197 <child>
198   - <object class="GtkNotebook" id="notebook1">
  198 + <object class="GtkNotebook" id="notebook">
199 199 <property name="visible">True</property>
200 200 <property name="can_focus">True</property>
201 201 <child>
lib/bitcoin/network/command_client.rb
... ... @@ -17,7 +17,7 @@
17 17 end
18 18  
19 19 def self.connect host, port, *args, &block
20   - client = EM.connect(host, port, self, host, port, block, *args)
  20 + client = EM.connect(host, port.to_i, self, host, port.to_i, block, *args)
21 21 end
22 22  
23 23 def post_init
... ... @@ -27,8 +27,11 @@
27 27  
28 28 def unbind
29 29 log.info { "Disconnected" }
30   - sleep 1
31   - EM.connect(@host, @port, self.class, @host, @port, @block, *@args)
  30 + callback :disconnected
  31 + EM.add_timer(1) do
  32 + reconnect(@host, @port)
  33 + post_init
  34 + end
32 35 end
33 36  
34 37 def request cmd, *args