|
|
|
import asyncio
|
|
|
|
from functools import wraps
|
|
|
|
import socketio
|
|
|
|
import socket
|
|
|
|
|
|
|
|
def get_ip_address():
|
|
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
|
|
try:
|
|
|
|
# This IP address doesn't need to be reachable, as we're only using it to find the local IP address
|
|
|
|
s.connect(("10.255.255.255", 1))
|
|
|
|
ip = s.getsockname()[0]
|
|
|
|
except Exception:
|
|
|
|
ip = "127.0.0.1"
|
|
|
|
finally:
|
|
|
|
s.close()
|
|
|
|
return ip
|
|
|
|
|
|
|
|
# Update these with the correct values for your host and server
|
|
|
|
HOST_SERVER_IP = "192.168.1.113"
|
|
|
|
HOST_SERVER_PORT = 4999
|
|
|
|
|
|
|
|
SERVER_IP = get_ip_address()
|
|
|
|
SERVER_PORT = 8000
|
|
|
|
|
|
|
|
sio = socketio.AsyncClient()
|
|
|
|
|
|
|
|
async def announce_server(**kwargs):
|
|
|
|
SERVER_NAME = "server_1"
|
|
|
|
server_name = kwargs.get("name", SERVER_NAME)
|
|
|
|
# SERVER_IP = kwargs.get("ip", SERVER_IP)
|
|
|
|
# SERVER_PORT = kwargs.get("port", SERVER_PORT)
|
|
|
|
# HOST_SERVER_IP = kwargs.get("host_server_ip", HOST_SERVER_IP)
|
|
|
|
# HOST_SERVER_PORT = kwargs.get("host_server_port", HOST_SERVER_PORT)
|
|
|
|
|
|
|
|
@sio.event
|
|
|
|
async def connect():
|
|
|
|
await sio.emit('register', {'name': server_name, 'ip': SERVER_IP, 'port': SERVER_PORT})
|
|
|
|
print("Announced server to host")
|
|
|
|
|
|
|
|
async def main():
|
|
|
|
# retry until we connect to the host
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
await sio.connect(f'http://{HOST_SERVER_IP}:{HOST_SERVER_PORT}')
|
|
|
|
break
|
|
|
|
except Exception as e:
|
|
|
|
print(e)
|
|
|
|
print("Failed to connect to host, retrying in 5 seconds")
|
|
|
|
await asyncio.sleep(5)
|
|
|
|
# await sio.connect(f'http://{HOST_SERVER_IP}:{HOST_SERVER_PORT}')
|
|
|
|
print("Connected to host")
|
|
|
|
|
|
|
|
@sio.on("heartbeat")
|
|
|
|
async def on_heartbeat():
|
|
|
|
print("Received heartbeat from host")
|
|
|
|
|
|
|
|
@sio.event
|
|
|
|
async def disconnect():
|
|
|
|
print("Disconnected from host")
|
|
|
|
|
|
|
|
await main()
|
|
|
|
|
|
|
|
def announce_server_decorator(task):
|
|
|
|
@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(name='test')
|
|
|
|
|
|
|
|
# Wait for host_block to finish
|
|
|
|
await host_block_thread
|
|
|
|
|
|
|
|
|
|
|
|
return asyncio.run(main())
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
# def announce_server_decorator(**outer_kwargs):
|
|
|
|
# def decorator(host_block_function):
|
|
|
|
# @wraps(host_block_function)
|
|
|
|
# def wrapper(*args, **kwargs):
|
|
|
|
|
|
|
|
# async def main(*args, **kwargs):
|
|
|
|
# loop = asyncio.get_event_loop()
|
|
|
|
# host_block_thread = loop.run_in_executor(None, host_block_function, *args, **kwargs)
|
|
|
|
|
|
|
|
# # Announce the server to the host
|
|
|
|
# await announce_server(**outer_kwargs)
|
|
|
|
|
|
|
|
# # Wait for host_block to finish
|
|
|
|
# await host_block_thread
|
|
|
|
|
|
|
|
# return asyncio.run(main(*args, **kwargs))
|
|
|
|
|
|
|
|
# return wrapper
|
|
|
|
# return decorator
|