Skip to content

Commit

Permalink
added app_message endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
rasswanth-s committed Feb 24, 2024
1 parent 45bd011 commit 6bfe9e4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
11 changes: 11 additions & 0 deletions packages/grid/veilid/server/main.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# third party
from fastapi import Body
from fastapi import FastAPI
from fastapi import Request
from typing_extensions import Annotated

# relative
from .veilid_core import VeilidConnectionSingleton
from .veilid_core import app_message
from .veilid_core import generate_dht_key
from .veilid_core import get_veilid_conn
from .veilid_core import retrieve_dht_key
Expand Down Expand Up @@ -36,6 +40,13 @@ async def retrieve_dht_key_endpoint() -> dict[str, str]:
return await retrieve_dht_key()


@app.post("/app_message")
async def app_message_endpoint(
request: Request, dht_key: Annotated[str, Body()], message: Annotated[bytes, Body()]
) -> dict[str, str]:
return await app_message(dht_key=dht_key, message=message)


@app.on_event("startup")
async def startup_event() -> None:
try:
Expand Down
42 changes: 42 additions & 0 deletions packages/grid/veilid/server/veilid_core.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
# stdlib
from typing import Callable
from typing import Optional
from typing import Union

# third party
import veilid
from veilid import KeyPair
from veilid import TypedKey
from veilid import ValueData
from veilid import VeilidUpdate
from veilid.json_api import _JsonRoutingContext
from veilid.json_api import _JsonVeilidAPI

# relative
Expand Down Expand Up @@ -87,3 +91,41 @@ async def retrieve_dht_key() -> dict[str, str]:
if dht_key is None:
return {"message": "DHT Key does not exist"}
return {"message": str(dht_key)}


async def get_dht_value(
router: _JsonRoutingContext,
dht_key: TypedKey,
subkey: int,
force_refresh: bool = True,
) -> Union[dict[str, str], ValueData]:
try:
await router.open_dht_record(key=dht_key, writer=None)
except Exception:
return {"message": f"DHT Key:{dht_key} does not exist in the veilid network"}

try:
return await router.get_dht_value(
key=dht_key, subkey=subkey, force_refresh=force_refresh
)
except Exception:
return {"message": f"Subkey:{subkey} does not exist in the DHT Key:{dht_key}"}


async def app_message(dht_key: str, message: bytes) -> dict[str, str]:
conn = await get_veilid_conn()
router = await (await conn.new_routing_context()).with_default_safety()

dht_key = veilid.TypedKey(dht_key)
dht_value = await get_dht_value(router, dht_key, 0)

if isinstance(dht_value, dict):
return dht_value

# Private Router to peer
prr_peer = await conn.import_remote_private_route(dht_value.data)

# Send message to peer
await router.app_message(prr_peer, message)

return {"message": "Message sent successfully"}

0 comments on commit 6bfe9e4

Please sign in to comment.