# -*- coding: utf-8 -*- r'''单独复现 test_package_remote.py 的第四节(Java 手表端)。 为什么单独跑: 整个测试里前三节都过了,第四节报 ConnectException: Connection refused 这不是链路抖动,是"根本没人在 8095 上监听"。 需要判断是中转进程死了,还是 Java 侧连错了地方。 做法:起中转 -> 确认存活 -> 跑 Java 测试 -> 再确认存活, 每一步都打印中转进程状态,坏在哪一步一目了然。 ''' from __future__ import annotations import json import os import shutil import subprocess import sys import tempfile import time import urllib.request import zipfile from pathlib import Path ROOT = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote") SERVER_ZIP = ROOT / "dist" / "ykxmz-server-1.8.2.zip" TOOLCHAIN = Path(r"C:\Users\shu\Desktop\deepseekagent\starklink\toolchain") JAVAC = TOOLCHAIN / "jdk17" / "bin" / "javac.exe" JAVA = TOOLCHAIN / "jdk17" / "bin" / "java.exe" RELAY_PORT = 8095 PAIR_CODE = "778899" def alive(base: str) -> str: try: with urllib.request.urlopen(base + "/v1/health", timeout=3) as r: snap = json.loads(r.read()) return "活着 设备=%d" % len(snap.get("devices", [])) except Exception as exc: # noqa: BLE001 return "!! 连不上: %s" % exc def main() -> int: work = Path(tempfile.mkdtemp(prefix="dbg_java_")) print(" 工作目录 %s" % work) with zipfile.ZipFile(SERVER_ZIP) as z: z.extractall(work) relay_dir = work / SERVER_ZIP.stem relay_py = relay_dir / "relay.py" print(" relay.py: %s" % relay_py.is_file()) data = work / "relaydata" data.mkdir(parents=True, exist_ok=True) env = dict(os.environ) env.update({ "RELAY_PORT": str(RELAY_PORT), "RELAY_BIND": "127.0.0.1", "RELAY_DATA": str(data), "RELAY_POLL": "8", "PYTHONIOENCODING": "utf-8", "PYTHONUNBUFFERED": "1", }) log = open(work / "relay.log", "wb") proc = subprocess.Popen([sys.executable, "-u", str(relay_py)], env=env, stdout=log, stderr=subprocess.STDOUT) base = "http://127.0.0.1:%d" % RELAY_PORT try: # 等中转起来 for _ in range(40): time.sleep(0.5) if alive(base).startswith("活着"): break print(" 1. 中转启动后: %s" % alive(base)) print(" 进程 poll() = %s(None 表示还活着)" % proc.poll()) # 编译 Java classes = work / "classes" classes.mkdir(parents=True, exist_ok=True) src_dir = ROOT / "android" / "src" / "com" / "lanremote" / "watch" sources = [src_dir / n for n in ("Json.java", "B64.java", "Transport.java", "RelayTransport.java", "TlsCompat.java", "RemoteClient.java", "DiscoveryCore.java")] sources.append(ROOT / "_tests" / "java" / "JavaRelayTest.java") comp = subprocess.run( [str(JAVAC), "--release", "8", "-encoding", "UTF-8", "-d", str(classes)] + [str(s) for s in sources], capture_output=True, text=True, encoding="utf-8", errors="replace") print(" 2. Java 编译: %s" % ("OK" if comp.returncode == 0 else comp.stderr[:300])) if comp.returncode != 0: return 1 print(" 编译后再看中转: %s" % alive(base)) # 跑 Java 测试 result = subprocess.run( [str(JAVA), "-Dfile.encoding=UTF-8", "-cp", str(classes), "JavaRelayTest", base, PAIR_CODE], capture_output=True, text=True, encoding="utf-8", errors="replace", timeout=120) print(" 3. Java 测试退出码 %d" % result.returncode) for line in result.stdout.splitlines(): print(" %s" % line) if result.stderr.strip(): print(" stderr: %s" % result.stderr.strip()[:400]) print(" 4. Java 跑完后中转: %s" % alive(base)) print(" 进程 poll() = %s" % proc.poll()) if proc.poll() is not None: print() print(" === 中转进程已经退出了,日志末尾 ===") log.flush() text = (work / "relay.log").read_text(encoding="utf-8", errors="replace") print(text[-2500:]) finally: try: proc.terminate() proc.wait(timeout=8) except Exception: # noqa: BLE001 proc.kill() log.close() print() print(" 日志留在: %s(没删,方便看)" % work) return 0 if __name__ == "__main__": sys.exit(main())