actors: Automatically add cookie by default in hive-create-actor.
[8sync.git] / 8sync / actors.scm
index 045a43a1ddc97dc9d9fb4f27767b4ac8edecfbd9..9a0bb30f18488019cef1ebba7b64fb5ea4d098e9 100644 (file)
@@ -309,8 +309,8 @@ to come after class definition."
 
   ;; This is the default, "simple" way to inherit and process messages.
   (actions #:init-value (build-actions
-                         ;; Default cleanup method is to do nothing.
-                         (*cleanup* (const #f)))
+                         ;; Default clean-up method is to do nothing.
+                         (*clean-up* (const #f)))
            #:allocation #:each-subclass))
 
 ;;; So these are the nicer representations of addresses.
@@ -406,13 +406,13 @@ to come after class definition."
             ;; 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)
-            (*cleanup-all* hive-handle-cleanup-all))))
+            (*clean-up-all* hive-handle-clean-up-all))))
 
 (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)
+(define-method (hive-handle-clean-up-all (hive <hive>) message)
   "Send a message to all actors in our registry to clean themselves up."
   ;; Unfortunately we have to do this hack and run over the list
   ;; twice, because hash-for-each would result in an unrewindable
@@ -421,7 +421,7 @@ to come after class definition."
     (hash-map->list (lambda (actor-id actor) actor-id)
                     (hive-actor-registry hive)))
   (for-each (lambda (actor-id)
-              (<- hive actor-id '*cleanup*))
+              (<- hive actor-id '*clean-up*))
             actor-ids))
 
 (define* (make-hive #:key hive-id)
@@ -440,7 +440,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)))
 
@@ -647,11 +647,13 @@ that method for documentation."
     actor-id))
 
 (define* (hive-create-actor hive actor-class #:rest init)
+  "Create an actor on HIVE using ACTOR-CLASS passing in INIT args"
   (%hive-create-actor hive actor-class
-                      init #f))
+                      init (symbol->string (class-name actor-class))))
 
 (define* (hive-create-actor* hive actor-class id-cookie #:rest init)
-  "Create an actor, but also add a 'cookie' to the name for debugging"
+  "Create an actor, but also allow customizing a 'cookie' added to the id
+for debugging"
   (%hive-create-actor hive actor-class
                       init id-cookie))
 
@@ -705,8 +707,13 @@ Like create-actor, but permits supplying an id-cookie."
                       init id-cookie))
 
 
-(define (self-destruct actor)
-  "Remove an actor from the hive."
+(define* (self-destruct actor #:key (clean-up #t))
+  "Remove an actor from the hive.
+
+Unless #:clean-up is set to #f, this will first have the actor handle
+its '*clean-up* action handler."
+  (when clean-up
+    (<-wait actor (actor-id actor) '*clean-up*))
   (hash-remove! (hive-actor-registry (actor-hive actor))
                 (actor-id actor)))
 
@@ -715,7 +722,8 @@ Like create-actor, but permits supplying an id-cookie."
 ;;; 8sync bootstrap utilities
 ;;; =========================
 
-(define* (run-hive hive initial-tasks)
+(define* (run-hive hive initial-tasks
+                   #:key (clean-up #t))
   "Start up an agenda and run HIVE in it with INITIAL-TASKS."
   (dynamic-wind
     (const #f)
@@ -724,13 +732,14 @@ Like create-actor, but permits supplying an id-cookie."
              (agenda (make-agenda #:pre-unwind-handler print-error-and-continue
                                   #:queue queue)))
         (start-agenda agenda)))
-    ;; Run cleanup
+    ;; Run clean-up
     (lambda ()
-      (run-hive-cleanup hive))))
+      (when clean-up
+        (run-hive-clean-up hive)))))
 
-(define (run-hive-cleanup hive)
+(define (run-hive-clean-up hive)
   (let ((queue (list->q (list (bootstrap-message hive (actor-id hive)
-                                                 '*cleanup-all*)))))
+                                                 '*clean-up-all*)))))
     (start-agenda
      (make-agenda #:queue queue))))