|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import argparse |
| 4 | +import sys |
| 5 | +import time |
| 6 | + |
| 7 | +from .raw_services import DEFAULT_RAW_SERVICES, is_supported_raw_service |
| 8 | + |
| 9 | + |
| 10 | +def main() -> int: |
| 11 | + parser = argparse.ArgumentParser(description="Raw capnp relay standalone reader") |
| 12 | + parser.add_argument("service", help="service name to subscribe to") |
| 13 | + parser.add_argument("--iterations", type=int, default=0, help="0 means forever") |
| 14 | + args = parser.parse_args() |
| 15 | + |
| 16 | + service = args.service.strip() |
| 17 | + if not is_supported_raw_service(service): |
| 18 | + print(f"unsupported raw service: {service}", file=sys.stderr) |
| 19 | + print("supported services:", ", ".join(DEFAULT_RAW_SERVICES), file=sys.stderr) |
| 20 | + return 2 |
| 21 | + |
| 22 | + try: |
| 23 | + from cereal import messaging |
| 24 | + except Exception as exc: |
| 25 | + print(f"messaging import failed: {exc}", file=sys.stderr) |
| 26 | + return 1 |
| 27 | + |
| 28 | + try: |
| 29 | + sock = messaging.sub_sock(service, conflate=True) |
| 30 | + except Exception as exc: |
| 31 | + print(f"failed to open socket for {service}: {exc}", file=sys.stderr) |
| 32 | + return 1 |
| 33 | + |
| 34 | + count = 0 |
| 35 | + while True: |
| 36 | + payload = sock.receive(non_blocking=False) |
| 37 | + if payload is None: |
| 38 | + continue |
| 39 | + print(f"{int(time.time() * 1000)} {service} {len(payload)}", flush=True) |
| 40 | + count += 1 |
| 41 | + if args.iterations > 0 and count >= args.iterations: |
| 42 | + return 0 |
| 43 | + |
| 44 | + |
| 45 | +if __name__ == "__main__": |
| 46 | + raise SystemExit(main()) |
0 commit comments