Skip to content

Commit

Permalink
added ping endpoint to veilid
Browse files Browse the repository at this point in the history
  • Loading branch information
rasswanth-s committed Mar 15, 2024
1 parent d56f190 commit dfb40e0
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
48 changes: 43 additions & 5 deletions notebooks/Testing/Veilid/Alice-Python-Server.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
},
{
Expand Down Expand Up @@ -235,7 +273,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.5"
"version": "3.11.8"
}
},
"nbformat": 4,
Expand Down
2 changes: 2 additions & 0 deletions packages/grid/veilid/server/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
DHT_KEY_CREDS = "syft-dht-key-creds"

USE_DIRECT_CONNECTION = True

TIMEOUT = 10 # in seconds
11 changes: 11 additions & 0 deletions packages/grid/veilid/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()]
Expand Down
20 changes: 20 additions & 0 deletions packages/grid/veilid/server/veilid_core.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# stdlib
import asyncio
from enum import Enum

# third party
from loguru import logger
import veilid
Expand All @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit dfb40e0

Please sign in to comment.