2021-06-24 15:50:27 +00:00
|
|
|
# SPDX-License-Identifier: ISC
|
|
|
|
|
2021-06-27 10:38:04 +00:00
|
|
|
import syndicate/assertions, syndicate/dataspaces, syndicate/events, syndicate/skeletons
|
|
|
|
import preserves, preserves/records
|
2021-06-24 15:50:27 +00:00
|
|
|
import asyncdispatch, tables, options
|
|
|
|
|
|
|
|
const N = 100000
|
|
|
|
|
|
|
|
const
|
|
|
|
BoxState = RecordClass(label: symbol"BoxState", arity: 1)
|
|
|
|
SetBox = RecordClass(label: symbol"SetBox", arity: 1)
|
|
|
|
|
|
|
|
|
|
|
|
proc boot(facet: Facet) =
|
|
|
|
|
|
|
|
facet.spawn("box") do (facet: Facet):
|
|
|
|
facet.declareField(value, int, 0)
|
|
|
|
|
|
|
|
discard facet.addEndpoint do (facet: Facet) -> EndpointSpec:
|
|
|
|
# echo "recomputing published BoxState ", facet.fields.value
|
2021-07-07 10:54:33 +00:00
|
|
|
let a = BoxState % value.get
|
2021-06-24 15:50:27 +00:00
|
|
|
result.assertion = some a
|
|
|
|
|
|
|
|
discard facet.addDataflow do (facet: Facet):
|
|
|
|
# echo "box dataflow saw new value ", facet.fields.value
|
2021-06-29 15:18:09 +00:00
|
|
|
if value.get == N:
|
2021-06-24 15:50:27 +00:00
|
|
|
facet.stop do (facet: Facet):
|
|
|
|
echo "terminated box root facet"
|
|
|
|
|
2021-07-07 10:54:33 +00:00
|
|
|
facet.onMessage(SetBox % `?*`) do (facet: Facet; vs: seq[Value]):
|
2021-07-07 09:47:17 +00:00
|
|
|
value.set(vs[0])
|
|
|
|
echo "box updated value ", vs[0]
|
2021-06-24 15:50:27 +00:00
|
|
|
|
|
|
|
facet.spawn("client") do (facet: Facet):
|
|
|
|
|
2021-07-07 10:54:33 +00:00
|
|
|
facet.onAsserted(BoxState % `?*`) do (facet: Facet; vs: seq[Value]):
|
|
|
|
let v = SetBox % vs[0].int.succ
|
2021-07-07 09:47:17 +00:00
|
|
|
# echo "client sending ", v
|
|
|
|
facet.send(v)
|
2021-06-24 15:50:27 +00:00
|
|
|
|
2021-07-07 10:54:33 +00:00
|
|
|
facet.onRetracted(BoxState % `?_`) do (facet: Facet; vs: seq[Value]):
|
2021-07-07 09:47:17 +00:00
|
|
|
echo "box gone"
|
2021-06-24 15:50:27 +00:00
|
|
|
|
|
|
|
facet.actor.dataspace.ground.addStopHandler do (_: Dataspace):
|
|
|
|
echo "stopping box-and-client"
|
|
|
|
|
2021-06-29 14:31:08 +00:00
|
|
|
waitFor bootModule("box-and-client", boot)
|