1 ;;; 8sync --- Asynchronous programming for Guile
2 ;;; Copyright © 2016, 2017 Christopher Allan Webber <cwebber@dustycloud.org>
4 ;;; This file is part of 8sync.
6 ;;; 8sync is free software: you can redistribute it and/or modify it
7 ;;; under the terms of the GNU Lesser General Public License as
8 ;;; published by the Free Software Foundation, either version 3 of the
9 ;;; License, or (at your option) any later version.
11 ;;; 8sync is distributed in the hope that it will be useful,
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU Lesser General Public License for more details.
16 ;;; You should have received a copy of the GNU Lesser General Public
17 ;;; License along with 8sync. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (8sync actors)
20 #:use-module (oop goops)
21 #:use-module (srfi srfi-9)
22 #:use-module (srfi srfi-9 gnu)
23 #:use-module (ice-9 control)
24 #:use-module (ice-9 format)
25 #:use-module (ice-9 match)
26 #:use-module (ice-9 pretty-print)
27 #:use-module (8sync agenda)
28 #:export (;; utilities... ought to go in their own module
30 big-random-number-string
31 simple-message-id-generator
39 ;;; Commenting out the <address> type for now;
40 ;;; it may be back when we have better serializers
43 address-actor-id address-hive-id
58 ;; There are more methods for the hive, but there's
59 ;; no reason for the outside world to look at them maybe?
61 bootstrap-actor bootstrap-actor*
63 create-actor create-actor*
68 message-to message-action message-from
69 message-id message-body message-in-reply-to
74 <- <-* <-wait <-wait* <-reply <-reply* <-reply-wait <-reply-wait*
76 call-with-message mbody-receive mbody-val
81 serialize-message write-message
82 serialize-message-pretty pprint-message
83 read-message read-message-from-string))
87 (make-parameter (random-state-from-platform)))
89 ;; Same size as a uuid4 I think...
90 (define random-number-size (expt 2 128))
92 (define (big-random-number)
93 (random random-number-size (%random-state)))
95 ;; Would be great to get this base64 encoded instead.
96 (define (big-random-number-string)
97 ;; @@: This is slow. Using format here is wasteful.
98 (format #f "~x" (big-random-number)))
100 ;; @@: This is slow. A mere ~275k / second on my (old) machine.
101 ;; The main cost seems to be in number->string.
102 (define (simple-message-id-generator)
103 ;; Prepending this cookie makes message ids unique per hive
104 (let ((prefix (format #f "~x:" (big-random-number)))
107 (set! counter (1+ counter))
108 (string-append prefix (number->string counter)))))
116 ;; @@: We may want to add a deferred-reply to the below, similar to
117 ;; what we had in XUDD, for actors which do their own response
118 ;; queueing.... ie, that might receive messages but need to shelve
119 ;; them to be acted upon after something else is taken care of.
121 (define-record-type <message>
122 (make-message-intern id to from action
123 body in-reply-to wants-reply
126 (id message-id) ; id of this message
127 (to message-to) ; actor id this is going to
128 (from message-from) ; actor id of sender
129 (action message-action) ; action (a symbol) to be handled
130 (body message-body) ; argument list "body" of message
131 (in-reply-to message-in-reply-to) ; message id this is in reply to, if any
132 (wants-reply message-wants-reply) ; whether caller is waiting for reply
133 (replied message-replied ; was this message replied to?
134 set-message-replied!))
137 (define* (make-message id to from action body
138 #:key in-reply-to wants-reply
140 (make-message-intern id to from action body
141 in-reply-to wants-reply replied))
143 (define (message-auto-reply? message)
144 (eq? (message-action message) '*auto-reply*))
146 (define (message-needs-reply? message)
147 "See if this message needs a reply still"
148 (and (message-wants-reply message)
149 (not (message-replied message))))
152 (define (kwarg-list-to-alist args)
153 (let loop ((remaining args)
156 (((? keyword? key) val rest ...)
158 (cons (cons (keyword->symbol key) val)
161 (_ (throw 'invalid-kwarg-list
162 "Invalid keyword argument list"
166 ;;; See: https://web.archive.org/web/20081223021934/http://mumble.net/~jar/articles/oo-moon-weinreb.html
167 ;;; (also worth seeing: http://mumble.net/~jar/articles/oo.html )
169 ;; This is the internal, generalized message sending method.
170 ;; Users shouldn't use it! Use the <-foo forms instead.
172 ;; @@: Could we get rid of some of the conditional checks through
175 (define-inlinable (send-message send-options from-actor to-id action
176 replying-to-message wants-reply?
178 (if replying-to-message
179 (set-message-replied! replying-to-message #t))
180 (let* ((hive (actor-hive from-actor))
182 (make-message (hive-gen-message-id hive) to-id
183 (actor-id from-actor) action
185 #:wants-reply wants-reply?
187 (if replying-to-message
188 (message-id replying-to-message)
191 (abort-to-prompt (hive-prompt (actor-hive from-actor))
192 from-actor new-message send-options)
193 ;; @@: It might be that eventually we pass in send-options
194 ;; here too. Since <-wait and <-reply-wait are the only ones
195 ;; that use it yet, for now it kind of just makes things
197 (8sync (hive-process-message hive new-message)))))
199 (define (<- to-id action . message-body-args)
200 "Send a message from an actor to another actor"
201 (send-message '() (%current-actor) to-id action
202 #f #f message-body-args))
204 (define (<-* send-options to-id action . message-body-args)
205 "Like <-*, but allows extra parameters via send-options"
206 (define* (really-send #:key (actor (%current-actor))
208 (send-message send-options actor to-id action
209 #f #f message-body-args))
210 (apply really-send send-options))
212 (define (<-wait to-id action . message-body-args)
213 "Send a message from an actor to another, but wait until we get a response"
214 (wait-maybe-handle-errors
215 (send-message '() (%current-actor) to-id action
216 #f #t message-body-args)))
218 (define (<-wait* send-options to-id action . message-body-args)
219 "Like <-wait, but allows extra parameters, for example whether to
221 (define* (really-send #:key (actor (%current-actor))
223 (apply wait-maybe-handle-errors
224 (send-message send-options actor to-id action
225 #f #t message-body-args)
227 (apply really-send send-options))
229 ;; TODO: Intelligently ~propagate(ish) errors on -wait functions.
230 ;; We might have `send-message-wait-brazen' to allow callers to
231 ;; not have an exception thrown and instead just have a message with
232 ;; the appropriate '*error* message returned.
234 (define (<-reply original-message . message-body-args)
236 (when (message-needs-reply? original-message)
237 (send-message '() (%current-actor) (message-from original-message) '*reply*
238 original-message #f message-body-args)))
240 (define (<-reply* send-options original-message . message-body-args)
241 "Like <-reply, but allows extra parameters via send-options"
242 (define* (really-send #:key (actor (%current-actor))
244 (send-message send-options actor
245 (message-from original-message) '*reply*
246 original-message #f message-body-args))
247 (when (message-needs-reply? original-message)
248 (apply really-send send-options)))
250 (define (<-auto-reply actor original-message)
251 "Auto-reply to a message. Internal use only!"
252 (send-message '() actor (message-from original-message) '*auto-reply*
253 original-message #f '()))
255 (define (<-reply-wait original-message . message-body-args)
256 "Reply to a messsage, but wait until we get a response"
257 (if (message-needs-reply? original-message)
258 (wait-maybe-handle-errors
259 (send-message '() (%current-actor)
260 (message-from original-message) '*reply*
261 original-message #t message-body-args))
264 (define (<-reply-wait* send-options original-message
266 "Like <-reply-wait, but allows extra parameters via send-options"
267 (define* (really-send #:key (actor (%current-actor))
269 (apply wait-maybe-handle-errors
270 (send-message send-options actor
271 (message-from original-message) '*reply*
272 original-message #t message-body-args)
274 (when (message-needs-reply? original-message)
275 (apply really-send send-options)))
277 (define* (wait-maybe-handle-errors message
280 "Before returning a message to a waiting caller, see if we need to
281 raise an exception if an error."
282 (define action (message-action message))
283 (cond ((and (eq? action '*error*)
285 (throw 'hive-unresumable-coroutine
286 "Won't resume coroutine; got an *error* as a reply"
292 ;;; Main actor implementation
293 ;;; =========================
295 (define (actor-inheritable-message-handler actor message)
296 (define action (message-action message))
297 (define (find-message-handler return)
298 (for-each (lambda (this-class)
300 (or (and (class-slot-definition this-class 'actions)
301 (class-slot-ref this-class 'actions))
303 (for-each (match-lambda
304 ((action-name . method)
305 (when (eq? action-name action)
308 (class-precedence-list (class-of actor)))
309 (throw 'action-not-found
310 "No appropriate action handler found for actor"
315 (call/ec find-message-handler))
316 (apply method actor message (message-body message)))
318 (define-syntax-rule (build-actions (symbol method) ...)
319 "Construct an alist of (symbol . method), where the method is wrapped
320 with wrap-apply to facilitate live hacking and allow the method definition
321 to come after class definition."
324 (wrap-apply method)) ...))
326 (define-class <actor> ()
328 (id #:init-keyword #:id
330 ;; The hive we're connected to.
331 ;; We need this to be able to send messages.
332 (hive #:init-keyword #:hive
333 #:accessor actor-hive)
334 ;; How we receive and process new messages
335 (message-handler #:init-value actor-inheritable-message-handler
336 ;; @@: There's no reason not to use #:class instead of
337 ;; #:each-subclass anywhere in this file, except for
338 ;; Guile bug #25211 (#:class is broken in Guile 2.2)
339 #:allocation #:each-subclass
340 #:getter actor-message-handler)
343 ;; - #t as in, send the init message, but don't wait (default)
344 ;; - 'wait, as in wait on the init message
345 ;; - #f as in don't bother to init
346 (should-init #:init-value #t
347 #:allocation #:each-subclass)
349 ;; This is the default, "simple" way to inherit and process messages.
350 (actions #:init-value (build-actions
351 ;; Default init method is to do nothing.
353 ;; Default cleanup method is to do nothing.
354 (*cleanup* (const #f)))
355 #:allocation #:each-subclass))
357 ;;; Addresses are vectors where the first part is the actor-id and
358 ;;; the second part is the hive-id. This works well enough... they
359 ;;; look decent being pretty-printed.
361 (define (make-address actor-id hive-id)
362 (vector actor-id hive-id))
364 (define (address-actor-id address)
365 (vector-ref address 0))
367 (define (address-hive-id address)
368 (vector-ref address 1))
370 (define (address->string address)
371 (string-append (address-actor-id address) "@"
372 (address-hive-id address)))
374 (define-method (actor-id-actor (actor <actor>))
375 "Get the actor id component of the actor-id"
376 (address-actor-id (actor-id actor)))
378 (define-method (actor-id-hive (actor <actor>))
379 "Get the hive id component of the actor-id"
380 (address-hive-id (actor-id actor)))
382 (define-method (actor-id-string (actor <actor>))
383 "Render the full actor id as a human-readable string"
384 (address->string (actor-id actor)))
386 (define %current-actor
389 (define (actor-alive? actor)
390 (hive-resolve-local-actor (actor-hive actor) (actor-id actor)))
397 (define-syntax-rule (define-actor class inherits
400 (define-class class inherits
401 (actions #:init-value (build-actions action ...)
402 #:allocation #:each-subclass)
408 ;;; Every actor has a hive. The hive is a kind of "meta-actor"
409 ;;; which routes all the rest of the actors in a system.
411 (define-generic hive-handle-failed-forward)
413 (define-class <hive> (<actor>)
414 (actor-registry #:init-thunk make-hash-table
415 #:getter hive-actor-registry)
416 (msg-id-generator #:init-thunk simple-message-id-generator
417 #:getter hive-msg-id-generator)
418 ;; Ambassadors are used (or will be) for inter-hive communication.
419 ;; These are special actors that know how to route messages to other hives.
420 (ambassadors #:init-thunk make-weak-key-hash-table
421 #:getter hive-ambassadors)
422 ;; Waiting coroutines
423 ;; This is a map from cons cell of message-id
424 ;; to a cons cell of (actor-id . coroutine)
425 ;; @@: Should we have a <waiting-coroutine> record type?
426 ;; @@: Should there be any way to clear out "old" coroutines?
427 (waiting-coroutines #:init-thunk make-hash-table
428 #:getter hive-waiting-coroutines)
430 ;; When actors send messages to each other they abort to this prompt
431 ;; to send the message, then carry on their way
432 (prompt #:init-thunk make-prompt-tag
433 #:getter hive-prompt)
434 (actions #:allocation #:each-subclass
437 ;; This is in the case of an ambassador failing to forward a
438 ;; message... it reports it back to the hive
439 (*failed-forward* hive-handle-failed-forward)
440 ;; These are called at start and end of run-hive
441 (*init-all* hive-handle-init-all)
442 (*cleanup-all* hive-handle-cleanup-all))))
444 (define-method (hive-handle-init-all (hive <hive>) message)
445 "Run *init* method on all actors in registry"
446 ;; We have to do this hack and run over the list
447 ;; twice, because hash-for-each would result in an unrewindable
448 ;; continuation, and to avoid the hash-map changing during the
451 (hash-map->list (lambda (actor-id actor) actor-id)
452 (hive-actor-registry hive)))
453 (for-each (lambda (actor-id)
454 (let* ((actor (hash-ref (hive-actor-registry hive)
456 (match (slot-ref actor 'should-init)
459 (<-wait actor-id '*init*))
461 (<- actor-id '*init*)))))
464 (define-method (hive-handle-failed-forward (hive <hive>) message)
465 "Handle an ambassador failing to forward a message"
468 (define-method (hive-handle-cleanup-all (hive <hive>) message)
469 "Send a message to all actors in our registry to clean themselves up."
470 ;; We have to do this hack and run over the list
471 ;; twice, because hash-for-each would result in an unrewindable
472 ;; continuation, and to avoid the hash-map changing during the
475 (hash-map->list (lambda (actor-id actor) actor-id)
476 (hive-actor-registry hive)))
477 (for-each (lambda (actor-id)
478 (<- actor-id '*cleanup*))
481 (define* (make-hive #:key hive-id)
482 (let ((hive (make <hive>
485 (big-random-number-string))))))
486 ;; Set the hive's actor reference to itself
487 (set! (actor-hive hive) hive)
488 ;; Register the actor with itself
489 (hive-register-actor! hive hive)
492 (define-method (hive-id (hive <hive>))
493 (actor-id-hive hive))
495 (define-method (hive-gen-actor-id (hive <hive>) cookie)
496 (make-address (if cookie
497 (string-append cookie ":" (big-random-number-string))
498 (big-random-number-string))
501 (define-method (hive-gen-message-id (hive <hive>))
502 "Generate a message id using HIVE's message id generator"
503 ((hive-msg-id-generator hive)))
505 (define-method (hive-resolve-local-actor (hive <hive>) actor-address)
506 (hash-ref (hive-actor-registry hive) actor-address))
508 (define-method (hive-resolve-ambassador (hive <hive>) ambassador-address)
509 (hash-ref (hive-ambassadors hive) ambassador-address))
511 (define-method (make-forward-request (hive <hive>) (ambassador <actor>) message)
512 (make-message (hive-gen-message-id hive) (actor-id ambassador)
513 ;; If we make the hive not an actor, we could either switch this
514 ;; to #f or to the original actor...?
515 ;; Maybe some more thinking should be done on what should
516 ;; happen in case of failure to forward? Handling ambassador failures
517 ;; seems like the primary motivation for the hive remaining an actor.
520 `((original . ,message))))
522 (define-method (hive-reply-with-error (hive <hive>) original-message
523 error-key error-args)
524 ;; We only supply the error-args if the original sender is on the same hive
525 (define (orig-actor-on-same-hive?)
526 (equal? (hive-id hive)
527 (address-hive-id (message-from original-message))))
528 (set-message-replied! original-message #t)
529 (let* ((new-message-body
530 (if (orig-actor-on-same-hive?)
531 `(#:original-message ,original-message
532 #:error-key ,error-key
533 #:error-args ,error-args)
534 `(#:original-message ,original-message
535 #:error-key ,error-key)))
536 (new-message (make-message (hive-gen-message-id hive)
537 (message-from original-message)
538 (actor-id hive) '*error*
540 #:in-reply-to (message-id original-message))))
541 ;; We only return a thunk, rather than run 8sync here, because if
542 ;; we ran 8sync in the middle of a catch we'd end up with an
543 ;; unresumable continuation.
544 (lambda () (hive-process-message hive new-message))))
546 (define-record-type <waiting-on-reply>
547 (make-waiting-on-reply actor-id kont send-options)
549 (actor-id waiting-on-reply-actor-id)
550 (kont waiting-on-reply-kont)
551 (send-options waiting-on-reply-send-options))
554 (define-method (hive-process-message (hive <hive>) message)
555 "Handle one message, or forward it via an ambassador"
556 (define (maybe-autoreply actor)
557 ;; Possibly autoreply
558 (if (message-needs-reply? message)
559 (<-auto-reply actor message)))
561 (define (resolve-actor-to)
562 "Get the actor the message was aimed at"
563 (let ((actor (hive-resolve-local-actor hive (message-to message))))
565 (throw 'actor-not-found
566 (format #f "Message ~a from ~a directed to nonexistant actor ~a"
568 (address->string (message-from message))
569 (address->string (message-to message)))
573 (define (call-catching-coroutine thunk)
574 (define queued-error-handling-thunk #f)
575 (define (call-catching-errors)
576 ;; TODO: maybe parameterize (or attach to hive) and use
577 ;; maybe-catch-all from agenda.scm
578 ;; @@: Why not just use with-throw-handler and let the catch
579 ;; happen at the agenda? That's what we used to do, but
580 ;; it ended up with a SIGABRT. See:
581 ;; http://lists.gnu.org/archive/html/bug-guile/2016-05/msg00003.html
584 ;; In the actor model, we don't totally crash on errors.
586 ;; If an error happens, we raise it
588 (if (message-needs-reply? message)
589 ;; If the message is waiting on a reply, let them know
590 ;; something went wrong.
591 ;; However, we have to do it outside of this catch
592 ;; routine, or we'll end up in an unrewindable continuation
594 (set! queued-error-handling-thunk
595 (hive-reply-with-error hive message key args)))
596 ;; print error message
597 (apply print-error-and-continue key args)))
598 ;; @@: This is a kludge. See above for why.
599 (if queued-error-handling-thunk
600 (8sync (queued-error-handling-thunk))))
601 (call-with-prompt (hive-prompt hive)
603 (lambda (kont actor message send-options)
604 ;; Register the coroutine
605 (hash-set! (hive-waiting-coroutines hive)
607 (make-waiting-on-reply
608 (actor-id actor) kont send-options))
609 ;; Send off the message
610 (8sync (hive-process-message hive message)))))
612 (define (process-local-message)
613 (let ((actor (resolve-actor-to)))
614 (call-catching-coroutine
616 (define message-handler (actor-message-handler actor))
617 ;; @@: Should a more general error handling happen here?
618 (parameterize ((%current-actor actor))
620 (message-handler actor message)))
621 (maybe-autoreply actor)
622 ;; Returning result allows actors to possibly make a run-request
623 ;; at the end of handling a message.
624 ;; ... We do want that, right?
627 (define (resume-waiting-coroutine)
628 (case (message-action message)
629 ;; standard reply / auto-reply
630 ((*reply* *auto-reply* *error*)
631 (call-catching-coroutine
633 (match (hash-remove! (hive-waiting-coroutines hive)
634 (message-in-reply-to message))
636 (if (not (equal? (message-to message)
637 (waiting-on-reply-actor-id waiting)))
638 (throw 'resuming-to-wrong-actor
639 "Attempted to resume a coroutine to the wrong actor!"
640 #:expected-actor-id (message-to message)
641 #:got-actor-id (waiting-on-reply-actor-id waiting)
643 (let* (;; @@: How should we resolve resuming coroutines to actors who are
645 (actor (resolve-actor-to))
646 (kont (waiting-on-reply-kont waiting))
647 (result (kont message)))
648 (maybe-autoreply actor)
650 (#f (throw 'no-waiting-coroutine
651 "message in-reply-to tries to resume nonexistent coroutine"
653 ;; Unhandled action for a reply!
655 (throw 'hive-unresumable-coroutine
656 "Won't resume coroutine, nonsense action on reply message"
657 #:action (message-action message)
658 #:message message))))
660 (define (process-remote-message)
661 ;; Find the ambassador
662 (let* ((remote-hive-id (hive-id (message-to message)))
663 (ambassador (hive-resolve-ambassador remote-hive-id))
664 (message-handler (actor-message-handler ambassador))
665 (forward-request (make-forward-request hive ambassador message)))
666 (message-handler ambassador forward-request)))
668 (let ((to (message-to message)))
669 ;; This seems to be an easy mistake to make, so check that addressing
672 (throw 'missing-addressee
673 "`to' field is missing on message"
675 (if (hive-actor-local? hive to)
676 (if (message-in-reply-to message)
677 (resume-waiting-coroutine)
678 (process-local-message))
679 (process-remote-message))))
681 (define-method (hive-actor-local? (hive <hive>) address)
682 (equal? (hive-id hive) (address-hive-id address)))
684 (define-method (hive-register-actor! (hive <hive>) (actor <actor>))
685 (hash-set! (hive-actor-registry hive) (actor-id actor) actor))
687 (define-method (%hive-create-actor (hive <hive>) actor-class
688 init-args id-cookie send-init?)
689 "Actual method called by bootstrap-actor / create-actor.
691 Since this is a define-method it can't accept fancy define* arguments,
692 so this gets called from the nicer bootstrap-actor interface. See
693 that method for documentation."
694 (let* ((actor-id (hive-gen-actor-id hive id-cookie))
695 (actor (apply make actor-class
699 (actor-should-init (slot-ref actor 'should-init)))
700 (hive-register-actor! hive actor)
701 ;; Maybe run actor init method
702 (when (and send-init? actor-should-init)
704 (if (eq? actor-should-init 'wait)
706 (send-method actor-id '*init*)))
707 ;; return the actor id
710 (define* (bootstrap-actor hive actor-class #:rest init-args)
711 "Create an actor on HIVE using ACTOR-CLASS passing in INIT-ARGS args"
712 (%hive-create-actor hive actor-class
713 init-args (symbol->string (class-name actor-class))
716 (define* (bootstrap-actor* hive actor-class id-cookie #:rest init-args)
717 "Create an actor, but also allow customizing a 'cookie' added to the id
719 (%hive-create-actor hive actor-class
723 (define (call-with-message message proc)
724 "Applies message body arguments into procedure, with message as first
725 argument. Similar to call-with-values in concept."
726 (apply proc message (message-body message)))
728 ;; (mbody-receive (<- bar baz)
732 ;; Emacs: (put 'mbody-receive 'scheme-indent-function 2)
734 ;; @@: Or receive-msg or receieve-message or??
735 (define-syntax-rule (mbody-receive arglist message body ...)
736 "Call body with arglist (which can accept arguments like lambda*)
737 applied from the message-body of message."
738 (call-with-message message
742 (define (mbody-val message)
743 "Retrieve the first value from the message-body of message.
744 Like single value return from a procedure call. Probably the most
745 common case when waiting on a reply from some action invocation."
746 (call-with-message message
747 (lambda (_ val) val)))
750 ;;; Various API methods for actors to interact with the system
751 ;;; ==========================================================
753 ;; TODO: move send-message and friends here...?
755 (define* (create-actor from-actor actor-class #:rest init-args)
756 "Create an instance of actor-class. Return the new actor's id.
758 This is the method actors should call directly (unless they want
759 to supply an id-cookie, in which case they should use
761 (%hive-create-actor (actor-hive from-actor) actor-class
765 (define* (create-actor* from-actor actor-class id-cookie #:rest init-args)
766 "Create an instance of actor-class. Return the new actor's id.
768 Like create-actor, but permits supplying an id-cookie."
769 (%hive-create-actor (actor-hive from-actor) actor-class
770 init-args id-cookie #t))
773 (define* (self-destruct actor #:key (cleanup #t))
774 "Remove an actor from the hive.
776 Unless #:cleanup is set to #f, this will first have the actor handle
777 its '*cleanup* action handler."
779 (<-wait (actor-id actor) '*cleanup*))
780 (hash-remove! (hive-actor-registry (actor-hive actor))
785 ;;; 8sync bootstrap utilities
786 ;;; =========================
788 (define* (run-hive hive initial-tasks
790 (handle-signals (list SIGINT SIGTERM)))
791 "Start up an agenda and run HIVE in it with INITIAL-TASKS.
794 - #:cleanup: Whether to run *cleanup* on all actors.
795 - #:handle-sigactions: a list of signals to set up interrupt
796 handlers for, so cleanup sill still happen as expected.
797 Defaults to a list of SIGINT and SIGTERM."
801 (define (run-it escape)
802 (define (handle-signal signum)
805 (for-each (lambda (signum)
806 (sigaction signum handle-signal))
808 (let* ((queue (list->q
809 (cons (bootstrap-message hive (actor-id hive) '*init-all*)
811 (agenda (make-agenda #:pre-unwind-handler print-error-and-continue
813 (run-agenda agenda)))
818 (run-hive-cleanup hive)))))
820 (define (run-hive-cleanup hive)
821 (let ((queue (list->q (list (bootstrap-message hive (actor-id hive)
824 (make-agenda #:queue queue))))
826 (define (bootstrap-message hive to-id action . message-body-args)
828 (apply <-* `(#:actor ,hive) to-id action message-body-args)))
832 ;;; Basic readers / writers
833 ;;; =======================
835 (define (serialize-message message)
836 "Serialize a message for read/write"
840 (message-from message)
841 (message-action message)
842 (message-body message)
843 (message-in-reply-to message)
844 (message-wants-reply message)
845 (message-replied message)))
847 (define* (write-message message #:optional (port (current-output-port)))
848 "Write out a message to a port for easy reading later.
850 Note that if a sub-value can't be easily written to something
851 Guile's `read' procedure knows how to read, this doesn't do anything
852 to improve that. You'll need a better serializer for that.."
853 (write (serialize-message message) port))
855 (define (serialize-message-pretty message)
856 "Serialize a message in a way that's easy for humans to read."
858 (id ,(message-id message))
859 (to ,(message-to message))
860 (from ,(message-from message))
861 (action ,(message-action message))
862 (body ,(message-body message))
863 (in-reply-to ,(message-in-reply-to message))
864 (wants-reply ,(message-wants-reply message))
865 (replied ,(message-replied message))))
867 (define (pprint-message message)
868 "Pretty print a message."
869 (pretty-print (serialize-message-pretty message)))
871 (define* (read-message #:optional (port (current-input-port)))
872 "Read a message serialized via serialize-message from PORT"
874 ((id to from action body in-reply-to wants-reply replied)
876 id to from action body
877 in-reply-to wants-reply replied))
879 (throw 'message-read-bad-structure
880 "Could not read message from structure"
883 (define (read-message-from-string message-str)
884 "Read message from MESSAGE-STR"
885 (with-input-from-string message-str
887 (read-message (current-input-port)))))