Skip to content

Commit

Permalink
Added app call endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
rasswanth-s committed Feb 25, 2024
1 parent bbb170c commit 139bcdf
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
39 changes: 39 additions & 0 deletions notebooks/Testing/Veilid/Alice-Python-Server.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
29 changes: 29 additions & 0 deletions notebooks/Testing/Veilid/Bob-Python-Server.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 8 additions & 0 deletions packages/grid/veilid/server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
29 changes: 28 additions & 1 deletion packages/grid/veilid/server/veilid_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}

0 comments on commit 139bcdf

Please sign in to comment.