Browse Source

adding client

main
Michael Pilosov 2 years ago
parent
commit
e717f60e06
  1. 17
      src/announce_server/__main__.py
  2. 49
      src/announce_server/client.py
  3. 30
      src/announce_server/server.py

17
src/announce_server/__main__.py

@ -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()

49
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)

30
src/announce_server/server.py

@ -1,3 +1,4 @@
import argparse
import asyncio import asyncio
import signal import signal
@ -142,7 +143,7 @@ def start_server(address, port, heartbeat_interval, heartbeat_timeout):
# heartbeat(sio, heartbeat_interval, heartbeat_timeout) # heartbeat(sio, heartbeat_interval, heartbeat_timeout)
# ) # )
# aiohttp_app = loop.create_task(web._run_app(app, host=address, port=port)) # aiohttp_app = loop.create_task(web._run_app(app, host=address, port=port))
# Python 3.6+ compatible. Supports any awaitable: # Python 3.6+ compatible. Supports any awaitable:
heartbeat_task = asyncio.ensure_future( heartbeat_task = asyncio.ensure_future(
heartbeat(sio, heartbeat_interval, heartbeat_timeout) heartbeat(sio, heartbeat_interval, heartbeat_timeout)
@ -164,9 +165,28 @@ def start_server(address, port, heartbeat_interval, heartbeat_timeout):
if __name__ == "__main__": 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( start_server(
address="0.0.0.0", address=args.ip,
port=4999, port=args.port,
heartbeat_interval=5, heartbeat_interval=args.heartbeat_interval,
heartbeat_timeout=3, heartbeat_timeout=args.heartbeat_timeout,
) )

Loading…
Cancel
Save