|
|
|
@ -1,5 +1,5 @@ |
|
|
|
|
class TinyFrame: |
|
|
|
|
def __init__(self, peer=1): |
|
|
|
|
def __init__(self, peer:int=1): |
|
|
|
|
self.write = None # the writer function should be attached here |
|
|
|
|
|
|
|
|
|
self.id_listeners = {} |
|
|
|
@ -38,6 +38,8 @@ class TinyFrame: |
|
|
|
|
|
|
|
|
|
self.reset_parser() |
|
|
|
|
|
|
|
|
|
self._CKSUM_BYTES = None # will be updated on first compose / accept |
|
|
|
|
|
|
|
|
|
def reset_parser(self): |
|
|
|
|
# parser state: SOF, ID, LEN, TYPE, HCK, PLD, PCK |
|
|
|
|
self.ps = 'SOF' |
|
|
|
@ -50,8 +52,7 @@ class TinyFrame: |
|
|
|
|
# received frame |
|
|
|
|
self.rf = TF_Msg() |
|
|
|
|
|
|
|
|
|
@property |
|
|
|
|
def _CKSUM_BYTES(self): |
|
|
|
|
def _calc_cksum_bytes(self): |
|
|
|
|
if self.CKSUM_TYPE == 'none' or self.CKSUM_TYPE is None: |
|
|
|
|
return 0 |
|
|
|
|
elif self.CKSUM_TYPE == 'xor': |
|
|
|
@ -63,7 +64,7 @@ class TinyFrame: |
|
|
|
|
else: |
|
|
|
|
raise Exception("Bad cksum type!") |
|
|
|
|
|
|
|
|
|
def _cksum(self, buffer): |
|
|
|
|
def _cksum(self, buffer) -> int: |
|
|
|
|
if self.CKSUM_TYPE == 'none' or self.CKSUM_TYPE is None: |
|
|
|
|
return 0 |
|
|
|
|
|
|
|
|
@ -84,11 +85,7 @@ class TinyFrame: |
|
|
|
|
else: |
|
|
|
|
raise Exception("Bad cksum type!") |
|
|
|
|
|
|
|
|
|
@property |
|
|
|
|
def _SOF_BYTES(self): |
|
|
|
|
return 1 if self.USE_SOF_BYTE else 0 |
|
|
|
|
|
|
|
|
|
def _gen_frame_id(self): |
|
|
|
|
def _gen_frame_id(self) -> int: |
|
|
|
|
""" |
|
|
|
|
Get a new frame ID |
|
|
|
|
""" |
|
|
|
@ -104,15 +101,15 @@ class TinyFrame: |
|
|
|
|
|
|
|
|
|
return frame_id |
|
|
|
|
|
|
|
|
|
def _pack(self, num, bytes): |
|
|
|
|
def _pack(self, num:int, bytes:int) -> bytes: |
|
|
|
|
""" Pack a number for a TF field """ |
|
|
|
|
return num.to_bytes(bytes, byteorder='big', signed=False) |
|
|
|
|
|
|
|
|
|
def _unpack(self, buf): |
|
|
|
|
def _unpack(self, buf) -> int: |
|
|
|
|
""" Unpack a number from a TF field """ |
|
|
|
|
return int.from_bytes(buf, byteorder='big', signed=False) |
|
|
|
|
|
|
|
|
|
def query(self, type, listener, pld=None, id=None): |
|
|
|
|
def query(self, type:int, listener, pld=None, id:int=None): |
|
|
|
|
""" Send a query """ |
|
|
|
|
(id, buf) = self._compose(type=type, pld=pld, id=id) |
|
|
|
|
|
|
|
|
@ -121,19 +118,21 @@ class TinyFrame: |
|
|
|
|
|
|
|
|
|
self.write(buf) |
|
|
|
|
|
|
|
|
|
def send(self, type, pld=None, id=None): |
|
|
|
|
def send(self, type:int, pld=None, id:int=None): |
|
|
|
|
""" Like query, but with no listener """ |
|
|
|
|
self.query(type=type, pld=pld, id=id, listener=None) |
|
|
|
|
|
|
|
|
|
def _compose(self, type, pld=None, id=None): |
|
|
|
|
def _compose(self, type:int, pld=None, id:int=None) -> tuple: |
|
|
|
|
""" |
|
|
|
|
Compose a frame. |
|
|
|
|
|
|
|
|
|
frame_id can be an ID of an existing session, None for a new session. |
|
|
|
|
""" |
|
|
|
|
if self._CKSUM_BYTES is None: |
|
|
|
|
self._CKSUM_BYTES = self._calc_cksum_bytes() |
|
|
|
|
|
|
|
|
|
if pld is None: |
|
|
|
|
pld = b'' |
|
|
|
|
pld = bytearray() |
|
|
|
|
|
|
|
|
|
if id is None: |
|
|
|
|
id = self._gen_frame_id() |
|
|
|
@ -162,9 +161,12 @@ class TinyFrame: |
|
|
|
|
for b in bytes: |
|
|
|
|
self.accept_byte(b) |
|
|
|
|
|
|
|
|
|
def accept_byte(self, b): |
|
|
|
|
def accept_byte(self, b:int): |
|
|
|
|
# TODO this seems ripe for rewrite to avoid repetitive code |
|
|
|
|
|
|
|
|
|
if self._CKSUM_BYTES is None: |
|
|
|
|
self._CKSUM_BYTES = self._calc_cksum_bytes() |
|
|
|
|
|
|
|
|
|
if self.ps == 'SOF': |
|
|
|
|
if self.USE_SOF_BYTE: |
|
|
|
|
if b != self.SOF_BYTE: |
|
|
|
@ -302,9 +304,9 @@ class TinyFrame: |
|
|
|
|
if rv == TF.CLOSE: |
|
|
|
|
self.fallback_listener = None |
|
|
|
|
|
|
|
|
|
def add_id_listener(self, id, lst, lifetime=None): |
|
|
|
|
def add_id_listener(self, id:int, lst, lifetime:float=None): |
|
|
|
|
""" |
|
|
|
|
Add a ID listener that expires in "lifetime" ticks |
|
|
|
|
Add a ID listener that expires in "lifetime" seconds |
|
|
|
|
|
|
|
|
|
listener function takes two arguments: |
|
|
|
|
tinyframe instance and a msg object |
|
|
|
@ -315,7 +317,7 @@ class TinyFrame: |
|
|
|
|
'age': 0, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
def add_type_listener(self, type, lst): |
|
|
|
|
def add_type_listener(self, type:int, lst): |
|
|
|
|
""" |
|
|
|
|
Add a type listener |
|
|
|
|
|
|
|
|
|