Add chat test that is compatible with syndicate-py
This commit is contained in:
parent
737869d790
commit
b39302791e
|
@ -1,36 +0,0 @@
|
||||||
# SPDX-FileCopyrightText: ☭ 2021 Emery Hemingway
|
|
||||||
# SPDX-License-Identifier: Unlicense
|
|
||||||
|
|
||||||
import std/[asyncdispatch, os]
|
|
||||||
import preserves, syndicate, syndicate/capabilities
|
|
||||||
import syndicate/protocols/simpleChatProtocol
|
|
||||||
|
|
||||||
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(), capabilities.mint()) 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()
|
|
|
@ -0,0 +1,60 @@
|
||||||
|
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
||||||
|
# SPDX-License-Identifier: Unlicense
|
||||||
|
|
||||||
|
import std/[asyncdispatch, asyncfile, os, parseopt]
|
||||||
|
import preserves, syndicate, syndicate/protocols/transportAddress
|
||||||
|
|
||||||
|
type
|
||||||
|
Present {.preservesRecord: "Present".} = object
|
||||||
|
username: string
|
||||||
|
Says {.preservesRecord: "Says".} = object
|
||||||
|
who, what: string
|
||||||
|
|
||||||
|
proc readStdin(facet: Facet; ds: Ref; username: string) =
|
||||||
|
let file = openAsync("/dev/stdin")
|
||||||
|
onStop(facet) do (turn: var Turn): close(file)
|
||||||
|
close(stdin)
|
||||||
|
proc readLine() {.gcsafe.} =
|
||||||
|
let future = readLine(file)
|
||||||
|
addCallback(future, facet) do (turn: var Turn):
|
||||||
|
var msg = read(future)
|
||||||
|
message(turn, ds, Says(who: username, what: msg))
|
||||||
|
readLine()
|
||||||
|
readLine()
|
||||||
|
|
||||||
|
proc main =
|
||||||
|
var
|
||||||
|
transport: Preserve[void]
|
||||||
|
cap: Preserve[Ref]
|
||||||
|
username = getEnv("USER")
|
||||||
|
calledWithArguments = false
|
||||||
|
for kind, key, val in getopt():
|
||||||
|
calledWithArguments = true
|
||||||
|
if kind == cmdLongOption:
|
||||||
|
case key
|
||||||
|
of "address", "transport":
|
||||||
|
transport = parsePreserves(val)
|
||||||
|
of "cap", "sturdy":
|
||||||
|
cap = parsePreserves(val, Ref)
|
||||||
|
of "user", "username":
|
||||||
|
username = val
|
||||||
|
|
||||||
|
if calledWithArguments:
|
||||||
|
runActor("chat") do (root: Ref; turn: var Turn):
|
||||||
|
var unixAddr: transportAddress.Unix
|
||||||
|
if fromPreserve(unixAddr, transport):
|
||||||
|
stderr.writeLine "connect to ", unixAddr, " with ", cap
|
||||||
|
connect(turn, unixAddr, cap) do (turn: var Turn; ds: Ref):
|
||||||
|
|
||||||
|
during(turn, ds, ?Present) do (who: string):
|
||||||
|
echo who, " joined"
|
||||||
|
do:
|
||||||
|
echo who, " left"
|
||||||
|
|
||||||
|
onMessage(turn, ds, ?Says) do (who: string, what: string):
|
||||||
|
echo who, ": ", what
|
||||||
|
|
||||||
|
discard publish(turn, ds, Present(username: username))
|
||||||
|
readStdin(turn.facet, ds, username)
|
||||||
|
|
||||||
|
main()
|
|
@ -1,29 +0,0 @@
|
||||||
# SPDX-FileCopyrightText: ☭ Emery Hemingway
|
|
||||||
# SPDX-License-Identifier: Unlicense
|
|
||||||
|
|
||||||
import std/asyncdispatch
|
|
||||||
|
|
||||||
import syndicate
|
|
||||||
import syndicate/protocols/simpleChatProtocol
|
|
||||||
|
|
||||||
bootDataspace("main") do (ds: Ref; turn: var Turn):
|
|
||||||
let me = "user_a"
|
|
||||||
let h = publish(turn, ds, Present(username: me))
|
|
||||||
message(turn, ds, Says(who: me, what: "goodbye"))
|
|
||||||
|
|
||||||
onPublish(turn, ds, ?Present) do (username: string):
|
|
||||||
echo "presence of ", username, " asserted"
|
|
||||||
|
|
||||||
onMessage(turn, ds, ?Says) do (who: string, what: string):
|
|
||||||
echo who, " says ", what
|
|
||||||
retract(turn, h)
|
|
||||||
echo "retracted something"
|
|
||||||
|
|
||||||
during(turn, ds, ?Present) do (username: string):
|
|
||||||
echo username, " arrived"
|
|
||||||
retract(turn, h)
|
|
||||||
do:
|
|
||||||
echo "someone departed"
|
|
||||||
quit()
|
|
||||||
|
|
||||||
runForever()
|
|
Loading…
Reference in New Issue