diff --git a/stomp.el b/stomp.el index 07773d1..cc671ef 100644 --- a/stomp.el +++ b/stomp.el @@ -3,16 +3,10 @@ (require 'map) -(defun stomp-connect (host port) - "Connect to a server supporting the STOMP protocol" - (message "Not implemented yet")) - ;; TODO: make this send more than just the command (defun stomp-send-frame (process frame) "Send a message to a STOMP process" - (process-send-string - process - (format "%s\n\n\u0000\n" (alist-get 'command frame)))) + (process-send-string process (stomp-frame-to-string frame))) (defun stomp-filter-function (callback) "Filter function to be used with processes" @@ -86,3 +80,18 @@ (condition-case nil (progn (search-forward-regexp "^$") (line-end-position)) (error nil)))) + +(defun stomp-frame-to-string (frame) + "Convert a lisp datastructure describing a STOMP frame into a + string that could be sent to a server or client. The + datastructure should be an alist and should at least have a + 'command key. Other optional keys are 'headers (another alist) + and 'content" + (unless (assq 'command frame) + (throw 'invalid-frame "A STOMP frame must define a command")) + (let ((headers (if (assq 'headers frame) (alist-get 'headers frame) '())) + (content (if (assq 'content frame) (alist-get 'content frame) ""))) + (format "%s\n%s\n%s\u0000" + (alist-get 'command frame) + (mapconcat (lambda (h) (format "%s:%s\n" (car h) (cdr h))) headers "") + content))) diff --git a/test.el b/test.el index ab9bd78..4a2ef20 100755 --- a/test.el +++ b/test.el @@ -57,3 +57,12 @@ (stomp-delete-frame buffer) (with-current-buffer buffer (should (equal (buffer-string) "BLA\nsome-header:value\nok:bye\n\ncontent\n\u0000\n"))))) + +(ert-deftest can-compose-frame () + "Can compose a valid STOMP frame from a LISP datastructure" + (let ((frame '()) (headers '())) + (map-put frame 'command "SEND") + (map-put headers "header" "value") + (map-put frame 'headers headers) + (map-put frame 'content "Some content") + (should (equal (stomp-frame-to-string frame) "SEND\nheader:value\n\nSome content\u0000"))))