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

250 lines
8.3 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-02-15 17:54:06 +03:00
+$ flow [=aeon fail=_| =rock:lake]
2023-02-01 19:05:18 +03:00
--
|_ [sub=(map [ship dude paths] flow) =bowl:gall result-type=type on-rock-type=type]
2023-02-16 16:49:25 +03:00
++ surf pine :: Subscribe to [ship dude path].
++ read :: See current subscribed states.
^- (map [ship dude paths] [fail=? rock:lake])
2023-02-01 19:05:18 +03:00
%- ~(run by sub)
|= =flow
2023-02-15 17:54:06 +03:00
[fail rock]:flow
2023-02-16 16:49:25 +03:00
:: :: Check poke-acks for errors.
:: :: 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]
^+ sub
?> ?=(%poke-ack -.sign)
?~ p.sign sub
%+ ~(jab by sub) [(slav %p ship) dude path]
|= =flow
2023-02-15 17:54:06 +03:00
?> =(aeon.flow (slav %ud aeon))
flow(fail &)
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-02-15 17:54:06 +03:00
=/ ship (slav %p ship)
2023-02-16 10:58:15 +03:00
=/ aeon (slav %ud aeon)
?: (lte aeon aeon:(~(got by sub) ship dude path)) ~
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)
^- (quip card:agent:gall _sub)
2023-02-15 17:54:06 +03:00
?- type.res
%yore
:_ sub :_ ~
(pine src.bowl dude.res path.res)
2023-02-01 19:05:18 +03:00
::
2023-02-15 17:54:06 +03:00
%nigh
:_ sub :_ ~
(behn-s25 [dude aeon path]:res)
2023-02-01 19:05:18 +03:00
::
2023-02-15 17:54:06 +03:00
%scry
=* current [src.bowl dude.res path.res]
=/ [wave=(unit wave:lake) =flow]
=/ old=flow (~(gut by sub) current *flow)
?- what.res
%rock ?> (gte aeon.res aeon.old)
`[aeon.res | rock.res]
2023-02-16 10:58:15 +03:00
%wave ~| [%weird-wave res=res old=old]
?> =(aeon.res +(aeon.old))
2023-02-15 17:54:06 +03:00
[`wave.res [aeon.res | (wash:lake rock.old wave.res)]]
==
2023-02-01 19:05:18 +03:00
:_ (~(put by sub) current flow)
2023-02-15 17:54:06 +03:00
%- flop
:~ (scry `+(aeon.res) src.bowl dude.res path.res)
:* %pass (zoom on-rock/(scot %ud aeon.flow)^(scot %p src.bowl)^dude.res^path.res)
2023-02-01 19:05:18 +03:00
%agent [our dap]:bowl
2023-02-15 17:54:06 +03:00
%poke %sss-on-rock on-rock-type ^- from
[path.res src.bowl dude.res rock.flow wave]
2023-02-01 19:05:18 +03:00
== ==
2023-02-15 17:54:06 +03:00
==
2023-02-16 16:49:25 +03:00
::
:: Non-public facing arms below
::
+$ from (on-rock:poke lake paths)
+$ into (response:poke lake paths)
+$ result (request:poke paths)
++ 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
=/ when ?~ when %~ (scot %ud u.when)
:* %pass (zoom request/scry/(scot %p who)^which^when^where)
%agent [who which]
%poke %sss-to-pub :- result-type ^- result
[where which ^when]
==
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-02-16 16:49:25 +03:00
+$ 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-02-15 17:54:06 +03:00
mem=(mip aeon [ship dude] @da)
2023-02-01 19:05:18 +03:00
==
--
|_ [pub=(map paths tide) =bowl:gall result-type=type]
+* 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]
^+ pub
%+ ~(jab by pub) path
|= =tide
2023-02-01 23:52:30 +03:00
(form tide(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
^+ pub
%+ ~(jab by pub) path
|= =tide
2023-02-01 23:52:30 +03:00
%* . (form tide(rul [0 1]))
2023-02-01 19:05:18 +03:00
rul rul.tide
2023-02-01 23:52:30 +03:00
wav ~
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-02-15 17:54:06 +03:00
^- (quip card:agent:gall _pub)
2023-02-01 19:05:18 +03:00
?~ ;;((soft ^path) path) ~| %need-path !!
=/ =tide (~(gut by pub) path *tide)
=/ next=aeon
2023-02-15 17:54:06 +03:00
.+ %+ max
2023-02-01 19:05:18 +03:00
(fall (bind (pry:rok rok.tide) head) 0)
(fall (bind (ram:wav wav.tide) head) 0)
2023-02-15 17:54:06 +03:00
::
:_ %+ ~(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)) tide
(form tide)
::
2023-02-17 12:22:58 +03:00
%+ murn ~(tap by (~(gut by mem.tide) next ~))
2023-02-15 17:54:06 +03:00
|= [[=ship =dude] =@da]
2023-02-17 12:22:58 +03:00
?: (lth da now.bowl) ~
`(send scry/wave/wave ship dude next path)
2023-02-16 16:49:25 +03:00
++ read :: See current published states.
^- (map paths rock:lake)
2023-02-01 19:05:18 +03:00
%- ~(run by pub)
|= =tide
=< rock
=/ 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-02-16 10:58:15 +03:00
^- (quip card:agent:gall _pub)
2023-02-01 19:05:18 +03:00
=/ =tide (~(gut by pub) path.req *tide)
2023-02-15 17:54:06 +03:00
?~ when.req
=/ last (fall (pry:rok rok.tide) *[=key =val]:rok)
:_ pub :_ ~
2023-02-16 10:58:15 +03:00
(send scry/rock/val.last src.bowl dude.req key.last path.req)
2023-02-15 17:54:06 +03:00
?^ dat=(get:wav wav.tide u.when.req)
:_ 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
key::(fall (ram:wav wav.tide) (pry:rok rok.tide) [=key val]:wav)
2023-02-15 17:54:06 +03:00
:_ pub :_ ~
2023-02-16 10:58:15 +03:00
(send yore/~ src.bowl [dude u.when path]:req)
2023-02-17 12:22:58 +03:00
:- ~[(send nigh/~ src.bowl [dude u.when path]:req)]
2023-02-15 17:54:06 +03:00
%+ ~(put by pub) path.req
%= tide mem
%^ ~(put bi mem.tide) u.when.req [src.bowl dude.req]
(add ~s25 now.bowl)
==
2023-02-16 16:49:25 +03:00
::
:: Non-public facing arms below
::
+$ into (request:poke paths)
+$ result (response:poke lake paths)
++ send
|= [payload=_|3:*(response:poke lake paths) =ship =dude =aeon path=paths]
^- card:agent:gall
=* mark (cat 3 %sss- name:lake)
:* %pass (zoom response/scry/(scot %p ship)^dude^(scot %ud aeon)^path)
%agent [ship dude]
%poke mark result-type ^- (response:poke lake paths)
[path dap.bowl aeon payload]
==
++ 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
--
--