1 ;;; 8sync --- Asynchronous programming for Guile
2 ;;; Copyright (C) 2016 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 ;;; =====================================================================
20 ;;; Robot Scanner test demo (from XUDD, originally).
22 ;;; Here's the premise. There's a warehouse full of droids, some
23 ;;; infected, and some not. The SecurityRobot is being sent in to clean
24 ;;; up the mess. It's capable of sending a message that infected droids
25 ;;; are susceptible to responding in a predictable way. Once it has
26 ;;; identified that a droid is infected, it shoots it full of holes till
27 ;;; the droid is terminated. The SecurityRobot goes from room to room
28 ;;; till things are cleared out.
30 ;;; Overseeing the operation is the "overseer". The security robot keeps
31 ;;; the overseer up to date on its progress as it goes. (For this demo,
32 ;;; the overseer is also responsible for initializing the world and
33 ;;; reporting info back to the user.)
34 ;;; =====================================================================
36 (use-modules (8sync systems actors)
40 (set! *random-state* (random-state-from-platform))
42 (define room-structure
43 ;; A list of (clean-droids infected-droids)
50 (define-simple-actor <overseer>
52 (lambda (actor message)
53 ;; Porting mostly straight up from super-imperative XUDD code.
54 (define previous-room #f)
55 (define first-room #f)
60 ((clean-droids infected-droids)
62 (define room (create-actor* actor <warehouse-room> "room"))
63 (define* (init-droid #:key infected)
64 (define droid (create-actor* actor <droid> "droid"
67 (<-wait actor droid 'register-with-room))
70 ;; Couldn't this just be folded into the warehouse room init?
71 ;; I guess it stress tests more the message sending process
73 (<- actor previous-room 'set-next-room
75 (<- actor room 'set-previous-room
78 ;; Set up clean droids in the room
81 (init-droid #:infected #f))
84 ;; Set up infected droids in the room
87 (init-droid #:infected #t))
90 (set! previous-room room)
92 (set! first-room room))))
97 (create-actor actor <security-robot>)))
98 (<- actor security-robot 'begin-mission
99 #:starting-room first-room
100 #:overseer (actor-id actor)))))
103 (lambda (actor message)
104 (display (message-ref message 'message))
108 ;;; A room full of robots.
109 (define-class <warehouse-room> (<actor>)
110 (droids #:init-value '())
111 (next-room #:init-value #f)
112 (previous-room #:init-value #f)
116 (make-action-dispatch
117 ((set-next-room actor message)
118 "Set the room following this"
119 (slot-set! actor 'next-room
120 (message-ref message 'id)))
122 ((set-previous-room actor message)
123 "Set the room previous to this"
124 (slot-set! actor 'previous-room
125 (message-ref message 'id)))
127 ((get-next-room actor message)
128 "Return a reference to the link following this"
129 (<-reply actor message
130 #:id (slot-ref actor 'next-room)))
132 ((get-previous-room actor message)
133 "Return a reference to the link preceding this"
134 (<-reply actor message
135 #:id (slot-ref actor 'previous-room)))
137 ((list-droids actor message)
138 "Return a list of all the droid ids we know of in this room"
139 (<-reply actor message
140 #:droid-ids (slot-ref actor 'droids)))
142 ((register-droid actor message)
143 "Register a droid as being in this room"
144 (slot-set! actor 'droids
145 (cons (message-ref message 'droid-id)
146 (slot-ref actor 'droids)))))))
149 ;;; A droid that may or may not be infected!
150 ;;; What will happen? Stay tuned!
151 (define-class <droid> (<actor>)
152 (infected #:init-keyword #:infected)
153 (room #:init-keyword #:room)
158 (make-action-dispatch
159 ((register-with-room actor message)
160 "Register ourselves as being in a room"
161 (let ((room-id (slot-ref actor 'room)))
162 (<-wait actor room-id
164 #:droid-id (actor-id actor))
165 (format #t "Droid ~a registered with room ~a\n"
166 (actor-id-actor actor)
167 (address-actor-id room-id))))
169 ((infection-expose actor message)
170 "Leak whether or not we're infected to a security droid"
171 (<-reply actor message
174 ((get-shot actor message)
175 "Get shot by bullets"
176 (let* ((damage (random 60))
177 (new-hp (- (slot-ref actor 'hp) damage))
178 (alive (> new-hp 0)))
179 ;; Set our health to the new value
180 (slot-set! actor 'hp new-hp)
181 (<-reply actor message
183 #:damage-taken damage
186 (format #t "~a: *Kaboom!*\n" (actor-id-actor actor))
187 (self-destruct actor)))))))
190 (define (droid-status-format shot-response)
191 (if (message-ref shot-response 'alive)
192 (format #f "Droid ~a shot; taken ~a damage. Still alive... ~a hp left."
193 (address-actor-id (message-from shot-response))
194 (message-ref shot-response 'damage-taken)
195 (message-ref shot-response 'hp-left))
196 (format #f "Droid ~a shot; taken ~a damage. Terminated."
197 (address-actor-id (message-from shot-response))
198 (message-ref shot-response 'damage-taken))))
201 ;;; Security robot... designed to seek out and destroy infected droids.
202 (define-simple-actor <security-robot>
203 ((begin-mission actor message)
204 ;; used to track the current room / if any rooms are remaining
205 (define room (message-ref message 'starting-room))
206 (define overseer (message-ref message 'overseer))
208 ;; Walk through all rooms, clearing out infected droids
209 ;; Continue this whil there's still another room to investigate.
214 #:message (format #f "Entering room ~a..."
215 (address-actor-id room)))
217 ;; Find all droids in this room and exterminate the infected ones.
218 (set! response (<-wait actor room 'list-droids))
222 ;; Looks like it's infected
224 (<-wait actor droid-id
227 ;; Inform that it's infected
231 (format #f "~a found to be infected... taking out"
232 (address-actor-id droid-id)))
234 ;; Keep firing till it's dead.
235 (let ((still-alive #t))
238 (<-wait actor droid-id 'get-shot)))
239 (<- actor overseer 'transmission
240 #:message (droid-status-format response))
241 (set! still-alive (message-ref response 'alive))))))
243 ;; Not infected... inform and go to the next one
245 (<- actor overseer 'transmission
247 (format #f "~a is clean... moving on."
248 (address-actor-id droid-id))))))
249 (message-ref response 'droid-ids))
251 ;; Switch to next room, if there is one.
252 (set! room (message-ref
253 (<-wait actor room 'get-next-room)
256 ;; Good job everyone! Shut down the operation.
257 (<- actor overseer 'transmission
258 #:message "Mission accomplished.")))
260 (define (main . args)
261 (define hive (make-hive))
262 (define overseer (hive-create-actor hive <overseer>))
263 (define initial-messages
264 (list (bootstrap-message hive overseer 'init-world)))
265 (ez-run-hive hive initial-messages))