Implemented handshake in json server and handoff to endpoint Endpoint handles connector handshake accept/decline then launches both its IO threads alongside its heartbeat thread which contains simple timeout logic, timeout calls connection handles to close, subsequently killing the endpoint
70 lines
2.2 KiB
Python
70 lines
2.2 KiB
Python
import asyncio
|
|
import logging
|
|
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
|
|
|
|
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)
|
|
log.info(message)
|
|
except KeyboardInterrupt:
|
|
return
|
|
finally:
|
|
writer.close()
|
|
await writer.wait_closed()
|
|
log.info("Disconnected from DynaLab core")
|
|
|
|
|
|
asyncio.run(main())
|