urbit/app/aqua.hoon

759 lines
19 KiB
Plaintext
Raw Normal View History

2019-02-06 05:21:41 +03:00
:: An aquarium of virtual ships. Put in some fish and watch them!
::
2019-02-02 00:49:14 +03:00
:: usage:
2019-02-02 04:00:15 +03:00
:: |start %aqua
2019-02-06 05:21:41 +03:00
:: /- aquarium
:: :aqua &pill .^(pill:aquarium %cx %/urbit/pill)
2019-02-02 04:14:11 +03:00
:: :aqua [%init ~[~bud ~dev]]
:: :aqua [%dojo ~[~bud ~dev] "[our eny (add 3 5)]"]
:: :aqua [%dojo ~[~bud] "|hi ~dev"]
2019-02-05 01:31:55 +03:00
:: :aqua [%wish ~[~bud ~dev] '(add 2 3)']
:: :aqua [%peek ~[~bud] /cx/~bud/home/(scot %da now)/app/curl/hoon]
:: :aqua [%dojo ~[~bud ~dev] '|mount %']
:: :aqua [%file ~[~bud ~dev] %/sys/vane]
:: :aqua [%pause-events ~[~bud ~dev]]
2019-02-02 00:49:14 +03:00
::
2019-02-02 04:00:15 +03:00
::
2019-02-06 05:21:41 +03:00
:: We get ++unix-event and ++pill from /-aquarium
2019-02-02 04:00:15 +03:00
::
2019-02-06 05:21:41 +03:00
/- aquarium
=, aquarium
2019-02-02 00:49:14 +03:00
=> $~ |%
++ move (pair bone card)
++ card
2019-02-02 04:00:15 +03:00
$% [%wait wire p=@da]
2019-02-02 00:49:14 +03:00
[%rest wire p=@da]
[%hiss wire p=(unit user:eyre) q=mark r=(cask hiss:eyre)]
2019-02-08 05:03:46 +03:00
[%diff %aqua-effects aqua-effects]
2019-02-02 00:49:14 +03:00
==
++ state
2019-02-02 04:00:15 +03:00
$: %0
pil=pill
2019-02-02 00:49:14 +03:00
assembled=*
2019-02-09 06:18:38 +03:00
tym=@da
init-cache=(map ship pier)
2019-02-02 00:49:14 +03:00
fleet-snaps=(map term (map ship pier))
piers=(map ship pier)
==
++ pier
2019-02-09 06:18:38 +03:00
$: snap=*
2019-02-09 02:21:40 +03:00
event-log=(list [@da unix-event])
2019-02-02 00:49:14 +03:00
next-events=(qeu unix-event)
processing-events=?
next-timer=(unit @da)
http-requests=(set @ud)
2019-02-02 00:49:14 +03:00
==
--
=, gall
2019-02-08 04:12:57 +03:00
::
2019-02-08 05:03:46 +03:00
:: aqua-effect-list: collect list of aqua effects to broadcast at once
:: to avoid gall backpressure
:: moves: Hoist moves into state for cleaner state management
2019-02-08 04:12:57 +03:00
::
2019-02-08 05:03:46 +03:00
=| unix-effects=(jar ship unix-effect)
2019-02-08 04:12:57 +03:00
=| moves=(list move)
2019-02-06 05:21:41 +03:00
|_ $: hid=bowl
2019-02-02 00:49:14 +03:00
state
==
2019-02-02 04:00:15 +03:00
::
:: Represents a single ship's state.
::
2019-02-02 00:49:14 +03:00
++ pe
|= who=ship
=+ (fall (~(get by piers) who) *pier)
=* pier-data -
|%
2019-02-08 04:12:57 +03:00
++ abet-pe
^+ this
2019-02-02 00:49:14 +03:00
=. piers (~(put by piers) who pier-data)
2019-02-08 04:12:57 +03:00
this
2019-02-02 00:49:14 +03:00
::
++ apex
2019-02-02 04:00:15 +03:00
=. pier-data *pier
2019-02-02 00:49:14 +03:00
=. snap assembled
~& r=(met 3 (jam snap))
2019-02-08 04:12:57 +03:00
..abet-pe
2019-02-02 00:49:14 +03:00
::
++ push-events
|= ova=(list unix-event)
2019-02-08 04:12:57 +03:00
^+ ..abet-pe
2019-02-02 00:49:14 +03:00
=. next-events (~(gas to next-events) ova)
2019-02-08 04:12:57 +03:00
..abet-pe
2019-02-02 00:49:14 +03:00
::
++ emit-moves
|= ms=(list move)
2019-02-08 05:03:46 +03:00
=. this (^emit-moves ms)
2019-02-08 04:12:57 +03:00
..abet-pe
2019-02-02 00:49:14 +03:00
::
2019-02-02 04:00:15 +03:00
:: Process the events in our queue.
::
2019-02-02 00:49:14 +03:00
++ plow
2019-02-08 04:12:57 +03:00
|- ^+ ..abet-pe
2019-02-02 00:49:14 +03:00
?: =(~ next-events)
2019-02-08 04:12:57 +03:00
..abet-pe
2019-02-02 00:49:14 +03:00
?. processing-events
2019-02-08 04:12:57 +03:00
..abet-pe
2019-02-02 00:49:14 +03:00
=^ ovo next-events ~(get to next-events)
=/ res (mox +47.snap)
?> ?=(%0 -.res)
2019-02-05 01:31:55 +03:00
=/ poke p.res
2019-02-09 02:21:40 +03:00
=. tym (max +(tym) now.hid)
=/ res (slum poke tym ovo)
=. event-log [[tym ovo] event-log]
2019-02-02 00:49:14 +03:00
=. snap +3.res
2019-02-08 04:12:57 +03:00
=. ..abet-pe (handle-effects ((list ovum) -.res))
2019-02-02 00:49:14 +03:00
$
::
2019-02-05 01:31:55 +03:00
:: Peek
::
++ peek
|= p=*
=/ res (mox +46.snap)
?> ?=(%0 -.res)
=/ peek p.res
~& [who=who %peeked (slum peek [now.hid p])]
2019-02-08 04:12:57 +03:00
..abet-pe
2019-02-05 01:31:55 +03:00
::
:: Wish
::
++ wish
|= txt=@t
=/ res (mox +22.snap)
?> ?=(%0 -.res)
=/ wish p.res
~& [who=who %wished (slum wish txt)]
2019-02-08 04:12:57 +03:00
..abet-pe
2019-02-05 01:31:55 +03:00
::
:: Restart outstanding requests
::
++ restore
2019-02-08 04:12:57 +03:00
^+ ..abet-pe
:: Restore behn
::
2019-02-08 04:12:57 +03:00
=. ..abet-pe
?~ next-timer
2019-02-08 04:12:57 +03:00
..abet-pe
(set-timer u.next-timer)
:: Restore eyre
::
=. http-requests ~
2019-02-08 04:12:57 +03:00
=. ..abet-pe (push-events [//http/0v1n.2m9vh %born ~]~)
..abet-pe
::
:: Cancel outstanding requests
::
++ sleep
2019-02-08 04:12:57 +03:00
^+ ..abet-pe
:: Sleep behn
::
2019-02-08 04:12:57 +03:00
=. ..abet-pe
?~ next-timer
2019-02-08 04:12:57 +03:00
..abet-pe
cancel-timer
:: Sleep eyre
::
:: Eyre doesn't support cancelling HTTP requests from userspace.
::
=. http-requests ~
2019-02-08 04:12:57 +03:00
..abet-pe
::
2019-02-02 04:00:15 +03:00
++ mox |=(* (mock [snap +<] scry))
::
:: Start/stop processing events. When stopped, events are added to
:: our queue but not processed.
::
2019-02-02 00:49:14 +03:00
++ start-processing-events .(processing-events &)
++ stop-processing-events .(processing-events |)
2019-02-02 04:00:15 +03:00
::
:: Handle all the effects produced by a single event.
2019-02-02 00:49:14 +03:00
::
++ handle-effects
|= effects=(list ovum)
2019-02-08 04:12:57 +03:00
^+ ..abet-pe
2019-02-02 00:49:14 +03:00
?~ effects
2019-02-08 04:12:57 +03:00
..abet-pe
=. ..abet-pe
2019-02-02 00:49:14 +03:00
=/ sof ((soft unix-effect) i.effects)
?~ sof
~& [who=who %unknown-effect i.effects]
2019-02-08 04:12:57 +03:00
..abet-pe
=. ..abet-pe
2019-02-06 05:21:41 +03:00
?- -.q.u.sof
2019-02-02 04:00:15 +03:00
%blit (handle-blit u.sof)
2019-02-02 00:49:14 +03:00
%send (handle-send u.sof)
%doze (handle-doze u.sof)
%thus (handle-thus u.sof)
%ergo (handle-ergo u.sof)
2019-02-06 05:21:41 +03:00
==
(publish-effect u.sof)
2019-02-02 00:49:14 +03:00
$(effects t.effects)
::
2019-02-02 04:00:15 +03:00
:: Would love to see a proper stateful terminal handler. Ideally,
:: you'd be able to ^X into the virtual ship, like the old ^W.
::
:: However, that's porbably not the primary way of interacting with
:: it. In practice, most of the time you'll be running from a file
:: (eg for automated testing) or fanning the same command to multiple
:: ships or otherwise making use of the fact that we can
:: programmatically send events.
::
++ handle-blit
|= [way=wire %blit blits=(list blit:dill)]
2019-02-08 04:12:57 +03:00
^+ ..abet-pe
2019-02-02 04:00:15 +03:00
=/ last-line
%+ roll blits
|= [b=blit:dill line=tape]
?- -.b
%lin (tape p.b)
%mor ~& "{<who>}: {line}" ""
%hop line
%bel line
%clr ""
%sag ~& [%save-jamfile-to p.b] line
%sav ~& [%save-file-to p.b] line
%url ~& [%activate-url p.b] line
==
~& last-line
2019-02-08 04:12:57 +03:00
..abet-pe
2019-02-02 04:00:15 +03:00
::
:: This needs a better SDN solution. Every ship should have an IP
:: address, and we should eventually test changing those IP
:: addresses.
::
:: For now, we broadcast every packet to every ship and rely on them
:: to drop them.
::
2019-02-02 00:49:14 +03:00
++ handle-send
|= [way=wire %send lan=lane:ames pac=@]
2019-02-08 04:12:57 +03:00
^+ ..abet-pe
2019-02-02 00:49:14 +03:00
=/ dest-ip
|- ^- (unit @if)
?- -.lan
%if `r.lan
%is ?~(q.lan ~ $(lan u.q.lan))
%ix `r.lan
==
?~ dest-ip
~& [%sending-no-destination who lan]
2019-02-08 04:12:57 +03:00
..abet-pe
2019-02-02 04:00:15 +03:00
?. &(=(0 (rsh 0 16 u.dest-ip)) =(1 (rsh 0 8 u.dest-ip)))
2019-02-02 00:49:14 +03:00
~& [%havent-implemented-direct-lanes who lan]
2019-02-08 04:12:57 +03:00
..abet-pe
2019-02-09 02:21:40 +03:00
:: ~& [who=who %blast-sending]
2019-02-02 04:00:15 +03:00
=/ hear [//newt/0v1n.2m9vh %hear lan pac]
=. this (blast-event hear)
:: =/ her ?:(=(~dev who) ~bud ~dev) ::ship (dis u.dest-ip 0xff)
:: ?. (~(has by piers) her)
:: ~& [%dropping who=who her=her]
2019-02-08 04:12:57 +03:00
:: ..abet-pe
2019-02-02 04:00:15 +03:00
:: ~& [%sending who=who her=her ip=`@ux`u.dest-ip]
:: =^ ms this
2019-02-08 04:12:57 +03:00
:: abet-pe:(push-events:(pe her) ~[hear])
..abet-pe
2019-02-02 04:00:15 +03:00
::
:: Would love to be able to control time more precisely, jumping
:: forward and whatnot.
2019-02-02 00:49:14 +03:00
::
++ handle-doze
|= [way=wire %doze tim=(unit @da)]
2019-02-08 04:12:57 +03:00
^+ ..abet-pe
2019-02-02 00:49:14 +03:00
?~ tim
?~ next-timer
2019-02-08 04:12:57 +03:00
..abet-pe
2019-02-02 00:49:14 +03:00
cancel-timer
?~ next-timer
(set-timer u.tim)
(set-timer:cancel-timer u.tim)
::
++ set-timer
|= tim=@da
=. tim +(tim) :: nobody's perfect
2019-02-09 06:18:38 +03:00
~& [who=who %setting-timer tim]
2019-02-02 00:49:14 +03:00
=. next-timer `tim
(emit-moves [ost.hid %wait /(scot %p who) tim]~)
::
++ cancel-timer
2019-02-09 06:18:38 +03:00
~& [who=who %cancell-timer (need next-timer)]
2019-02-02 00:49:14 +03:00
(emit-moves [ost.hid %rest /(scot %p who) (need next-timer)]~)
::
++ take-wake
|= [way=wire ~]
2019-02-09 06:18:38 +03:00
~& [who=who %wakey now.hid]
=. next-timer ~
%- push-events:(pe who)
[//behn/0v1n.2m9vh %wake ~]~
::
:: Handle outgoing HTTP request
::
++ handle-thus
|= [way=wire %thus num=@ud req=(unit hiss:eyre)]
2019-02-08 04:12:57 +03:00
^+ ..abet-pe
?~ req
?. (~(has in http-requests) num)
2019-02-08 04:12:57 +03:00
..abet-pe
:: Eyre doesn't support cancelling HTTP requests from userspace,
:: so we remove it from our state so we won't pass along the
:: response.
::
~& [who=who %cant-cancel-thus num=num]
=. http-requests (~(del in http-requests) num)
2019-02-08 04:12:57 +03:00
..abet-pe
=. http-requests (~(put in http-requests) num)
%- emit-moves :_ ~
:* ost.hid
%hiss
/(scot %p who)/(scot %ud num)
~
%httr
[%hiss u.req]
==
::
:: Pass HTTP response back to virtual ship
::
++ take-sigh-httr
|= [way=wire res=httr:eyre]
2019-02-08 04:12:57 +03:00
^+ ..abet-pe
?> ?=([@ ~] way)
=/ num (slav %ud i.way)
?. (~(has in http-requests) num)
~& [who=who %ignoring-httr num=num]
2019-02-08 04:12:57 +03:00
..abet-pe
=. http-requests (~(del in http-requests) num)
(push-events [//http/0v1n.2m9vh %they num res]~)
::
:: Got error in HTTP response
::
++ take-sigh-tang
|= [way=wire tan=tang]
2019-02-08 04:12:57 +03:00
^+ ..abet-pe
?> ?=([@ ~] way)
=/ num (slav %ud i.way)
?. (~(has in http-requests) num)
~& [who=who %ignoring-httr num=num]
2019-02-08 04:12:57 +03:00
..abet-pe
=. http-requests (~(del in http-requests) num)
%- (slog tan)
2019-02-08 04:12:57 +03:00
..abet-pe
::
:: We should mirror a mount point of child to a clay desk of host.
:: For now, we just allow injecting a change to the child, so we
:: throw away ergos.
::
++ handle-ergo
|= [way=wire %ergo mount-point=@tas mod=mode:clay]
2019-02-08 04:12:57 +03:00
^+ ..abet-pe
2019-02-09 02:21:40 +03:00
~& [who=who %file-changes (lent mod)] :: (turn mod head)]
2019-02-08 04:12:57 +03:00
..abet-pe
::
2019-02-06 05:21:41 +03:00
:: Give effect to our subscribers
::
++ publish-effect
|= ovo=unix-effect
2019-02-08 04:12:57 +03:00
^+ ..abet-pe
2019-02-08 05:03:46 +03:00
=. unix-effects (~(add ja unix-effects) who ovo)
..abet-pe
--
::
++ this .
::
:: ++apex-aqua and ++abet-aqua must bookend calls from gall
::
++ apex-aqua
^+ this
=: moves ~
unix-effects ~
==
this
::
++ abet-aqua
^- (quip move _this)
=. this
2019-02-06 05:21:41 +03:00
%- emit-moves
%+ murn ~(tap by sup.hid)
|= [b=bone her=ship pax=path]
^- (unit move)
2019-02-08 05:03:46 +03:00
?. ?=([%effects @ ~] pax)
2019-02-06 05:21:41 +03:00
~
2019-02-08 05:03:46 +03:00
=/ who (slav %p i.t.pax)
=/ fx (~(get ja unix-effects) who)
?~ fx
~
`[b %diff %aqua-effects who fx]
[(flop moves) this]
::
++ emit-moves
|= ms=(list move)
=. moves (weld ms moves)
this
2019-02-02 04:00:15 +03:00
::
::
:: Run all events on all ships until all queues are empty
::
2019-02-02 00:49:14 +03:00
++ plow-all
2019-02-08 04:12:57 +03:00
|- ^+ this
2019-02-02 00:49:14 +03:00
=/ who
=/ pers ~(tap by piers)
|- ^- (unit ship)
?~ pers
~
?: &(?=(^ next-events.q.i.pers) processing-events.q.i.pers)
~& [%new-events p.i.pers]
`p.i.pers
$(pers t.pers)
~& plowing=who
?~ who
2019-02-08 04:12:57 +03:00
this
=. this abet-pe:plow:(pe u.who)
$
2019-02-02 00:49:14 +03:00
::
2019-02-06 05:21:41 +03:00
:: Subscribe to effects from a ship
::
++ peer-effects
|= pax=path
^- (quip move _this)
?. ?=([@ ~] pax)
~& [%aqua-bad-peer-effects pax]
`this
?~ (slaw %p i.pax)
~& [%aqua-bad-peer-effects-ship pax]
2019-02-08 05:03:46 +03:00
!!
2019-02-06 05:21:41 +03:00
`this
::
2019-02-02 04:00:15 +03:00
:: Load a pill and assemble arvo. Doesn't send any of the initial
:: events.
::
2019-02-02 00:49:14 +03:00
++ poke-pill
|= p=pill
^- (quip move _this)
2019-02-08 05:03:46 +03:00
=. this apex-aqua =< abet-aqua
2019-02-02 00:49:14 +03:00
=. pil p
~& lent=(met 3 (jam boot-ova.pil))
=/ res=toon :: (each * (list tank))
(mock [boot-ova.pil [2 [0 3] [0 2]]] scry)
?- -.res
%0
~& %suc
=. assembled +7.p.res
2019-02-08 04:12:57 +03:00
this
2019-02-02 00:49:14 +03:00
::
%1
~& [%vere-blocked p.res]
2019-02-08 04:12:57 +03:00
this
2019-02-02 00:49:14 +03:00
::
%2
~& %vere-fail
%- (slog p.res)
2019-02-08 04:12:57 +03:00
this
2019-02-02 00:49:14 +03:00
==
::
2019-02-06 05:21:41 +03:00
:: Handle commands from CLI
2019-02-02 04:00:15 +03:00
::
:: Should put some thought into arg structure, maybe make a mark.
::
2019-02-06 05:21:41 +03:00
:: Should convert some of these to just rewrite into ++poke-events.
::
2019-02-02 00:49:14 +03:00
++ poke-noun
|= val=*
^- (quip move _this)
2019-02-08 05:03:46 +03:00
=. this apex-aqua =< abet-aqua
2019-02-08 04:12:57 +03:00
^+ this
2019-02-02 04:00:15 +03:00
:: Could potentially factor out the three lines of turn-ships
:: boilerplate
::
2019-02-02 00:49:14 +03:00
?+ val ~|(%bad-noun-arg !!)
[%init hers=*]
2019-02-09 06:18:38 +03:00
=/ hers ((list ship) hers.val)
?~ hers
this
=^ ms this (poke-aqua-events [%init-ship i.hers]~)
(emit-moves ms)
:: %+ turn-ships ((list ship) hers.val)
:: |= [who=ship thus=_this]
:: =. this thus
:: ~& [%initting who]
:: %- push-events:apex:(pe who)
:: ^- (list unix-event)
:: :~ `unix-event`[/ %wack 0] :: eny
:: `unix-event`[/ %whom who] :: eny
:: `unix-event`[//newt/0v1n.2m9vh %barn ~]
:: `unix-event`[//behn/0v1n.2m9vh %born ~]
:: `unix-event`[//term/1 %boot %fake who]
:: `unix-event`-.userspace-ova.pil
:: `unix-event`[//http/0v1n.2m9vh %born ~]
:: `unix-event`[//http/0v1n.2m9vh %live 8.080 `8.445]
:: `unix-event`[//term/1 %belt %ctl `@c`%x]
:: ==
2019-02-02 00:49:14 +03:00
::
[%dojo hers=* command=*]
%+ turn-ships ((list ship) hers.val)
2019-02-02 04:00:15 +03:00
|= [who=ship thus=_this]
=. this thus
%- push-events:(pe who)
^- (list unix-event)
:~
[//term/1 %belt %ctl `@c`%e]
[//term/1 %belt %ctl `@c`%u]
[//term/1 %belt %txt ((list @c) (tape command.val))]
[//term/1 %belt %ret ~]
==
::
[%raw-event hers=* ovo=*]
=/ ovo ((soft unix-event) ovo.val)
?~ ovo
~& %ovo-not-an-event
2019-02-08 04:12:57 +03:00
this
%+ turn-ships ((list ship) hers.val)
|= [who=ship thus=_this]
=. this thus
(push-events:(pe who) ~[u.ovo])
::
[%file hers=* pax=*]
=/ pax (path pax.val)
?> ?=([@ @ @ *] pax)
=/ file [/text/plain (as-octs:mimes:html .^(@ %cx pax))]
%+ turn-ships ((list ship) hers.val)
|= [who=ship thus=_this]
=. this thus
%- push-events:(pe who)
[//sync/0v1n.2m9vh %into i.t.pax | [t.t.t.pax `file]~]~
2019-02-05 01:31:55 +03:00
::
[%peek hers=* p=*]
%+ turn-ships ((list ship) hers.val)
|= [who=ship thus=_this]
=. this thus
(peek:(pe who) p.val)
::
[%wish hers=* p=@t]
%+ turn-ships ((list ship) hers.val)
|= [who=ship thus=_this]
=. this thus
(wish:(pe who) p.val)
::
[%unpause-events hers=*]
%+ turn-ships ((list ship) hers.val)
|= [who=ship thus=_this]
=. this thus
start-processing-events:(pe who)
::
[%pause-events hers=*]
%+ turn-ships ((list ship) hers.val)
|= [who=ship thus=_this]
=. this thus
stop-processing-events:(pe who)
2019-02-02 00:49:14 +03:00
::
[%snap-fleet lab=@tas]
=. fleet-snaps (~(put by fleet-snaps) lab.val piers)
2019-02-08 04:12:57 +03:00
this
2019-02-02 00:49:14 +03:00
::
[%restore-fleet lab=@tas]
2019-02-12 05:46:36 +03:00
=^ ms this (poke-aqua-events [%restore-snap lab.val]~)
(emit-moves ms)
2019-02-02 00:49:14 +03:00
==
::
2019-02-08 04:12:57 +03:00
:: Apply a list of events tagged by ship
2019-02-06 05:21:41 +03:00
::
++ poke-aqua-events
|= events=(list aqua-event)
^- (quip move _this)
2019-02-08 05:03:46 +03:00
=. this apex-aqua =< abet-aqua
2019-02-06 05:21:41 +03:00
%+ turn-events events
|= [ovo=aqua-event thus=_this]
=. this thus
?- -.ovo
%init-ship
=/ prev (~(get by init-cache) who.ovo)
2019-02-12 00:42:54 +03:00
?: &(?=(^ prev) (lth who.ovo ~marzod))
~& [%loading-cached-ship who.ovo]
=. this (restore-ships ~[who.ovo] init-cache)
(pe who.ovo)
2019-02-09 06:18:38 +03:00
=. this abet-pe:sleep:(pe who.ovo)
=/ initted
=< plow
%- push-events:apex:(pe who.ovo)
^- (list unix-event)
:~ [/ %wack 0] :: eny
[/ %whom who.ovo] :: eny
[//newt/0v1n.2m9vh %barn ~]
[//behn/0v1n.2m9vh %born ~]
[//term/1 %boot %fake who.ovo]
-.userspace-ova.pil
[//http/0v1n.2m9vh %born ~]
[//http/0v1n.2m9vh %live 8.080 `8.445]
[//term/1 %belt %ctl `@c`%x]
==
=. this abet-pe:initted
=. init-cache
%+ ~(put by init-cache) who.ovo
(~(got by piers) who.ovo)
(pe who.ovo)
2019-02-09 00:34:24 +03:00
::
%pause-events
stop-processing-events:(pe who.ovo)
2019-02-12 05:46:36 +03:00
::
%snap-ships
=. fleet-snaps
%+ ~(put by fleet-snaps) lab.ovo
%- malt
%+ murn hers.ovo
|= her=ship
^- (unit (pair ship pier))
=+ per=(~(get by piers) her)
?~ per
~
`[her u.per]
(pe -.hers.ovo)
::
%restore-snap
=. this
%+ turn-ships (turn ~(tap by piers) head)
|= [who=ship thus=_this]
=. this thus
sleep:(pe who)
2019-02-12 22:26:48 +03:00
=. piers (~(uni by piers) (~(got by fleet-snaps) lab.ovo))
2019-02-12 05:46:36 +03:00
=. this
%+ turn-ships (turn ~(tap by piers) head)
|= [who=ship thus=_this]
=. this thus
restore:(pe who)
(pe ~bud) :: XX why ~bud? need an example
2019-02-06 05:21:41 +03:00
::
%event
2019-02-09 02:21:40 +03:00
~& ev=-.q.ovo.ovo
2019-02-06 05:21:41 +03:00
(push-events:(pe who.ovo) [ovo.ovo]~)
==
::
2019-02-02 04:00:15 +03:00
:: Run a callback function against a list of ships, aggregating state
:: and plowing all ships at the end.
::
:: I think we should use patterns like this more often. Because we
:: don't, here's some points to be aware.
::
:: `fun` must take `this` as a parameter, since it needs to be
:: downstream of previous state changes. You could use `state` as
:: the state variable, but it muddles the code and it's not clear
:: whether it's better. You could use the `_(pe)` core if you're
:: sure you'll never need to refer to anything outside of your pier,
:: but I don't think we can guarantee that.
::
:: The callback function must start with `=. this thus`, or else
:: you don't get the new state. Would be great if you could hot-swap
:: that context in here, but we don't know where to put it unless we
:: restrict the callbacks to always have `this` at a particular axis,
:: and that doesn't feel right
::
2019-02-06 05:21:41 +03:00
++ turn-plow
|* arg=mold
|= [hers=(list arg) fun=$-([arg _this] _(pe))]
2019-02-08 04:12:57 +03:00
|- ^+ this
2019-02-02 00:49:14 +03:00
?~ hers
2019-02-08 04:12:57 +03:00
plow-all
=. this
abet-pe:plow:(fun i.hers this)
$(hers t.hers, this this)
2019-02-02 04:00:15 +03:00
::
2019-02-06 05:21:41 +03:00
++ turn-ships (turn-plow ship)
++ turn-events (turn-plow aqua-event)
::
2019-02-02 04:00:15 +03:00
:: Send the same event to all ships
::
++ blast-event
|= ovo=unix-event
=/ pers ~(tap by piers)
|- ^+ this
?~ pers
this
2019-02-08 04:12:57 +03:00
=. this
abet-pe:(push-events:(pe p.i.pers) ~[ovo])
2019-02-02 04:00:15 +03:00
$(pers t.pers)
::
:: Restore ships
::
++ restore-ships
|= [hers=(list ship) from=(map ship pier)]
=. this
%+ turn-ships hers
|= [who=ship thus=_this]
=. this thus
sleep:(pe who)
=. piers
%- ~(gas by piers)
%+ turn hers
|= her=ship
[her (~(got by from) her)]
=. this
%+ turn-ships hers
|= [who=ship thus=_this]
=. this thus
restore:(pe who)
this
::
2019-02-02 04:00:15 +03:00
:: Received timer wake
2019-02-02 00:49:14 +03:00
::
++ wake
|= [way=wire ~]
^- (quip move _this)
2019-02-08 05:03:46 +03:00
=. this apex-aqua =< abet-aqua
?> ?=([@ *] way)
2019-02-02 00:49:14 +03:00
=/ who (,@p (slav %p i.way))
%+ turn-ships ~[who]
|= [who=ship thus=_this]
=. this thus
(take-wake:(pe who) t.way ~)
::
:: Received inbound HTTP response
::
++ sigh-httr
|= [way=wire res=httr:eyre]
^- (quip move _this)
2019-02-08 05:03:46 +03:00
=. this apex-aqua =< abet-aqua
?> ?=([@ *] way)
=/ who (,@p (slav %p i.way))
~& [%received-httr who]
%+ turn-ships ~[who]
|= [who=ship thus=_this]
=. this thus
(take-sigh-httr:(pe who) t.way res)
::
:: Received inbound HTTP response error
::
++ sigh-tang
|= [way=wire tan=tang]
^- (quip move _this)
2019-02-08 05:03:46 +03:00
=. this apex-aqua =< abet-aqua
?> ?=([@ *] way)
=/ who (,@p (slav %p i.way))
~& [%received-httr who]
%+ turn-ships ~[who]
|= [who=ship thus=_this]
=. this thus
(take-sigh-tang:(pe who) t.way tan)
2019-02-02 00:49:14 +03:00
::
2019-02-12 05:46:36 +03:00
:: Handle scry to aqua
::
++ peek-x-fleet-snap
|= pax=path
^- (unit (unit [%noun noun]))
~& [%peeking pax]
?. ?=([@ ~] pax)
~
:^ ~ ~ %noun
(~(has by fleet-snaps) i.pax)
::
2019-02-02 04:00:15 +03:00
:: Trivial scry for mock
::
2019-02-02 00:49:14 +03:00
++ scry |=([* *] ~)
::
2019-02-02 04:00:15 +03:00
:: Throw away old state if it doesn't soft to new state.
::
2019-02-02 00:49:14 +03:00
++ prep
|= old/(unit noun)
^- [(list move) _+>.$]
2019-02-12 05:46:36 +03:00
~& prep=%aqua
2019-02-02 00:49:14 +03:00
?~ old
`+>.$
=+ new=((soft state) u.old)
?~ new
`+>.$
`+>.$(+<+ u.new)
--