separate commands from transport

This commit is contained in:
ThomasV 2011-12-22 22:16:14 +03:00
parent c8ddf49833
commit 789639ff74

View file

@ -358,6 +358,21 @@ def client_thread(ipaddr,conn):
conn.close()
return
out = do_command(cmd, data, ipaddr)
if out:
#print ipaddr, cmd, len(out)
try:
conn.send(out)
except:
print "error, could not send"
finally:
conn.close()
def do_command(cmd, data, ipaddr):
if cmd=='b':
out = "%d"%block_number
@ -372,8 +387,7 @@ def client_thread(ipaddr,conn):
if version[0]=="0": version = "v" + version
except:
print "error", data
conn.close()
return
return None
print time.strftime("[%d/%m/%Y-%H:%M:%S]"), "new session", ipaddr, addresses[0] if addresses else addresses, len(addresses), version
@ -388,8 +402,7 @@ def client_thread(ipaddr,conn):
session_id, addresses = ast.literal_eval(data)
except:
print "error"
conn.close()
return
return None
print time.strftime("[%d/%m/%Y-%H:%M:%S]"), "update session", ipaddr, addresses[0] if addresses else addresses, len(addresses)
@ -414,18 +427,38 @@ def client_thread(ipaddr,conn):
print "wallets", wallets
elif cmd == 'bccapi_getAccountInfo':
import electrum
from electrum import int_to_hex
v, wallet_id = ast.literal_eval(data)
w = wallets.get(wallet_id)
if w is not None:
num = len(w.addresses)
c, u = w.get_balance()
out = electrum.int_to_hex(num,4) + electrum.int_to_hex(c,8) + electrum.int_to_hex( c+u, 8 )
out = int_to_hex(num,4) + int_to_hex(c,8) + int_to_hex( c+u, 8 )
out = out.decode('hex')
else:
print "error",data
out = "error"
elif cmd == 'bccapi_getAccountStatement':
from electrum import int_to_hex
v, wallet_id = ast.literal_eval(data)
w = wallets.get(wallet_id)
if w is not None:
num = len(w.addresses)
c, u = w.get_balance()
total_records = num_records = 0
out = int_to_hex(num,4) + int_to_hex(c,8) + int_to_hex( c+u, 8 ) + int_to_hex( total_records ) + int_to_hex( num_records )
out = out.decode('hex')
else:
print "error",data
out = "error"
elif cmd == 'bccapi_getSendCoinForm':
out = ''
elif cmd == 'bccapi_submitTransaction':
out = ''
elif cmd=='poll':
session_id = data
session = sessions.get(session_id)
@ -513,15 +546,7 @@ def client_thread(ipaddr,conn):
else:
out = None
if out:
#print ipaddr, cmd, len(out)
try:
conn.send(out)
except:
print "error, could not send"
finally:
conn.close()
return out