Replace actor and relay API

This commit is contained in:
Emery Hemingway 2024-03-04 18:20:59 +00:00
parent eb5d4d9a57
commit 15d2e8bfb4
3 changed files with 28 additions and 14 deletions

View File

@ -188,16 +188,7 @@ macro during*(turn: untyped; ds: Cap; pattern: Pattern; publishBody: untyped) =
`callbackProc`
discard observe(`turn`, `ds`, `pattern`, during(`callbackSym`))
type BootProc = proc (turn: var Turn; ds: Cap) {.closure.}
type DeprecatedBootProc = proc (ds: Cap; turn: var Turn) {.closure.}
proc runActor*(name: string; bootProc: BootProc) =
## Run an `Actor` to completion.
let actor = bootDataspace(name, bootProc)
while actor.running:
ioqueue.run()
proc runActor*(name: string; bootProc: DeprecatedBootProc) {.deprecated.} =
## Run an `Actor` to completion.
runActor(name) do (turn: var Turn, ds: Cap):
bootProc(ds, turn)
proc runActor*(name: string; bootProc: TurnAction) =
## Boot an actor `Actor` and churn ioqueue once.
let actor = bootActor(name, bootProc)
ioqueue.run()

View File

@ -472,6 +472,9 @@ proc spawnActor*(name: string; turn: var Turn; bootProc: TurnAction; initialAsse
run(actor, bootProc, newOutBound)
actor
proc spawn*(name: string; turn: var Turn; bootProc: TurnAction; initialAssertions = initHashSet[Handle]()): Actor {.discardable.} =
spawnActor(name, turn, bootProc, initialAssertions)
proc newInertCap*(): Cap =
let a = bootActor("inert") do (turn: var Turn): turn.stop()
Cap(relay: a.root)
@ -590,6 +593,9 @@ proc stopActor*(facet: Facet) =
proc stopActor*(turn: var Turn) =
stopActor(turn.facet)
proc stop*(actor: Actor) =
stopActor(actor.root)
proc freshen*(turn: var Turn, act: TurnAction) =
assert(turn.queues.len == 0, "Attempt to freshen a non-stale Turn")
run(turn.facet, act)

View File

@ -548,8 +548,14 @@ proc spawnRelays*(turn: var Turn; ds: Cap) =
type BootProc* = proc (turn: var Turn; ds: Cap) {.closure.}
const defaultRoute* = "<route [<stdio>]>"
proc envRoute*: Route =
var text = getEnv("SYNDICATE_ROUTE")
## Get an route to a Syndicate capability from the calling environment.
## On UNIX this is the SYNDICATE_ROUTE environmental variable with a
## fallack to a defaultRoute_.
## See https://git.syndicate-lang.org/syndicate-lang/syndicate-protocols/raw/branch/main/schemas/gatekeeper.prs.
var text = getEnv("SYNDICATE_ROUTE", defaultRoute)
if text == "":
var tx = (getEnv("XDG_RUNTIME_DIR", "/run/user/1000") / "dataspace").toPreserves
result.transports = @[initRecord("unix", tx)]
@ -560,7 +566,18 @@ proc envRoute*: Route =
raise newException(ValueError, "failed to parse $SYNDICATE_ROUTE " & $pr)
proc resolve*(turn: var Turn; ds: Cap; route: Route; bootProc: BootProc) =
## Resolve `route` within `ds` and call `bootProc` with resolved capabilities.
during(turn, ds, ResolvePath ?: {0: ?route, 3: ?:ResolvedAccepted}) do (dst: Cap):
bootProc(turn, dst)
proc resolveEnvironment*(turn: var Turn; bootProc: BootProc) =
## Resolve a capability from the calling environment
## and call `bootProc`. See envRoute_.
let
ds = newDataspace(turn)
pat = ResolvePath ?: {0: ?envRoute(), 3: ?:ResolvedAccepted}
during(turn, ds, pat) do (dst: Cap):
bootProc(turn, dst)
spawnRelays(turn, ds)
# TODO: define a runActor that comes preloaded with relaying