Update copyrights.
[8sync.git] / demos / actors / robotscanner.scm
index 9ab57657d815841068dd9593b0aeb0a696dc4ea4..7ed6dd2f23c0c5dd9eabecc7d87a5cd6222aef6c 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.
 ;;;
@@ -33,7 +33,7 @@
 ;;; reporting info back to the user.)
 ;;; =====================================================================
 
-(use-modules (8sync systems actors)
+(use-modules (8sync actors)
              (oop goops)
              (ice-9 match))
 
     (5 0)
     (2 1)))
 
-(define-simple-actor <overseer>
-  (init-world
-   (lambda (actor message)
-     ;; Porting mostly straight up from super-imperative XUDD code.
-     (define previous-room #f)
-     (define first-room #f)
-
-     ;; Set up all rooms
-     (for-each
-      (match-lambda
-        ((clean-droids infected-droids)
-         ;; Create this room
-         (define room (create-actor* actor <warehouse-room> "room"))
-         (define* (init-droid #:key infected)
-           (define droid (create-actor* actor <droid> "droid"
-                                        #:infected infected
-                                        #:room room))
-           (<-wait actor droid 'register-with-room))
-
-         ;; Link rooms.
-         ;; Couldn't this just be folded into the warehouse room init?
-         ;; I guess it stress tests more the message sending process
-         (when previous-room
-           (<- actor previous-room 'set-next-room
-               #:id room)
-           (<- actor room 'set-previous-room
-               #:id previous-room))
-
-         ;; Set up clean droids in the room
-         (for-each
-          (lambda _
-            (init-droid #:infected #f))
-          (iota clean-droids))
-
-         ;; Set up infected droids in the room
-         (for-each
-          (lambda _
-            (init-droid #:infected #t))
-          (iota clean-droids))
-
-         (set! previous-room room)
-         (if (not first-room)
-             (set! first-room room))))
-      room-structure)
-
-     ;; Add security robot
-     (let ((security-robot
-            (create-actor actor <security-robot>)))
-       (<- actor security-robot 'begin-mission
-           #:starting-room first-room
-           #:overseer (actor-id actor)))))
-
-  (transmission
-   (lambda* (actor message #:key text)
-     (display text)
-     (newline))))
+(define-actor <overseer> (<actor>)
+  ((init-world
+    (lambda (actor message)
+      ;; Porting mostly straight up from super-imperative XUDD code.
+      (define previous-room #f)
+      (define first-room #f)
+
+      ;; Set up all rooms
+      (for-each
+       (match-lambda
+         ((clean-droids infected-droids)
+          ;; Create this room
+          (define room (create-actor* actor <warehouse-room> "room"))
+          (define* (init-droid #:key infected)
+            (define droid (create-actor* actor <droid> "droid"
+                                         #:infected infected
+                                         #:room room))
+            (<-wait droid 'register-with-room))
+
+          ;; Link rooms.
+          ;; Couldn't this just be folded into the warehouse room init?
+          ;; I guess it stress tests more the message sending process
+          (when previous-room
+            (<- previous-room 'set-next-room
+                #:id room)
+            (<- room 'set-previous-room
+                #:id previous-room))
+
+          ;; Set up clean droids in the room
+          (for-each
+           (lambda _
+             (init-droid #:infected #f))
+           (iota clean-droids))
+
+          ;; Set up infected droids in the room
+          (for-each
+           (lambda _
+             (init-droid #:infected #t))
+           (iota clean-droids))
+
+          (set! previous-room room)
+          (if (not first-room)
+              (set! first-room room))))
+       room-structure)
+
+      ;; Add security robot
+      (let ((security-robot
+             (create-actor actor <security-robot>)))
+        (<- security-robot 'begin-mission
+            #:starting-room first-room
+            #:overseer (actor-id actor)))))
+
+   (transmission
+    (lambda* (actor message #:key text)
+      (display text)
+      (newline)))))
 
 
 ;;; A room full of robots.
     (get-next-room
      (lambda (actor message)
        "Return a reference to the link following this"
-       (<-reply actor message (slot-ref actor 'next-room))))
+       (<-reply message (slot-ref actor 'next-room))))
 
     (get-previous-room
      (lambda (actor message)
        "Return a reference to the link preceding this"
-       (<-reply actor message (slot-ref actor 'previous-room))))
+       (<-reply message (slot-ref actor 'previous-room))))
 
     (list-droids
      (lambda (actor message)
        "Return a list of all the droid ids we know of in this room"
-       (<-reply actor message
+       (<-reply message
                 #:droid-ids (slot-ref actor 'droids))))
 
     (register-droid
      (lambda (actor message)
        "Register ourselves as being in a room"
        (let ((room-id (slot-ref actor 'room)))
-         (<-wait actor room-id
-                 'register-droid
+         (<-wait room-id 'register-droid
                  #:droid-id (actor-id actor))
          (format #t "Droid ~a registered with room ~a\n"
                  (actor-id-actor actor)
     (infection-expose
      (lambda (actor message)
        "Leak whether or not we're infected to a security droid"
-       (<-reply actor message (slot-ref actor 'infected))))
+       (<-reply message (slot-ref actor 'infected))))
 
     (get-shot
      (lambda (actor message)
               (alive (> new-hp 0)))
          ;; Set our health to the new value
          (slot-set! actor 'hp new-hp)
-         (<-reply actor message
+         (<-reply message
                   #:hp-left new-hp
                   #:damage-taken damage
                   #:alive alive)
 
 
 ;;; Security robot... designed to seek out and destroy infected droids.
-(define-simple-actor <security-robot>
-  (begin-mission security-robot-begin-mission))
+(define-actor <security-robot> (<actor>)
+  ((begin-mission security-robot-begin-mission)))
 
 (define* (security-robot-begin-mission actor message
                                        #:key starting-room overseer)
   ;; Continue this whil there's still another room to investigate.
   (define response)
   (while room
-    (<- actor overseer 'transmission
+    (<- overseer 'transmission
         #:text (format #f "Entering room ~a..."
                        (address-actor-id room)))
 
     ;; Find all droids in this room and exterminate the infected ones.
-    (msg-receive (_ #:key list-droids droid-ids #:allow-other-keys)
-        (<-wait actor room 'list-droids)
+    (mbody-receive (_ #:key list-droids droid-ids #:allow-other-keys)
+        (<-wait room 'list-droids)
       (for-each
        (lambda (droid-id)
          (cond
           ;; Looks like it's infected
-          ((msg-val (<-wait actor droid-id 'infection-expose))
+          ((mbody-val (<-wait droid-id 'infection-expose))
            ;; Inform that it's infected
-           (<- actor overseer 'transmission
+           (<- overseer 'transmission
                #:text (format #f "~a found to be infected... taking out"
                               (address-actor-id droid-id)))
 
            ;; Keep firing till it's dead.
            (let ((still-alive #t))
              (while still-alive
-               (msg-receive (response #:key alive #:allow-other-keys)
-                   (<-wait actor droid-id 'get-shot)
-                 (<- actor overseer 'transmission
+               (mbody-receive (response #:key alive #:allow-other-keys)
+                   (<-wait droid-id 'get-shot)
+                 (<- overseer 'transmission
                      #:text (droid-status-format response))
                  (set! still-alive alive)))))
 
           ;; Not infected... inform and go to the next one
           (else
-           (<- actor overseer 'transmission
+           (<- overseer 'transmission
                #:text
                (format #f "~a is clean... moving on."
                        (address-actor-id droid-id))))))
        droid-ids))
 
     ;; Switch to next room, if there is one.
-    (set! room (msg-val (<-wait actor room 'get-next-room))))
+    (set! room (mbody-val (<-wait room 'get-next-room))))
 
   ;; Good job everyone!  Shut down the operation.
-  (<- actor overseer 'transmission
+  (<- overseer 'transmission
       #:text "Mission accomplished."))
 
 (define (main . args)
   (define hive (make-hive))
-  (define overseer (hive-create-actor hive <overseer>))
+  (define overseer (bootstrap-actor hive <overseer>))
   (define initial-messages
     (list (bootstrap-message hive overseer 'init-world)))
-  (ez-run-hive hive initial-messages))
+  (run-hive hive initial-messages))