Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support NEW_PID_EXT #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions src/erlang-pid.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

(eval-when (:compile-toplevel :load-toplevel :execute)
(defconstant +pid-ext+ 103)
(defconstant +new-pid-ext+ 88)
)


Expand Down Expand Up @@ -51,14 +52,18 @@
(defmethod decode-erlang-object ((tag (eql +pid-ext+)) bytes pos)
(decode-external-pid bytes pos))

(defmethod decode-erlang-object ((tag (eql +new-pid-ext+)) bytes pos)
(decode-external-pid bytes pos t))

(defun decode-erlang-pid (bytes &optional (pos 0))
(let ((tag (aref bytes pos)))
(case tag
(#.+pid-ext+ (decode-external-pid bytes (1+ pos)))
((#.+pid-ext+ #.+new-pid-ext+)
(decode-external-pid bytes (1+ pos) (eql tag #.+new-pid-ext+)))
(otherwise
(error 'unexpected-erlang-term
:received-tag tag
:expected-tags (list +pid-ext+))) )))
:expected-tags (list +pid-ext+ +new-pid-ext+))) )))


;; PID_EXT
Expand All @@ -68,21 +73,32 @@
;; | 103 | Node | ID | Serial | Creation |
;; +-----+------+----+--------+----------+
;;
;; NEW_PID_EXT
;; +-----+------+----+--------+----------+
;; | 1 | N | 4 | 4 | 4 |
;; +-----+------+----+--------+----------+
;; | 88 | Node | ID | Serial | Creation |
;; +-----+------+----+--------+----------+
;;

(defun encode-external-pid (pid)
(with-slots (node id serial creation) pid
(concatenate 'nibbles:simple-octet-vector
(vector +pid-ext+)
(vector +new-pid-ext+)
(encode-erlang-object node)
id
serial
(vector creation))))
(uint32-to-bytes creation))))

(defun decode-external-pid (bytes &optional (pos 0))
(defun decode-external-pid (bytes &optional (pos 0) (new-pid-ext nil))
(multiple-value-bind (node pos1) (decode-erlang-atom bytes pos)
(values (make-instance 'erlang-pid
:node node
:id (subseq bytes pos1 (+ pos1 4))
:serial (subseq bytes (+ pos1 4) (+ pos1 8))
:creation (aref bytes (+ pos1 8)))
(+ pos1 9))))
:creation (if new-pid-ext
(subseq bytes (+ pos1 8) (+ pos1 12))
(aref bytes (+ pos1 8))))
(if new-pid-ext
(+ pos1 12)
(+ pos1 9)))))
7 changes: 7 additions & 0 deletions test/decode-tests.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,13 @@
'erlang-pid))
)

(test decode-new-pid
;; NEW_PID_EXT
(is (typep (decode (nibbles:octet-vector 131 88 100 0 4 65 66 66 65
0 0 0 1 87 0 13 140 0 0 0 1))
'erlang-pid))
)

(test decode-port
;; PORT_EXT
(is (typep (decode
Expand Down
6 changes: 3 additions & 3 deletions test/encode-tests.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@
(encode (list 1)))))
)

(test encode-pid
;; PID_EXT
(is (equalp (nibbles:octet-vector 131 103 115 4 65 66 66 65 0 0 0 1 0 0 0 0 1)
(test encode-new-pid
;; NEW_PID_EXT
(is (equalp (nibbles:octet-vector 131 88 115 4 65 66 66 65 0 0 0 1 0 0 0 0 0 0 0 1)
(encode (make-pid "ABBA" #(0 0 0 1) #(0 0 0 0) 1))))
)

Expand Down