;;; 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.
;;;
(use-modules (8sync actors)
(oop goops)
- (ice-9 match))
+ (ice-9 match)
+ (fibers conditions))
(set! *random-state* (random-state-from-platform))
(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* <warehouse-room> "room"))
+ (define* (init-droid #:key infected)
+ (define droid (create-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 <security-robot>)))
+ (<- security-robot 'begin-mission
+ #:starting-room first-room
+ #:overseer (actor-id actor)))))
+
+ (transmission
+ (lambda* (actor message #:key text)
+ (display text)
+ (newline)))
+ (done!
+ (lambda* (actor message)
+ (signal-condition! (.done? actor)))))
+ (done? #:init-keyword #:done?
+ #:accessor .done?))
;;; A room full of robots.
(actions
#:allocation #:each-subclass
- #:init-value
+ #:init-thunk
(build-actions
(set-next-room
(lambda* (actor message #:key id)
(get-next-room
(lambda (actor message)
"Return a reference to the link following this"
- (<-reply actor message (slot-ref actor 'next-room))))
+ (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))))
+ (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
- #:droid-ids (slot-ref actor 'droids))))
+ (slot-ref actor 'droids)))
(register-droid
(lambda* (actor message #:key droid-id)
(actions
#:allocation #:each-subclass
- #:init-value
+ #:init-thunk
(build-actions
(register-with-room
(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))))
+ (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
- #:hp-left new-hp
- #:damage-taken damage
- #:alive alive)
(when (not alive)
(format #t "~a: *Kaboom!*\n" (actor-id-actor actor))
- (self-destruct actor))))))))
+ (self-destruct actor))
+ (values #:hp-left new-hp
+ #:damage-taken damage
+ #:alive alive)))))))
-(define (droid-status-format shot-response)
- (call-with-message
- shot-response
- (lambda* (_ #:key alive damage-taken hp-left)
- (if alive
- (format #f "Droid ~a shot; taken ~a damage. Still alive... ~a hp left."
- (address-actor-id (message-from shot-response))
- damage-taken hp-left)
- (format #f "Droid ~a shot; taken ~a damage. Terminated."
- (address-actor-id (message-from shot-response))
- damage-taken)))))
+(define* (droid-status-format droid-id alive damage-taken hp-left)
+ (if alive
+ (format #f "Droid ~a shot; taken ~a damage. Still alive... ~a hp left."
+ (address-actor-id droid-id)
+ damage-taken hp-left)
+ (format #f "Droid ~a shot; taken ~a damage. Terminated."
+ (address-actor-id droid-id)
+ damage-taken)))
;;; 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)
+ (let ((droid-ids (<-wait room 'list-droids)))
(for-each
(lambda (droid-id)
(cond
;; Looks like it's infected
- ((msg-val (<-wait actor droid-id 'infection-expose))
+ ((<-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
- #:text (droid-status-format response))
- (set! still-alive alive)))))
+ (call-with-values
+ (lambda () (<-wait droid-id 'get-shot))
+ (lambda* (#:key hp-left damage-taken alive)
+ (<- overseer 'transmission
+ #:text (droid-status-format droid-id alive damage-taken hp-left))
+ (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 (<-wait room 'get-next-room)))
;; Good job everyone! Shut down the operation.
- (<- actor overseer 'transmission
- #:text "Mission accomplished."))
+ (<-wait overseer 'transmission
+ #:text "Mission accomplished.")
+ (<- overseer 'done!))
(define (main . args)
- (define hive (make-hive))
- (define overseer (hive-create-actor hive <overseer>))
- (define initial-messages
- (list (bootstrap-message hive overseer 'init-world)))
- (ez-run-hive hive initial-messages))
+ (run-hive
+ (lambda (hive)
+ (define done? (make-condition))
+ (define overseer (create-actor <overseer> #:done? done?))
+ (<- overseer 'init-world)
+ (wait done?))))