From d3e740ccf39b0456d8ea56cd58b0d623712bf637 Mon Sep 17 00:00:00 2001 From: Hugo Thunnissen Date: Tue, 16 Oct 2018 11:40:34 +0200 Subject: [PATCH] Use find-message-end-point and test with real process --- run-process.el | 13 ++++++++++--- stomp.el | 30 +++++++++++++++--------------- test.el | 3 ++- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/run-process.el b/run-process.el index 54ddbcd..76ba00a 100644 --- a/run-process.el +++ b/run-process.el @@ -1,10 +1,17 @@ (require 'map) (load-file "/home/hugo/projects/stomp/stomp.el") -(setq proc (make-process +;; (setq proc (make-process +;; :name "stomp-client" +;; :buffer "Barry" +;; :command "nc localhost 61613" +;; :filter (stomp-filter-function (lambda (message) (message "%s" message))))) + +(setq proc (make-network-process :name "stomp-client" - :buffer "Barry" - :command "nc localhost 61613" + :buffer "stomp-messages" + :host "127.0.0.1" + :service 61613 :filter (stomp-filter-function (lambda (message) (message "%s" message))))) (setq message ()) diff --git a/stomp.el b/stomp.el index 53210b1..c9c6bfd 100644 --- a/stomp.el +++ b/stomp.el @@ -1,3 +1,4 @@ +;; -*- lexical-binding: t -*- ;; stomp.el, interpret STOMP messages. (require 'map) @@ -22,7 +23,7 @@ (goto-char (point-max)) (insert string) (let ((message (stomp-shift-message (current-buffer)))) - (if message (callback message)))))) + (if message (funcall callback message)))))) (defun stomp-shift-message (buffer) "Read a message from a buffer. After reading a message, it will @@ -32,16 +33,10 @@ (message (stomp-delete-message buffer) message) (t nil)))) -;; TODO: use `stomp-find-messsage-end-point` (defun stomp-delete-message (buffer) "Delete the top-most messsage in a buffer" (with-current-buffer buffer - (goto-char (point-min)) - (while (not (or - (string-equal (thing-at-point 'line) "\u0000\n") - (string-equal (thing-at-point 'line) "\u0000\r\n"))) - (forward-line)) - (delete-region (point-min) (+ (line-end-position) 1)))) + (delete-region (point-min) (+ 2x (stomp-find-message-end-point buffer))))) (defun stomp-read-message (buffer) "Attempt to read a single message from a buffer. @@ -52,14 +47,16 @@ (goto-char (point-min)) (while (search-forward "\r" nil t) (replace-match "")) - (goto-char (point-min)) - (let ((message ()) (headers ()) (content)) + (let ((message ()) (headers ()) (content) + (headers-end-point (stomp-find-headers-end-point buffer))) + (goto-char (point-min)) (map-put message 'command (current-word)) - (setq headers (stomp-read-headers buffer)) + + (setq headers (stomp-read-headers buffer headers-end-point)) ;; TODO: take content-length header into account (setq content (buffer-substring - (stomp-find-headers-end-point buffer) + (+ 1 headers-end-point) (stomp-find-message-end-point buffer))) (cond (content @@ -67,16 +64,19 @@ (map-put message 'content content)) (t nil))))) -(defun stomp-read-headers (buffer &optional headers) +(defun stomp-read-headers (buffer end-point &optional headers) "Attempt to read the headers of a stomp message in *buffer*" (with-current-buffer buffer (unless headers (goto-char (point-min))) (forward-line) (let ((header (split-string (thing-at-point 'line) ":"))) (cond + ((= (line-end-position) end-point) headers) ((> (length header) 2) (throw 'invalid-header nil)) - ((= (length header) 1) headers) - (t (stomp-read-headers buffer (push headers header))))))) + (t (stomp-read-headers buffer end-point + (map-put headers + (car header) + (replace-regexp-in-string "\n$" "" (car (last header)))))))))) (defun stomp-find-message-end-point (buffer) "Find the null byte at the end of a STOMP message" diff --git a/test.el b/test.el index b53260a..2927955 100755 --- a/test.el +++ b/test.el @@ -18,7 +18,8 @@ (load (format "%sstomp.el" default-directory) nil t) ;; Simple test for basic buffer -(pp (stomp-read-message (simple-message-buffer))) +(setq recieved (stomp-read-message (simple-message-buffer))) +(pp (alist-get 'command recieved)) ;; Test for buffer containing carriage returns (pp (stomp-read-message (carriage-return-message-buffer)))