(match message
(('start)
(send-message system 'print "Welcome to chat!\n"
- "Your client address is " self ".\n"
+ "Your client address is " (address->string self) ".\n"
"Type '/help' for a list of commands.\n")
(send-message system 'read self))
(('show-msg from text)
(if (string-null? arg)
(send-message system 'print "Missing address of client.")
(begin
- (set! recipients (cons arg recipients))
+ (set! recipients (cons (string->address arg) recipients))
(send-message system 'print "Added recipient to chat."))))
((or "c" "clear")
(set! recipients '())
;; Simple Actor Machine
;;
-;; Houses a population of actors which can communicate using messages
-;; with actors on the same machine or other machines via the network.
+;; A virtual machine which houses a population of actors which can
+;; communicate using messages with actors on the same machine or other
+;; machines via the network.
(module sam
(boot-sam
make-actor
send-message
- send-message-later)
+ send-message-later
+ address->string
+ string->address)
(import scheme
(chicken base)
srfi-18 ; threads
srfi-69 ; hash-table
uuid ; ids for actors
- uri-generic
udp
fifo)
(define sam-port 8000)
(define (make-address host port id)
- (uri->string
- (make-uri #:scheme "actor"
- #:host host
- #:port port
- #:path (list '/ id))))
+ (list id host port))
(define (make-local-address . args)
(make-address sam-host
(car args))))
(define (address-id address)
- (cadr (uri-path (uri-reference address))))
-
- (define address->uri uri-reference)
+ (car address))
+ (define (address-host address)
+ (cadr address))
+ (define (address-port address)
+ (caddr address))
+ (define (address->string address)
+ (with-output-to-string
+ (lambda () (write address))))
+ (define (string->address str)
+ (with-input-from-string str read))
(define (address-local? address)
- (let ((uri (address->uri address)))
- (and (equal? (uri-host uri) sam-host)
- (equal? (uri-port uri) sam-port))))
+ (and (equal? (address-host address) sam-host)
+ (equal? (address-port address) sam-port)))
(define actor-table (make-hash-table))
(define (send-network-message address . message)
(let ((s (udp-open-socket))
- (uri (address->uri address))
(packet (with-output-to-string
(lambda ()
(write (cons address message))))))
(udp-bind! s #f 0)
(udp-connect! s
- (uri-host uri)
- (uri-port uri))
+ (address-host address)
+ (address-port address))
(udp-send s packet)
(udp-close-socket s)))
(let ((s (udp-open-socket*)))
(udp-bind! s #f sam-port)
(let loop ()
- (let-values (((n str) (udp-recv s 1024)))
+ (let-values (((n str) (udp-recv s 65536)))
(match (with-input-from-string str read)
((address message ...)
(apply send-message (cons address message)))