Retry with Packet.resend() instead of Packet.send() for #8

This commit is contained in:
Aaron Heise 2023-02-19 06:45:58 -06:00
parent b41bcbbc44
commit 07cf0e3d05
No known key found for this signature in database
GPG Key ID: 6BA54088C41DE8BF
3 changed files with 29 additions and 9 deletions

View File

@ -35,6 +35,10 @@ class MessageOutletBase(ABC):
def send(self, raw: bytes) -> _TReceipt:
raise NotImplemented()
@abstractmethod
def resend(self, receipt: _TReceipt) -> _TReceipt:
raise NotImplemented()
@property
@abstractmethod
def mdu(self):
@ -330,8 +334,12 @@ class Messenger(contextlib.AbstractContextManager):
state = MessageState.MSGSTATE_NEW if not message.receipt else outlet.get_receipt_state(message.receipt)
if state in [MessageState.MSGSTATE_NEW, MessageState.MSGSTATE_FAILED]:
try:
self._log.debug(f"Sending packet for {message}")
message.receipt = outlet.send(message.raw)
if message.receipt:
self._log.debug(f"Resending packet for {message}")
message.receipt = outlet.resend(message.receipt)
else:
self._log.debug(f"Sending packet for {message}")
message.receipt = outlet.send(message.raw)
except Exception as ex:
self._log.exception(f"Error sending message {message}")
elif state in [MessageState.MSGSTATE_SENT]:

View File

@ -377,10 +377,14 @@ class RNSOutlet(LSOutletBase):
def teardown(self):
self.link.teardown()
def send(self, raw: bytes) -> RNS.PacketReceipt:
def send(self, raw: bytes) -> RNS.Packet:
packet = RNS.Packet(self.link, raw)
packet.send()
return packet.receipt
return packet
def resend(self, packet: RNS.Packet) -> RNS.Packet:
packet.resend()
return packet
@property
def mdu(self) -> int:
@ -394,8 +398,8 @@ class RNSOutlet(LSOutletBase):
def is_usuable(self):
return True #self.link.status in [RNS.Link.ACTIVE]
def get_receipt_state(self, receipt: RNS.PacketReceipt) -> MessageState:
status = receipt.get_status()
def get_receipt_state(self, packet: RNS.Packet) -> MessageState:
status = packet.receipt.get_status()
if status == RNS.PacketReceipt.SENT:
return protocol.MessageState.MSGSTATE_SENT
if status == RNS.PacketReceipt.DELIVERED:

View File

@ -22,6 +22,8 @@ class Receipt:
def __init__(self, state: rnsh.protocol.MessageState, raw: bytes):
self.state = state
self.raw = raw
self.msgid = uuid.uuid4()
self.tries = 1
class MessageOutletTest(rnsh.protocol.MessageOutletBase):
@ -39,6 +41,11 @@ class MessageOutletTest(rnsh.protocol.MessageOutletBase):
self.receipts.append(receipt)
return receipt
def resend(self, receipt: Receipt) -> Receipt:
receipt.tries += 1
receipt.state = rnsh.protocol.MessageState.MSGSTATE_SENT
return receipt
def set_packet_received_callback(self, cb: Callable[[rnsh.protocol.MessageOutletBase, bytes], None]):
self.packet_callback = cb
@ -104,12 +111,13 @@ def test_send_one_retry():
receipt.state = rnsh.protocol.MessageState.MSGSTATE_FAILED
module_logger.info("set failed")
time.sleep(retry_interval)
assert len(outlet.receipts) == 2
receipt = outlet.receipts[1]
assert len(outlet.receipts) == 1
assert receipt == outlet.receipts[0]
assert receipt.state == rnsh.protocol.MessageState.MSGSTATE_SENT
assert receipt.tries == 2
receipt.state = rnsh.protocol.MessageState.MSGSTATE_DELIVERED
time.sleep(retry_interval)
assert len(outlet.receipts) == 2
assert len(outlet.receipts) == 1
assert not message.tracked