working client

This commit is contained in:
Michael Pilosov 2023-03-19 23:09:40 -06:00
parent e717f60e06
commit b87f5a329c
3 changed files with 120 additions and 10 deletions

View File

@ -77,4 +77,10 @@ The full syntax is equivalent to:
```bash
announce_server start_registry --address 0.0.0.0 --port 4999 --heartbeat_interval 5 --heartbeat_timeout 3
```
```
To test connections, run:
```bash
announce_server start_client --host-ip 0.0.0.0 --host-port 4999
```

View File

@ -1,6 +1,11 @@
import argparse
import asyncio
import argparse
import signal
import threading
from http.server import HTTPServer, SimpleHTTPRequestHandler
from .decorator import register_service
from .get_ip import get_ip_address
import socketio
sio = socketio.AsyncClient()
@ -27,23 +32,118 @@ async def main(host_ip, host_port):
await sio.wait()
def exit_handler(sig, frame):
global server_should_exit
server_should_exit.set()
# def start_client(host_ip="0.0.0.0", host_port=4999):
# @register_service(name="test client", ip=get_ip_address(), port=13373, host_ip=host_ip, host_port=host_port)
# def server(port=13373):
# global server_should_exit
# server_should_exit = threading.Event()
# httpd = HTTPServer(('', port), SimpleHTTPRequestHandler)
# print(f"Serving HTTP on 0.0.0.0 port {port} (http://0.0.0.0:{port}/) ...")
# def serve_forever_nonblocking():
# while not server_should_exit.is_set():
# httpd.handle_request()
# server_thread = threading.Thread(target=serve_forever_nonblocking)
# server_thread.start()
# return server_thread
# signal.signal(signal.SIGINT, exit_handler)
# signal.signal(signal.SIGTERM, exit_handler)
# server_thread = server()
# try:
# asyncio.run(main(host_ip, host_port))
# finally:
# print("Shutting down test client...")
# server_should_exit.set()
# # server_thread.join()
# def start_client(host_ip="0.0.0.0", host_port=4999):
# @register_service(name="test client", ip=get_ip_address(), port=13373, host_ip=host_ip, host_port=host_port)
# def server(port=13373):
# httpd = HTTPServer(('', port), SimpleHTTPRequestHandler)
# print(f"Serving HTTP on 0.0.0.0 port {port} (http://0.0.0.0:{port}/) ...")
# httpd.serve_forever()
# async def cleanup_and_shutdown():
# try:
# server()
# except KeyboardInterrupt:
# print("Cleaning up and shutting down...")
# loop = asyncio.get_event_loop()
# loop.run_until_complete(cleanup_and_shutdown())
# def start_client(host_ip="0.0.0.0", host_port=4999):
# @register_service(name="test client", ip=get_ip_address(), port=13373, host_ip=host_ip, host_port=host_port)
# def server(port=13373):
# httpd = HTTPServer(('', port), SimpleHTTPRequestHandler)
# print(f"Serving HTTP on 0.0.0.0 port {port} (http://0.0.0.0:{port}/) ...")
# httpd.serve_forever()
# signal.signal(signal.SIGINT, exit_handler)
# signal.signal(signal.SIGTERM, exit_handler)
# try:
# server()
# except (KeyboardInterrupt, asyncio.exceptions.CancelledError):
# print("Cleaning up and shutting down...")
def start_client(host_ip="0.0.0.0", host_port=4999):
asyncio.run(main(host_ip, host_port))
@register_service(name="test client", ip=get_ip_address(), port=13373, host_ip=host_ip, host_port=host_port)
def server(port=13373):
httpd = HTTPServer(('', port), SimpleHTTPRequestHandler)
print(f"Serving HTTP on 0.0.0.0 port {port} (http://0.0.0.0:{port}/) ...")
def serve_forever_nonblocking():
while not server_should_exit.is_set():
httpd.handle_request()
httpd.shutdown()
httpd.server_close()
server_thread = threading.Thread(target=serve_forever_nonblocking)
server_thread.start()
return server_thread
server_should_exit = threading.Event()
signal.signal(signal.SIGINT, exit_handler)
signal.signal(signal.SIGTERM, exit_handler)
try:
server_thread = server()
asyncio.run(main(host_ip, host_port))
except (KeyboardInterrupt, asyncio.exceptions.CancelledError):
print("Cleaning up and shutting down...")
finally:
print("Shutting down test client...")
server_should_exit.set()
# server_thread.join()
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)",
default="0.0.0.0",
help="Host IP address (default: 0.0.0.0)",
)
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)

View File

@ -143,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)
@ -166,7 +166,10 @@ 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(
"--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"
)
@ -182,8 +185,9 @@ if __name__ == "__main__":
type=float,
help="Heartbeat timeout in seconds",
)
args = parser.parse_args()
start_server(
address=args.ip,
port=args.port,