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)))))
47 (define (beh-proc beh)
49 (define (beh-parent beh)
55 (send-message recipient 'pong)
60 (define (make-address host port id)
63 (define (make-local-address . args)
64 (make-address sam-host
70 (define (address-id address)
72 (define (address-host address)
74 (define (address-port address)
76 (define (address->string address)
78 (make-uri #:scheme "actor"
79 #:host (address-host address)
80 #:port (address-port address)
81 #:path (list '/ (address-id address)))))
82 (define (string->address str)
83 (let ((uri (uri-reference str)))
84 (make-address (uri-host uri)
86 (cadr (uri-path uri)))))
88 (define (address-local? address)
89 (and (equal? (address-host address) sam-host)
90 (equal? (address-port address) sam-port)))
92 (define actor-table (make-hash-table))
94 (define (make-actor beh)
95 (let* ((address (make-local-address))
96 (id (address-id address)))
97 (hash-table-set! actor-table id beh)
100 (define (deliver-message address . message)
101 (let ((id (address-id address)))
102 (log-trace "DELIVERING to " id ": " message)
103 (let loop ((beh (hash-table-ref/default actor-table id #f)))
106 (match (apply (beh-proc beh) (cons address message))
107 ('done (hash-table-delete! actor-table id))
110 (log-trace "Passing to parent behaviour...")
111 (loop (beh-parent beh)))
112 ((? procedure? new-beh) (hash-table-set! actor-table id new-beh))
114 (log-msg "Warning: behaviour of actor " id " returned invalid value.")))
116 (log-msg "Warning: actor " id " crashed evaluating message " message)
117 (print-error-message o)))
118 (log-msg "Warning: DISCARDING message to unknown actor " id ": " message)))))
122 (define local-queue-mutex (make-mutex "message queue"))
123 (define message-available-mutex (make-mutex "message available"))
124 (mutex-lock! message-available-mutex #f #f)
125 (define local-queue (make-fifo))
127 (define (send-message address . message)
128 (log-trace "SENDING to " address ": " message)
129 (apply (if (address-local? address)
131 send-network-message)
132 (cons address message)))
134 (define (send-local-message address . message)
135 (mutex-lock! local-queue-mutex)
136 (fifo-push local-queue (cons address message))
137 (mutex-unlock! message-available-mutex)
138 (mutex-unlock! local-queue-mutex))
140 (define (send-network-message address . message)
141 (let ((s (udp-open-socket))
142 (packet (with-output-to-string
144 (write (cons address message))))))
147 (address-host address)
148 (address-port address))
150 (udp-close-socket s)))
152 (define (send-message-later address time . message)
156 (apply send-message (cons address message)))))
158 (define (next-local-message)
160 (mutex-lock! message-available-mutex #f #f)
161 (mutex-lock! local-queue-mutex)
162 (set! res (fifo-pop local-queue))
163 (if (not (fifo-empty? local-queue))
164 (mutex-unlock! message-available-mutex))
165 (mutex-unlock! local-queue-mutex)
168 (define (start-scheduler)
170 (apply deliver-message (next-local-message))
176 (define (start-network-listener)
179 (let ((s (udp-open-socket*)))
180 (udp-bind! s #f sam-port)
182 (let-values (((n str) (udp-recv s 65536)))
183 (match (with-input-from-string str read)
184 ((address message ...)
185 (apply send-message (cons address message)))
187 (log-msg "Warning: received badly formatted message string '" str "'"))))
192 (define reader-queue-mutex (make-mutex "reader queue"))
193 (define reader-available-mutex (make-mutex "reader available"))
194 (mutex-lock! reader-available-mutex #f #f)
195 (define reader-queue (make-fifo))
197 (define (next-reader)
199 (mutex-lock! reader-available-mutex #f #f)
200 (mutex-lock! reader-queue-mutex)
201 (set! res (fifo-pop reader-queue))
202 (if (not (fifo-empty? reader-queue))
203 (mutex-unlock! reader-available-mutex))
204 (mutex-unlock! reader-queue-mutex)
207 (define (start-console)
211 (let ((reader (next-reader)))
212 (##sys#thread-block-for-i/o! (current-thread) 0 #t)
214 (send-message reader (read-line)))
217 ;; System initialization
222 (log-msg "System actor received shutdown message.")
226 (('print strings ...) =>
227 (apply print strings)
231 (mutex-lock! reader-queue-mutex)
232 (fifo-push reader-queue reader)
233 (mutex-unlock! reader-available-mutex)
234 (mutex-unlock! reader-queue-mutex)
239 (start-network-listener)
240 (let ((system (make-actor system-beh))
244 (set! main (make-actor main-beh)))
246 (log-msg "Error starting main actor. Is main-beh defined?")
248 (send-message main system))
251 (define (print-usage)
252 (print "Simple Actor Machine v" sam-version "\n"
254 "Usage: sam -h|--help\n"
255 " sam [-n hostname] [-p port] source-file-1 [source-file-2 [...]] "))
257 (let loop ((args (cdr (argv))))
259 (((or "-h" "--help"))
261 (((or "-p" "--port") pstr rest ...)
262 (set! sam-port (string->number pstr))
264 (((or "-n" "--hostname") hstr rest ...)
267 (((or "-t" "--trace") rest ...)
268 (log-msg "Enabling trace debugging")
271 (((? file-exists? filename) rest ...)
272 (log-msg "Loading " filename)
276 (log-msg "Booting SAM\n")
279 (print "Unrecognised argument '" (car args) "'.\n")