Use find-message-end-point and test with real process

master
Hugo Thunnissen 6 years ago
parent 97b1f2bc95
commit d3e740ccf3

@ -1,10 +1,17 @@
(require 'map) (require 'map)
(load-file "/home/hugo/projects/stomp/stomp.el") (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" :name "stomp-client"
:buffer "Barry" :buffer "stomp-messages"
:command "nc localhost 61613" :host "127.0.0.1"
:service 61613
:filter (stomp-filter-function (lambda (message) (message "%s" message))))) :filter (stomp-filter-function (lambda (message) (message "%s" message)))))
(setq message ()) (setq message ())

@ -1,3 +1,4 @@
;; -*- lexical-binding: t -*-
;; stomp.el, interpret STOMP messages. ;; stomp.el, interpret STOMP messages.
(require 'map) (require 'map)
@ -22,7 +23,7 @@
(goto-char (point-max)) (goto-char (point-max))
(insert string) (insert string)
(let ((message (stomp-shift-message (current-buffer)))) (let ((message (stomp-shift-message (current-buffer))))
(if message (callback message)))))) (if message (funcall callback message))))))
(defun stomp-shift-message (buffer) (defun stomp-shift-message (buffer)
"Read a message from a buffer. After reading a message, it will "Read a message from a buffer. After reading a message, it will
@ -32,16 +33,10 @@
(message (stomp-delete-message buffer) message) (message (stomp-delete-message buffer) message)
(t nil)))) (t nil))))
;; TODO: use `stomp-find-messsage-end-point`
(defun stomp-delete-message (buffer) (defun stomp-delete-message (buffer)
"Delete the top-most messsage in a buffer" "Delete the top-most messsage in a buffer"
(with-current-buffer buffer (with-current-buffer buffer
(goto-char (point-min)) (delete-region (point-min) (+ 2x (stomp-find-message-end-point buffer)))))
(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))))
(defun stomp-read-message (buffer) (defun stomp-read-message (buffer)
"Attempt to read a single message from a buffer. "Attempt to read a single message from a buffer.
@ -52,14 +47,16 @@
(goto-char (point-min)) (goto-char (point-min))
(while (search-forward "\r" nil t) (while (search-forward "\r" nil t)
(replace-match "")) (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)) (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 ;; TODO: take content-length header into account
(setq content (buffer-substring (setq content (buffer-substring
(stomp-find-headers-end-point buffer) (+ 1 headers-end-point)
(stomp-find-message-end-point buffer))) (stomp-find-message-end-point buffer)))
(cond (cond
(content (content
@ -67,16 +64,19 @@
(map-put message 'content content)) (map-put message 'content content))
(t nil))))) (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*" "Attempt to read the headers of a stomp message in *buffer*"
(with-current-buffer buffer (with-current-buffer buffer
(unless headers (goto-char (point-min))) (unless headers (goto-char (point-min)))
(forward-line) (forward-line)
(let ((header (split-string (thing-at-point 'line) ":"))) (let ((header (split-string (thing-at-point 'line) ":")))
(cond (cond
((= (line-end-position) end-point) headers)
((> (length header) 2) (throw 'invalid-header nil)) ((> (length header) 2) (throw 'invalid-header nil))
((= (length header) 1) headers) (t (stomp-read-headers buffer end-point
(t (stomp-read-headers buffer (push headers header))))))) (map-put headers
(car header)
(replace-regexp-in-string "\n$" "" (car (last header))))))))))
(defun stomp-find-message-end-point (buffer) (defun stomp-find-message-end-point (buffer)
"Find the null byte at the end of a STOMP message" "Find the null byte at the end of a STOMP message"

@ -18,7 +18,8 @@
(load (format "%sstomp.el" default-directory) nil t) (load (format "%sstomp.el" default-directory) nil t)
;; Simple test for basic buffer ;; 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 ;; Test for buffer containing carriage returns
(pp (stomp-read-message (carriage-return-message-buffer))) (pp (stomp-read-message (carriage-return-message-buffer)))

Loading…
Cancel
Save