diff --git a/notebooks/Testing/Veilid/Alice-Python-Server.ipynb b/notebooks/Testing/Veilid/Alice-Python-Server.ipynb index 8564567beef..3e1b7065c2c 100644 --- a/notebooks/Testing/Veilid/Alice-Python-Server.ipynb +++ b/notebooks/Testing/Veilid/Alice-Python-Server.ipynb @@ -152,20 +152,58 @@ }, { "cell_type": "markdown", - "id": "fd824cca-2a7f-4ea9-9e67-1c06d1f8bec2", + "id": "ddba6e22-96ee-46d7-8251-fcaa4140253b", "metadata": {}, "source": [ - "### Send AppMessage using VLD Key to Peer" + "### Ping Peer " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "3de4b843-f3a2-4d96-bd48-121ae2b6f197", + "metadata": {}, + "outputs": [], + "source": [ + "peer_vld_key = str(input(\"Enter Peer VLD Key\"))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "575c3441-cd11-4a42-ab4e-0bde3e5d5c72", + "metadata": {}, + "outputs": [], + "source": [ + "peer_vld_key" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "64d0b338-a439-4982-b739-24c056833be1", + "metadata": {}, + "outputs": [], + "source": [ + "res = requests.post(f\"http://{host}:{port}/ping/{peer_vld_key}\")" ] }, { "cell_type": "code", "execution_count": null, - "id": "25cfb508-dd08-44b9-85c9-e6aa07e96a97", + "id": "3ce13553-dae5-442e-bd56-2dddb526c0f2", "metadata": {}, "outputs": [], "source": [ - "peer_vld_key = input(\"Enter Peer VLD Key\")" + "res.json()" + ] + }, + { + "cell_type": "markdown", + "id": "fd824cca-2a7f-4ea9-9e67-1c06d1f8bec2", + "metadata": {}, + "source": [ + "### Send AppMessage using VLD Key to Peer" ] }, { @@ -235,7 +273,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.5" + "version": "3.11.8" } }, "nbformat": 4, diff --git a/packages/grid/veilid/server/constants.py b/packages/grid/veilid/server/constants.py index 0714b9e0902..d0c5bd85627 100644 --- a/packages/grid/veilid/server/constants.py +++ b/packages/grid/veilid/server/constants.py @@ -9,3 +9,5 @@ DHT_KEY_CREDS = "syft-dht-key-creds" USE_DIRECT_CONNECTION = True + +TIMEOUT = 10 # in seconds diff --git a/packages/grid/veilid/server/main.py b/packages/grid/veilid/server/main.py index 1bb6bb0cbd9..cabb8ee0360 100644 --- a/packages/grid/veilid/server/main.py +++ b/packages/grid/veilid/server/main.py @@ -20,6 +20,7 @@ from .veilid_core import app_message from .veilid_core import generate_vld_key from .veilid_core import healthcheck +from .veilid_core import ping from .veilid_core import retrieve_vld_key # Logging Configuration @@ -63,6 +64,16 @@ async def retrieve_vld_key_endpoint() -> ResponseModel: raise HTTPException(status_code=500, detail=str(e)) +@app.post("/ping/{vld_key}", response_model=ResponseModel) +async def ping_endpoint(request: Request, vld_key: str) -> ResponseModel: + try: + logger.info(f"Received ping request:{vld_key}") + res = await ping(vld_key) + return ResponseModel(message=res) + except Exception as e: + raise HTTPException(status_code=500, detail=str(e)) + + @app.post("/app_message", response_model=ResponseModel) async def app_message_endpoint( request: Request, vld_key: Annotated[str, Body()], message: Annotated[bytes, Body()] diff --git a/packages/grid/veilid/server/veilid_core.py b/packages/grid/veilid/server/veilid_core.py index 5364a6c547b..695bb94d856 100644 --- a/packages/grid/veilid/server/veilid_core.py +++ b/packages/grid/veilid/server/veilid_core.py @@ -1,3 +1,7 @@ +# stdlib +import asyncio +from enum import Enum + # third party from loguru import logger import veilid @@ -11,6 +15,7 @@ from veilid.types import RouteId # relative +from .constants import TIMEOUT from .constants import USE_DIRECT_CONNECTION from .veilid_connection import get_routing_context from .veilid_connection import get_veilid_conn @@ -19,6 +24,11 @@ from .veilid_db import store_dht_key_creds +class PingResponse(Enum): + SUCCESS = "SUCCESS" + FAIL = "FAIL" + + async def create_private_route( conn: _JsonVeilidAPI, stability: Stability = veilid.Stability.RELIABLE, @@ -156,6 +166,16 @@ async def app_call(vld_key: str, message: bytes) -> bytes: return result +async def ping(vld_key: str) -> str: + async with await get_veilid_conn() as conn: + try: + _ = await asyncio.wait_for(conn.debug(f"ping {vld_key}"), timeout=TIMEOUT) + return PingResponse.SUCCESS.value + except Exception as e: + logger.error(f"Failed to ping {vld_key} : {e}") + return PingResponse.FAIL.value + + # TODO: Modify healthcheck endpoint to check public internet ready async def healthcheck() -> bool: async with await get_veilid_conn() as conn: