forked from circuitsacul/Starboard-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_ipc.py
50 lines (38 loc) · 1.31 KB
/
run_ipc.py
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
import asyncio
import pathlib
import signal
import ssl
from typing import Dict
import websockets
CLIENTS: Dict[str, websockets.WebSocketServerProtocol] = {}
SSL_CONTEXT = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
SSL_CONTEXT.load_cert_chain(pathlib.Path("localhost.pem"))
async def dispatch(data):
for _, client in CLIENTS.items():
await client.send(data)
async def serve(ws: websockets.WebSocketServerProtocol, path: str):
cluster_name = await ws.recv()
if isinstance(cluster_name, bytes):
cluster_name = cluster_name.decode()
if cluster_name in CLIENTS:
print(f"IPC: {cluster_name} attempted reconnection")
await ws.close(4029, "already connected")
return
CLIENTS[cluster_name] = ws
try:
await ws.send(b'{"status":"ok"}')
print(f"IPC: {cluster_name} connected successfully")
async for msg in ws:
await dispatch(msg)
finally:
CLIENTS.pop(cluster_name)
print(f"IPC: {cluster_name} disconnected")
def run():
signal.signal(signal.SIGINT, signal.SIG_DFL)
signal.signal(signal.SIGTERM, signal.SIG_DFL)
server = websockets.serve(serve, "localhost", 4000, ssl=SSL_CONTEXT)
loop = asyncio.get_event_loop()
loop.run_until_complete(server)
loop.run_forever()
if __name__ == "__main__":
run()