44 lines
1.6 KiB
Python
Executable file
44 lines
1.6 KiB
Python
Executable file
#!/usr/bin/env python3
|
|
import argparse
|
|
import json
|
|
import socket
|
|
import sys
|
|
import threading
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="HoloLake GLP local realtime connector")
|
|
parser.add_argument("--descriptor", required=True)
|
|
parser.add_argument("--bridge-id", required=True)
|
|
parser.add_argument("--persona-id")
|
|
parser.add_argument("--self-test", action="store_true")
|
|
args = parser.parse_args()
|
|
descriptor = json.load(open(args.descriptor, encoding="utf-8"))
|
|
if descriptor.get("protocol") != "GLP_LOCAL_REALTIME/1":
|
|
raise SystemExit("descriptor protocol rejected")
|
|
host_port = descriptor["endpoint"].removeprefix("tcp://").split(":", 1)
|
|
connection = socket.create_connection((host_port[0], int(host_port[1])), timeout=5)
|
|
stream = connection.makefile("rwb", buffering=0)
|
|
hello = {"messageType": "hello", "protocol": descriptor["protocol"], "token": descriptor["token"], "bridgeId": args.bridge_id, "personaId": args.persona_id}
|
|
stream.write((json.dumps(hello, ensure_ascii=False) + "\n").encode())
|
|
welcome = stream.readline().decode().strip()
|
|
print(welcome, flush=True)
|
|
if args.self_test:
|
|
payload = json.loads(welcome)
|
|
raise SystemExit(0 if payload.get("type") == "welcome" else 1)
|
|
|
|
def receive():
|
|
while True:
|
|
line = stream.readline()
|
|
if not line:
|
|
return
|
|
print(line.decode().rstrip(), flush=True)
|
|
|
|
threading.Thread(target=receive, daemon=True).start()
|
|
for line in sys.stdin:
|
|
if line.strip():
|
|
stream.write((line.rstrip("\n") + "\n").encode())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|