Giant blob of minor changes
[dotfiles/.git] / .local / bin / rpyc_registry.py
1 #!/usr/bin/python
2 """
3 The registry server listens to broadcasts on UDP port 18812, answering to
4 discovery queries by clients and registering keepalives from all running
5 servers. In order for clients to use discovery, a registry service must
6 be running somewhere on their local network.
7 """
8 from plumbum import cli
9 from rpyc.utils.registry import REGISTRY_PORT, DEFAULT_PRUNING_TIMEOUT
10 from rpyc.utils.registry import UDPRegistryServer, TCPRegistryServer
11 from rpyc.lib import setup_logger
12
13
14 class RegistryServer(cli.Application):
15     mode = cli.SwitchAttr(["-m", "--mode"], cli.Set("UDP", "TCP"), default="UDP",
16                           help="Serving mode")
17
18     ipv6 = cli.Flag(["-6", "--ipv6"], help="use ipv6 instead of ipv4")
19
20     port = cli.SwitchAttr(["-p", "--port"], cli.Range(0, 65535), default=REGISTRY_PORT,
21                           help="The UDP/TCP listener port")
22
23     logfile = cli.SwitchAttr(["--logfile"], str, default=None,
24                              help="The log file to use; the default is stderr")
25
26     quiet = cli.SwitchAttr(["-q", "--quiet"], help="Quiet mode (only errors are logged)")
27
28     pruning_timeout = cli.SwitchAttr(["-t", "--timeout"], int,
29                                      default=DEFAULT_PRUNING_TIMEOUT, help="Set a custom pruning timeout (in seconds)")
30
31     def main(self):
32         if self.mode == "UDP":
33             server = UDPRegistryServer(host='::' if self.ipv6 else '0.0.0.0', port=self.port,
34                                        pruning_timeout=self.pruning_timeout)
35         elif self.mode == "TCP":
36             server = TCPRegistryServer(port=self.port, pruning_timeout=self.pruning_timeout)
37         setup_logger(self.quiet, self.logfile)
38         server.start()
39
40
41 if __name__ == "__main__":
42     RegistryServer.run()