commit 6b1079be7ce096e586227c9567b8cf6a42e592eb
parent 9adad69b67348ead4e5434e81b776083e3ff044d
Author: ThomasV <thomasv@gitorious>
Date: Sat, 17 Mar 2012 02:02:53 +0300
Merge branch 'master' of gitorious.org:electrum/electrum
Diffstat:
2 files changed, 58 insertions(+), 40 deletions(-)
diff --git a/client/interface.py b/client/interface.py
@@ -208,6 +208,8 @@ class TCPInterface(Interface):
def __init__(self, host, port):
Interface.__init__(self, host, port)
+ self.message_id = 0
+ self.messages = {}
self.tx_event = threading.Event()
self.addresses_waiting_for_status = []
@@ -217,18 +219,11 @@ class TCPInterface(Interface):
self.up_to_date_event = threading.Event()
self.up_to_date_event.clear()
- def send(self, cmd, params = []):
- request = json.dumps( { 'method':cmd, 'params':params } )
+ def send(self, method, params = []):
+ request = json.dumps( { 'id':self.message_id, 'method':method, 'params':params } )
+ self.messages[self.message_id] = (method, params)
self.s.send( request + '\n' )
-
- def send_tx(self, data):
- self.tx_event.clear()
- self.send('transaction.broadcast', data )
- print "waiting for event.."
- self.tx_event.wait()
- out = self.tx_result
- print "result:", out
- return out
+ self.message_id += 1
def listen_thread(self, wallet):
try:
@@ -248,42 +243,45 @@ class TCPInterface(Interface):
c = out[0:s]
out = out[s+1:]
c = json.loads(c)
- cmd = c.get('method')
- data = c.get('result')
- if cmd == 'server.banner':
- self.message = data
- self.was_updated = True
+ #print c
+ msg_id = c.get('id')
+ result = c.get('result')
+ error = c.get('error')
- elif cmd == 'server.peers':
- self.servers = map( lambda x:x[1], data )
+ if msg_id is None:
+ print "error: message without ID"
+ continue
- elif cmd == 'transaction.broadcast':
- self.tx_result = data
- self.tx_event.set()
+ method, params = self.messages[msg_id]
- elif cmd == 'numblocks.subscribe':
- self.blocks = data
+ if method == 'server.banner':
+ self.message = result
+ self.was_updated = True
- elif cmd =='address.subscribe':
- addr = c.get('address')
- status = c.get('status')
+ elif method == 'server.peers':
+ self.servers = map( lambda x:x[1], result )
+
+ elif method == 'address.subscribe':
+ addr = params[0]
if addr in self.addresses_waiting_for_status:
self.addresses_waiting_for_status.remove(addr)
- if wallet.status.get(addr) != status:
- wallet.status[addr] = status
- self.send('address.get_history', addr)
- self.addresses_waiting_for_history.append(addr)
-
- elif cmd == 'address.get_history':
- addr = c.get('address')
+ wallet.receive_status_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)
- wallet.history[addr] = data
- wallet.synchronize()
- wallet.update_tx_history()
- wallet.save()
+ wallet.receive_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
+
else:
print "received message:", c
@@ -301,19 +299,29 @@ class TCPInterface(Interface):
def update_wallet(self,wallet):
self.up_to_date_event.wait()
+ def send_tx(self, data):
+ self.tx_event.clear()
+ self.send('transaction.broadcast', [data] )
+ self.tx_event.wait()
+ return self.tx_result
+
def subscribe(self,address):
- self.send('address.subscribe', address)
+ self.send('address.subscribe', [address])
self.addresses_waiting_for_status.append(address)
def get_servers(self):
self.send('server.peers')
+ def get_history(self,addr):
+ self.send('address.get_history', [addr])
+ self.addresses_waiting_for_history.append(addr)
+
def start_session(self, wallet):
self.s = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
self.s.settimeout(1)
self.s.connect(( self.host, self.port))
thread.start_new_thread(self.listen_thread, (wallet,))
- self.send('client.version', wallet.electrum_version)
+ self.send('client.version', [wallet.electrum_version])
self.send('server.banner')
self.send('numblocks.subscribe')
for address in wallet.all_addresses():
diff --git a/client/wallet.py b/client/wallet.py
@@ -703,6 +703,17 @@ class Wallet:
else:
return s
+ def receive_status_callback(self, addr, status):
+ if self.status.get(addr) != status:
+ self.status[addr] = status
+ self.interface.get_history(addr)
+
+ def receive_history_callback(self, addr, data):
+ self.history[addr] = data
+ self.synchronize()
+ self.update_tx_history()
+ self.save()
+
def get_tx_history(self):
lines = self.tx_history.values()
lines = sorted(lines, key=operator.itemgetter("nTime"))
@@ -828,7 +839,6 @@ class Wallet:
self.imported_keys[k] = c
self.save()
-
def get_alias(self, alias, interactive = False, show_message=None, question = None):
try:
target, signing_address, auth_name = self.read_alias(alias)