syndicate-nim/tests/chat.nim

41 lines
1.1 KiB
Nim
Raw Normal View History

2021-09-21 17:10:13 +00:00
# SPDX-FileCopyrightText: ☭ 2021 Emery Hemingway
# SPDX-License-Identifier: Unlicense
2022-05-21 18:21:35 +00:00
import std/[asyncdispatch, os]
import preserves, syndicate, syndicate/capabilities
import syndicate/protocols/simpleChatProtocol
2021-09-24 19:25:47 +00:00
2022-05-21 18:21:35 +00:00
proc mintCap: SturdyRef =
var key: array[16, byte]
mint(key, "syndicate")
2021-09-24 19:25:47 +00:00
2022-05-21 18:21:35 +00:00
proc unixSocketPath: string =
result = getEnv("SYNDICATE_SOCK")
if result == "":
result = getEnv("XDG_RUNTIME_DIR", "/run/user/1000") / "dataspace"
2021-09-24 19:25:47 +00:00
2022-05-21 18:21:35 +00:00
bootDataspace("main") do (root: Ref; turn: var Turn):
connectUnix(turn, unixSocketPath(), mintCap()) do (turn: var Turn; ds: Ref):
2021-09-24 19:25:47 +00:00
var
username: string
usernameHandle: Handle
proc updateUsername(turn: var Turn; u: string) =
username = u
2021-10-28 17:43:56 +00:00
var p = Present(username: username)
2022-05-21 18:21:35 +00:00
replace(turn, ds, usernameHandle, p)
2021-09-24 19:25:47 +00:00
updateUsername(turn, "user" & $getCurrentProcessId())
2022-05-21 18:21:35 +00:00
during(turn, ds, ?Present) do (username: string):
2021-10-29 14:55:49 +00:00
echo username, " arrived"
2022-05-21 18:21:35 +00:00
do:
echo username, " left"
2021-10-29 14:55:49 +00:00
2022-05-21 18:21:35 +00:00
onMessage(turn, ds, ?Says) do (who: string; what: string):
2021-10-29 14:55:49 +00:00
echo who, ": ", what
2021-10-28 17:43:56 +00:00
message(turn, ds, Says(who: username, what: "hello"))
2021-09-24 19:25:47 +00:00
2022-05-21 18:21:35 +00:00
runForever()