Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
e8d04cff7c | ||
|
38cd99fa73 | ||
|
945f485243 | ||
|
fe15bbbf1e | ||
|
b87f5a329c | ||
|
e717f60e06 | ||
|
25a7f003d8 |
2
Makefile
2
Makefile
@ -11,6 +11,6 @@ install:
|
|||||||
pip install -e .[dev,pub]
|
pip install -e .[dev,pub]
|
||||||
|
|
||||||
test-api:
|
test-api:
|
||||||
docker run --rm -ti -p 4999:4999 python:3.7-slim bash -c "pip install announce_server==0.0.2rc2; pip install 'python-socketsio[asyncio_client]==5.0'; announce_server start_registry"
|
docker run --rm -ti -p 4999:4999 python:3.7-slim bash -c "pip install -U announce_server~=0.0.2rc0; announce_server start_registry"
|
||||||
|
|
||||||
.PHONY: build clean pub install test-api
|
.PHONY: build clean pub install test-api
|
@ -78,3 +78,9 @@ The full syntax is equivalent to:
|
|||||||
```bash
|
```bash
|
||||||
announce_server start_registry --address 0.0.0.0 --port 4999 --heartbeat_interval 5 --heartbeat_timeout 3
|
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
|
||||||
|
```
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import argparse
|
import argparse
|
||||||
|
|
||||||
from announce_server import register_service
|
from announce_server import register_service
|
||||||
|
from announce_server.client import start_client
|
||||||
from announce_server.server import start_server
|
from announce_server.server import start_server
|
||||||
|
|
||||||
|
|
||||||
@ -31,6 +32,20 @@ def main():
|
|||||||
help="Heartbeat timeout in seconds",
|
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()
|
args = parser.parse_args()
|
||||||
|
|
||||||
if args.command == "start_registry":
|
if args.command == "start_registry":
|
||||||
@ -40,6 +55,8 @@ def main():
|
|||||||
heartbeat_interval=args.heartbeat_interval,
|
heartbeat_interval=args.heartbeat_interval,
|
||||||
heartbeat_timeout=args.heartbeat_timeout,
|
heartbeat_timeout=args.heartbeat_timeout,
|
||||||
)
|
)
|
||||||
|
elif args.command == "start_client":
|
||||||
|
start_client(host_ip=args.host_ip, host_port=args.host_port)
|
||||||
else:
|
else:
|
||||||
parser.print_help()
|
parser.print_help()
|
||||||
|
|
||||||
|
70
src/announce_server/client.py
Normal file
70
src/announce_server/client.py
Normal file
@ -0,0 +1,70 @@
|
|||||||
|
import argparse
|
||||||
|
import asyncio
|
||||||
|
import signal
|
||||||
|
import sys
|
||||||
|
import threading
|
||||||
|
from http.server import HTTPServer, SimpleHTTPRequestHandler
|
||||||
|
|
||||||
|
import socketio
|
||||||
|
|
||||||
|
from .decorator import register_service
|
||||||
|
from .get_ip import get_ip_address
|
||||||
|
|
||||||
|
http_server_thread = None
|
||||||
|
|
||||||
|
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):
|
||||||
|
def start_server():
|
||||||
|
global http_server_thread
|
||||||
|
print(f"Serving HTTP on 0.0.0.0 port {port} (http://0.0.0.0:{port}/) ...")
|
||||||
|
httpd = HTTPServer(("", port), SimpleHTTPRequestHandler)
|
||||||
|
httpd.serve_forever()
|
||||||
|
server_thread = threading.Thread(target=start_server)
|
||||||
|
server_thread.daemon = True
|
||||||
|
server_thread.start()
|
||||||
|
server_thread.join()
|
||||||
|
|
||||||
|
def signal_handler(signal, frame):
|
||||||
|
print("Cleaning up and shutting down...")
|
||||||
|
|
||||||
|
# If the HTTP server thread is running, shut it down
|
||||||
|
if http_server_thread is not None and http_server_thread.is_alive():
|
||||||
|
http_server_thread.shutdown()
|
||||||
|
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
signal.signal(signal.SIGINT, signal_handler)
|
||||||
|
signal.signal(signal.SIGTERM, signal_handler)
|
||||||
|
try:
|
||||||
|
server()
|
||||||
|
signal.pause()
|
||||||
|
except asyncio.exceptions.CancelledError:
|
||||||
|
print("CancelledError")
|
||||||
|
# signal_handler(signal.SIGINT, None)
|
||||||
|
pass
|
||||||
|
finally:
|
||||||
|
print("Shutting down test client...")
|
||||||
|
sys.exit(0)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
parser = argparse.ArgumentParser(description="Start announce_server client.")
|
||||||
|
parser.add_argument(
|
||||||
|
"--host-ip",
|
||||||
|
type=str,
|
||||||
|
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)
|
@ -6,6 +6,7 @@ import socketio
|
|||||||
|
|
||||||
sio = socketio.AsyncClient()
|
sio = socketio.AsyncClient()
|
||||||
|
|
||||||
|
import signal
|
||||||
|
|
||||||
async def _announce_server(**kwargs):
|
async def _announce_server(**kwargs):
|
||||||
SERVER_NAME = kwargs.get("name", "server_1")
|
SERVER_NAME = kwargs.get("name", "server_1")
|
||||||
@ -68,6 +69,38 @@ def register_service(task=None, **outer_kwargs):
|
|||||||
|
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
# def register_service(task=None, **outer_kwargs):
|
||||||
|
# if task is None:
|
||||||
|
# return lambda f: register_service(f, **outer_kwargs)
|
||||||
|
|
||||||
|
# @wraps(task)
|
||||||
|
# def wrapper(*args, **kwargs):
|
||||||
|
# async def main(*args, **kwargs):
|
||||||
|
# loop = asyncio.get_event_loop()
|
||||||
|
# host_block_thread = loop.run_in_executor(None, task)
|
||||||
|
|
||||||
|
# # Announce the server to the host
|
||||||
|
# await _announce_server(**outer_kwargs)
|
||||||
|
|
||||||
|
# # Set up signal handlers to clean up properly
|
||||||
|
# def signal_handler(signum, frame):
|
||||||
|
# print(f"Received signal {signum}. Cleaning up and shutting down...")
|
||||||
|
# host_block_thread.cancel()
|
||||||
|
# import sys
|
||||||
|
# sys.exit(0)
|
||||||
|
|
||||||
|
# signal.signal(signal.SIGINT, signal_handler)
|
||||||
|
# signal.signal(signal.SIGTERM, signal_handler)
|
||||||
|
|
||||||
|
# # Wait for host_block to finish or to be cancelled
|
||||||
|
# try:
|
||||||
|
# await host_block_thread
|
||||||
|
# except asyncio.CancelledError:
|
||||||
|
# pass
|
||||||
|
|
||||||
|
# return asyncio.run(main())
|
||||||
|
|
||||||
|
# return wrapper
|
||||||
|
|
||||||
def announce_server(*args, **kwargs):
|
def announce_server(*args, **kwargs):
|
||||||
"""Wrapper for register_service"""
|
"""Wrapper for register_service"""
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import argparse
|
||||||
import asyncio
|
import asyncio
|
||||||
import signal
|
import signal
|
||||||
|
|
||||||
@ -164,9 +165,30 @@ def start_server(address, port, heartbeat_interval, heartbeat_timeout):
|
|||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
start_server(
|
parser = argparse.ArgumentParser(description="Start announce_server client.")
|
||||||
address="0.0.0.0",
|
|
||||||
port=4999,
|
parser.add_argument("--ip", default="0.0.0.0", help="IP address of the host server")
|
||||||
heartbeat_interval=5,
|
parser.add_argument(
|
||||||
heartbeat_timeout=3,
|
"--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=args.ip,
|
||||||
|
port=args.port,
|
||||||
|
heartbeat_interval=args.heartbeat_interval,
|
||||||
|
heartbeat_timeout=args.heartbeat_timeout,
|
||||||
)
|
)
|
||||||
|
Loading…
Reference in New Issue
Block a user