41 lines
1.1 KiB
Nim
41 lines
1.1 KiB
Nim
# SPDX-FileCopyrightText: ☭ 2021 Emery Hemingway
|
|
# SPDX-License-Identifier: Unlicense
|
|
|
|
import std/[asyncdispatch, os]
|
|
import preserves, syndicate, syndicate/capabilities
|
|
import syndicate/protocols/simpleChatProtocol
|
|
|
|
proc mintCap: SturdyRef =
|
|
var key: array[16, byte]
|
|
mint(key, "syndicate")
|
|
|
|
proc unixSocketPath: string =
|
|
result = getEnv("SYNDICATE_SOCK")
|
|
if result == "":
|
|
result = getEnv("XDG_RUNTIME_DIR", "/run/user/1000") / "dataspace"
|
|
|
|
bootDataspace("main") do (root: Ref; turn: var Turn):
|
|
connectUnix(turn, unixSocketPath(), mintCap()) do (turn: var Turn; ds: Ref):
|
|
var
|
|
username: string
|
|
usernameHandle: Handle
|
|
|
|
proc updateUsername(turn: var Turn; u: string) =
|
|
username = u
|
|
var p = Present(username: username)
|
|
replace(turn, ds, usernameHandle, p)
|
|
|
|
updateUsername(turn, "user" & $getCurrentProcessId())
|
|
|
|
during(turn, ds, ?Present) do (username: string):
|
|
echo username, " arrived"
|
|
do:
|
|
echo username, " left"
|
|
|
|
onMessage(turn, ds, ?Says) do (who: string; what: string):
|
|
echo who, ": ", what
|
|
|
|
message(turn, ds, Says(who: username, what: "hello"))
|
|
|
|
runForever()
|