shrub/lib/tapp.hoon

282 lines
6.7 KiB
Plaintext
Raw Normal View History

2019-05-28 23:23:06 +03:00
/- tapp-sur=tapp
2019-05-23 22:26:53 +03:00
/+ trad
2019-05-29 01:38:14 +03:00
|* $: state-type=mold
command-type=mold
poke-data=mold
out-peer-data=mold
in-peer-data=mold
==
2019-05-23 22:26:53 +03:00
|%
2019-05-29 01:38:14 +03:00
++ tapp-sur (^tapp-sur poke-data out-peer-data)
2019-05-28 23:23:06 +03:00
++ card card:tapp-sur
++ sign sign:tapp-sur
++ contract contract:tapp-sur
2019-05-29 01:38:14 +03:00
++ command
$% [%poke command=command-type]
[%peer =path]
[%diff =dock =path =in-peer-data]
==
2019-05-23 22:26:53 +03:00
::
2019-05-28 23:23:06 +03:00
++ trad-lib (^trad sign card contract)
2019-05-23 22:26:53 +03:00
++ trad trad:trad-lib
::
+$ move (pair bone card)
++ tapp-trad (trad state-type)
+$ tapp-state
2019-05-29 01:38:14 +03:00
$: waiting=(qeu command)
2019-05-23 22:26:53 +03:00
active=(unit eval-form:eval:tapp-trad)
app-state=state-type
==
2019-05-29 01:38:14 +03:00
::
:: The form of a tapp that only handles pokes
::
++ tapp-core-poke
$_ ^|
|_ [bowl:gall state-type]
++ handle-command
|~ command-type
*form:tapp-trad
--
::
++ create-tapp-poke
|= handler=tapp-core-poke
%- create-tapp-poke-peer
|_ [=bowl:gall state=state-type]
++ handle-command ~(handle-command handler bowl state)
++ handle-peer |=(* (trad-fail:trad-lib %no-peer-handler >path< ~))
--
::
:: The form of a tapp that only handles pokes and peers
::
++ tapp-core-poke-peer
$_ ^|
|_ [bowl:gall state-type]
++ handle-command
|~ command-type
*form:tapp-trad
::
++ handle-peer
|~ path
*form:tapp-trad
--
::
++ create-tapp-poke-peer
|= handler=tapp-core-poke-peer
%- create-tapp-all
|_ [=bowl:gall state=state-type]
++ handle-command ~(handle-command handler bowl state)
++ handle-peer ~(handle-peer handler bowl state)
++ handle-diff |=(* (trad-fail:trad-lib %no-diff-handler >path< ~))
--
::
:: The form of a tapp that only handles pokes and diffs
::
++ tapp-core-poke-diff
$_ ^|
|_ [bowl:gall state-type]
++ handle-command
|~ command-type
*form:tapp-trad
::
++ handle-diff
|~ [dock path in-peer-data]
*form:tapp-trad
--
::
++ create-tapp-poke-diff
|= handler=tapp-core-poke-diff
%- create-tapp-all
|_ [=bowl:gall state=state-type]
++ handle-command ~(handle-command handler bowl state)
++ handle-peer |=(=path (trad-fail:trad-lib %no-peer-handler >path< ~))
++ handle-diff ~(handle-diff handler bowl state)
--
::
:: The form of a tapp
::
++ tapp-core-all
$_ ^|
|_ [bowl:gall state-type]
++ handle-command
|~ command-type
*form:tapp-trad
::
++ handle-peer
|~ path
*form:tapp-trad
::
++ handle-diff
|~ [dock path in-peer-data]
*form:tapp-trad
--
::
++ create-tapp-all
|= handler=tapp-core-all
2019-05-23 22:26:53 +03:00
|_ [=bowl:gall tapp-state]
++ this-tapp .
2019-05-25 05:42:37 +03:00
++ prep
|= old-state=*
^- (quip move _this-tapp)
2019-05-28 23:23:06 +03:00
~& [%tapp-loaded dap.bowl]
2019-05-25 05:42:37 +03:00
=/ old ((soft tapp-state) old-state)
?~ old
`this-tapp
`this-tapp(+<+ u.old)
2019-05-23 22:26:53 +03:00
::
:: Start a command
::
2019-05-29 02:06:53 +03:00
++ poke
2019-05-23 22:26:53 +03:00
|= command=command-type
^- (quip move _this-tapp)
2019-05-29 01:38:14 +03:00
=. waiting (~(put to waiting) %poke command)
2019-05-23 22:26:53 +03:00
?^ active
~& [%waiting-until-current-trad-finishes waiting]
`this-tapp
start-trad
::
2019-05-29 01:38:14 +03:00
:: Receive subscription request
::
++ peer
|= =path
^- (quip move _this-tapp)
=. waiting (~(put to waiting) %peer path)
?^ active
`this-tapp
start-trad
::
:: Receive subscription response
::
++ diff
|= [=wire =in-peer-data]
^- (quip move _this-tapp)
?> ?=([@ @ *] wire)
=/ her (slav %p i.wire)
=* app i.t.wire
=* pax t.t.wire
=. waiting (~(put to waiting) %diff [her app] pax in-peer-data)
?^ active
`this-tapp
start-trad
::
2019-05-23 22:26:53 +03:00
:: Pass response to trad
::
++ sigh-httr
|= [=wire =httr:eyre]
^- (quip move _this-tapp)
(take-trad bowl `[wire %sigh httr])
2019-05-23 22:26:53 +03:00
::
:: Failed http request
::
++ sigh-tang
|= [=wire =tang]
^- (quip move _this-tapp)
(oob-fail-trad %failed-sigh tang)
2019-05-23 22:26:53 +03:00
::
2019-05-26 05:17:18 +03:00
++ wake
|= [=wire error=(unit tang)]
^- (quip move _this-tapp)
?^ error
(oob-fail-trad %timer-fire-failed u.error)
(take-trad bowl `[wire %wake ~])
2019-05-26 05:17:18 +03:00
::
2019-05-23 22:26:53 +03:00
:: Continue computing trad
::
++ take-trad
|= =trad-input:trad-lib
^- (quip move _this-tapp)
=/ m tapp-trad
?~ active
:: Can't cancel HTTP requests, so we might get answers after end
:: of computation
::
?: ?=([~ @ %sigh *] in.trad-input)
`this-tapp
2019-05-23 22:26:53 +03:00
~| %no-active-trad !!
=^ r=[moves=(list move) =eval-result:eval:m] u.active
(take:eval:m u.active ost.bowl trad-input)
2019-05-23 22:26:53 +03:00
=> .(active `(unit eval-form:eval:tapp-trad)`active) :: TMI
=^ moves=(list move) this-tapp
?- -.eval-result.r
%next `this-tapp
%fail (fail-trad [contracts err]:eval-result.r)
%done (done-trad [contracts value]:eval-result.r)
2019-05-23 22:26:53 +03:00
==
[(weld moves.r moves) this-tapp]
::
:: Fails currently-running trad
::
++ oob-fail-trad
(cury fail-trad contracts:(need active))
::
2019-05-23 22:26:53 +03:00
:: Called on trad failure
::
++ fail-trad
|= [contracts=(set contract) err=(pair term tang)]
2019-05-23 22:26:53 +03:00
^- (quip move _this-tapp)
2019-05-29 01:38:14 +03:00
%- %- slog
:* leaf+(trip dap.bowl)
leaf+"tapp command failed"
leaf+(trip p.err)
q.err
==
(finish-trad contracts)
2019-05-23 22:26:53 +03:00
::
:: Called on trad success
::
++ done-trad
|= [contracts=(set contract) state=state-type]
2019-05-23 22:26:53 +03:00
^- (quip move _this-tapp)
=. app-state state
(finish-trad contracts)
2019-05-23 22:26:53 +03:00
::
:: Called whether trad failed or succeeded
::
++ finish-trad
|= contracts=(set contract)
2019-05-23 22:26:53 +03:00
^- (quip move _this-tapp)
=^ moves-1 this-tapp (cancel-contracts contracts)
2019-05-23 22:26:53 +03:00
=. active ~
=. waiting +:~(get to waiting)
=^ moves-2 this-tapp start-trad
[(weld moves-1 moves-2) this-tapp]
::
2019-05-23 22:26:53 +03:00
::
:: Try to start next command
::
++ start-trad
^- (quip move _this-tapp)
?. =(~ active)
~| %trad-already-active !!
2019-05-29 01:38:14 +03:00
=/ next=(unit command) ~(top to waiting)
2019-05-23 22:26:53 +03:00
?~ next
`this-tapp
=. active
:- ~
%- from-form:eval:tapp-trad
^- form:tapp-trad
2019-05-29 01:38:14 +03:00
?- -.u.next
%poke (~(handle-command handler bowl app-state) command.u.next)
%peer (~(handle-peer handler bowl app-state) path.u.next)
%diff (~(handle-diff handler bowl app-state) +.u.next)
==
(take-trad bowl ~)
::
:: Cancel outstanding contracts
::
++ cancel-contracts
|= contracts=(set contract)
^- (quip move this-tapp)
[(zing (turn ~(tap in contracts) cancel-contract)) this-tapp]
::
:: Cancel individual contract
::
++ cancel-contract
|= =contract
^- (list move)
?- -.contract
%wait [ost.bowl %rest /(scot %da at.contract) at.contract]~
%hiss ~ :: can't cancel; will ignore response
==
2019-05-23 22:26:53 +03:00
--
--