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)
26 (define sam-host "localhost")
27 (define sam-port 8000)
29 (define sam-version "0.1")
34 (define (make-address host port id)
37 (define (make-local-address . args)
38 (make-address sam-host
44 (define (address-id address)
46 (define (address-host address)
48 (define (address-port address)
50 (define (address->string address)
52 (make-uri #:scheme "actor"
53 #:host (address-host address)
54 #:port (address-port address)
55 #:path (list '/ (address-id address)))))
56 (define (string->address str)
57 (let ((uri (uri-reference str)))
58 (make-address (uri-host uri)
60 (cadr (uri-path uri)))))
62 (define (address-local? address)
63 (and (equal? (address-host address) sam-host)
64 (equal? (address-port address) sam-port)))
66 (define actor-table (make-hash-table))
68 (define (make-actor beh)
69 (let* ((address (make-local-address))
70 (id (address-id address)))
71 (hash-table-set! actor-table id beh)
74 (define (deliver-message address . message)
75 (if trace (print "Delivering to " address ": " message))
76 (let ((id (address-id address)))
77 (let ((behaviour (hash-table-ref/default actor-table id '())))
79 (print "Warning: discarded message " message
80 " to unknown actor id " id)
81 (match (apply (hash-table-ref actor-table id) (cons address message))
82 ('done (hash-table-delete! actor-table id))
84 (new-beh (hash-table-set! actor-table id new-beh)))))))
88 (define local-queue-mutex (make-mutex "message queue"))
89 (define message-available-mutex (make-mutex "message available"))
90 (mutex-lock! message-available-mutex #f #f)
91 (define local-queue (make-fifo))
93 (define (send-message address . message)
94 (apply (if (address-local? address)
97 (cons address message)))
99 (define (send-local-message address . message)
100 (mutex-lock! local-queue-mutex)
101 (fifo-push local-queue (cons address message))
102 (mutex-unlock! message-available-mutex)
103 (mutex-unlock! local-queue-mutex))
105 (define (send-network-message address . message)
106 (let ((s (udp-open-socket))
107 (packet (with-output-to-string
109 (write (cons address message))))))
112 (address-host address)
113 (address-port address))
115 (udp-close-socket s)))
117 (define (send-message-later address time . message)
121 (apply send-message (cons address message)))))
123 (define (next-local-message)
125 (mutex-lock! message-available-mutex #f #f)
126 (mutex-lock! local-queue-mutex)
127 (set! res (fifo-pop local-queue))
128 (if (not (fifo-empty? local-queue))
129 (mutex-unlock! message-available-mutex))
130 (mutex-unlock! local-queue-mutex)
133 (define (start-scheduler)
135 (apply deliver-message (next-local-message))
141 (define (start-network-listener)
144 (let ((s (udp-open-socket*)))
145 (udp-bind! s #f sam-port)
147 (let-values (((n str) (udp-recv s 65536)))
148 (match (with-input-from-string str read)
149 ((address message ...)
150 (apply send-message (cons address message)))
152 (print "Warning: received badly formatted message string '" str "'"))))
157 (define reader-queue-mutex (make-mutex "reader queue"))
158 (define reader-available-mutex (make-mutex "reader available"))
159 (mutex-lock! reader-available-mutex #f #f)
160 (define reader-queue (make-fifo))
162 (define (next-reader)
164 (mutex-lock! reader-available-mutex #f #f)
165 (mutex-lock! reader-queue-mutex)
166 (set! res (fifo-pop reader-queue))
167 (if (not (fifo-empty? reader-queue))
168 (mutex-unlock! reader-available-mutex))
169 (mutex-unlock! reader-queue-mutex)
172 (define (start-console)
176 (let ((reader (next-reader)))
177 (##sys#thread-block-for-i/o! (current-thread) 0 #t)
179 (send-message reader (read-line)))
182 ;; System initialization
184 (define (system-beh self . message)
188 (print "## System actor received shutdown message.")
192 (('print strings ...)
193 (apply print strings)
197 (mutex-lock! reader-queue-mutex)
198 (fifo-push reader-queue reader)
199 (mutex-unlock! reader-available-mutex)
200 (mutex-unlock! reader-queue-mutex)
205 (start-network-listener)
206 (send-message (make-actor main-beh) (make-actor system-beh))
209 (define (print-usage)
210 (print "Simple Actor Machine v" sam-version "\n"
212 "Usage: sam -h|--help\n"
213 " sam [-n hostname] [-p port] source-file-1 [source-file-2 [...]] "))
216 (let loop ((args (cdr (argv))))
218 (((or "-h" "--help"))
220 (((or "-p" "--port") pstr rest ...)
221 (set! sam-port (string->number pstr))
223 (((or "-n" "--hostname") hstr rest ...)
226 (((? file-exists? filename) rest ...))
227 (print* "Loading " filename "...")
232 (boot-sam host port main-beh))
234 (print "Unrecognised argument '" (car args) "'.\n")