electrum

Electrum Bitcoin wallet
git clone https://git.parazyd.org/electrum
Log | Files | Refs | Submodules

commit c5d42f36142e0bcd480b19b72485bc83d1179962
parent 94cc6aa93a04a71a1a8618594cb713e2d5f3c789
Author: thomasv <thomasv@gitorious>
Date:   Wed, 16 May 2012 17:29:05 +0200

slightly more robust connection procedure

Diffstat:
Melectrum | 1-
Mlib/gui.py | 4++--
Mlib/gui_qt.py | 2+-
Mlib/interface.py | 48+++++++++++++++++++++++++++---------------------
4 files changed, 30 insertions(+), 25 deletions(-)

diff --git a/electrum b/electrum @@ -98,7 +98,6 @@ if __name__ == '__main__': exit(1) if not found: exit(1) - gui.main(url) wallet.save() sys.exit(0) diff --git a/lib/gui.py b/lib/gui.py @@ -1140,7 +1140,7 @@ class ElectrumWindow: interface = self.wallet.interface if self.funds_error: text = "Not enough funds" - elif interface.is_connected: + elif interface and interface.is_connected: self.network_button.set_tooltip_text("Connected to %s:%d.\n%d blocks"%(interface.host, interface.port, self.wallet.blocks)) if self.wallet.blocks == -1: self.status_image.set_from_stock(gtk.STOCK_NO, gtk.ICON_SIZE_MENU) @@ -1159,7 +1159,7 @@ class ElectrumWindow: if u: text += "[%s unconfirmed]"%( format_satoshis(u,True,self.wallet.num_zeros).strip() ) else: self.status_image.set_from_stock(gtk.STOCK_NO, gtk.ICON_SIZE_MENU) - self.network_button.set_tooltip_text("Trying to contact %s.\n%d blocks"%(interface.host, self.wallet.blocks)) + self.network_button.set_tooltip_text("Trying to contact %s.\n%d blocks"%(self.wallet.server, self.wallet.blocks)) text = "Not connected" self.status_bar.pop(self.context_id) diff --git a/lib/gui_qt.py b/lib/gui_qt.py @@ -206,7 +206,7 @@ class ElectrumWindow(QMainWindow): self.emit(QtCore.SIGNAL('updatesignal')) def update_wallet(self): - if self.wallet.interface.is_connected: + if self.wallet.interface and self.wallet.interface.is_connected: if self.wallet.blocks == -1: text = "Connecting..." icon = QIcon(":icons/status_disconnected.png") diff --git a/lib/interface.py b/lib/interface.py @@ -211,6 +211,8 @@ class TcpStratumInterface(Interface): def __init__(self, host, port): Interface.__init__(self, host, port) + + def init_socket(self): self.s = socket.socket( socket.AF_INET, socket.SOCK_STREAM ) self.s.settimeout(5*60) self.s.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) @@ -277,7 +279,28 @@ class WalletSynchronizer(threading.Thread): self.daemon = True self.wallet = wallet self.loop = loop - self.start_interface() + self.init_interface() + + def init_interface(self): + try: + host, port, protocol = self.wallet.server.split(':') + port = int(port) + except: + self.wallet.pick_random_server() + host, port, protocol = self.wallet.server.split(':') + port = int(port) + + #print protocol, host, port + if protocol == 't': + InterfaceClass = TcpStratumInterface + elif protocol == 'h': + InterfaceClass = HttpStratumInterface + else: + print "unknown protocol" + InterfaceClass = TcpStratumInterface + + self.interface = InterfaceClass(host, port) + self.wallet.interface = self.interface def handle_response(self, r): @@ -330,27 +353,8 @@ class WalletSynchronizer(threading.Thread): def start_interface(self): - try: - host, port, protocol = self.wallet.server.split(':') - port = int(port) - except: - self.wallet.pick_random_server() - host, port, protocol = self.wallet.server.split(':') - port = int(port) - - #print protocol, host, port - if protocol == 't': - InterfaceClass = TcpStratumInterface - elif protocol == 'h': - InterfaceClass = HttpStratumInterface - else: - print "unknown protocol" - InterfaceClass = TcpStratumInterface - - self.interface = InterfaceClass(host, port) + self.interface.init_socket() self.interface.start() - self.wallet.interface = self.interface - if self.interface.is_connected: self.wallet.start_session(self.interface) @@ -358,6 +362,7 @@ class WalletSynchronizer(threading.Thread): def run(self): import socket, time + self.start_interface() while True: while self.interface.is_connected: new_addresses = self.wallet.synchronize() @@ -384,6 +389,7 @@ class WalletSynchronizer(threading.Thread): self.wallet.gui_callback() if self.loop: time.sleep(5) + self.init_interface() self.start_interface() continue else: