From 18e283bb842b7c3e32c53ec753f9c3212184f191 Mon Sep 17 00:00:00 2001 From: Teknique Date: Mon, 24 Jul 2023 16:18:58 -0700 Subject: [PATCH] WIP snapshot. --- veilid-python/chat.py | 92 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100755 veilid-python/chat.py diff --git a/veilid-python/chat.py b/veilid-python/chat.py new file mode 100755 index 00000000..29328980 --- /dev/null +++ b/veilid-python/chat.py @@ -0,0 +1,92 @@ +#!/usr/bin/env python + +import asyncio +import sys + +import veilid + +QUIT = b"QUIT" + + +async def cb(*args, **kwargs): + return + print(f"{args=}") + print(f"{kwargs=}") + + +async def chatter(rc: veilid.api.RoutingContext, key, send_channel: int, recv_channel: int): + last_seq = -1 + + send_subkey = veilid.types.ValueSubkey(send_channel) + recv_subkey = veilid.types.ValueSubkey(recv_channel) + + while True: + try: + msg = input("SEND> ") + except EOFError: + print("Closing the chat.") + await rc.set_dht_value(key, send_subkey, QUIT) + return + + await rc.set_dht_value(key, send_subkey, msg.encode()) + + while True: + resp = await rc.get_dht_value(key, recv_subkey, True) + if resp is None: + continue + if resp.seq == last_seq: + continue + + if resp.data == QUIT: + print("Other end closed the chat.") + return + + print(f"RECV< {resp.data.decode()}") + last_seq = resp.seq + break + + +async def start(): + conn = await veilid.json_api_connect("localhost", 5959, cb) + + rc = await conn.new_routing_context() + async with rc: + rec = await rc.create_dht_record(veilid.DHTSchema.dflt(2)) + print(f"Chat key: {rec.key}") + print(rec.owner) + print(vars(rec)) + + await chatter(rc, rec.key, 0, 1) + + await rc.close_dht_record(rec.key) + await rc.delete_dht_record(rec.key) + + +async def respond(key, writer): + conn = await veilid.json_api_connect("localhost", 5959, cb) + + rc = await conn.new_routing_context() + async with rc: + await chatter(rc, key, 1, 0) + + +async def clean(key): + conn = await veilid.json_api_connect("localhost", 5959, cb) + + rc = await conn.new_routing_context() + async with rc: + await rc.close_dht_record(key) + await rc.delete_dht_record(key) + + +if __name__ == "__main__": + if sys.argv[1] == "--start": + func = start() + elif sys.argv[1] == "--respond": + func = respond(sys.argv[2], sys.argv[3]) + elif sys.argv[1] == "--clean": + func = clean(sys.argv[2]) + else: + 1 / 0 + + asyncio.run(func)