-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathecho.py
More file actions
63 lines (53 loc) · 2.14 KB
/
Copy pathecho.py
File metadata and controls
63 lines (53 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
"""Echo service — registers `echo` and replies to every chat with `echo: <text>`.
Doubles as the manual smoke test for the ensemble Python client.
Usage:
python echo.py --socket /run/ensemble/sock --auth-seed /path/to/seed
python echo.py --addr localhost:9090 --auth-seed /path/to/seed
"""
from __future__ import annotations
import argparse
import asyncio
import sys
from ensemble import (
ACL,
ChatMessage,
Client,
ConnectionRequest,
)
async def main() -> int:
parser = argparse.ArgumentParser()
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument("--socket", help="Path to the daemon's gRPC unix socket")
group.add_argument("--addr", help="Daemon gRPC TCP address, e.g. localhost:9090")
parser.add_argument("--auth-seed", help="Path to the admin-key seed file")
parser.add_argument("--name", default="echo", help="Service name to register (default: echo)")
parser.add_argument("--tls", action="store_true", help="Use TLS for the gRPC channel")
args = parser.parse_args()
async with Client(
socket_path=args.socket,
addr=args.addr,
tls=args.tls,
auth_seed=args.auth_seed,
) as client:
async with await client.register(
args.name,
acl=ACL.CONTACTS,
description="echoes incoming chat messages",
) as svc:
print(f"Registered '{args.name}' as {svc.address} ({svc.onion})", flush=True)
async for ev in svc.events():
if isinstance(ev, ChatMessage):
print(f" <- {ev.from_addr}: {ev.text}", flush=True)
reply = f"echo: {ev.text}"
await svc.send_message(ev.from_addr, reply)
print(f" -> {ev.from_addr}: {reply}", flush=True)
elif isinstance(ev, ConnectionRequest):
print(f" accepting connection from {ev.from_addr}", flush=True)
await svc.accept_connection(ev.request_id)
return 0
if __name__ == "__main__":
try:
sys.exit(asyncio.run(main()))
except KeyboardInterrupt:
sys.exit(130)