actors: New read-message procedures, touch up write-message procedures
[8sync.git] / 8sync / systems / actors.scm
index 2d42897a03740c3be374473d192d9b463e289a71..bd164be2c26199e1f2fa1a6730085dddd162e777 100644 (file)
@@ -38,7 +38,9 @@
             actor-hive
             actor-message-handler
 
-            <address>
+            ;;; Commenting out the <address> type for now;
+            ;;; it may be back when we have better serializers
+            ;; <address>
             make-address address?
             address-actor-id address-hive-id
 
 ;;; =========================
 
 (define-class <actor> ()
-  ;; An <address> object
+  ;; An address object
   (id #:init-thunk (require-slot "id")
       #:init-keyword #:id
       #:getter actor-id)
 (define-method (actor-message-handler (actor <actor>))
   (slot-ref actor 'message-handler))
 
-(define-record-type <address>
-  (make-address actor-id hive-id)  ; @@: Do we want the trailing -id?
-  address?
-  (actor-id address-actor-id)
-  (hive-id address-hive-id))
+;;; So these are the nicer representations of addresses.
+;;; However, they don't serialize so easily with scheme read/write, so we're
+;;; using the simpler cons cell version below for now.
 
-(set-record-type-printer!
- <address>
- (lambda (record port)
-   (format port "<address: ~s@~s>"
-           (address-actor-id record) (address-hive-id record))))
+;; (define-record-type <address>
+;;   (make-address actor-id hive-id)  ; @@: Do we want the trailing -id?
+;;   address?
+;;   (actor-id address-actor-id)
+;;   (hive-id address-hive-id))
+;;
+;; (set-record-type-printer!
+;;  <address>
+;;  (lambda (record port)
+;;    (format port "<address: ~s@~s>"
+;;            (address-actor-id record) (address-hive-id record))))
+;;
+
+(define (make-address actor-id hive-id)
+  (cons actor-id hive-id))
+
+(define (address-actor-id address)
+  (car address))
+
+(define (address-hive-id address)
+  (cdr address))
 
 (define (address->string address)
   (string-append (address-actor-id address) "@"
@@ -557,10 +573,11 @@ an integer."
    (message-action message)
    (message-body message)
    (message-in-reply-to message)
+   (message-wants-reply message)
    (message-replied message)
    (message-deferred-reply message)))
 
-(define (write-message message port)
+(define* (write-message message #:optional (port (current-output-port)))
   "Write out a message to a port for easy reading later.
 
 Note that if a sub-value can't be easily written to something
@@ -577,9 +594,28 @@ to improve that.  You'll need a better serializer for that.."
     (action ,(message-action message))
     (body ,(message-body message))
     (in-reply-to ,(message-in-reply-to message))
+    (wants-reply ,(message-wants-reply message))
     (replied ,(message-replied message))
     (deferred-reply ,(message-deferred-reply message))))
 
 (define (pprint-message message)
   "Pretty print a message."
   (pretty-print (serialize-message-pretty message)))
+
+(define* (read-message #:optional (port (current-input-port)))
+  "Read a message serialized via serialize-message from PORT"
+  (match (read port)
+    ((id to from action body in-reply-to wants-reply replied deferred-reply)
+     (make-message-intern
+      id to from action body
+      in-reply-to wants-reply replied deferred-reply))
+    (anything-else
+     (throw 'message-read-bad-structure
+            "Could not read message from structure"
+            anything-else))))
+
+(define (read-message-from-string message-str)
+  "Read message from MESSAGE-STR"
+  (with-input-from-string message-str
+    (lambda ()
+      (read-message (current-input-port)))))