#!/usr/bin/env bash
# Real demos for P148 "How TCP Sockets Work". Made on the Mac (Docker Desktop).
# A pizza kitchen's order system: four containers from one stock Ubuntu 24.04 image (lab/Dockerfile: Ubuntu's own
# Python 3.12, ss and tcpdump) on one private Docker network, 10.48.0.0/24:
#   kitchen   10.48.0.10   the kitchen's order screen: lab/kitchen.py listens on TCP port 9000
#   tablet1   10.48.0.21   the waiter's tablet Ana uses on screen: lab/tablet.py sends orders to kitchen:9000
#   tablet2   10.48.0.22   two more waiters' tablets, connected off screen
#   tablet3   10.48.0.23
# Every command shown is typed into a real interactive bash in a pty, one window per terminal (demo_driver.py);
# every connection, ticket, error and packet is real. TSO/GSO/GRO are off so tcpdump sees each real packet.
set -euo pipefail
cd "$(dirname "$0")"
P=ce-p148
BOXES="kitchen:10.48.0.10 tablet1:10.48.0.21 tablet2:10.48.0.22 tablet3:10.48.0.23"
cleanup() {
  for b in $BOXES; do docker rm -f $P-${b%:*} >/dev/null 2>&1 || true; done
  docker network rm $P-net >/dev/null 2>&1 || true
}
cleanup
[ -n "${KEEP:-}" ] || trap cleanup EXIT
docker build -q -t $P lab >/dev/null
docker network create --subnet 10.48.0.0/24 --gateway 10.48.0.1 $P-net >/dev/null
for b in $BOXES; do
  n=${b%:*}
  docker run -d --name $P-$n --hostname $n --network $P-net --network-alias $n --ip ${b#*:} \
    --cap-add NET_ADMIN --cap-add NET_RAW $P >/dev/null
  docker exec $P-$n ethtool -K eth0 tso off gso off gro off >/dev/null 2>&1 || true
done
python3 demo_driver.py $P
