diff --git a/notebooks/Testing/Veilid/Alice-Python-Server.ipynb b/notebooks/Testing/Veilid/Alice-Python-Server.ipynb index 59949a952b8..97c19b1b34a 100644 --- a/notebooks/Testing/Veilid/Alice-Python-Server.ipynb +++ b/notebooks/Testing/Veilid/Alice-Python-Server.ipynb @@ -100,6 +100,45 @@ "app_message = requests.post(f\"http://{host}:{port}/app_message\", json=json_data)" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "ff09ab92-3423-483a-abf3-51e8c2448cf9", + "metadata": {}, + "outputs": [], + "source": [ + "app_message.content" + ] + }, + { + "cell_type": "markdown", + "id": "4d0d9e39-bf05-4ef3-b00a-2bb605f041ee", + "metadata": {}, + "source": [ + "### Send AppCall using DHT Key to Self" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b8bc9f54-b2f0-4f88-8897-f640866ba2ed", + "metadata": {}, + "outputs": [], + "source": [ + "json_data = {\"dht_key\": self_dht_key, \"message\": \"Hello to app call\"}\n", + "app_call = requests.post(f\"http://{host}:{port}/app_call\", json=json_data)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "2c1c4148-461a-459e-846a-fad332a7ce3a", + "metadata": {}, + "outputs": [], + "source": [ + "app_call.json()" + ] + }, { "cell_type": "markdown", "id": "fd824cca-2a7f-4ea9-9e67-1c06d1f8bec2", diff --git a/notebooks/Testing/Veilid/Bob-Python-Server.ipynb b/notebooks/Testing/Veilid/Bob-Python-Server.ipynb index e8ccaf2899a..c0b92df4115 100644 --- a/notebooks/Testing/Veilid/Bob-Python-Server.ipynb +++ b/notebooks/Testing/Veilid/Bob-Python-Server.ipynb @@ -100,6 +100,35 @@ "app_message = requests.post(f\"http://{host}:{port}/app_message\", json=json_data)" ] }, + { + "cell_type": "markdown", + "id": "3ed2c114-eab7-4be7-bd89-d5ec3a7ec4c2", + "metadata": {}, + "source": [ + "### Send AppCall using DHT Key to Self" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "db49c78d-9767-4358-aa00-e740ce04e000", + "metadata": {}, + "outputs": [], + "source": [ + "json_data = {\"dht_key\": self_dht_key, \"message\": \"Hello to app call\"}\n", + "app_call = requests.post(f\"http://{host}:{port}/app_call\", json=json_data)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "9bc0a69e-7cff-42fc-8859-e5de6edacdeb", + "metadata": {}, + "outputs": [], + "source": [ + "app_call.json()" + ] + }, { "cell_type": "markdown", "id": "73eee970-bb61-4014-9380-1944587b929a", diff --git a/packages/grid/veilid/server/main.py b/packages/grid/veilid/server/main.py index 4324b4aaece..4dc8cc13109 100644 --- a/packages/grid/veilid/server/main.py +++ b/packages/grid/veilid/server/main.py @@ -11,6 +11,7 @@ # relative from .veilid_core import VeilidConnectionSingleton +from .veilid_core import app_call from .veilid_core import app_message from .veilid_core import generate_dht_key from .veilid_core import get_veilid_conn @@ -57,6 +58,13 @@ async def app_message_endpoint( return await app_message(dht_key=dht_key, message=message) +@app.post("/app_call") +async def app_call_endpoint( + request: Request, dht_key: Annotated[str, Body()], message: Annotated[bytes, Body()] +) -> dict[str, str]: + return await app_call(dht_key=dht_key, message=message) + + @app.on_event("startup") async def startup_event() -> None: try: diff --git a/packages/grid/veilid/server/veilid_core.py b/packages/grid/veilid/server/veilid_core.py index 030d44b8219..166a5e8956c 100644 --- a/packages/grid/veilid/server/veilid_core.py +++ b/packages/grid/veilid/server/veilid_core.py @@ -31,6 +31,11 @@ async def main_callback(update: VeilidUpdate) -> None: if update.kind == veilid.VeilidUpdateKind.APP_MESSAGE: logger.info(f"Received App Message: {update.detail.message}") + elif update.kind == veilid.VeilidUpdateKind.APP_CALL: + logger.info(f"Received App Call: {update.detail.message}") + async with await get_veilid_conn() as conn: + await conn.app_call_reply(update.detail.call_id, b"Reply from App Call") + async def noop_callback(update: VeilidUpdate) -> None: pass @@ -165,7 +170,29 @@ async def app_message(dht_key: str, message: bytes) -> dict[str, str]: # TODO: change to debug logger.info(f"Private Route of Peer: {prr_peer} ") - # Send message to peer + # Send app message to peer await router.app_message(prr_peer, message) return {"message": "Message sent successfully"} + + +async def app_call(dht_key: str, message: bytes) -> dict[str, str]: + async with await get_veilid_conn() as conn: + async with await get_routing_context(conn) as router: + dht_key = veilid.TypedKey(dht_key) + # TODO: change to debug + logger.info(f"App Call to DHT Key: {dht_key}") + dht_value = await get_dht_value(router, dht_key, 0) + # TODO: change to debug + logger.info(f"DHT Value:{dht_value}") + if isinstance(dht_value, dict): + return dht_value + + # Private Router to peer + prr_peer = await conn.import_remote_private_route(dht_value.data) + # TODO: change to debug + logger.info(f"Private Route of Peer: {prr_peer} ") + + result = await router.app_call(prr_peer, message) + + return {"message": result}