Replaced URIs as optional string representation of addresses.
[sam.git] / sam.scm
diff --git a/sam.scm b/sam.scm
index 110a55c..88ae320 100644 (file)
--- a/sam.scm
+++ b/sam.scm
@@ -1,13 +1,16 @@
 ;; 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)
   (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)
+    (uri->string
+     (make-uri #:scheme "actor"
+               #:host (address-host address)
+               #:port (address-port address)
+               #:path (list '/ (address-id address)))))
+  (define (string->address str)
+    (let ((uri (uri-reference str)))
+      (make-address (uri-host uri)
+                    (uri-port uri)
+                    (cadr (uri-path uri)))))
 
   (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))
 
@@ -67,7 +78,8 @@
     (let ((id (address-id address)))
       (let ((behaviour (hash-table-ref/default actor-table id '())))
         (if (null? behaviour)
-            (print "Warning: discarded message" message " to unknown actor " address)
+            (print "Warning: discarded message " message
+                   " to unknown actor id " id)
             (match (apply (hash-table-ref actor-table id) (cons address message))
               ('done (hash-table-delete! actor-table id))
               ('sleep 'do-nothing)
 
   (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)))