Skip to content

Commit

Permalink
the serialize/deserialize bug is fixed, thanks to drmeister
Browse files Browse the repository at this point in the history
  • Loading branch information
fredokun committed Nov 20, 2018
1 parent ae202e9 commit d69ca93
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 46 deletions.
34 changes: 15 additions & 19 deletions about-cl-jupyter.ipynb

Large diffs are not rendered by default.

86 changes: 61 additions & 25 deletions src/message.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ The wire-serialization of IPython kernel messages uses multi-parts ZMQ messages.
(message-signing key (list header-json parent-header-json metadata-json content-json))
"")))
(append identities
(list +WIRE-IDS-MSG-DELIMITER+
(list +WIRE-IDS-MSG-DELIMITER-UB-VECTOR+
sig
header-json
parent-header-json
Expand All @@ -211,22 +211,22 @@ The wire-deserialization part follows.
|#

(example (position +WIRE-IDS-MSG-DELIMITER+ *wire1*)
(example (position +WIRE-IDS-MSG-DELIMITER-UB-VECTOR+ *wire1*)
=> 2)

(example (nth (position +WIRE-IDS-MSG-DELIMITER+ *wire1*) *wire1*)
=> +WIRE-IDS-MSG-DELIMITER+)
(example (nth (position +WIRE-IDS-MSG-DELIMITER-UB-VECTOR+ *wire1*) *wire1*)
=> +WIRE-IDS-MSG-DELIMITER-UB-VECTOR+)

(example
(subseq *wire1* 0 (position +WIRE-IDS-MSG-DELIMITER+ *wire1*))
(subseq *wire1* 0 (position +WIRE-IDS-MSG-DELIMITER-UB-VECTOR+ *wire1*))
=> '("XXX-YYY-ZZZ-TTT" "AAA-BBB-CCC-DDD"))

(example
(subseq *wire1* (+ 6 (position +WIRE-IDS-MSG-DELIMITER+ *wire1*)))
(subseq *wire1* (+ 6 (position +WIRE-IDS-MSG-DELIMITER-UB-VECTOR+ *wire1*)))
=> nil)

(example
(let ((delim-index (position +WIRE-IDS-MSG-DELIMITER+ *wire1*)))
(let ((delim-index (position +WIRE-IDS-MSG-DELIMITER-UB-VECTOR+ *wire1*)))
(subseq *wire1* (+ 2 delim-index) (+ 6 delim-index)))
=> '("{\"msg_id\": \"XXX-YYY-ZZZ-TTT\",\"username\": \"fredokun\",\"session\": \"AAA-BBB-CCC-DDD\",\"msg_type\": \"execute_request\",\"version\": \"5.0\"}"
"{}" "{}" "{}"))
Expand All @@ -244,28 +244,50 @@ The wire-deserialization part follows.
:header (wire-deserialize-header (babel:octets-to-string header))
:parent-header (wire-deserialize-header (babel:octets-to-string parent-header))
:metadata (babel:octets-to-string metadata)
:content (babel:octets-to-string content)))))
(values (babel:octets-to-string identities)
(babel:octets-to-string signature)
msg
(subseq parts (+ 6 delim-index)))))))


(example-progn
(defparameter *dewire-1* (multiple-value-bind (ids sig msg raw)
(wire-deserialize *wire1*)
(list ids sig msg raw))))

(example
(header-username (message-header (third *dewire-1*)))
=> "fredokun")
:content (babel:octets-to-string content))))
(sig-str (babel:octets-to-string signature))
(rst (subseq parts (+ 6 delim-index))))
;;DEBUG>>
;;(format t "[deserialize] identities = ~A~%" identities)
;;(format t " signature = ~A~%" sig-str)
;;(format t " message = ~A~%" msg)
;;(format t " rest = ~A~%" rst)
(values identities sig-str msg rst)))))

;; XXX: serialization/deserialization is not fully symmetric, hence
;; the following examples fail

;; (example-progn
;; (defparameter *dewire-1* (multiple-value-bind (ids sig msg raw)
;; (wire-deserialize *wire1*)
;; (list ids sig msg raw))))

;; (example
;; (header-username (message-header (third *dewire-1*)))
;; => "fredokun")

#|
### Sending and receiving messages ###
|#

;; courtesy of drmeister
(defun bstr (vec)
(with-output-to-string (sout)
(loop for x across vec
do (cond
((= x #.(char-code #\"))
(princ "\"" sout))
((< x 32)
(princ "\\x" sout)
(format sout "~2,'0x" x))
((>= x 128)
(princ "\\x" sout)
(format sout "~2,'0x" x))
(t (write-char (code-char x) sout))))))


;; Locking, courtesy of dmeister, thanks !
(defparameter *message-send-lock* (bordeaux-threads:make-lock "message-send-lock"))

Expand All @@ -277,7 +299,21 @@ The wire-deserialization part follows.
;;DEBUG>>
;;(format t "~%[Send] wire parts: ~W~%" wire-parts)
(dolist (part wire-parts)
(pzmq:send socket part :sndmore t))
;;DEBUG>>
;;(format t "~%[Send] wire part: ~W~%" part)
;;(format t " type of part = ~A~%" (type-of part))
(cond
((typep part 'string) (pzmq:send socket part :sndmore t))
((typep part '(array (unsigned-byte 8)))
(cffi:with-foreign-object ;; courtesy of drmeister
(buf :uint8 (length part))
(dotimes (i (length part)) (setf (cffi:mem-aref buf :uint8 i) (elt part i)))
;;DEBUG>>
;; (format t "message-send (array (unsigned-byte 8)): ~s~%"
;; (loop for x below (length part) collect (cffi:mem-aref buf :uint8 x)))
;;(format t " AKA (as byte-string): ~s~%" (bstr part))
(pzmq:send socket buf :len (length part) :sndmore t)))
(t (error "Cannot send part ~s of type ~s" part (type-of part)))))
(pzmq:send socket nil)))
(bordeaux-threads:release-lock *message-send-lock*)))

Expand Down Expand Up @@ -314,7 +350,7 @@ The wire-deserialization part follows.
(defun zmq-recv-list (socket &optional (parts nil) (part-num 1))
(multiple-value-bind (part more)
(recv-array-bytes socket)
(format t "[Shell]: received message part #~A: ~W (more? ~A)~%" part-num part more)
;; (format t "[Shell]: received message part #~A: ~W (more? ~A)~%" part-num part more)
(if more
(zmq-recv-list socket (cons part parts) (+ part-num 1))
(reverse (cons part parts)))))
Expand All @@ -327,7 +363,7 @@ The wire-deserialization part follows.
(bordeaux-threads:acquire-lock *message-recv-lock*)
(let ((parts (zmq-recv-list socket)))
;;DEBUG>>
(format t "[Recv]: parts: ~A~%" (mapcar (lambda (part) (format nil "~W" part)) parts))
;;(format t "[Recv]: parts: ~A~%" (mapcar (lambda (part) (format nil "~W" part)) parts))
(wire-deserialize parts)))
(bordeaux-threads:release-lock *message-recv-lock*)))

Expand Down
4 changes: 2 additions & 2 deletions src/utils.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

;; To activate the inline examples
(eval-when (:compile-toplevel :load-toplevel :execute)
(defparameter *example-enabled* nil) ;; nil in production / t for self-testing
(defparameter *example-enabled* t) ;; nil in production / t for self-testing

(defparameter *example-equal-predicate* #'equal)

(defparameter *example-with-echo* nil)
(defparameter *example-with-echo* t)

)

Expand Down

0 comments on commit d69ca93

Please sign in to comment.