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)
(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 ())

@ -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"

@ -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)))

Loading…
Cancel
Save