Skip to content

Commit fcf5f2e

Browse files
committed
acknowledge messages with IDs the library doesn't know
1 parent b13f1e5 commit fcf5f2e

2 files changed

Lines changed: 21 additions & 15 deletions

File tree

jamulus.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -757,14 +757,18 @@ def main_pack(self, key, values, count):
757757

758758
return data
759759

760-
def main_unpack(self, data):
760+
def main_unpack(self, data, ackn, addr):
761761
"""
762762
Decode a Jamulus 'main frame'
763763
764764
Parameters
765765
----------
766766
data : bytearray
767767
encoded data
768+
ackn : bool
769+
send acknowledgement messages when needed
770+
addr : tuple(str, int)
771+
host/port for sending the acknoledgement
768772
769773
Returns
770774
-------
@@ -785,13 +789,18 @@ def main_unpack(self, data):
785789

786790
# unpack main frame
787791
main_values, offset = self.unpack(FORMAT["MAIN_FRAME"], data)
792+
id = main_values["id"]
793+
count = main_values["count"]
788794

789795
# verify there's no data left
790796
if offset != len(data):
791797
raise ValueError("invalid message length ({}/{}) {}".format(offset, len(data)))
792798

799+
# send acknowledgement
800+
if ackn:
801+
self.send_ack(addr, id, count)
802+
793803
# verify ID is valid
794-
id = main_values["id"]
795804
if id not in MSG_KEYS.keys() or id == 0:
796805
raise ValueError("invalid message ID ({})".format(id))
797806

@@ -803,24 +812,23 @@ def main_unpack(self, data):
803812
# unpack data
804813
values = self.prot_unpack(format, main_values["data"], repeat=repeat)
805814

806-
return key, main_values["count"], values
815+
return key, count, values
807816

808-
def send_ack(self, addr, key, count):
817+
def send_ack(self, addr, id, count):
809818
"""
810819
Send an acknowledgement message
811820
812-
acknowledgement message is only sent if required for the given key
821+
acknowledgement message is only sent if required for the given ID
813822
814823
Parameters
815824
----------
816825
addr : tuple(str, int)
817826
host/port to send to
818-
key : str
819-
key of message that gets acknowledged
827+
id : int
828+
ID of message that gets acknowledged
820829
count : int
821-
count of the message that gets acknowledged
830+
count of message that gets acknowledged
822831
"""
823-
id = MSG_IDS.get(key, 0)
824832
if id > MSG_IDS["ACKN"] and id < MSG_IDS["CLM_START"]:
825833
self.sendto(
826834
addr=addr,
@@ -856,7 +864,7 @@ def log_message(self, addr, key, count="-", length="", values=None, recv=True):
856864
key,
857865
length,
858866
)
859-
if self.log_data and key != "ACKN" and values is not None and len(values) > 0:
867+
if self.log_data and values is not None and len(values) > 0:
860868
output += " {}".format(values)
861869
print(output)
862870

@@ -931,10 +939,8 @@ def recvfrom(self, timeout=None, ackn=True, bufsize=MAX_SIZE_BYTES_NETW_BUF):
931939
try:
932940
# detect protocol messages
933941
if len(data) >= 9 and data[:2] == b"\x00\x00":
934-
key, count, values = self.main_unpack(data)
942+
key, count, values = self.main_unpack(data, ackn, addr)
935943
self.log_message(addr, key, count=count, length=len(data), values=values, recv=True)
936-
if ackn:
937-
self.send_ack(addr, key, count)
938944

939945
# assume audio messages
940946
elif len(data) >= 1:

test_jamulus.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ def test_main_pack(self):
9494
self.assertEqual(data.hex(), "0000e903000400000000006f60")
9595

9696
def test_main_unpack(self):
97-
key, count, values = self.jc.main_unpack(bytearray.fromhex("0000ef0300000018cb"))
97+
key, count, values = self.jc.main_unpack(bytearray.fromhex("0000ef0300000018cb"), ackn=False, addr=None)
9898
self.assertEqual(key, "CLM_REQ_SERVER_LIST")
9999
self.assertEqual(count, 0)
100100
self.assertEqual(values, {})
101101

102-
key, count, values = self.jc.main_unpack(bytearray.fromhex("0000e903000400000000006f60"))
102+
key, count, values = self.jc.main_unpack(bytearray.fromhex("0000e903000400000000006f60"), ackn=False, addr=None)
103103
self.assertEqual(key, "CLM_PING_MS")
104104
self.assertEqual(count, 0)
105105
self.assertEqual(values, {"time": 0})

0 commit comments

Comments
 (0)