1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-23 06:48:16 +00:00

Added session depth

This commit is contained in:
slush 2013-09-09 15:36:17 +02:00
parent 8e5abb560e
commit 813fb233a1
2 changed files with 92 additions and 62 deletions

View File

@ -64,6 +64,9 @@ class BitkeyClient(object):
print '----------------------' print '----------------------'
print "Sending", self._pprint(msg) print "Sending", self._pprint(msg)
try:
self.transport.session_begin()
self.transport.write(msg) self.transport.write(msg)
resp = self.transport.read_blocking() resp = self.transport.read_blocking()
@ -87,6 +90,9 @@ class BitkeyClient(object):
return self.call(msg2) return self.call(msg2)
finally:
self.transport.session_end()
if isinstance(resp, proto.Failure): if isinstance(resp, proto.Failure):
self.message_func(resp.message) self.message_func(resp.message)
@ -132,6 +138,9 @@ class BitkeyClient(object):
start = time.time() start = time.time()
try:
self.transport.session_begin()
# Prepare and send initial message # Prepare and send initial message
tx = proto.SignTx() tx = proto.SignTx()
tx.inputs_count = len(inputs) tx.inputs_count = len(inputs)
@ -175,6 +184,9 @@ class BitkeyClient(object):
res = self.call(inputs[res.request_index]) res = self.call(inputs[res.request_index])
continue continue
finally:
self.transport.session_end()
print "SIGNED IN %.03f SECONDS, CALLED %d MESSAGES, %d BYTES" % \ print "SIGNED IN %.03f SECONDS, CALLED %d MESSAGES, %d BYTES" % \
(time.time() - start, counter, len(serialized_tx)) (time.time() - start, counter, len(serialized_tx))

View File

@ -7,6 +7,7 @@ class NotImplementedException(Exception):
class Transport(object): class Transport(object):
def __init__(self, device, *args, **kwargs): def __init__(self, device, *args, **kwargs):
self.device = device self.device = device
self.session_depth = 0
self._open() self._open()
def _open(self): def _open(self):
@ -21,9 +22,26 @@ class Transport(object):
def _read(self): def _read(self):
raise NotImplementedException("Not implemented") raise NotImplementedException("Not implemented")
def _session_begin(self):
pass
def _session_end(self):
pass
def ready_to_read(self): def ready_to_read(self):
raise NotImplementedException("Not implemented") raise NotImplementedException("Not implemented")
def session_begin(self):
if self.session_depth == 0:
self._session_begin()
self.session_depth += 1
def session_end(self):
self.session_depth -= 1
self.session_depth = max(0, self.session_depth)
if self.session_depth == 0:
self._session_end()
def close(self): def close(self):
self._close() self._close()