actors: Allow actors to specify whether to handle an *init* action.
[8sync.git] / 8sync / actors.scm
index 1927465a804007d1d64fbead3e84d6bf8141a7e1..1b120ba77916e1e3f9bd08029cfa4398218a5e66 100644 (file)
@@ -1,5 +1,5 @@
 ;;; 8sync --- Asynchronous programming for Guile
-;;; Copyright (C) 2016 Christopher Allan Webber <cwebber@dustycloud.org>
+;;; Copyright © 2016, 2017 Christopher Allan Webber <cwebber@dustycloud.org>
 ;;;
 ;;; This file is part of 8sync.
 ;;;
             actor-id-hive
             actor-id-string
 
-            actor-am-i-alive?
+            actor-alive?
 
             build-actions
 
-            define-simple-actor
+            define-actor
 
             <hive>
             make-hive
             ;; There are more methods for the hive, but there's
             ;; no reason for the outside world to look at them maybe?
             hive-id
-            hive-create-actor hive-create-actor*
+            bootstrap-actor bootstrap-actor*
 
             create-actor create-actor*
             self-destruct
 
             message-auto-reply?
 
-            <- <-wait <-wait* <-reply <-reply-wait <-reply-wait*
+            <- <-* <-wait <-wait* <-reply <-reply* <-reply-wait <-reply-wait*
 
-            call-with-message msg-receive msg-val
+            call-with-message mbody-receive mbody-val
 
-            ez-run-hive
+            run-hive
             bootstrap-message
 
             serialize-message write-message
         ;;   confusing.
         (8sync (hive-process-message hive new-message)))))
 
-
-(define (<- from-actor to-id action . message-body-args)
+(define (<- to-id action . message-body-args)
   "Send a message from an actor to another actor"
-  (send-message '() from-actor to-id action
+  (send-message '() (%current-actor) to-id action
                 #f #f message-body-args))
 
-(define (<-wait* send-options from-actor to-id action . message-body-args)
-  "Like <-wait, but allows extra parameters, for example whether to
-#:accept-errors"
-  (apply wait-maybe-handle-errors
-         (send-message send-options from-actor to-id action
-                       #f #t message-body-args)
-         send-options))
+(define (<-* send-options to-id action . message-body-args)
+  "Like <-*, but allows extra parameters via send-options"
+  (define* (really-send #:key (actor (%current-actor))
+                        #:allow-other-keys)
+    (send-message send-options actor to-id action
+                  #f #f message-body-args))
+  (apply really-send send-options))
 
-(define (<-wait from-actor to-id action . message-body-args)
+(define (<-wait to-id action . message-body-args)
   "Send a message from an actor to another, but wait until we get a response"
-  (apply <-wait* '() from-actor to-id action message-body-args))
+  (wait-maybe-handle-errors
+   (send-message '() (%current-actor) to-id action
+                 #f #t message-body-args)))
+
+(define (<-wait* send-options to-id action . message-body-args)
+  "Like <-wait, but allows extra parameters, for example whether to
+#:accept-errors"
+  (define* (really-send #:key (actor (%current-actor))
+                        #:allow-other-keys)
+    (apply wait-maybe-handle-errors
+           (send-message send-options actor to-id action
+                         #f #t message-body-args)
+           send-options))
+  (apply really-send send-options))
 
 ;; TODO: Intelligently ~propagate(ish) errors on -wait functions.
 ;;   We might have `send-message-wait-brazen' to allow callers to
 ;;   not have an exception thrown and instead just have a message with
 ;;   the appropriate '*error* message returned.
 
-(define (<-reply from-actor original-message . message-body-args)
+(define (<-reply original-message . message-body-args)
   "Reply to a message"
-  (send-message '() from-actor (message-from original-message) '*reply*
-                original-message #f message-body-args))
-
-(define (<-auto-reply from-actor original-message)
+  (when (message-needs-reply? original-message)
+    (send-message '() (%current-actor) (message-from original-message) '*reply*
+                  original-message #f message-body-args)))
+
+(define (<-reply* send-options original-message . message-body-args)
+  "Like <-reply, but allows extra parameters via send-options"
+  (define* (really-send #:key (actor (%current-actor))
+                        #:allow-other-keys)
+    (send-message send-options actor
+                  (message-from original-message) '*reply*
+                  original-message #f message-body-args))
+  (when (message-needs-reply? original-message)
+    (apply really-send send-options)))
+
+(define (<-auto-reply actor original-message)
   "Auto-reply to a message.  Internal use only!"
-  (send-message '() from-actor (message-from original-message) '*auto-reply*
+  (send-message '() actor (message-from original-message) '*auto-reply*
                 original-message #f '()))
 
-(define (<-reply-wait* send-options from-actor original-message
-                       . message-body-args)
+(define (<-reply-wait original-message . message-body-args)
   "Reply to a messsage, but wait until we get a response"
-  (apply wait-maybe-handle-errors
-         (send-message send-options from-actor
-                       (message-from original-message) '*reply*
-                       original-message #t message-body-args)
-         send-options))
-
-(define (<-reply-wait from-actor original-message . message-body-args)
-  "Reply to a messsage, but wait until we get a response"
-  (apply <-reply-wait* '() from-actor original-message message-body-args))
+  (if (message-needs-reply? original-message)
+      (wait-maybe-handle-errors
+       (send-message '() (%current-actor)
+                     (message-from original-message) '*reply*
+                     original-message #t message-body-args))
+      #f))
+
+(define (<-reply-wait* send-options original-message
+                       . message-body-args)
+  "Like <-reply-wait, but allows extra parameters via send-options"
+  (define* (really-send #:key (actor (%current-actor))
+                        #:allow-other-keys)
+    (apply wait-maybe-handle-errors
+           (send-message send-options actor
+                         (message-from original-message) '*reply*
+                         original-message #t message-body-args)
+           send-options))
+  (when (message-needs-reply? original-message)
+    (apply really-send send-options)))
 
 (define* (wait-maybe-handle-errors message
                                    #:key accept-errors
@@ -283,6 +315,14 @@ raise an exception if an error."
     (call/ec find-message-handler))
   (apply method actor message (message-body message)))
 
+(define-syntax-rule (build-actions (symbol method) ...)
+  "Construct an alist of (symbol . method), where the method is wrapped
+with wrap-apply to facilitate live hacking and allow the method definition
+to come after class definition."
+  (list
+   (cons (quote symbol)
+         (wrap-apply method)) ...))
+
 (define-class <actor> ()
   ;; An address object
   (id #:init-keyword #:id
@@ -299,26 +339,24 @@ raise an exception if an error."
                    #:allocation #:each-subclass
                    #:getter actor-message-handler)
 
+  ;; valid values are:
+  ;;  - #t as in, send the init message, but don't wait (default)
+  ;;  - 'wait, as in wait on the init message
+  ;;  - #f as in don't bother to init
+  (should-init #:init-value #t
+               #:allocation #:each-subclass)
+
   ;; This is the default, "simple" way to inherit and process messages.
-  (actions #:init-value '()
+  (actions #:init-value (build-actions
+                         ;; Default init method is to do nothing.
+                         (*init* (const #f))
+                         ;; Default cleanup method is to do nothing.
+                         (*cleanup* (const #f)))
            #:allocation #:each-subclass))
 
-;;; So these are the nicer representations of addresses.
-;;; However, they don't serialize so easily with scheme read/write, so we're
-;;; using the simpler cons cell version below for now.
-
-;; (define-record-type <address>
-;;   (make-address actor-id hive-id)  ; @@: Do we want the trailing -id?
-;;   address?
-;;   (actor-id address-actor-id)
-;;   (hive-id address-hive-id))
-;;
-;; (set-record-type-printer!
-;;  <address>
-;;  (lambda (record port)
-;;    (format port "<address: ~s@~s>"
-;;            (address-actor-id record) (address-hive-id record))))
-;;
+;;; Addresses are vectors where the first part is the actor-id and
+;;; the second part is the hive-id.  This works well enough... they
+;;; look decent being pretty-printed.
 
 (define (make-address actor-id hive-id)
   (vector actor-id hive-id))
@@ -348,7 +386,7 @@ raise an exception if an error."
 (define %current-actor
   (make-parameter #f))
 
-(define (actor-am-i-alive? actor)
+(define (actor-alive? actor)
   (hive-resolve-local-actor (actor-hive actor) (actor-id actor)))
 
 
@@ -356,18 +394,13 @@ raise an exception if an error."
 ;;; Actor utilities
 ;;; ===============
 
-(define-syntax-rule (build-actions (symbol method) ...)
-  "Construct an alist of (symbol . method), where the method is wrapped
-with wrap-apply to facilitate live hacking and allow the method definition
-to come after class definition."
-  (list
-   (cons (quote symbol)
-         (wrap-apply method)) ...))
-
-(define-syntax-rule (define-simple-actor class action ...)
-  (define-class class (<actor>)
+(define-syntax-rule (define-actor class inherits
+                      (action ...)
+                      slots ...)
+  (define-class class inherits
     (actions #:init-value (build-actions action ...)
-             #:allocation #:each-subclass)))
+             #:allocation #:each-subclass)
+    slots ...))
 
 \f
 ;;; The Hive
@@ -403,12 +436,48 @@ to come after class definition."
            (build-actions
             ;; This is in the case of an ambassador failing to forward a
             ;; message... it reports it back to the hive
-            (*failed-forward* hive-handle-failed-forward))))
+            (*failed-forward* hive-handle-failed-forward)
+            ;; These are called at start and end of run-hive
+            (*init-all* hive-handle-init-all)
+            (*cleanup-all* hive-handle-cleanup-all))))
+
+(define-method (hive-handle-init-all (hive <hive>) message)
+  "Run *init* method on all actors in registry"
+  ;; We have to do this hack and run over the list
+  ;; twice, because hash-for-each would result in an unrewindable
+  ;; continuation, and to avoid the hash-map changing during the
+  ;; middle of this.
+  (define actor-ids
+    (hash-map->list (lambda (actor-id actor) actor-id)
+                    (hive-actor-registry hive)))
+  (for-each (lambda (actor-id)
+              (let* ((actor (hash-ref (hive-actor-registry hive)
+                                      actor-id)))
+                (match (slot-ref actor 'should-init)
+                  (#f #f)
+                  ('wait
+                   (<-wait actor-id '*init*))
+                  (_
+                   (<- actor-id '*init*)))))
+            actor-ids))
 
 (define-method (hive-handle-failed-forward (hive <hive>) message)
   "Handle an ambassador failing to forward a message"
   'TODO)
 
+(define-method (hive-handle-cleanup-all (hive <hive>) message)
+  "Send a message to all actors in our registry to clean themselves up."
+  ;; We have to do this hack and run over the list
+  ;; twice, because hash-for-each would result in an unrewindable
+  ;; continuation, and to avoid the hash-map changing during the
+  ;; middle of this.
+  (define actor-ids
+    (hash-map->list (lambda (actor-id actor) actor-id)
+                    (hive-actor-registry hive)))
+  (for-each (lambda (actor-id)
+              (<- actor-id '*cleanup*))
+            actor-ids))
+
 (define* (make-hive #:key hive-id)
   (let ((hive (make <hive>
                 #:id (make-address
@@ -416,6 +485,8 @@ to come after class definition."
                                  (big-random-number-string))))))
     ;; Set the hive's actor reference to itself
     (set! (actor-hive hive) hive)
+    ;; Register the actor with itself
+    (hive-register-actor! hive hive)
     hive))
 
 (define-method (hive-id (hive <hive>))
@@ -423,7 +494,7 @@ to come after class definition."
 
 (define-method (hive-gen-actor-id (hive <hive>) cookie)
   (make-address (if cookie
-                    (string-append cookie "-" (big-random-number-string))
+                    (string-append cookie ":" (big-random-number-string))
                     (big-random-number-string))
                 (hive-id hive)))
 
@@ -614,50 +685,61 @@ to come after class definition."
   (hash-set! (hive-actor-registry hive) (actor-id actor) actor))
 
 (define-method (%hive-create-actor (hive <hive>) actor-class
-                                   init id-cookie)
-  "Actual method called by hive-create-actor.
+                                   init-args id-cookie send-init?)
+  "Actual method called by bootstrap-actor / create-actor.
 
 Since this is a define-method it can't accept fancy define* arguments,
-so this gets called from the nicer hive-create-actor interface.  See
+so this gets called from the nicer bootstrap-actor interface.  See
 that method for documentation."
   (let* ((actor-id (hive-gen-actor-id hive id-cookie))
          (actor (apply make actor-class
                        #:hive hive
                        #:id actor-id
-                       init)))
+                       init-args))
+         (actor-should-init (slot-ref actor 'should-init)))
     (hive-register-actor! hive actor)
+    ;; Maybe run actor init method
+    (when (and send-init? actor-should-init)
+      (let ((send-method
+             (if (eq? actor-should-init 'wait)
+                 <-wait <-)))
+        (send-method actor-id '*init*)))
     ;; return the actor id
     actor-id))
 
-(define* (hive-create-actor hive actor-class #:rest init)
+(define* (bootstrap-actor hive actor-class #:rest init-args)
+  "Create an actor on HIVE using ACTOR-CLASS passing in INIT-ARGS args"
   (%hive-create-actor hive actor-class
-                      init #f))
+                    init-args (symbol->string (class-name actor-class))
+                    #f))
 
-(define* (hive-create-actor* hive actor-class id-cookie #:rest init)
-  "Create an actor, but also add a 'cookie' to the name for debugging"
+(define* (bootstrap-actor* hive actor-class id-cookie #:rest init-args)
+  "Create an actor, but also allow customizing a 'cookie' added to the id
+for debugging"
   (%hive-create-actor hive actor-class
-                      init id-cookie))
+                    init-args id-cookie
+                    #f))
 
 (define (call-with-message message proc)
   "Applies message body arguments into procedure, with message as first
 argument.  Similar to call-with-values in concept."
   (apply proc message (message-body message)))
 
-;; (msg-receive (<- bar baz)
+;; (mbody-receive (<- bar baz)
 ;;     (baz)
 ;;   basil)
 
-;; Emacs: (put 'msg-receive 'scheme-indent-function 2)
+;; Emacs: (put 'mbody-receive 'scheme-indent-function 2)
 
 ;; @@: Or receive-msg or receieve-message or??
-(define-syntax-rule (msg-receive arglist message body ...)
+(define-syntax-rule (mbody-receive arglist message body ...)
   "Call body with arglist (which can accept arguments like lambda*)
 applied from the message-body of message."
   (call-with-message message
                      (lambda* arglist
                        body ...)))
 
-(define (msg-val message)
+(define (mbody-val message)
   "Retrieve the first value from the message-body of message.
 Like single value return from a procedure call.  Probably the most
 common case when waiting on a reply from some action invocation."
@@ -670,26 +752,31 @@ common case when waiting on a reply from some action invocation."
 
 ;; TODO: move send-message and friends here...?
 
-(define* (create-actor from-actor actor-class #:rest init)
+(define* (create-actor from-actor actor-class #:rest init-args)
   "Create an instance of actor-class.  Return the new actor's id.
 
 This is the method actors should call directly (unless they want
 to supply an id-cookie, in which case they should use
 create-actor*)."
   (%hive-create-actor (actor-hive from-actor) actor-class
-                      init #f))
+                      init-args #f #t))
 
 
-(define* (create-actor* from-actor actor-class id-cookie #:rest init)
+(define* (create-actor* from-actor actor-class id-cookie #:rest init-args)
   "Create an instance of actor-class.  Return the new actor's id.
 
 Like create-actor, but permits supplying an id-cookie."
   (%hive-create-actor (actor-hive from-actor) actor-class
-                      init id-cookie))
+                      init-args id-cookie #t))
 
 
-(define (self-destruct actor)
-  "Remove an actor from the hive."
+(define* (self-destruct actor #:key (cleanup #t))
+  "Remove an actor from the hive.
+
+Unless #:cleanup is set to #f, this will first have the actor handle
+its '*cleanup* action handler."
+  (when cleanup
+    (<-wait (actor-id actor) '*cleanup*))
   (hash-remove! (hive-actor-registry (actor-hive actor))
                 (actor-id actor)))
 
@@ -698,16 +785,47 @@ Like create-actor, but permits supplying an id-cookie."
 ;;; 8sync bootstrap utilities
 ;;; =========================
 
-(define* (ez-run-hive hive initial-tasks)
-  "Start up an agenda and run HIVE in it with INITIAL-TASKS."
-  (let* ((queue (list->q initial-tasks))
-         (agenda (make-agenda #:pre-unwind-handler print-error-and-continue
-                              #:queue queue)))
-    (start-agenda agenda)))
+(define* (run-hive hive initial-tasks
+                   #:key (cleanup #t)
+                   (handle-signals (list SIGINT SIGTERM)))
+  "Start up an agenda and run HIVE in it with INITIAL-TASKS.
+
+Keyword arguments:
+ - #:cleanup: Whether to run *cleanup* on all actors.
+ - #:handle-sigactions: a list of signals to set up interrupt
+   handlers for, so cleanup sill still happen as expected.
+   Defaults to a list of SIGINT and SIGTERM."
+  (dynamic-wind
+    (const #f)
+    (lambda ()
+      (define (run-it escape)
+        (define (handle-signal signum)
+          (restore-signals)
+          (escape signum))
+        (for-each (lambda (signum)
+                    (sigaction signum handle-signal))
+                  handle-signals)
+        (let* ((queue (list->q
+                       (cons (bootstrap-message hive (actor-id hive) '*init-all*)
+                             initial-tasks)))
+               (agenda (make-agenda #:pre-unwind-handler print-error-and-continue
+                                    #:queue queue)))
+          (run-agenda agenda)))
+      (call/ec run-it))
+    ;; Run cleanup
+    (lambda ()
+      (when cleanup
+        (run-hive-cleanup hive)))))
+
+(define (run-hive-cleanup hive)
+  (let ((queue (list->q (list (bootstrap-message hive (actor-id hive)
+                                                 '*cleanup-all*)))))
+    (run-agenda
+     (make-agenda #:queue queue))))
 
 (define (bootstrap-message hive to-id action . message-body-args)
   (wrap
-   (apply <- hive to-id action message-body-args)))
+   (apply <-* `(#:actor ,hive) to-id action message-body-args)))
 
 
 \f