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 machine or other
5 ;; machines via the network.
12 (chicken process-context)
27 (define sam-host "localhost")
28 (define sam-port 8000)
30 (define sam-version "0.1")
35 (define (make-address host port id)
38 (define (make-local-address . args)
39 (make-address sam-host
45 (define (address-id address)
47 (define (address-host address)
49 (define (address-port address)
51 (define (address->string address)
53 (make-uri #:scheme "actor"
54 #:host (address-host address)
55 #:port (address-port address)
56 #:path (list '/ (address-id address)))))
57 (define (string->address str)
58 (let ((uri (uri-reference str)))
59 (make-address (uri-host uri)
61 (cadr (uri-path uri)))))
63 (define (address-local? address)
64 (and (equal? (address-host address) sam-host)
65 (equal? (address-port address) sam-port)))
67 (define actor-table (make-hash-table))
69 (define (make-actor beh)
70 (let* ((address (make-local-address))
71 (id (address-id address)))
72 (hash-table-set! actor-table id beh)
75 (define (deliver-message address . message)
76 (if trace (print "Delivering to " address ": " message))
77 (let ((id (address-id address)))
78 (let ((behaviour (hash-table-ref/default actor-table id '())))
80 (print "Warning: discarded message " message
81 " to unknown actor id " id)
82 (match (apply (hash-table-ref actor-table id) (cons address message))
83 ('done (hash-table-delete! actor-table id))
85 (new-beh (hash-table-set! actor-table id new-beh)))))))
89 (define local-queue-mutex (make-mutex "message queue"))
90 (define message-available-mutex (make-mutex "message available"))
91 (mutex-lock! message-available-mutex #f #f)
92 (define local-queue (make-fifo))
94 (define (send-message address . message)
95 (apply (if (address-local? address)
98 (cons address message)))
100 (define (send-local-message address . message)
101 (mutex-lock! local-queue-mutex)
102 (fifo-push local-queue (cons address message))
103 (mutex-unlock! message-available-mutex)
104 (mutex-unlock! local-queue-mutex))
106 (define (send-network-message address . message)
107 (let ((s (udp-open-socket))
108 (packet (with-output-to-string
110 (write (cons address message))))))
113 (address-host address)
114 (address-port address))
116 (udp-close-socket s)))
118 (define (send-message-later address time . message)
122 (apply send-message (cons address message)))))
124 (define (next-local-message)
126 (mutex-lock! message-available-mutex #f #f)
127 (mutex-lock! local-queue-mutex)
128 (set! res (fifo-pop local-queue))
129 (if (not (fifo-empty? local-queue))
130 (mutex-unlock! message-available-mutex))
131 (mutex-unlock! local-queue-mutex)
134 (define (start-scheduler)
136 (apply deliver-message (next-local-message))
142 (define (start-network-listener)
145 (let ((s (udp-open-socket*)))
146 (udp-bind! s #f sam-port)
148 (let-values (((n str) (udp-recv s 65536)))
149 (match (with-input-from-string str read)
150 ((address message ...)
151 (apply send-message (cons address message)))
153 (print "Warning: received badly formatted message string '" str "'"))))
158 (define reader-queue-mutex (make-mutex "reader queue"))
159 (define reader-available-mutex (make-mutex "reader available"))
160 (mutex-lock! reader-available-mutex #f #f)
161 (define reader-queue (make-fifo))
163 (define (next-reader)
165 (mutex-lock! reader-available-mutex #f #f)
166 (mutex-lock! reader-queue-mutex)
167 (set! res (fifo-pop reader-queue))
168 (if (not (fifo-empty? reader-queue))
169 (mutex-unlock! reader-available-mutex))
170 (mutex-unlock! reader-queue-mutex)
173 (define (start-console)
177 (let ((reader (next-reader)))
178 (##sys#thread-block-for-i/o! (current-thread) 0 #t)
180 (send-message reader (read-line)))
183 ;; System initialization
185 (define (system-beh self . message)
189 (print "## System actor received shutdown message.")
193 (('print strings ...)
194 (apply print strings)
198 (mutex-lock! reader-queue-mutex)
199 (fifo-push reader-queue reader)
200 (mutex-unlock! reader-available-mutex)
201 (mutex-unlock! reader-queue-mutex)
206 (start-network-listener)
207 (let ((system (make-actor system-beh))
211 (set! main (make-actor main-beh)))
213 (print "Error starting main actor. Is main-beh defined?")
215 (send-message main system))
218 (define (print-usage)
219 (print "Simple Actor Machine v" sam-version "\n"
221 "Usage: sam -h|--help\n"
222 " sam [-n hostname] [-p port] source-file-1 [source-file-2 [...]] "))
225 (let loop ((args (cdr (argv))))
227 (((or "-h" "--help"))
229 (((or "-p" "--port") pstr rest ...)
230 (set! sam-port (string->number pstr))
232 (((or "-n" "--hostname") hstr rest ...)
235 (((? file-exists? filename) rest ...)
236 (print* "Loading " filename "...")
243 (print "Unrecognised argument '" (car args) "'.\n")