|
|
@ -25,8 +25,12 @@ SERVER_PORT = 8000 |
|
|
|
sio = socketio.AsyncClient() |
|
|
|
|
|
|
|
async def announce_server(): |
|
|
|
await sio.connect(f'http://{HOST_SERVER_IP}:{HOST_SERVER_PORT}') |
|
|
|
await sio.emit('register', {'name': SERVER_NAME, 'ip': SERVER_IP, 'port': SERVER_PORT}) |
|
|
|
@sio.event |
|
|
|
async def connect(): |
|
|
|
await sio.emit('register', {'name': SERVER_NAME, 'ip': SERVER_IP, 'port': SERVER_PORT}) |
|
|
|
|
|
|
|
async def main(): |
|
|
|
await sio.connect(f'http://{HOST_SERVER_IP}:{HOST_SERVER_PORT}') |
|
|
|
|
|
|
|
@sio.on("heartbeat") |
|
|
|
async def on_heartbeat(): |
|
|
@ -36,20 +40,43 @@ async def announce_server(): |
|
|
|
async def disconnect(): |
|
|
|
print("Disconnected from host") |
|
|
|
|
|
|
|
await main() |
|
|
|
|
|
|
|
# def announce_server_decorator(host_block_function): |
|
|
|
# @wraps(host_block_function) |
|
|
|
# def wrapper(*args, **kwargs): |
|
|
|
# loop = asyncio.get_event_loop() |
|
|
|
|
|
|
|
# # Start the server announcement task |
|
|
|
# announce_task = loop.create_task(announce_server()) |
|
|
|
|
|
|
|
# # Run the original host_block function |
|
|
|
# result = host_block_function(*args, **kwargs) |
|
|
|
|
|
|
|
# # Cancel the announcement task after the host_block function is done |
|
|
|
# announce_task.cancel() |
|
|
|
|
|
|
|
# return result |
|
|
|
|
|
|
|
# return wrapper |
|
|
|
|
|
|
|
def announce_server_decorator(host_block_function): |
|
|
|
@wraps(host_block_function) |
|
|
|
def wrapper(*args, **kwargs): |
|
|
|
loop = asyncio.get_event_loop() |
|
|
|
|
|
|
|
# Start the server announcement task |
|
|
|
announce_task = loop.create_task(announce_server()) |
|
|
|
async def main(*args, **kwargs): |
|
|
|
loop = asyncio.get_event_loop() |
|
|
|
host_block_thread = loop.run_in_executor(None, host_block_function) |
|
|
|
|
|
|
|
# Run the original host_block function |
|
|
|
result = host_block_function(*args, **kwargs) |
|
|
|
# Announce the server to the host |
|
|
|
await announce_server() |
|
|
|
# announce_task = loop.create_task(announce_server()) |
|
|
|
|
|
|
|
# Cancel the announcement task after the host_block function is done |
|
|
|
announce_task.cancel() |
|
|
|
# run announcement task infinitely in background but allow host_block to run |
|
|
|
# await asyncio.gather(announce_task, host_block_thread) |
|
|
|
# Wait for host_block to finish |
|
|
|
await host_block_thread |
|
|
|
|
|
|
|
return result |
|
|
|
# announce_task.cancel() |
|
|
|
|
|
|
|
return asyncio.run(main()) |
|
|
|
return wrapper |
|
|
|