diff --git a/src/announce_server/__main__.py b/src/announce_server/__main__.py index 92302da..649f35f 100644 --- a/src/announce_server/__main__.py +++ b/src/announce_server/__main__.py @@ -1,6 +1,7 @@ import argparse from announce_server import register_service +from announce_server.client import start_client from announce_server.server import start_server @@ -31,6 +32,20 @@ def main(): help="Heartbeat timeout in seconds", ) + # Start client subcommand + start_client_parser = subparsers.add_parser( + "start_client", help="Start the client server" + ) + start_client_parser.add_argument( + "--host-ip", + type=str, + default="0.0.0.0", + help="Host IP address (default: 0.0.0.0)", + ) + start_client_parser.add_argument( + "--host-port", type=int, default=4999, help="Host port number (default: 4999)" + ) + args = parser.parse_args() if args.command == "start_registry": @@ -40,6 +55,8 @@ def main(): heartbeat_interval=args.heartbeat_interval, heartbeat_timeout=args.heartbeat_timeout, ) + elif args.command == "start_client": + start_client(host_ip=args.host_ip, host_port=args.host_port) else: parser.print_help() diff --git a/src/announce_server/client.py b/src/announce_server/client.py new file mode 100644 index 0000000..7a01bfc --- /dev/null +++ b/src/announce_server/client.py @@ -0,0 +1,49 @@ +import argparse +import asyncio + +import socketio + +sio = socketio.AsyncClient() + + +@sio.event +async def connect(): + print("Connected to the host!") + + +@sio.event +async def disconnect(): + print("Disconnected from the host.") + + +@sio.event +async def heartbeat(): + print("Received heartbeat from the host.") + + +async def main(host_ip, host_port): + print(f"Connecting to host at {host_ip}:{host_port}") + await sio.connect(f"http://{host_ip}:{host_port}") + await sio.wait() + + +def start_client(host_ip="0.0.0.0", host_port=4999): + asyncio.run(main(host_ip, host_port)) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Start announce_server client.") + + parser.add_argument( + "--host-ip", + type=str, + default="127.0.0.1", + help="Host IP address (default: 127.0.0.1)", + ) + parser.add_argument( + "--host-port", type=int, default=4999, help="Host port number (default: 4999)" + ) + + args = parser.parse_args() + + start_client(args.host_ip, args.host_port) diff --git a/src/announce_server/server.py b/src/announce_server/server.py index da0ddbe..dde2f76 100644 --- a/src/announce_server/server.py +++ b/src/announce_server/server.py @@ -1,3 +1,4 @@ +import argparse import asyncio import signal @@ -142,7 +143,7 @@ def start_server(address, port, heartbeat_interval, heartbeat_timeout): # heartbeat(sio, heartbeat_interval, heartbeat_timeout) # ) # aiohttp_app = loop.create_task(web._run_app(app, host=address, port=port)) - + # Python 3.6+ compatible. Supports any awaitable: heartbeat_task = asyncio.ensure_future( heartbeat(sio, heartbeat_interval, heartbeat_timeout) @@ -164,9 +165,28 @@ def start_server(address, port, heartbeat_interval, heartbeat_timeout): if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Start announce_server client.") + parser.add_argument("--ip", default="0.0.0.0", help="IP address of the host server") + parser.add_argument( + "--port", default=4999, type=int, help="Port of the host server" + ) + parser.add_argument( + "--heartbeat-interval", + default=5, + type=float, + help="Heartbeat interval in seconds", + ) + parser.add_argument( + "--heartbeat-timeout", + default=3, + type=float, + help="Heartbeat timeout in seconds", + ) + + args = parser.parse_args() start_server( - address="0.0.0.0", - port=4999, - heartbeat_interval=5, - heartbeat_timeout=3, + address=args.ip, + port=args.port, + heartbeat_interval=args.heartbeat_interval, + heartbeat_timeout=args.heartbeat_timeout, )