Files
lotherk dc44fc25a3 dtchat: add the headless peer mode (T5.7)
dtchat-headless starts the TLS listener (--listen [HOST:]PORT, port 0 prints the real port) and one reconnecting link per --peer, loads/creates the identity in --config, prints JSON events (ready, peer-online, peer-offline, message, typing, file-offer, error) and takes JSON commands on stdin (send by host:port or peer id, quit). dc_local_peer_conn() and the ack id let a link know its peer. tests/p2p.sh runs two instances over loopback: A listens, B connects, B sends, A answers by id, both quit; standalone run prints 'tests/p2p.sh: all ok'. Wired into noinst_PROGRAMS and mcde-tests.sh (expect 29 ok). make mcde-host still blocked by pid 2126511 (release/sourcesets/usb).
2026-11-16 20:43:06 +00:00

94 lines
2.7 KiB
Bash
Executable File

#!/bin/sh
# License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
#
# p2p.sh -- two headless dtchat instances talk over loopback (T5.7)
#
# usage: tests/p2p.sh [path-to-dtchat-headless]
# default: build/mcde-host/programs/dtchat/dtchat-headless
#
# A listens on an ephemeral port, B connects. B sends a message, A answers
# the peer by its id, both quit. Every step must show up in the JSON log.
set -u
TOP=$(cd "$(dirname "$0")/.." && pwd)
BIN=${1:-"$TOP/../../../build/mcde-host/programs/dtchat/dtchat-headless"}
case $BIN in
/*) ;;
*) BIN=$(cd "$(dirname "$BIN")" && pwd)/$(basename "$BIN") ;;
esac
[ -x "$BIN" ] || { echo "p2p: no $BIN" >&2; exit 2; }
TMP=$(mktemp -d /tmp/dtchat-p2p-XXXXXX) || exit 2
A_IN=$TMP/a.in
B_IN=$TMP/b.in
mkfifo "$A_IN" "$B_IN" || exit 2
exec 3<>"$A_IN"
exec 4<>"$B_IN"
cleanup() {
exec 3>&- 2>/dev/null
exec 4>&- 2>/dev/null
[ -n "${PA:-}" ] && kill "$PA" 2>/dev/null
[ -n "${PB:-}" ] && kill "$PB" 2>/dev/null
rm -rf "$TMP"
}
wait_for() { # file, fixed string, tries
i=0
while [ "$i" -lt "$3" ]; do
if grep -q -F "$2" "$1" 2>/dev/null; then
return 0
fi
sleep 0.2
i=$((i + 1))
done
return 1
}
fail() {
echo "p2p: FAIL $*" >&2
echo "--- a.log" >&2
cat "$TMP/a.log" >&2 2>/dev/null
echo "--- b.log" >&2
cat "$TMP/b.log" >&2 2>/dev/null
cleanup
exit 1
}
trap cleanup EXIT INT TERM
"$BIN" --config "$TMP/a" --listen 127.0.0.1:0 <&3 >"$TMP/a.log" 2>"$TMP/a.err" &
PA=$!
wait_for "$TMP/a.log" '"event":"ready"' 50 || fail "A did not become ready"
PORT=$(sed -n 's/.*"listen":\([0-9][0-9]*\).*/\1/p' "$TMP/a.log" | head -1)
[ -n "$PORT" ] || fail "no listen port"
echo "p2p: A listens on $PORT"
"$BIN" --config "$TMP/b" --peer "127.0.0.1:$PORT" <&4 >"$TMP/b.log" 2>"$TMP/b.err" &
PB=$!
wait_for "$TMP/a.log" '"event":"peer-online"' 50 || fail "A saw no peer"
wait_for "$TMP/b.log" '"event":"peer-online"' 50 || fail "B saw no peer"
echo "p2p: both peers are online"
AID=$(sed -n 's/.*"event":"peer-online".*"id":"\([^"]*\)".*/\1/p' "$TMP/a.log" | tail -1)
[ -n "$AID" ] || fail "no peer id on A"
echo "{\"cmd\":\"send\",\"peer\":\"127.0.0.1:$PORT\",\"text\":\"hello from b\"}" >&4
wait_for "$TMP/a.log" '"text":"hello from b"' 50 || fail "A got no message from B"
echo "p2p: B -> A message arrived"
# A answers the connecting peer by the id it learned
echo "{\"cmd\":\"send\",\"peer\":\"$AID\",\"text\":\"reply from a\"}" >&3
wait_for "$TMP/b.log" '"text":"reply from a"' 50 || fail "B got no reply from A"
echo "p2p: A -> B reply arrived"
echo "{\"cmd\":\"quit\"}" >&4
echo "{\"cmd\":\"quit\"}" >&3
wait "$PB" 2>/dev/null || fail "B did not exit cleanly"
PB=
wait "$PA" 2>/dev/null || fail "A did not exit cleanly"
PA=
echo "p2p: both exited"
echo "tests/p2p.sh: all ok"