better encapsulation
This commit is contained in:
parent
2859251c02
commit
4b6e163a39
|
@ -164,7 +164,11 @@ if __name__ == '__main__':
|
||||||
|
|
||||||
# open session
|
# open session
|
||||||
if cmd not in ['password', 'mktx', 'history', 'label', 'contacts', 'help', 'validateaddress', 'signmessage', 'verifymessage', 'eval', 'create', 'addresses', 'import']:
|
if cmd not in ['password', 'mktx', 'history', 'label', 'contacts', 'help', 'validateaddress', 'signmessage', 'verifymessage', 'eval', 'create', 'addresses', 'import']:
|
||||||
interface.start_session(wallet)
|
|
||||||
|
addresses = wallet.all_addresses()
|
||||||
|
version = wallet.electrum_version
|
||||||
|
address_callback = wallet.retrieve_status_callback
|
||||||
|
interface.start_session(addresses, version, address_callback)
|
||||||
interface.update_wallet(wallet)
|
interface.update_wallet(wallet)
|
||||||
wallet.save()
|
wallet.save()
|
||||||
|
|
||||||
|
|
|
@ -27,9 +27,11 @@ DEFAULT_SERVERS = ['ecdsa.org','electrum.novit.ro'] # list of default servers
|
||||||
|
|
||||||
|
|
||||||
class Interface:
|
class Interface:
|
||||||
def __init__(self, host, port):
|
def __init__(self, host, port, address_callback, history_callback):
|
||||||
self.host = host
|
self.host = host
|
||||||
self.port = port
|
self.port = port
|
||||||
|
self.address_callback = address_callback
|
||||||
|
self.history_callback = history_callback
|
||||||
|
|
||||||
self.servers = DEFAULT_SERVERS # actual list from IRC
|
self.servers = DEFAULT_SERVERS # actual list from IRC
|
||||||
self.rtime = 0
|
self.rtime = 0
|
||||||
|
@ -56,15 +58,10 @@ class Interface:
|
||||||
class PollingInterface(Interface):
|
class PollingInterface(Interface):
|
||||||
""" non-persistent connection. synchronous calls"""
|
""" non-persistent connection. synchronous calls"""
|
||||||
|
|
||||||
def __init__(self, host, port):
|
def start_session(self, addresses, version):
|
||||||
Interface.__init__(self, host, port)
|
|
||||||
|
|
||||||
def start_session(self, wallet):
|
|
||||||
addresses = wallet.all_addresses()
|
|
||||||
version = wallet.electrum_version
|
|
||||||
out = self.handler('session.new', [ version, addresses ] )
|
out = self.handler('session.new', [ version, addresses ] )
|
||||||
self.session_id, self.message = ast.literal_eval( out )
|
self.session_id, self.message = ast.literal_eval( out )
|
||||||
thread.start_new_thread(self.poll_thread, (wallet,))
|
thread.start_new_thread(self.poll_thread, ())
|
||||||
|
|
||||||
def update_session(self, addresses):
|
def update_session(self, addresses):
|
||||||
out = self.handler('session.update', [ self.session_id, addresses ] )
|
out = self.handler('session.update', [ self.session_id, addresses ] )
|
||||||
|
@ -77,16 +74,16 @@ class PollingInterface(Interface):
|
||||||
out = self.handler('address.get_history', address )
|
out = self.handler('address.get_history', address )
|
||||||
return out
|
return out
|
||||||
|
|
||||||
def get_history(self, addr, history_callback):
|
def get_history(self, addr):
|
||||||
data = self.retrieve_history(addr)
|
data = self.retrieve_history(addr)
|
||||||
apply(history_callback, (addr, data) )
|
apply(self.history_callback, (addr, data) )
|
||||||
self.was_updated = True
|
self.was_updated = True
|
||||||
|
|
||||||
def subscribe(self, addr, status_callback):
|
def subscribe(self, addr):
|
||||||
status = self.handler('address.subscribe', [ self.session_id, addr ] )
|
status = self.handler('address.subscribe', [ self.session_id, addr ] )
|
||||||
apply(status_callback, (addr, status) )
|
apply(self.address_callback, (addr, status) )
|
||||||
|
|
||||||
def update_wallet(self, wallet):
|
def update_wallet(self):
|
||||||
while True:
|
while True:
|
||||||
changed_addresses = self.poll()
|
changed_addresses = self.poll()
|
||||||
if changed_addresses:
|
if changed_addresses:
|
||||||
|
@ -96,7 +93,7 @@ class PollingInterface(Interface):
|
||||||
break
|
break
|
||||||
|
|
||||||
for addr, status in changed_addresses.items():
|
for addr, status in changed_addresses.items():
|
||||||
wallet.receive_status_callback(addr, status)
|
apply(self.address_callback, (addr, status))
|
||||||
|
|
||||||
#if is_new or wallet.remote_url:
|
#if is_new or wallet.remote_url:
|
||||||
# self.was_updated = True
|
# self.was_updated = True
|
||||||
|
@ -114,10 +111,10 @@ class PollingInterface(Interface):
|
||||||
self.blocks = int(blocks)
|
self.blocks = int(blocks)
|
||||||
return changed_addr
|
return changed_addr
|
||||||
|
|
||||||
def poll_thread(self, wallet):
|
def poll_thread(self):
|
||||||
while self.is_connected:
|
while self.is_connected:
|
||||||
try:
|
try:
|
||||||
self.update_wallet(wallet)
|
self.update_wallet()
|
||||||
time.sleep(self.poll_interval())
|
time.sleep(self.poll_interval())
|
||||||
except socket.gaierror:
|
except socket.gaierror:
|
||||||
break
|
break
|
||||||
|
@ -215,8 +212,8 @@ import threading
|
||||||
class TCPInterface(Interface):
|
class TCPInterface(Interface):
|
||||||
"""json-rpc over persistent TCP connection, asynchronous"""
|
"""json-rpc over persistent TCP connection, asynchronous"""
|
||||||
|
|
||||||
def __init__(self, host, port):
|
def __init__(self, host, port, acb, hcb):
|
||||||
Interface.__init__(self, host, port)
|
Interface.__init__(self, host, port, acb, hcb)
|
||||||
self.message_id = 0
|
self.message_id = 0
|
||||||
self.messages = {}
|
self.messages = {}
|
||||||
|
|
||||||
|
@ -233,7 +230,7 @@ class TCPInterface(Interface):
|
||||||
self.s.send( request + '\n' )
|
self.s.send( request + '\n' )
|
||||||
self.message_id += 1
|
self.message_id += 1
|
||||||
|
|
||||||
def listen_thread(self, wallet):
|
def listen_thread(self):
|
||||||
try:
|
try:
|
||||||
self.is_connected = True
|
self.is_connected = True
|
||||||
out = ''
|
out = ''
|
||||||
|
@ -274,13 +271,13 @@ class TCPInterface(Interface):
|
||||||
addr = params[0]
|
addr = params[0]
|
||||||
if addr in self.addresses_waiting_for_status:
|
if addr in self.addresses_waiting_for_status:
|
||||||
self.addresses_waiting_for_status.remove(addr)
|
self.addresses_waiting_for_status.remove(addr)
|
||||||
wallet.receive_status_callback(addr, result)
|
apply(self.address_callback,(addr, result))
|
||||||
|
|
||||||
elif method == 'address.get_history':
|
elif method == 'address.get_history':
|
||||||
addr = params[0]
|
addr = params[0]
|
||||||
if addr in self.addresses_waiting_for_history:
|
if addr in self.addresses_waiting_for_history:
|
||||||
self.addresses_waiting_for_history.remove(addr)
|
self.addresses_waiting_for_history.remove(addr)
|
||||||
wallet.receive_history_callback(addr, result)
|
apply(self.history_callback, (addr, result))
|
||||||
self.was_updated = True
|
self.was_updated = True
|
||||||
|
|
||||||
elif method == 'transaction.broadcast':
|
elif method == 'transaction.broadcast':
|
||||||
|
@ -304,7 +301,7 @@ class TCPInterface(Interface):
|
||||||
self.is_connected = False
|
self.is_connected = False
|
||||||
self.disconnected_event.set()
|
self.disconnected_event.set()
|
||||||
|
|
||||||
def update_wallet(self,wallet):
|
def update_wallet(self,cb):
|
||||||
self.up_to_date_event.wait()
|
self.up_to_date_event.wait()
|
||||||
|
|
||||||
def send_tx(self, data):
|
def send_tx(self, data):
|
||||||
|
@ -313,27 +310,27 @@ class TCPInterface(Interface):
|
||||||
self.tx_event.wait()
|
self.tx_event.wait()
|
||||||
return self.tx_result
|
return self.tx_result
|
||||||
|
|
||||||
def subscribe(self, address, callback):
|
def subscribe(self, address):
|
||||||
self.send('address.subscribe', [address])
|
self.send('address.subscribe', [address])
|
||||||
self.addresses_waiting_for_status.append(address)
|
self.addresses_waiting_for_status.append(address)
|
||||||
|
|
||||||
def get_servers(self):
|
def get_servers(self):
|
||||||
self.send('server.peers')
|
self.send('server.peers')
|
||||||
|
|
||||||
def get_history(self, addr, callback):
|
def get_history(self, addr):
|
||||||
self.send('address.get_history', [addr])
|
self.send('address.get_history', [addr])
|
||||||
self.addresses_waiting_for_history.append(addr)
|
self.addresses_waiting_for_history.append(addr)
|
||||||
|
|
||||||
def start_session(self, wallet):
|
def start_session(self, addresses, version):
|
||||||
self.s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
|
self.s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
|
||||||
self.s.settimeout(1)
|
self.s.settimeout(1)
|
||||||
self.s.connect(( self.host, self.port))
|
self.s.connect(( self.host, self.port))
|
||||||
thread.start_new_thread(self.listen_thread, (wallet,))
|
thread.start_new_thread(self.listen_thread, ())
|
||||||
self.send('client.version', [wallet.electrum_version])
|
self.send('client.version', [version])
|
||||||
self.send('server.banner')
|
self.send('server.banner')
|
||||||
self.send('numblocks.subscribe')
|
self.send('numblocks.subscribe')
|
||||||
for address in wallet.all_addresses():
|
for addr in addresses:
|
||||||
self.subscribe(address, wallet.receive_status_callback)
|
self.subscribe(addr)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -346,13 +343,15 @@ def new_interface(wallet):
|
||||||
else:
|
else:
|
||||||
host = random.choice( DEFAULT_SERVERS ) # random choice when the wallet is created
|
host = random.choice( DEFAULT_SERVERS ) # random choice when the wallet is created
|
||||||
port = wallet.port
|
port = wallet.port
|
||||||
|
address_cb = wallet.receive_status_callback
|
||||||
|
history_cb = wallet.receive_history_callback
|
||||||
|
|
||||||
if port == 50000:
|
if port == 50000:
|
||||||
interface = NativeInterface(host,port)
|
interface = NativeInterface(host, port, address_cb, history_cb)
|
||||||
elif port == 50001:
|
elif port == 50001:
|
||||||
interface = TCPInterface(host,port)
|
interface = TCPInterface(host, port, address_cb, history_cb)
|
||||||
elif port in [80, 81, 8080, 8081]:
|
elif port in [80, 81, 8080, 8081]:
|
||||||
interface = HttpInterface(host,port)
|
interface = HttpInterface(host, port, address_cb, history_cb)
|
||||||
else:
|
else:
|
||||||
print "unknown port number: %d. using native protocol."%port
|
print "unknown port number: %d. using native protocol."%port
|
||||||
interface = NativeInterface(host,port)
|
interface = NativeInterface(host,port)
|
||||||
|
@ -363,7 +362,9 @@ def new_interface(wallet):
|
||||||
def loop_interfaces_thread(wallet):
|
def loop_interfaces_thread(wallet):
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
wallet.interface.start_session(wallet)
|
addresses = wallet.all_addresses()
|
||||||
|
version = wallet.electrum_version
|
||||||
|
wallet.interface.start_session(addresses, version)
|
||||||
wallet.interface.get_servers()
|
wallet.interface.get_servers()
|
||||||
|
|
||||||
wallet.interface.disconnected_event.wait()
|
wallet.interface.disconnected_event.wait()
|
||||||
|
|
|
@ -456,7 +456,7 @@ class Wallet:
|
||||||
|
|
||||||
def create_new_address(self, bool):
|
def create_new_address(self, bool):
|
||||||
address = self.create_new_address_without_history(bool)
|
address = self.create_new_address_without_history(bool)
|
||||||
self.interface.subscribe(address, self.receive_status_callback)
|
self.interface.subscribe(address)
|
||||||
return address
|
return address
|
||||||
|
|
||||||
|
|
||||||
|
@ -702,7 +702,7 @@ class Wallet:
|
||||||
def receive_status_callback(self, addr, status):
|
def receive_status_callback(self, addr, status):
|
||||||
if self.status.get(addr) != status:
|
if self.status.get(addr) != status:
|
||||||
self.status[addr] = status
|
self.status[addr] = status
|
||||||
self.interface.get_history(addr, self.receive_history_callback)
|
self.interface.get_history(addr)
|
||||||
|
|
||||||
def receive_history_callback(self, addr, data):
|
def receive_history_callback(self, addr, data):
|
||||||
#print "updating history for", addr
|
#print "updating history for", addr
|
||||||
|
|
Loading…
Reference in New Issue