Project: reliable delivery

Deliver a report over a lossy radio with packets, acknowledgements and resending.

F10.9NetworksGCSE25 min

Covers OCR J277 1.3.2 Wired and wireless networks, protocols and layers · AQA 8525 3.5 Fundamentals of computer networks · Edexcel 1CP2 4.1.6 Protocols

Do this lesson in the simulator

Radio messages get lost. On a real network, packets are dropped by busy routers and damaged by interference, yet your downloads still arrive perfectly. The trick is in TCP: number every packet, wait for the receiver to say it arrived, and send again if it did not. In this project you build that protocol and use it to get a report safely to the Base robot.

The protocol

The Base follows these rules:

  • Every packet is <number>/<total>:<piece>.
  • When a packet arrives safely, the Base replies ack <number>: acknowledged.
  • When a packet arrives damaged, it replies resend <number>.

Your robot must follow the rules on its side:

  1. Send one packet.
  2. Wait up to one second for a reply.
  3. If the reply is ack with this packet's number, move on to the next packet.
  4. Otherwise, whether the reply was resend or nothing came at all, send the same packet again.

This is called stop-and-wait: the sender stops after each packet and waits for it to be acknowledged.

A timeline between your robot and the Base: packet 1 arrives and gets ack 1, so the robot moves on; packet 2 arrives damaged and gets resend 2, so the robot sends the same packet again; a packet that is lost gets no reply, so after one second the robot sends it againyour robotBasearrives safely1/3:robot 7 reportack 1ack with this number:move onarrives damaged2/3:ing: mat cleardamagedresend 2not an ack:send it again2/3:ing: mat clearlost on the waya packetlost1 second,no reply:send it again
Your robot only ever has one packet on its way. It moves on only when the reply is the ack for that packet's number; a resend, or silence for a whole second, both mean the same thing: send that packet again.

Try one packet

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

def wait_reply(seconds=1.0):
    for tick in range(int(seconds * 10)):
        wait(0.1)
        for sender, text in messages():
            return text
    return None

send("1/3:robot 7 report")
print("the base says:", wait_reply())
send("2/3:ing: mat clear")
print("the base says:", wait_reply())

Run this in the simulator

The second packet was damaged on the way, so the Base asks for it again. Your program has to notice and send it again, however many times it takes.

Plan it first

  • a function to split the message into packets (you wrote one in lesson F10.5);
  • a loop over the packets;
  • inside it, a loop that sends and waits until this packet is acknowledged;
  • a count of every transmission, including the ones sent again.

Task: deliver the report

Split message into packets of 14 characters, in the form <number>/<total>:<piece>. Deliver them to the Base with stop-and-wait. Print sent <number> each time you send a packet (including when you send it again) and print each reply you get. Never send the next packet before the last one is acknowledged. When every packet is acknowledged, print delivered in <n> transmissions, and turn the LED green.

# the two lines every program starts with: the commands, then the robot
from bugbot import *
connect()

message = "robot 7 reporting: mat clear, battery ok"

Challenges

  1. Give up after 5 tries of the same packet, and print failed at packet <n>.
  2. Send all three packets at once, then resend only the ones not acknowledged. How many transmissions does that take?
  3. Why would a live video call use UDP rather than waiting for acknowledgements?