"""Project 3's Go-Back-N sender, with its fixed window replaced by a congestion controller. Given. In Project 3 the sender kept `window` packets in flight, a number you chose. Here the number changes as the transfer runs, and it is the smaller of two: window = min(cwnd, rwnd) cwnd is the congestion window, the controller's guess at what the network can carry, and rwnd is what the receiver says it has room for. This file does the sending, the timer and the bookkeeping. It calls your controller (congestion.py) at three moments and asks it for the window before every send: cc.on_new_ack(n) an ACK moved the base forward by n packets cc.on_dup_ack(k) the k-th duplicate ACK in a row arrived; return True to resend the base packet now (fast retransmit) cc.on_timeout() the timer for the oldest packet ran out After a timeout the sender goes back to the base and resends from there, as much as the window allows: that part is Project 3's Go-Back-N. Every event is written to `self.trace`, which run.py saves as a CSV. The retransmission timer is fixed (`timeout`), as in Project 3. Real TCP measures the round trip and sets its timer from it (RFC 6298). References: RFC 5681, TCP congestion control Peterson and Davie, section 6.3, TCP congestion control """ import socket import time import packet from receiver import rwnd_of class Sender: def __init__(self, sock, peer, cc, timeout=0.25, max_fin_tries=20): self.sock, self.peer, self.cc = sock, peer, cc self.timeout = timeout self.max_fin_tries = max_fin_tries self.stats = {"data_packets": 0, "transmissions": 0, "retransmissions": 0, "fast_retransmits": 0, "timeouts": 0} self.trace = [] self._start = None def _note(self, event, base, nxt): cc = self.cc self.trace.append({"t_ms": round((time.monotonic() - self._start) * 1000, 1), "event": event, "cwnd": round(cc.cwnd, 2), "ssthresh": round(cc.ssthresh, 2), "rwnd": cc.rwnd, "window": cc.window(), "base": base, "in_flight": nxt - base}) def _transmit(self, seq, chunk, sent_once): self.sock.sendto(packet.encode(packet.DATA, seq, chunk), self.peer) self.stats["transmissions"] += 1 if seq in sent_once: self.stats["retransmissions"] += 1 sent_once.add(seq) def send(self, data): """Deliver `data`, letting the controller set the window. Returns the stats dict.""" self._start = time.monotonic() chunks = packet.split(data) self.stats["data_packets"] = len(chunks) sent_once = set() base = nxt = dups = 0 timer = None self._note("start", base, nxt) while base < len(chunks): while nxt < len(chunks) and nxt < base + self.cc.window(): self._transmit(nxt, chunks[nxt], sent_once) nxt += 1 if timer is None: timer = time.monotonic() + self.timeout wait = (timer - time.monotonic()) if timer is not None else self.timeout self.sock.settimeout(max(0.001, wait)) try: got = packet.decode(self.sock.recvfrom(2048)[0]) except socket.timeout: got = None if got and got[0] == packet.ACK: rwnd = rwnd_of(got[2]) if rwnd is not None: self.cc.set_rwnd(rwnd) ack = got[1] if ack > base: newly, base, dups = ack - base, ack, 0 nxt = max(nxt, base) self.cc.on_new_ack(newly) timer = None if base == nxt else time.monotonic() + self.timeout self._note("ack", base, nxt) elif ack == base and nxt > base: dups += 1 if self.cc.on_dup_ack(dups): self._transmit(base, chunks[base], sent_once) self.stats["fast_retransmits"] += 1 self._note("fast_retransmit", base, nxt) else: self._note("dup_ack", base, nxt) elif nxt == base: self._note("window_update", base, nxt) if timer is not None and time.monotonic() >= timer: self.cc.on_timeout() self.stats["timeouts"] += 1 nxt, dups = base, 0 # go back to the base, as Project 3 did timer = time.monotonic() + self.timeout self._note("timeout", base, nxt) self._close(len(chunks)) self.stats["seconds"] = round(time.monotonic() - self._start, 3) return self.stats def _close(self, count): fin = packet.encode(packet.FIN, count) for _ in range(self.max_fin_tries): self.sock.sendto(fin, self.peer) self.sock.settimeout(self.timeout) try: got = packet.decode(self.sock.recvfrom(2048)[0]) except socket.timeout: continue if got and got[0] == packet.FINACK: return