urbit/pkg/base-dev/lib/sss.hoon

372 lines
13 KiB
Plaintext
Raw Normal View History

2023-02-01 19:05:18 +03:00
/- *sss
2023-02-15 17:54:06 +03:00
/+ *mip
2023-02-01 19:05:18 +03:00
::
|%
2023-02-16 16:49:25 +03:00
++ mk-subs :: Create sub-map.
|* [=(lake) paths=mold]
-:+6:(da lake paths)
::
++ mk-pubs :: Create pub-map.
|* [=(lake) paths=mold]
-:+6:(du lake paths)
::
++ mk-mar :: Create mar.
2023-02-01 19:05:18 +03:00
|* =(lake)
|_ =(response:poke lake *)
++ grow
|%
++ noun response
--
++ grab
|%
++ noun (response:poke lake *)
--
++ grad %noun
--
++ fled :: Like +sped but head is a path.
|= vax=vase
^- vase
:_ q.vax
%- ~(play ut p.vax)
=- [%wtgr [%wtts - [%& 2]~] [%$ 1]]
2023-02-15 17:54:06 +03:00
=/ pax ~| %path-none ;;(path -.q.vax)
2023-02-01 19:05:18 +03:00
|- ^- spec
?~ pax [%base %null]
[%bccl ~[[%leaf %ta -.pax] $(pax +.pax)]]
::
2023-02-15 17:54:06 +03:00
++ zoom |= =noun ~| %need-path $/sss/;;(path noun)
2023-02-16 16:49:25 +03:00
::
++ da :: Manage subscriptions.
2023-02-01 19:05:18 +03:00
|* [=(lake) paths=mold]
=>
|%
2023-03-27 20:21:19 +03:00
+$ from (on-rock:poke lake paths)
+$ into (response:poke lake paths)
+$ result (request:poke paths)
+$ fail [paths ship dude]
+$ flow [=aeon stale=_| fail=_| =rock:lake]
+$ subs [%0 (map [ship dude paths] (unit flow))]
2023-02-01 19:05:18 +03:00
--
2023-03-27 20:21:19 +03:00
|= $: sub=subs
=bowl:gall
result-type=type
on-rock-type=type
fail-type=type
==
=> .(sub +.sub)
|%
++ surf :: Subscribe to [ship dude path].
|= which=[ship dude paths]
^- (quip card:agent:gall subs)
?+ flow=(~(get by sub) which) `0/sub
~ [~[(pine which)] 0/(~(put by sub) which ~)]
[~ ~] [~[(pine which)] 0/sub]
[~ ~ [* %& * *]] [~[(scry `+(aeon.u.u.flow) which)] 0/sub]
==
++ quit (corl (lead %0) ~(del by sub)) :: Unsub from [ship dude path].
2023-02-16 16:49:25 +03:00
++ read :: See current subscribed states.
2023-03-27 20:21:19 +03:00
^- (map [ship dude paths] [stale=? fail=? =rock:lake])
%- malt %+ murn ~(tap by sub)
|= [key=[ship dude paths] val=(unit flow)]
?~ val ~
`[key +.u.val]
:: :: Check poke-ack for errors.
2023-02-16 16:49:25 +03:00
:: :: If an %sss-on-rock poke nacks,
++ chit :: that state is flagged as failed.
2023-02-01 19:05:18 +03:00
|= [[aeon=term ship=term dude=term path=paths] =sign:agent:gall]
2023-03-27 20:21:19 +03:00
^- subs
:- %0
2023-02-01 19:05:18 +03:00
?> ?=(%poke-ack -.sign)
?~ p.sign sub
%+ ~(jab by sub) [(slav %p ship) dude path]
2023-03-27 20:21:19 +03:00
|= (unit flow)
=/ =flow (need +<)
2023-02-15 17:54:06 +03:00
?> =(aeon.flow (slav %ud aeon))
2023-03-27 20:21:19 +03:00
`flow(fail &)
:: :: Check poke-ack for errors.
:: :: If a scry request nacks,
++ tell :: that state is flagged as stale.
|= [[ship=term =dude aeon=term path=paths] =sign:agent:gall]
^- (quip card:agent:gall subs)
?> ?=(%poke-ack -.sign)
?~ p.sign `0/sub
=/ current [ship=(slav %p ship) dude=dude path=path]
?+ flow=(~(get by sub) current) `0/sub
[~ ~ *]
=. stale.u.u.flow &
:_ 0/(~(put by sub) current u.flow)
~[(on-rock-poke current u.u.flow ~)]
::
[~ ~]
:_ 0/(~(del by sub) current) :_ ~
:* %pass (zoom surf-fail/aeon/ship/dude/path)
%agent [our dap]:bowl
%poke %sss-surf-fail fail-type ^- fail
[path ship dude]:current
==
==
2023-02-16 16:49:25 +03:00
:: :: Check if we're still interested
:: :: in a wave. If no, no-op.
:: :: If yes, scry.
++ behn :: (See https://gist.github.com/belisarius222/7f8452bfea9b199c0ed717ab1778f35b)
2023-02-15 17:54:06 +03:00
|= [ship=term =dude aeon=term path=paths]
2023-02-01 19:05:18 +03:00
^- (list card:agent:gall)
2023-03-27 20:21:19 +03:00
%- fall :_ ~ %- mole |.
2023-02-15 17:54:06 +03:00
=/ ship (slav %p ship)
2023-02-16 10:58:15 +03:00
=/ aeon (slav %ud aeon)
2023-03-27 20:21:19 +03:00
?: (lte aeon aeon:(fall (~(got by sub) ship dude path) *flow)) ~
2023-02-16 10:58:15 +03:00
~[(scry `aeon ship dude path)]
2023-02-01 19:05:18 +03:00
::
2023-02-16 16:49:25 +03:00
++ apply :: Handle response from publisher.
2023-02-01 19:05:18 +03:00
|= res=(response:poke lake paths)
2023-03-27 20:21:19 +03:00
^- (quip card:agent:gall subs)
%- fall :_ `0/sub %- mole |.
=* current [src.bowl dude.res path.res]
=/ old=flow (fall (~(got by sub) current) *flow)
2023-02-15 17:54:06 +03:00
?- type.res
2023-03-27 20:21:19 +03:00
%tomb
=/ =flow old(stale &)
:_ 0/(~(put by sub) current `flow) :_ ~
(on-rock-poke current flow ~)
::
2023-02-15 17:54:06 +03:00
%yore
2023-03-27 20:21:19 +03:00
:_ 0/sub :_ ~
2023-02-15 17:54:06 +03:00
(pine src.bowl dude.res path.res)
2023-02-01 19:05:18 +03:00
::
2023-02-15 17:54:06 +03:00
%nigh
2023-03-27 20:21:19 +03:00
:_ 0/sub :_ ~
2023-02-15 17:54:06 +03:00
(behn-s25 [dude aeon path]:res)
2023-02-01 19:05:18 +03:00
::
2023-02-15 17:54:06 +03:00
%scry
=/ [wave=(unit wave:lake) =flow]
?- what.res
%rock ?> (gte aeon.res aeon.old)
2023-03-27 20:21:19 +03:00
[~ [aeon.res | | rock.res]]
%wave ?> =(aeon.res +(aeon.old))
[`wave.res [aeon.res | | (wash:lake rock.old wave.res)]]
2023-02-15 17:54:06 +03:00
==
2023-03-27 20:21:19 +03:00
:_ 0/(~(put by sub) current `flow)
:~ (on-rock-poke current flow wave)
(scry `+(aeon.res) src.bowl dude.res path.res)
==
2023-02-15 17:54:06 +03:00
==
2023-02-16 16:49:25 +03:00
::
:: Non-public facing arms below
::
++ behn-s25
|= [=dude =aeon path=noun]
^- card:agent:gall
2023-02-17 12:22:58 +03:00
:* %pass (zoom behn/(scot %p src.bowl)^dude^(scot %ud aeon)^path)
2023-02-16 16:49:25 +03:00
%arvo %b %wait (add ~s25 now.bowl)
==
++ pine |= [ship dude paths] (scry ~ +<)
++ scry
|= [when=(unit aeon) who=ship which=dude where=paths]
^- card:agent:gall
2023-03-27 20:21:19 +03:00
=/ when ?~ when ~ (scot %ud u.when)
:* %pass (zoom scry-request/(scot %p who)^which^when^where)
2023-02-16 16:49:25 +03:00
%agent [who which]
%poke %sss-to-pub :- result-type ^- result
2023-03-27 20:21:19 +03:00
[where dap.bowl ^when]
==
++ on-rock-poke
|= [[=ship =dude path=paths] flow wave=(unit wave:lake)]
^- card:agent:gall
:* %pass (zoom on-rock/(scot %ud aeon)^(scot %p ship)^dude^path)
%agent [our dap]:bowl
%poke %sss-on-rock on-rock-type ^- from
[path ship dude stale fail rock wave]
2023-02-16 16:49:25 +03:00
==
2023-02-01 19:05:18 +03:00
--
2023-02-16 16:49:25 +03:00
++ du :: Manage publications.
2023-02-01 19:05:18 +03:00
|* [=(lake) paths=mold]
=>
|%
2023-03-27 20:21:19 +03:00
+$ into (request:poke paths)
+$ result (response:poke lake paths)
+$ rule [rocks=_1 waves=_5] :: Retention policy
2023-02-01 19:05:18 +03:00
+$ tide
$: rok=((mop aeon rock:lake) gte)
wav=((mop aeon wave:lake) lte)
rul=rule
2023-03-27 20:21:19 +03:00
mem=(mip ship dude @da)
==
+$ buoy
$: tid=$~(*tide $@(aeon tide))
alo=(unit (set ship))
2023-02-01 19:05:18 +03:00
==
2023-03-27 20:21:19 +03:00
+$ pubs [%0 (map paths buoy)]
2023-02-01 19:05:18 +03:00
--
2023-03-27 20:21:19 +03:00
|= [pub=pubs =bowl:gall result-type=type]
=> .(pub +.pub)
=* rok ((on aeon rock:lake) gte)
=* wav ((on aeon wave:lake) lte)
|%
2023-02-16 16:49:25 +03:00
++ rule :: Set new retention policy.
2023-02-01 19:05:18 +03:00
|= [path=paths =^rule]
2023-03-27 20:21:19 +03:00
^- pubs
:- %0
2023-02-01 19:05:18 +03:00
%+ ~(jab by pub) path
2023-03-27 20:21:19 +03:00
|= =buoy
?@ tid.buoy buoy
buoy(tid (form tid.buoy(rul rule)))
2023-02-01 19:05:18 +03:00
::
2023-02-16 16:49:25 +03:00
++ wipe :: Create new rock and wipe rest.
2023-02-01 19:05:18 +03:00
|= path=paths
2023-03-27 20:21:19 +03:00
^- pubs
:- %0
2023-02-01 19:05:18 +03:00
%+ ~(jab by pub) path
2023-03-27 20:21:19 +03:00
|= =buoy
?@ tid.buoy buoy
%* . buoy(tid (form tid.buoy(rul [0 1])))
rul.tid rul.tid.buoy
wav.tid ~
2023-02-01 19:05:18 +03:00
==
2023-02-16 16:49:25 +03:00
++ give :: Give a wave on a path.
2023-02-01 19:05:18 +03:00
|= [path=paths =wave:lake]
2023-03-27 20:21:19 +03:00
^- (quip card:agent:gall pubs)
?~ ((soft ^path) path) ~| %need-path !!
=/ buoy (~(gut by pub) path *buoy)
?@ tide=tid.buoy ~| %dead-path !! ::TODO is this good behavior?
=/ next=aeon +((latest tide))
:- %+ murn ~(tap bi mem.tide)
|= [=ship =dude =@da]
?: (lth da now.bowl) ~
`(send scry/wave/wave ship dude next path)
:- %0
%+ ~(put by pub) path
=/ last=[=aeon =rock:lake] (fall (pry:rok rok.tide) *[key val]:rok)
=. wav.tide (put:wav wav.tide next wave)
=. mem.tide (~(del by mem.tide) next)
?. =(next (add aeon.last waves.rul.tide)) buoy
buoy(tid (form tide))
::
++ perm :: Change permissions with gate.
|= [where=(list paths) diff=$-((unit (set ship)) (unit (set ship)))]
^- pubs
%+ edit where
|= =buoy
=/ new=_alo.buoy (diff alo.buoy)
?@ tid.buoy buoy(alo new)
%= buoy
alo new
mem.tid ?~ new mem.tid.buoy
%. mem.tid.buoy
~(int by (malt (turn ~(tap in u.new) (late *(map @ @)))))
==
++ public (curr perm _~) :: Make list of paths public.
++ secret (curr perm _`~) :: Make list of paths secret.
:: :: Block ships from paths.
++ block :: No-ops on public paths.
|= [who=(list ship) whence=(list paths)]
^- pubs
%+ perm whence
|= old=(unit (set ship))
?~ old ~ `(~(dif in u.old) (sy who))
2023-03-27 20:21:19 +03:00
:: :: Allow ships to paths.
++ allow :: Any public paths will no-op.
|= [who=(list ship) where=(list paths)]
^- pubs
%+ perm where
|= old=(unit (set ship))
?~ old ~ `(~(gas in u.old) who)
2023-03-27 20:21:19 +03:00
:: :: Kill a list of paths, i.e. tell
++ kill :: subs to not expect updates.
(curr edit |=(=buoy buoy(tid (latest tid.buoy))))
:: :: Reopen list of killed paths.
++ live :: No-ops on live paths.
%+ curr edit
|= =buoy
?^ tid.buoy buoy
%*(. buoy(tid *tide) rok.tid (put:rok ~ +(tid.buoy) *rock:lake))
::
2023-02-16 16:49:25 +03:00
++ read :: See current published states.
2023-03-27 20:21:19 +03:00
^- (map paths [allowed=(unit (set ship)) =rock:lake])
%- malt %+ murn ~(tap by pub)
|= [path=paths =buoy]
^- (unit [paths (unit (set ship)) rock:lake])
?@ tide=tid.buoy ~
:^ ~ path alo.buoy =< rock
2023-02-01 19:05:18 +03:00
=/ snap=[=aeon =rock:lake] (fall (pry:rok rok.tide) *[key val]:rok)
%+ roll (tap:wav (lot:wav wav.tide `aeon.snap ~))
|= [[=aeon =wave:lake] =_snap]
?. =(aeon +(aeon.snap)) snap
[aeon (wash:lake rock.snap wave)]
::
2023-02-16 16:49:25 +03:00
++ apply :: Handle request from subscriber.
2023-02-01 19:05:18 +03:00
|= req=(request:poke paths)
2023-03-27 20:21:19 +03:00
^- (quip card:agent:gall pubs)
=/ =buoy (~(gut by pub) path.req *buoy)
?< &(?=(^ alo.buoy) !(~(has in u.alo.buoy) src.bowl))
?@ tid.buoy
:_ 0/pub :_ ~
(send tomb/~ src.bowl dude.req tid.buoy path.req)
2023-02-15 17:54:06 +03:00
?~ when.req
2023-03-27 20:21:19 +03:00
=/ last (fall (pry:rok rok.tid.buoy) *[=key =val]:rok)
:_ 0/pub :_ ~
2023-02-16 10:58:15 +03:00
(send scry/rock/val.last src.bowl dude.req key.last path.req)
2023-03-27 20:21:19 +03:00
?^ dat=(get:wav wav.tid.buoy u.when.req)
:_ 0/pub :_ ~
2023-02-16 10:58:15 +03:00
(send scry/wave/u.dat src.bowl [dude u.when path]:req)
2023-03-17 02:05:09 +03:00
?: %+ lte u.when.req
2023-03-27 20:21:19 +03:00
key::(fall (ram:wav wav.tid.buoy) (pry:rok rok.tid.buoy) [=key val]:wav)
:_ 0/pub :_ ~
2023-02-16 10:58:15 +03:00
(send yore/~ src.bowl [dude u.when path]:req)
2023-03-27 20:21:19 +03:00
?> =(u.when.req +((latest tid.buoy)))
2023-02-17 12:22:58 +03:00
:- ~[(send nigh/~ src.bowl [dude u.when path]:req)]
2023-03-27 20:21:19 +03:00
:- %0
2023-02-15 17:54:06 +03:00
%+ ~(put by pub) path.req
2023-03-27 20:21:19 +03:00
%= buoy
mem.tid (~(put bi mem.tid.buoy) src.bowl dude.req (add ~s25 now.bowl))
2023-02-15 17:54:06 +03:00
==
2023-02-16 16:49:25 +03:00
::
:: Non-public facing arms below
::
++ send
|= [payload=_|3:*(response:poke lake paths) =ship =dude =aeon path=paths]
^- card:agent:gall
=* mark (cat 3 %sss- name:lake)
2023-03-27 20:21:19 +03:00
:* %pass (zoom scry-response/(scot %p ship)^dude^(scot %ud aeon)^path)
2023-02-16 16:49:25 +03:00
%agent [ship dude]
%poke mark result-type ^- (response:poke lake paths)
[path dap.bowl aeon payload]
==
2023-03-27 20:21:19 +03:00
++ latest
|= =$@(aeon tide)
^- aeon
?@ tide tide
%+ max (fall (bind (pry:rok rok.tide) head) 0)
(fall (bind (ram:wav wav.tide) head) 0)
::
++ edit
|= [ps=(list paths) edit=$-(buoy buoy)]
^- pubs
:- %0
%- ~(rep in (sy ps))
|= [path=paths =_pub]
%- fall :_ pub %- mole |.
(~(jab by pub) path edit)
::
2023-02-16 16:49:25 +03:00
++ form
|= =tide
^+ tide
=/ max-rock=[=aeon =rock:lake] (fall (pry:rok rok.tide) *[key val]:rok)
=/ max-wave (fall (bind (ram:wav wav.tide) head) 0)
=. rok.tide
%+ gas:rok +<-:gas:rok
%- tab:rok :_ [~ +(rocks.rul.tide)]
?: ?| =(waves.rul.tide 0)
(lth max-wave (add aeon.max-rock waves.rul.tide))
==
rok.tide
%+ put:rok rok.tide
%+ roll (tab:wav wav.tide `aeon.max-rock max-wave)
|: [*[now=aeon =wave:lake] `[prev=aeon =rock:lake]`max-rock]
~| %aeon-awry
?> =(now +(prev))
[now (wash:lake rock wave)]
~| %rock-zero
tide(wav (lot:wav wav.tide (bind (ram:rok rok.tide) |=([r=@ *] (dec r))) ~))
2023-02-01 19:05:18 +03:00
--
--