74 lines
2.4 KiB
Python
74 lines
2.4 KiB
Python
import asyncio
|
|
import logging
|
|
from time import monotonic
|
|
from uuid import uuid4
|
|
|
|
from dynalab_core.protocols.packets import ProtocolMessage
|
|
from rich.logging import RichHandler
|
|
|
|
from dynalab_core.protocols.common import VersionDescriptor
|
|
from dynalab_core.protocols.constants import PROTOCOL_VERSION
|
|
from dynalab_core.protocols.json.wire import read_message, write_message
|
|
from dynalab_core.protocols.packets.handshake import ConnectorHello, DynaLabHello
|
|
from dynalab_core.protocols.packets.heartbeat import Heartbeat
|
|
|
|
CONNECTOR_VERSION = VersionDescriptor(type="alpha", major=0, minor=0, patch=1)
|
|
|
|
logging.basicConfig(
|
|
level=logging.DEBUG,
|
|
format="%(name)s %(message)s",
|
|
datefmt="%H:%M:%S",
|
|
handlers=[RichHandler(rich_tracebacks=True, show_path=False)],
|
|
force=True,
|
|
)
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
async def main() -> None:
|
|
reader, writer = await asyncio.open_connection(
|
|
host="127.0.0.1",
|
|
port=8765,
|
|
)
|
|
|
|
log.info("Connected to DynaLab core")
|
|
|
|
try:
|
|
try:
|
|
message = await asyncio.wait_for(read_message(reader), timeout=30.0)
|
|
except TimeoutError:
|
|
log.warning("Timed out waiting for server hello")
|
|
return
|
|
if not isinstance(message, DynaLabHello):
|
|
log.warning("Expected server hello, received %s", message.type)
|
|
return
|
|
dynalab_hello = message
|
|
log.debug("Received hello from core instance %s", dynalab_hello.instance_id)
|
|
connector_hello = ConnectorHello(
|
|
connector_uuid=uuid4(),
|
|
protocol_version=PROTOCOL_VERSION,
|
|
connector_name="Test connector",
|
|
connector_version=CONNECTOR_VERSION.get_version(),
|
|
)
|
|
try:
|
|
await asyncio.wait_for(write_message(writer, connector_hello), timeout=5.0)
|
|
except TimeoutError:
|
|
log.warning("Timed out sending connector hello")
|
|
return
|
|
log.info("Connector hello sent for %s", connector_hello.connector_uuid)
|
|
|
|
try:
|
|
while True:
|
|
message = await read_message(reader)
|
|
if isinstance(message, Heartbeat):
|
|
message.return_timestamp = round(monotonic() * 1000)
|
|
await write_message(writer, message)
|
|
except KeyboardInterrupt:
|
|
return
|
|
finally:
|
|
writer.close()
|
|
await writer.wait_closed()
|
|
log.info("Disconnected from DynaLab core")
|
|
|
|
|
|
asyncio.run(main())
|