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)))))
46 ;; (See also macros defined in sam-macros.scm.)
48 (define (beh-proc beh)
50 (define (beh-parent beh)
56 (send-message recipient 'pong)
61 (define (make-address host port id)
64 (define (make-local-address . args)
65 (make-address sam-host
71 (define (address-id address)
73 (define (address-host address)
75 (define (address-port address)
77 (define (address->string address)
79 (make-uri #:scheme "actor"
80 #:host (address-host address)
81 #:port (address-port address)
82 #:path (list '/ (address-id address)))))
83 (define (string->address str)
84 (let ((uri (uri-reference str)))
85 (make-address (uri-host uri)
87 (cadr (uri-path uri)))))
89 (define (address-local? address)
90 (and (equal? (address-host address) sam-host)
91 (equal? (address-port address) sam-port)))
93 (define actor-table (make-hash-table))
95 (define (make-actor beh)
96 (let* ((address (make-local-address))
97 (id (address-id address)))
98 (hash-table-set! actor-table id beh)
101 (define (deliver-message address . message)
102 (let ((id (address-id address)))
103 (log-trace "DELIVERING to " id ": " message)
104 (let loop ((beh (hash-table-ref/default actor-table id #f)))
107 (match (apply (beh-proc beh) (cons address message))
108 ('done (hash-table-delete! actor-table id))
111 (log-trace "Passing to parent behaviour...")
112 (loop (beh-parent beh)))
113 ((? procedure? new-beh) (hash-table-set! actor-table id new-beh))
115 (log-msg "Warning: behaviour of actor " id " returned invalid value.")))
117 (log-msg "Warning: actor " id " crashed evaluating message " message)
118 (print-error-message o)))
119 (log-msg "Warning: DISCARDING message to unknown actor " id ": " message)))))
123 (define local-queue-mutex (make-mutex "message queue"))
124 (define message-available-mutex (make-mutex "message available"))
125 (mutex-lock! message-available-mutex #f #f)
126 (define local-queue (make-fifo))
128 (define (send-message address . message)
129 (log-trace "SENDING to " address ": " message)
130 (apply (if (address-local? address)
132 send-network-message)
133 (cons address message)))
135 (define (send-local-message address . message)
136 (mutex-lock! local-queue-mutex)
137 (fifo-push local-queue (cons address message))
138 (mutex-unlock! message-available-mutex)
139 (mutex-unlock! local-queue-mutex))
141 (define (send-network-message address . message)
142 (let ((s (udp-open-socket))
143 (packet (with-output-to-string
145 (write (cons address message))))))
148 (address-host address)
149 (address-port address))
151 (udp-close-socket s)))
153 (define (send-message-later address time . message)
157 (apply send-message (cons address message)))))
159 (define (next-local-message)
161 (mutex-lock! message-available-mutex #f #f)
162 (mutex-lock! local-queue-mutex)
163 (set! res (fifo-pop local-queue))
164 (if (not (fifo-empty? local-queue))
165 (mutex-unlock! message-available-mutex))
166 (mutex-unlock! local-queue-mutex)
169 (define (start-scheduler)
171 (apply deliver-message (next-local-message))
177 (define (start-network-listener)
180 (let ((s (udp-open-socket*)))
181 (udp-bind! s #f sam-port)
183 (let-values (((n str) (udp-recv s 65536)))
184 (match (with-input-from-string str read)
185 ((address message ...)
186 (apply send-message (cons address message)))
188 (log-msg "Warning: received badly formatted message string '" str "'"))))
193 (define reader-queue-mutex (make-mutex "reader queue"))
194 (define reader-available-mutex (make-mutex "reader available"))
195 (mutex-lock! reader-available-mutex #f #f)
196 (define reader-queue (make-fifo))
198 (define (next-reader)
200 (mutex-lock! reader-available-mutex #f #f)
201 (mutex-lock! reader-queue-mutex)
202 (set! res (fifo-pop reader-queue))
203 (if (not (fifo-empty? reader-queue))
204 (mutex-unlock! reader-available-mutex))
205 (mutex-unlock! reader-queue-mutex)
208 (define (start-console)
212 (let ((reader (next-reader)))
213 (##sys#thread-block-for-i/o! (current-thread) 0 #t)
215 (send-message reader (read-line)))
218 ;; System initialization
223 (log-msg "System actor received shutdown message.")
227 (('print strings ...) =>
228 (apply print strings)
232 (mutex-lock! reader-queue-mutex)
233 (fifo-push reader-queue reader)
234 (mutex-unlock! reader-available-mutex)
235 (mutex-unlock! reader-queue-mutex)
240 (start-network-listener)
241 (let ((system (make-actor system-beh))
245 (set! main (make-actor main-beh)))
247 (log-msg "Error starting main actor. Is main-beh defined?")
249 (send-message main system))
252 (define (print-usage)
253 (print "Simple Actor Machine v" sam-version "\n"
255 "Usage: sam -h|--help\n"
256 " sam [-n hostname] [-p port] source-file-1 [source-file-2 [...]] "))
258 (let loop ((args (cdr (argv))))
260 (((or "-h" "--help"))
262 (((or "-p" "--port") pstr rest ...)
263 (set! sam-port (string->number pstr))
265 (((or "-n" "--hostname") hstr rest ...)
268 (((or "-t" "--trace") rest ...)
269 (log-msg "Enabling trace debugging")
272 (((? file-exists? filename) rest ...)
273 (log-msg "Loading " filename)
277 (log-msg "Booting SAM\n")
280 (print "Unrecognised argument '" (car args) "'.\n")