slightly more robust connection procedure

This commit is contained in:
thomasv 2012-05-16 17:29:05 +02:00
parent 94cc6aa93a
commit c5d42f3614
4 changed files with 30 additions and 25 deletions

View File

@ -98,7 +98,6 @@ if __name__ == '__main__':
exit(1)
if not found: exit(1)
gui.main(url)
wallet.save()
sys.exit(0)

View File

@ -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)

View File

@ -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")

View File

@ -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: