82 lines
2.8 KiB
Python
82 lines
2.8 KiB
Python
"""Run against the real server through the local OpenSSH dynamic forward."""
|
|
from contextlib import contextmanager
|
|
import argparse
|
|
import json
|
|
import socket
|
|
import ssl
|
|
import struct
|
|
import time
|
|
from pathlib import Path
|
|
|
|
|
|
def exact(stream, n):
|
|
result = bytearray()
|
|
while len(result) < n:
|
|
chunk = stream.recv(n - len(result))
|
|
if not chunk:
|
|
raise EOFError("truncated message")
|
|
result.extend(chunk)
|
|
return result
|
|
|
|
|
|
def send(stream, kind, data):
|
|
stream.sendall(struct.pack(">4sHHQ", b"RPS1", 1, kind, len(data)) + data)
|
|
|
|
|
|
def receive(stream):
|
|
magic, version, kind, n = struct.unpack(">4sHHQ", exact(stream, 16))
|
|
assert (magic, version) == (b"RPS1", 1) and n <= 33554464
|
|
data = exact(stream, n)
|
|
if kind == 6:
|
|
raise ValueError(data[4:].decode())
|
|
return kind, data
|
|
|
|
|
|
@contextmanager
|
|
def connection(ca, port=11080, hostname="infra-gpu"):
|
|
ctx = ssl.create_default_context(cafile=ca)
|
|
ctx.minimum_version = ctx.maximum_version = ssl.TLSVersion.TLSv1_3
|
|
with socket.create_connection(("127.0.0.1", port), timeout=120) as sock:
|
|
sock.sendall(bytes.fromhex("05 01 00"))
|
|
assert exact(sock, 2) == bytes.fromhex("05 00")
|
|
sock.sendall(bytes.fromhex("05 01 00 01 7f 00 00 01") + struct.pack(">H", 7443))
|
|
assert exact(sock, 4) == bytes.fromhex("05 00 00 01")
|
|
exact(sock, 6)
|
|
with ctx.wrap_socket(sock, server_hostname=hostname) as tls:
|
|
yield tls
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument("--ca", required=True)
|
|
parser.add_argument("--config", required=True)
|
|
parser.add_argument("--delay", type=float, default=0)
|
|
parser.add_argument("--socks-port", type=int, default=11080)
|
|
args = parser.parse_args()
|
|
raw = Path(args.config).read_bytes()
|
|
config = json.loads(raw)
|
|
g = config["simulation"]["generations"]
|
|
view = config["view"]
|
|
k = view["aggregation_side"]
|
|
aw, ah = (view["roi"][key] // k for key in ("width", "height"))
|
|
with connection(args.ca,args.socks_port) as tls:
|
|
send(tls, 1, raw)
|
|
assert receive(tls) == (2, raw)
|
|
last = -1
|
|
count = 0
|
|
while last < g:
|
|
kind, data = receive(tls)
|
|
frame, gen, w, h, side, fmt = struct.unpack_from(">QQIIII", data)
|
|
assert kind == 3 and frame == count + 1 and last < gen <= g
|
|
assert count or gen == 0
|
|
assert (w, h, side, fmt) == (aw, ah, k, 1) and len(data) == 32 + w*h*4
|
|
assert all(alpha == 255 for alpha in data[35::4])
|
|
time.sleep(args.delay)
|
|
send(tls, 4, struct.pack(">Q", frame))
|
|
count, last = frame, gen
|
|
assert receive(tls) == (5, struct.pack(">QI", g, 0))
|
|
print(f"PASS generation={last} frames={count}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|