reliable-transport/sr.py
2026-10-19 13:24:25 -06:00

47 lines
1.8 KiB
Python

"""Selective Repeat. Your group writes the sender and the receiver.
Selective Repeat also keeps up to `window` packets in flight, but ACKs are
individual: ACK n means "I have packet n", nothing more. The sender keeps a
timer for each packet and resends only the ones that time out. The receiver
keeps packets that arrive early, inside its window, and hands the data over
in order once the gap before them fills. P&D 2.5.2 describes both sides.
"""
import socket
import time
import packet
import stopwait
class Sender(stopwait.Sender):
def __init__(self, sock, peer, window=8, timeout=0.1, max_fin_tries=20):
super().__init__(sock, peer, window, timeout, max_fin_tries)
self.window = window
def send(self, data):
"""Deliver `data` reliably, resending only what times out, then close.
YOUR TASK. The window starts at the oldest packet not yet ACKed, and it
slides when that packet is ACKed. Count transmissions and
retransmissions in self.stats. Finish with self._close(number of data
packets).
"""
raise NotImplementedError("sr.Sender.send")
class Receiver(stopwait.Receiver):
def __init__(self, sock, window=8, linger=0.5):
super().__init__(sock, linger)
self.window = window
raise NotImplementedError("sr.Receiver") # delete this line when receive() is written
def receive(self):
"""Return every byte the sender sent, in order.
YOUR TASK. ACK each data packet you can use by its own number. Keep a
packet that arrives ahead of a gap, as long as it is inside your
window. When the FIN arrives and nothing is missing, finish the way
stop-and-wait does, with self._finish(peer).
"""
raise NotImplementedError("sr.Receiver.receive")