Implemented handshake and heartbeat

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
This commit is contained in:
2026-08-04 22:11:53 +01:00
parent 98814b122c
commit f3b1a537f0
17 changed files with 1030 additions and 96 deletions
+15 -4
View File
@@ -1,17 +1,28 @@
from time import sleep
import logging
from rich.logging import RichHandler
from dynalab_core import Core
from dynalab_core.config import CoreConfig
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__)
config = CoreConfig(port=8765)
print(config)
log.info("Configured manual core on %s", config.bind_str())
dl_core = Core(config)
dl_core.start()
print("started")
try:
while True:
dl_core.wait(1)
print("waiting")
except KeyboardInterrupt:
log.info("Received keyboard interrupt")
dl_core.stop()
+52 -3
View File
@@ -1,4 +1,25 @@
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:
@@ -7,14 +28,42 @@ async def main() -> None:
port=8765,
)
print("Connected")
log.info("Connected to DynaLab core")
try:
await asyncio.sleep(10)
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()
print("Disconnected")
log.info("Disconnected from DynaLab core")
asyncio.run(main())