-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcentral_server.py
executable file
·81 lines (61 loc) · 2.17 KB
/
central_server.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/python3
import jamulus
import argparse
import signal
import sys
def argument_parser():
parser = argparse.ArgumentParser()
parser.add_argument("--port", type=int, default=jamulus.DEFAULT_PORT, help="local port number")
parser.add_argument(
"--log-data",
action="store_true",
help="log protocol data",
)
return parser.parse_args()
def main():
# get arguments
args = argument_parser()
# create jamulus connector
jc = jamulus.JamulusConnector(port=args.port, log_data=args.log_data)
# create empty server list
server_list = {}
# receive messages indefinitely
while True:
addr, key, count, values = jc.recvfrom()
if key == "AUDIO":
# stop clients from connecting
jc.sendto(addr, "CLM_DISCONNECTION")
elif key in ["CLM_REGISTER_SERVER", "CLM_REGISTER_SERVER_EX"]:
# add server to list
values["ip"] = addr[0]
server_list[addr] = values
print("registering server\n{}".format(values))
# send successful registration response
jc.sendto(addr, "CLM_REGISTER_SERVER_RESP", {"status": 0})
elif key == "CLM_UNREGISTER_SERVER":
print("unregistering server")
# remove server from list
if addr in server_list.keys():
del server_list[addr]
elif key == "CLM_REQ_SERVER_LIST":
# send server list with dummy entry in first position
server_list_send = [
{
"ip": "0.0.0.0",
"port": 0,
"country_id": 0,
"max_clients": 0,
"permanent": 0,
"name": "",
"internal_address": "",
"city": "",
}
] + list(server_list.values())
print("sending {} servers\n{}".format(len(server_list_send), server_list_send))
jc.sendto(addr, "CLM_SERVER_LIST", server_list_send)
def signal_handler(sig, frame):
print()
sys.exit(0)
if __name__ == "__main__":
signal.signal(signal.SIGINT, signal_handler)
main()