1 ;; Simple Actor Machine
3 ;; A virtual machine which houses a population of actors which can
4 ;; communicate using messages with actors on the same host or other
5 ;; hosts via the network.
12 (chicken process-context)
28 (define sam-host "localhost")
29 (define sam-port 8000)
31 (define sam-version "0.1")
35 (define (log-msg . args)
36 (with-output-to-port (current-error-port)
38 (apply print (cons "## " args)))))
40 (define (log-trace . args)
41 (with-output-to-port (current-error-port)
43 (if trace (apply log-msg args)))))
45 (define (->stringrep arg)
46 (with-output-to-string
51 ;; (See also macros defined in sam-macros.scm.)
53 (define (beh-proc beh)
55 (define (beh-parent beh)
61 (send-message recipient 'pong))))
65 (define (make-address host port id)
68 (define (make-local-address . args)
69 (make-address sam-host
75 (define (address-id address)
77 (define (address-host address)
79 (define (address-port address)
81 (define (address->string address)
83 (make-uri #:scheme "actor"
84 #:host (address-host address)
85 #:port (address-port address)
86 #:path (list '/ (address-id address)))))
87 (define (string->address str)
88 (let ((uri (uri-reference str)))
89 (make-address (uri-host uri)
91 (cadr (uri-path uri)))))
93 (define (address-local? address)
94 (and (equal? (address-host address) sam-host)
95 (equal? (address-port address) sam-port)))
97 (define actor-table (make-hash-table))
99 (define (make-actor beh)
100 (let* ((address (make-local-address))
101 (id (address-id address)))
102 (hash-table-set! actor-table id beh)
105 (define (deliver-message address . message)
106 (let ((id (address-id address)))
107 (log-trace "DELIVERING to " id ": " (->stringrep message))
108 (let loop ((beh (hash-table-ref/default actor-table id #f)))
111 (match (apply (beh-proc beh) (cons address message))
112 ('done (hash-table-delete! actor-table id))
114 (log-trace "Passing to parent behaviour...")
115 (loop (beh-parent beh)))
116 ((? procedure? new-beh) (hash-table-set! actor-table id new-beh))
118 'do-nothing)) ; sleep is now the default
120 (log-msg "Warning: actor " id " crashed evaluating message " (->stringrep message))
121 (print-error-message o)))
122 (log-msg "Warning: DISCARDING message to unknown actor " id ": " (->stringrep message))))))
126 (define local-queue-mutex (make-mutex "message queue"))
127 (define message-available-mutex (make-mutex "message available"))
128 (mutex-lock! message-available-mutex #f #f)
129 (define local-queue (make-fifo))
131 (define (send-message address . message)
132 (log-trace "SENDING to " address ": " (->stringrep message))
133 (apply (if (address-local? address)
135 send-network-message)
136 (cons address message)))
138 (define (send-local-message address . message)
139 (mutex-lock! local-queue-mutex)
140 (fifo-push local-queue (cons address message))
141 (mutex-unlock! message-available-mutex)
142 (mutex-unlock! local-queue-mutex))
144 (define (send-network-message address . message)
145 (let ((s (udp-open-socket))
146 (packet (with-output-to-string
148 (write (cons address message))))))
151 (address-host address)
152 (address-port address))
154 (udp-close-socket s)))
156 (define (send-message-later address time . message)
160 (apply send-message (cons address message)))))
162 (define (next-local-message)
164 (mutex-lock! message-available-mutex #f #f)
165 (mutex-lock! local-queue-mutex)
166 (set! res (fifo-pop local-queue))
167 (if (not (fifo-empty? local-queue))
168 (mutex-unlock! message-available-mutex))
169 (mutex-unlock! local-queue-mutex)
172 (define (start-scheduler)
174 (apply deliver-message (next-local-message))
180 (define (start-network-listener)
183 (let ((s (udp-open-socket*)))
184 (udp-bind! s #f sam-port)
186 (let-values (((n str) (udp-recv s 65536)))
187 (match (with-input-from-string str read)
188 ((address message ...)
189 (apply send-message (cons address message)))
191 (log-msg "Warning: received badly formatted message string '" str "'"))))
196 (define reader-queue-mutex (make-mutex "reader queue"))
197 (define reader-available-mutex (make-mutex "reader available"))
198 (mutex-lock! reader-available-mutex #f #f)
199 (define reader-queue (make-fifo))
201 (define (next-reader)
203 (mutex-lock! reader-available-mutex #f #f)
204 (mutex-lock! reader-queue-mutex)
205 (set! res (fifo-pop reader-queue))
206 (if (not (fifo-empty? reader-queue))
207 (mutex-unlock! reader-available-mutex))
208 (mutex-unlock! reader-queue-mutex)
211 (define (start-console)
215 (let ((reader (next-reader)))
216 (##sys#thread-block-for-i/o! (current-thread) 0 #t)
218 (send-message reader (read-line)))
221 ;; System initialization
223 (define-beh system-beh
226 (log-msg "System actor received shutdown message.")
230 (('print strings ...) =>
231 (apply print strings))
234 (mutex-lock! reader-queue-mutex)
235 (fifo-push reader-queue reader)
236 (mutex-unlock! reader-available-mutex)
237 (mutex-unlock! reader-queue-mutex)))
241 (start-network-listener)
242 (let ((system (make-actor system-beh))
246 (set! main (make-actor main-beh)))
248 (log-msg "Error starting main actor. Is main-beh defined?")
250 (send-message main system))
253 (define (print-usage)
254 (print "Simple Actor Machine v" sam-version "\n"
256 "Usage: sam -h|--help\n"
257 " sam [-n hostname] [-p port] source-file-1 [source-file-2 [...]] "))
259 (let loop ((args (cdr (argv))))
261 (((or "-h" "--help"))
263 (((or "-p" "--port") pstr rest ...)
264 (set! sam-port (string->number pstr))
266 (((or "-n" "--hostname") hstr rest ...)
269 (((or "-t" "--trace") rest ...)
270 (log-msg "Enabling trace debugging")
273 (((? file-exists? filename) rest ...)
274 (log-msg "Loading " filename)
278 (log-msg "Booting SAM\n")
281 (print "Unrecognised argument '" (car args) "'.\n")