From 1c03bd8c7abf326e9c67f3fc59dab24258975755 Mon Sep 17 00:00:00 2001 From: Jazz Turner-Baggs <473256+jazzz@users.noreply.github.com> Date: Sun, 28 Sep 2025 14:45:02 -0700 Subject: [PATCH] fix: folder generation --- README.md | 2 +- examples/pingpong.nim | 69 ++++++++++++++++++++++++++++++++++++ examples/tui/persistence.nim | 10 +++++- 3 files changed, 79 insertions(+), 2 deletions(-) create mode 100644 examples/pingpong.nim diff --git a/README.md b/README.md index c18511d..6cc97a3 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ make update make all # Run the Text Interface -./build/tui +./build/tui --name= ``` ## Details diff --git a/examples/pingpong.nim b/examples/pingpong.nim new file mode 100644 index 0000000..daf3b8c --- /dev/null +++ b/examples/pingpong.nim @@ -0,0 +1,69 @@ +import chronicles +import chronos + +import chat_sdk +import content_types + + +proc getContent(content: ContentFrame): string = + # Skip type checks and assume its a TextFrame + let m = decode(content.bytes, TextFrame).valueOr: + raise newException(ValueError, fmt"Badly formed Content") + return fmt"{m}" + +proc main() {.async.} = + + # Create Configurations + var cfg_saro = DefaultConfig() + var cfg_raya = DefaultConfig() + + # Cross pollinate Peers - No Waku discovery is used in this example + cfg_saro.staticPeers.add(cfg_raya.getMultiAddr()) + cfg_raya.staticPeers.add(cfg_saro.getMultiAddr()) + + # Create Clients + var saro = newClient(cfg_saro, createIdentity("Saro")) + var raya = newClient(cfg_raya, createIdentity("Raya")) + + # Wire Callbacks + saro.onNewMessage(proc(convo: Conversation, msg: ContentFrame) {.async.} = + echo " Saro <------ :: " & getContent(msg) + await sleepAsync(10000) + discard await convo.sendMessage(saro.ds, initTextFrame("Ping").toContentFrame()) + ) + + saro.onDeliveryAck(proc(convo: Conversation, msgId: string) {.async.} = + echo " Saro -- Read Receipt for " & msgId + ) + + + + + raya.onNewMessage(proc(convo: Conversation, msg: ContentFrame) {.async.} = + echo " ------> Raya :: " & getContent(msg) + await sleepAsync(10000) + sdiscard await convo.sendMessage(raya.ds, initTextFrame("Pong").toContentFrame()) + ) + + raya.onNewConversation(proc(convo: Conversation) {.async.} = + echo " ------> Raya :: New Conversation: " & convo.id() + discard await convo.sendMessage(raya.ds, initTextFrame("Hello").toContentFrame()) + ) + raya.onDeliveryAck(proc(convo: Conversation, msgId: string) {.async.} = + echo " raya -- Read Receipt for " & msgId + ) + + + await saro.start() + await raya.start() + + await sleepAsync(5000) + + # Perform OOB Introduction: Raya -> Saro + let raya_bundle = raya.createIntroBundle() + discard await saro.newPrivateConversation(raya_bundle) + + await sleepAsync(10000) # Run for some time + + saro.stop() + raya.stop() \ No newline at end of file diff --git a/examples/tui/persistence.nim b/examples/tui/persistence.nim index 606f856..3582914 100644 --- a/examples/tui/persistence.nim +++ b/examples/tui/persistence.nim @@ -57,8 +57,12 @@ proc toIdent(s: SavedConfig): Identity = proc register(name: string, multiAddr: string) {.async.} = notice "Registering Account", name=name, maddr=multiAddr + + if not dirExists(REGISTRATION_DIR): + createDir(REGISTRATION_DIR) + try: - writeFile(joinPath(".registry", fmt"{name.toLower()}.maddr"), multiAddr) + writeFile(joinPath(REGISTRATION_DIR, fmt"{name.toLower()}.maddr"), multiAddr) except IOError as e: echo "Failed to write registration file: ", e.msg raise e @@ -105,6 +109,10 @@ proc saveCfg(name:string, cfg: Config) = let json = jsonutils.toJson(s) + + if not dirExists(KEY_DIR): + createDir(KEY_DIR) + try: writeFile(joinPath(KEY_DIR, fmt"{name.toLower()}.cfg"), $json) except IOError as e: