2012-01-19 08:11:36 -08:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Electrum - lightweight Bitcoin client
|
|
|
|
# Copyright (C) 2011 thomasv@gitorious
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
|
|
import random, socket, ast
|
2012-03-18 00:12:48 -07:00
|
|
|
import thread, threading, traceback, sys, time, json
|
2012-02-03 06:45:41 -08:00
|
|
|
|
2012-03-14 10:00:24 -07:00
|
|
|
DEFAULT_TIMEOUT = 5
|
2012-03-19 07:57:47 -07:00
|
|
|
DEFAULT_SERVERS = ['electrum.bitcoins.sk','ecdsa.org','electrum.novit.ro'] # list of default servers
|
2012-03-14 10:00:24 -07:00
|
|
|
|
2012-02-03 06:45:41 -08:00
|
|
|
|
2012-01-19 08:11:36 -08:00
|
|
|
class Interface:
|
2012-03-17 04:51:16 -07:00
|
|
|
def __init__(self, host, port, address_callback=None, history_callback=None, newblock_callback=None):
|
2012-03-14 10:21:27 -07:00
|
|
|
self.host = host
|
|
|
|
self.port = port
|
2012-03-17 04:36:27 -07:00
|
|
|
self.address_callback = address_callback
|
|
|
|
self.history_callback = history_callback
|
2012-03-17 04:51:16 -07:00
|
|
|
self.newblock_callback = newblock_callback
|
2012-03-14 10:21:27 -07:00
|
|
|
|
2012-03-19 07:57:47 -07:00
|
|
|
self.servers = [] # actual list from IRC
|
2012-01-19 08:11:36 -08:00
|
|
|
self.rtime = 0
|
|
|
|
self.blocks = 0
|
|
|
|
self.message = ''
|
2012-02-08 04:37:14 -08:00
|
|
|
self.was_updated = True # fixme: use a semaphore
|
2012-03-17 04:07:36 -07:00
|
|
|
self.is_up_to_date = False
|
2012-01-19 08:11:36 -08:00
|
|
|
|
2012-03-14 07:35:39 -07:00
|
|
|
self.is_connected = False
|
|
|
|
self.disconnected_event = threading.Event()
|
|
|
|
self.disconnected_event.clear()
|
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
#only asynchrnous
|
|
|
|
self.addresses_waiting_for_status = []
|
|
|
|
self.addresses_waiting_for_history = []
|
|
|
|
self.tx_event = threading.Event()
|
|
|
|
self.up_to_date_event = threading.Event()
|
|
|
|
self.up_to_date_event.clear()
|
|
|
|
|
2012-03-18 00:07:40 -07:00
|
|
|
#json
|
|
|
|
self.message_id = 0
|
|
|
|
self.messages = {}
|
2012-03-17 15:32:36 -07:00
|
|
|
|
2012-03-12 09:55:33 -07:00
|
|
|
def send_tx(self, data):
|
2012-03-18 00:07:40 -07:00
|
|
|
self.tx_event.clear()
|
|
|
|
self.send([('transaction.broadcast', [data])])
|
|
|
|
self.tx_event.wait()
|
|
|
|
return self.tx_result
|
2012-03-12 09:55:33 -07:00
|
|
|
|
|
|
|
|
2012-03-17 07:46:24 -07:00
|
|
|
def start_session(self, addresses, version):
|
2012-03-14 06:55:12 -07:00
|
|
|
pass
|
|
|
|
|
2012-03-12 09:55:33 -07:00
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
def handle_json_response(self, c):
|
2012-03-19 11:20:55 -07:00
|
|
|
#print repr(c)
|
2012-03-17 15:32:36 -07:00
|
|
|
msg_id = c.get('id')
|
|
|
|
result = c.get('result')
|
|
|
|
error = c.get('error')
|
2012-03-19 11:20:55 -07:00
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
if msg_id is None:
|
|
|
|
print "error: message without ID"
|
|
|
|
return
|
|
|
|
|
|
|
|
method, params = self.messages[msg_id]
|
|
|
|
if error:
|
|
|
|
print "received error:", c, method, params
|
|
|
|
else:
|
2012-03-19 11:20:55 -07:00
|
|
|
self.handle_response(method, params, result)
|
|
|
|
self.is_up_to_date = True
|
2012-03-19 05:54:59 -07:00
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def handle_response(self, method, params, result):
|
|
|
|
|
2012-03-19 05:54:59 -07:00
|
|
|
if method == 'server.banner':
|
2012-03-17 15:32:36 -07:00
|
|
|
self.message = result
|
|
|
|
self.was_updated = True
|
|
|
|
|
|
|
|
elif method == 'session.poll':
|
2012-03-19 05:54:59 -07:00
|
|
|
# native poll
|
2012-03-19 02:19:42 -07:00
|
|
|
blocks, changed_addresses = result
|
2012-03-17 15:32:36 -07:00
|
|
|
if blocks == -1: raise BaseException("session not found")
|
|
|
|
self.blocks = int(blocks)
|
|
|
|
if changed_addresses:
|
|
|
|
self.is_up_to_date = False
|
|
|
|
self.was_updated = True
|
|
|
|
for addr, status in changed_addresses.items():
|
|
|
|
apply(self.address_callback, (addr, status))
|
|
|
|
else:
|
|
|
|
self.is_up_to_date = True
|
|
|
|
|
|
|
|
elif method == 'server.peers':
|
2012-03-19 12:56:20 -07:00
|
|
|
#print "Received server list: ", result
|
2012-03-17 15:32:36 -07:00
|
|
|
self.servers = map( lambda x:x[1], result )
|
|
|
|
|
|
|
|
elif method == 'address.subscribe':
|
|
|
|
addr = params[-1]
|
|
|
|
if addr in self.addresses_waiting_for_status:
|
|
|
|
self.addresses_waiting_for_status.remove(addr)
|
|
|
|
apply(self.address_callback,(addr, result))
|
|
|
|
|
|
|
|
elif method == 'address.get_history':
|
|
|
|
addr = params[0]
|
|
|
|
if addr in self.addresses_waiting_for_history:
|
|
|
|
self.addresses_waiting_for_history.remove(addr)
|
|
|
|
apply(self.history_callback, (addr, result))
|
|
|
|
self.was_updated = True
|
|
|
|
|
|
|
|
elif method == 'transaction.broadcast':
|
|
|
|
self.tx_result = result
|
|
|
|
self.tx_event.set()
|
|
|
|
|
|
|
|
elif method == 'numblocks.subscribe':
|
|
|
|
self.blocks = result
|
|
|
|
if self.newblock_callback: apply(self.newblock_callback,(result,))
|
2012-03-19 02:19:42 -07:00
|
|
|
|
2012-03-19 23:02:39 -07:00
|
|
|
elif method == 'client.version':
|
2012-03-19 11:20:55 -07:00
|
|
|
pass
|
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
else:
|
2012-03-19 23:02:39 -07:00
|
|
|
print "unknown message:", method, params, result
|
2012-03-17 15:32:36 -07:00
|
|
|
|
|
|
|
|
2012-03-19 05:54:59 -07:00
|
|
|
def subscribe(self, addresses):
|
|
|
|
messages = []
|
|
|
|
for addr in addresses:
|
|
|
|
messages.append(('address.subscribe', [addr]))
|
|
|
|
self.addresses_waiting_for_status.append(addr)
|
|
|
|
self.send(messages)
|
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
|
2012-03-19 07:57:47 -07:00
|
|
|
def get_servers(self, wallet):
|
|
|
|
# loop over default servers
|
|
|
|
# requesting servers could be an independent process
|
|
|
|
addresses = wallet.all_addresses()
|
|
|
|
version = wallet.electrum_version
|
|
|
|
|
|
|
|
for server in DEFAULT_SERVERS:
|
|
|
|
print "connecting to", server
|
|
|
|
try:
|
|
|
|
self.host = server
|
|
|
|
self.start_session(addresses, version)
|
|
|
|
wallet.host = self.host
|
|
|
|
break
|
|
|
|
except socket.timeout:
|
|
|
|
continue
|
|
|
|
except socket.error:
|
|
|
|
continue
|
|
|
|
except:
|
|
|
|
traceback.print_exc(file=sys.stdout)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-17 04:03:57 -07:00
|
|
|
class PollingInterface(Interface):
|
2012-03-17 04:07:36 -07:00
|
|
|
""" non-persistent connection. synchronous calls"""
|
2012-01-19 08:11:36 -08:00
|
|
|
|
2012-03-17 04:36:27 -07:00
|
|
|
def start_session(self, addresses, version):
|
2012-03-18 00:07:40 -07:00
|
|
|
self.send([('session.new', [ version, addresses ])] )
|
2012-03-19 07:57:47 -07:00
|
|
|
self.send([('server.peers',[])])
|
2012-03-19 05:54:59 -07:00
|
|
|
thread.start_new_thread(self.poll_thread, (5,))
|
2012-02-08 04:37:14 -08:00
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
def get_history(self, address):
|
2012-03-18 00:07:40 -07:00
|
|
|
self.send([('address.get_history', [address] )])
|
2012-03-17 03:56:31 -07:00
|
|
|
|
2012-03-19 11:20:55 -07:00
|
|
|
def poll(self):
|
|
|
|
self.send([('session.poll', [])])
|
|
|
|
|
2012-03-17 04:36:27 -07:00
|
|
|
def update_wallet(self):
|
2012-03-17 03:56:31 -07:00
|
|
|
while True:
|
2012-03-19 11:20:55 -07:00
|
|
|
self.poll()
|
2012-03-17 15:32:36 -07:00
|
|
|
if self.is_up_to_date: break
|
2012-03-17 03:56:31 -07:00
|
|
|
|
|
|
|
#if is_new or wallet.remote_url:
|
|
|
|
# self.was_updated = True
|
|
|
|
# is_new = wallet.synchronize()
|
|
|
|
# wallet.update_tx_history()
|
|
|
|
# wallet.save()
|
|
|
|
# return is_new
|
|
|
|
#else:
|
|
|
|
# return False
|
2012-02-08 04:37:14 -08:00
|
|
|
|
2012-03-19 05:54:59 -07:00
|
|
|
def poll_thread(self, poll_interval):
|
2012-03-14 07:35:39 -07:00
|
|
|
while self.is_connected:
|
2012-02-08 04:37:14 -08:00
|
|
|
try:
|
2012-03-17 04:36:27 -07:00
|
|
|
self.update_wallet()
|
2012-03-19 05:54:59 -07:00
|
|
|
time.sleep(poll_interval)
|
2012-03-14 07:35:39 -07:00
|
|
|
except socket.gaierror:
|
|
|
|
break
|
|
|
|
except socket.error:
|
|
|
|
break
|
2012-02-08 04:37:14 -08:00
|
|
|
except:
|
|
|
|
traceback.print_exc(file=sys.stdout)
|
2012-03-14 07:35:39 -07:00
|
|
|
break
|
|
|
|
|
|
|
|
self.is_connected = False
|
|
|
|
self.disconnected_event.set()
|
2012-02-08 04:37:14 -08:00
|
|
|
|
|
|
|
|
2012-03-12 09:55:33 -07:00
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-17 04:03:57 -07:00
|
|
|
class NativeInterface(PollingInterface):
|
|
|
|
|
2012-03-18 00:07:40 -07:00
|
|
|
def send(self, messages):
|
2012-03-17 04:03:57 -07:00
|
|
|
import time
|
|
|
|
cmds = {'session.new':'new_session',
|
2012-03-17 08:50:24 -07:00
|
|
|
'server.peers':'peers',
|
2012-03-17 04:03:57 -07:00
|
|
|
'session.poll':'poll',
|
|
|
|
'transaction.broadcast':'tx',
|
|
|
|
'address.get_history':'h',
|
|
|
|
'address.subscribe':'address.subscribe'
|
|
|
|
}
|
2012-03-17 15:32:36 -07:00
|
|
|
|
|
|
|
for m in messages:
|
|
|
|
method, params = m
|
|
|
|
cmd = cmds[method]
|
|
|
|
|
2012-03-19 02:19:42 -07:00
|
|
|
if cmd == 'poll':
|
|
|
|
params = self.session_id
|
|
|
|
|
2012-03-20 07:30:36 -07:00
|
|
|
if cmd == 'address.subscribe':
|
|
|
|
params = [ self.session_id] + params
|
|
|
|
|
2012-03-18 00:07:40 -07:00
|
|
|
if cmd in ['h', 'tx']:
|
2012-03-17 15:32:36 -07:00
|
|
|
str_params = params[0]
|
|
|
|
elif type(params) != type(''):
|
|
|
|
str_params = repr( params )
|
|
|
|
else:
|
|
|
|
str_params = params
|
|
|
|
t1 = time.time()
|
|
|
|
request = repr ( (cmd, str_params) ) + "#"
|
|
|
|
s = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
|
|
|
|
s.settimeout(DEFAULT_TIMEOUT)
|
2012-03-19 07:57:47 -07:00
|
|
|
s.connect(( self.host, self.port) )
|
2012-03-17 15:32:36 -07:00
|
|
|
s.send( request )
|
|
|
|
out = ''
|
|
|
|
while 1:
|
|
|
|
msg = s.recv(1024)
|
|
|
|
if msg: out += msg
|
|
|
|
else: break
|
|
|
|
s.close()
|
|
|
|
self.rtime = time.time() - t1
|
|
|
|
self.is_connected = True
|
2012-03-19 02:19:42 -07:00
|
|
|
|
|
|
|
if cmd in[ 'peers','h','poll']:
|
2012-03-17 15:32:36 -07:00
|
|
|
out = ast.literal_eval( out )
|
|
|
|
|
|
|
|
if out=='': out=None #fixme
|
|
|
|
|
2012-03-19 05:54:59 -07:00
|
|
|
if cmd == 'new_session':
|
|
|
|
self.session_id, self.message = ast.literal_eval( out )
|
|
|
|
self.was_updated = True
|
|
|
|
else:
|
|
|
|
self.handle_response(method, params, out)
|
2012-03-17 15:32:36 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-17 04:03:57 -07:00
|
|
|
|
2012-03-12 09:55:33 -07:00
|
|
|
|
2012-03-17 04:03:57 -07:00
|
|
|
class HttpInterface(PollingInterface):
|
2012-03-14 08:31:31 -07:00
|
|
|
|
2012-03-19 02:19:42 -07:00
|
|
|
def start_session(self, addresses, version):
|
|
|
|
self.session_id = None
|
2012-03-19 07:57:47 -07:00
|
|
|
self.send([('client.version', [version]), ('server.banner',[]), ('numblocks.subscribe',[]), ('server.peers',[])])
|
2012-03-19 05:54:59 -07:00
|
|
|
self.subscribe(addresses)
|
|
|
|
thread.start_new_thread(self.poll_thread, (15,))
|
2012-03-19 02:19:42 -07:00
|
|
|
|
2012-03-19 11:20:55 -07:00
|
|
|
def poll(self):
|
|
|
|
self.send( [] )
|
2012-03-17 15:32:36 -07:00
|
|
|
|
2012-03-18 00:07:40 -07:00
|
|
|
def send(self, messages):
|
2012-03-19 02:19:42 -07:00
|
|
|
import urllib2, json, time, cookielib
|
|
|
|
|
|
|
|
cj = cookielib.CookieJar()
|
|
|
|
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
|
|
|
|
urllib2.install_opener(opener)
|
2012-03-17 15:32:36 -07:00
|
|
|
|
2012-03-19 11:20:55 -07:00
|
|
|
t1 = time.time()
|
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
data = []
|
|
|
|
for m in messages:
|
|
|
|
method, params = m
|
|
|
|
if type(params) != type([]): params = [params]
|
|
|
|
data.append( { 'method':method, 'id':self.message_id, 'params':params } )
|
|
|
|
self.messages[self.message_id] = (method, params)
|
|
|
|
self.message_id += 1
|
|
|
|
|
2012-03-19 11:20:55 -07:00
|
|
|
if data:
|
|
|
|
data_json = json.dumps(data)
|
|
|
|
else:
|
|
|
|
# poll with GET
|
|
|
|
data_json = None
|
2012-03-19 02:19:42 -07:00
|
|
|
|
2012-03-19 11:20:55 -07:00
|
|
|
host = 'http://%s:%d'%( self.host, self.port )
|
2012-03-19 02:19:42 -07:00
|
|
|
headers = {'content-type': 'application/json'}
|
|
|
|
if self.session_id:
|
|
|
|
headers['cookie'] = 'SESSION=%s'%self.session_id
|
|
|
|
|
|
|
|
req = urllib2.Request(host, data_json, headers)
|
2012-03-14 08:31:31 -07:00
|
|
|
response_stream = urllib2.urlopen(req)
|
2012-03-19 02:19:42 -07:00
|
|
|
|
|
|
|
for index, cookie in enumerate(cj):
|
|
|
|
if cookie.name=='SESSION':
|
|
|
|
self.session_id = cookie.value
|
|
|
|
|
2012-03-19 11:20:55 -07:00
|
|
|
response = response_stream.read()
|
|
|
|
if response:
|
2012-03-19 23:02:39 -07:00
|
|
|
#print "response",response
|
2012-03-19 11:20:55 -07:00
|
|
|
response = json.loads( response )
|
|
|
|
if type(response) is not type([]):
|
|
|
|
self.handle_json_response(response)
|
|
|
|
else:
|
|
|
|
for item in response:
|
|
|
|
self.handle_json_response(item)
|
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
|
2012-03-14 08:31:31 -07:00
|
|
|
self.rtime = time.time() - t1
|
|
|
|
self.is_connected = True
|
2012-03-17 15:32:36 -07:00
|
|
|
|
2012-03-14 08:31:31 -07:00
|
|
|
|
|
|
|
|
2012-03-12 09:55:33 -07:00
|
|
|
|
2012-03-17 05:12:16 -07:00
|
|
|
class AsynchronousInterface(Interface):
|
2012-03-17 04:07:36 -07:00
|
|
|
"""json-rpc over persistent TCP connection, asynchronous"""
|
2012-03-12 09:55:33 -07:00
|
|
|
|
2012-03-17 04:36:27 -07:00
|
|
|
def listen_thread(self):
|
2012-03-13 08:43:56 -07:00
|
|
|
try:
|
|
|
|
self.is_connected = True
|
|
|
|
out = ''
|
2012-03-14 07:35:39 -07:00
|
|
|
while self.is_connected:
|
|
|
|
try: msg = self.s.recv(1024)
|
|
|
|
except socket.timeout: continue
|
2012-03-13 08:43:56 -07:00
|
|
|
out += msg
|
2012-03-13 09:31:29 -07:00
|
|
|
if msg == '':
|
|
|
|
self.is_connected = False
|
2012-03-14 07:35:39 -07:00
|
|
|
print "disconnected."
|
|
|
|
|
2012-03-13 08:43:56 -07:00
|
|
|
while True:
|
|
|
|
s = out.find('\n')
|
|
|
|
if s==-1: break
|
|
|
|
c = out[0:s]
|
|
|
|
out = out[s+1:]
|
|
|
|
c = json.loads(c)
|
2012-03-17 15:32:36 -07:00
|
|
|
self.handle_json_response(c)
|
2012-03-14 06:55:12 -07:00
|
|
|
if self.addresses_waiting_for_status or self.addresses_waiting_for_history:
|
|
|
|
self.is_up_to_date = False
|
|
|
|
else:
|
|
|
|
self.is_up_to_date = True
|
|
|
|
self.up_to_date_event.set()
|
2012-03-17 15:32:36 -07:00
|
|
|
|
2012-03-13 08:43:56 -07:00
|
|
|
except:
|
|
|
|
traceback.print_exc(file=sys.stdout)
|
2012-03-14 07:35:39 -07:00
|
|
|
|
|
|
|
self.is_connected = False
|
|
|
|
self.disconnected_event.set()
|
2012-03-14 06:55:12 -07:00
|
|
|
|
2012-03-20 07:30:36 -07:00
|
|
|
def update_wallet(self):
|
2012-03-14 06:55:12 -07:00
|
|
|
self.up_to_date_event.wait()
|
2012-03-13 08:43:56 -07:00
|
|
|
|
2012-03-17 08:02:28 -07:00
|
|
|
def send(self, messages):
|
|
|
|
out = ''
|
|
|
|
for m in messages:
|
|
|
|
method, params = m
|
|
|
|
request = json.dumps( { 'id':self.message_id, 'method':method, 'params':params } )
|
|
|
|
self.messages[self.message_id] = (method, params)
|
|
|
|
self.message_id += 1
|
|
|
|
out += request + '\n'
|
|
|
|
self.s.send( out )
|
|
|
|
|
2012-03-17 04:36:27 -07:00
|
|
|
def get_history(self, addr):
|
2012-03-17 08:02:28 -07:00
|
|
|
self.send([('address.get_history', [addr])])
|
2012-03-17 07:46:24 -07:00
|
|
|
self.addresses_waiting_for_history.append(addr)
|
2012-03-16 14:18:58 -07:00
|
|
|
|
2012-03-17 04:36:27 -07:00
|
|
|
def start_session(self, addresses, version):
|
2012-03-14 06:55:12 -07:00
|
|
|
self.s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
|
2012-03-14 07:38:03 -07:00
|
|
|
self.s.settimeout(1)
|
2012-03-14 06:55:12 -07:00
|
|
|
self.s.connect(( self.host, self.port))
|
2012-03-17 04:36:27 -07:00
|
|
|
thread.start_new_thread(self.listen_thread, ())
|
2012-03-19 07:57:47 -07:00
|
|
|
self.send([('client.version', [version]), ('server.banner',[]), ('numblocks.subscribe',[]), ('server.peers',[])])
|
2012-03-17 08:02:28 -07:00
|
|
|
self.subscribe(addresses)
|
2012-03-14 06:55:12 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-03-17 15:32:36 -07:00
|
|
|
|
2012-03-14 06:55:12 -07:00
|
|
|
def new_interface(wallet):
|
2012-03-14 10:00:24 -07:00
|
|
|
if wallet.host:
|
|
|
|
host = wallet.host
|
|
|
|
else:
|
|
|
|
host = random.choice( DEFAULT_SERVERS ) # random choice when the wallet is created
|
2012-03-14 06:55:12 -07:00
|
|
|
port = wallet.port
|
2012-03-17 04:36:27 -07:00
|
|
|
address_cb = wallet.receive_status_callback
|
|
|
|
history_cb = wallet.receive_history_callback
|
2012-03-14 10:00:24 -07:00
|
|
|
|
2012-03-14 06:55:12 -07:00
|
|
|
if port == 50000:
|
2012-03-17 05:12:16 -07:00
|
|
|
InterfaceClass = NativeInterface
|
2012-03-14 06:55:12 -07:00
|
|
|
elif port == 50001:
|
2012-03-17 05:12:16 -07:00
|
|
|
InterfaceClass = AsynchronousInterface
|
2012-03-14 08:31:31 -07:00
|
|
|
elif port in [80, 81, 8080, 8081]:
|
2012-03-17 05:12:16 -07:00
|
|
|
InterfaceClass = HttpInterface
|
2012-03-14 06:55:12 -07:00
|
|
|
else:
|
2012-03-14 08:13:12 -07:00
|
|
|
print "unknown port number: %d. using native protocol."%port
|
2012-03-17 05:12:16 -07:00
|
|
|
InterfaceClass = NativeInterface
|
|
|
|
|
|
|
|
interface = InterfaceClass(host, port, address_cb, history_cb)
|
2012-03-14 08:08:23 -07:00
|
|
|
|
2012-03-14 06:55:12 -07:00
|
|
|
return interface
|
2012-03-19 07:57:47 -07:00
|
|
|
|
2012-03-14 06:55:12 -07:00
|
|
|
|
|
|
|
def loop_interfaces_thread(wallet):
|
|
|
|
while True:
|
|
|
|
try:
|
2012-03-17 04:36:27 -07:00
|
|
|
addresses = wallet.all_addresses()
|
|
|
|
version = wallet.electrum_version
|
|
|
|
wallet.interface.start_session(addresses, version)
|
2012-03-14 08:08:23 -07:00
|
|
|
|
|
|
|
wallet.interface.disconnected_event.wait()
|
|
|
|
print "Disconnected"
|
2012-03-14 06:55:12 -07:00
|
|
|
except socket.error:
|
2012-03-14 08:08:23 -07:00
|
|
|
print "socket error"
|
2012-03-14 06:55:12 -07:00
|
|
|
time.sleep(5)
|
2012-03-14 07:35:39 -07:00
|
|
|
except:
|
|
|
|
traceback.print_exc(file=sys.stdout)
|
2012-03-18 04:38:40 -07:00
|
|
|
time.sleep(5)
|
2012-03-14 07:35:39 -07:00
|
|
|
continue
|
2012-03-14 08:08:23 -07:00
|
|
|
|
2012-03-14 08:13:12 -07:00
|
|
|
print "Starting new session: %s:%d"%(wallet.host,wallet.port)
|
2012-03-14 06:55:12 -07:00
|
|
|
wallet.interface = new_interface(wallet)
|