Renamed connection.el

* lisp/net/connection.el: Renamed to dictionary-connection.el, also
prefixing all functions with "dictionary-" prefix
* lisp/net/dictionary.el: Adapt to renamed functions
This commit is contained in:
Torsten Hilbrich 2020-10-05 06:56:59 +02:00
parent b6227446d9
commit 658ec3ccee
3 changed files with 170 additions and 173 deletions

View file

@ -1,159 +0,0 @@
;;; connection.el --- TCP-based client connection
;; Author: Torsten Hilbrich <torsten.hilbrich@gmx.net>
;; Keywords: network
;; Version: 1.11
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; connection allows to handle TCP-based connections in client mode
;; where text-based information are exchanged. There is special
;; support for handling CR LF (and the usual CR LF . CR LF
;; terminater).
;;; Code:
(eval-when-compile
(require 'cl))
(defmacro connection-p (connection)
"Returns non-nil if `connection' is a connection object"
(list 'get connection ''connection))
(defmacro connection-read-point (connection)
"Return the read point of the connection object."
(list 'get connection ''connection-read-point))
(defmacro connection-process (connection)
"Return the process of the connection object."
(list 'get connection ''connection-process))
(defmacro connection-buffer (connection)
"Return the buffer of the connection object."
(list 'get connection ''connection-buffer))
(defmacro connection-set-read-point (connection point)
"Set the read-point for `connection' to `point'."
(list 'put connection ''connection-read-point point))
(defmacro connection-set-process (connection process)
"Set the process for `connection' to `process'."
(list 'put connection ''connection-process process))
(defmacro connection-set-buffer (connection buffer)
"Set the buffer for `connection' to `buffer'."
(list 'put connection ''connection-buffer buffer))
(defun connection-create-data (buffer process point)
"Create a new connection data based on `buffer', `process', and `point'."
(let ((connection (make-symbol "connection")))
(put connection 'connection t)
(connection-set-read-point connection point)
(connection-set-process connection process)
(connection-set-buffer connection buffer)
connection))
(defun connection-open (server port)
"Open a connection to `server' and `port'.
A data structure identifing the connection is returned"
(let ((process-buffer (generate-new-buffer (format " connection to %s:%s"
server
port)))
(process))
(with-current-buffer process-buffer
(setq process (open-network-stream "connection" process-buffer
server port))
(connection-create-data process-buffer process (point-min)))))
(defun connection-status (connection)
"Return the status of the connection.
Possible return values are the symbols:
nil: argument is no connection object
'none: argument has no connection
'up: connection is open and buffer is existing
'down: connection is closed
'alone: connection is not associated with a buffer"
(if (connection-p connection)
(let ((process (connection-process connection))
(buffer (connection-buffer connection)))
(if (not process)
'none
(if (not (buffer-live-p buffer))
'alone
(if (not (eq (process-status process) 'open))
'down
'up))))
nil))
(defun connection-close (connection)
"Force closing of the connection."
(if (connection-p connection)
(progn
(let ((buffer (connection-buffer connection))
(process (connection-process connection)))
(if process
(delete-process process))
(if buffer
(kill-buffer buffer))
(connection-set-process connection nil)
(connection-set-buffer connection nil)))))
(defun connection-send (connection data)
"Send `data' to the process."
(unless (eq (connection-status connection) 'up)
(error "Connection is not up"))
(with-current-buffer (connection-buffer connection)
(goto-char (point-max))
(connection-set-read-point connection (point))
(process-send-string (connection-process connection) data)))
(defun connection-send-crlf (connection data)
"Send `data' together with CRLF to the process."
(connection-send connection (concat data "\r\n")))
(defun connection-read (connection delimiter)
"Read data until `delimiter' is found inside the buffer."
(unless (eq (connection-status connection) 'up)
(error "Connection is not up"))
(let ((case-fold-search nil)
match-end)
(with-current-buffer (connection-buffer connection)
(goto-char (connection-read-point connection))
;; Wait until there is enough data
(while (not (search-forward-regexp delimiter nil t))
(accept-process-output (connection-process connection) 3)
(goto-char (connection-read-point connection)))
(setq match-end (point))
;; Return the result
(let ((result (buffer-substring (connection-read-point connection)
match-end)))
(connection-set-read-point connection match-end)
result))))
(defun connection-read-crlf (connection)
"Read until a line is completedx with CRLF"
(connection-read connection "\015?\012"))
(defun connection-read-to-point (connection)
"Read until a line is consisting of a single point"
(connection-read connection "\015?\012[.]\015?\012"))
(provide 'connection)
;;; connection.el ends here

View file

@ -0,0 +1,156 @@
;;; dictionary-connection.el --- TCP-based client connection for dictionary
;; Author: Torsten Hilbrich <torsten.hilbrich@gmx.net>
;; Keywords: network
;; Version: 1.11
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;; dictionary-connection allows to handle TCP-based connections in
;; client mode where text-based information are exchanged. There is
;; special support for handling CR LF (and the usual CR LF . CR LF
;; terminater).
;;; Code:
(defmacro dictionary-connection-p (connection)
"Returns non-nil if `connection' is a connection object"
(list 'get connection ''connection))
(defmacro dictionary-connection-read-point (connection)
"Return the read point of the connection object."
(list 'get connection ''dictionary-connection-read-point))
(defmacro dictionary-connection-process (connection)
"Return the process of the connection object."
(list 'get connection ''dictionary-connection-process))
(defmacro dictionary-connection-buffer (connection)
"Return the buffer of the connection object."
(list 'get connection ''dictionary-connection-buffer))
(defmacro dictionary-connection-set-read-point (connection point)
"Set the read-point for `connection' to `point'."
(list 'put connection ''dictionary-connection-read-point point))
(defmacro dictionary-connection-set-process (connection process)
"Set the process for `connection' to `process'."
(list 'put connection ''dictionary-connection-process process))
(defmacro dictionary-connection-set-buffer (connection buffer)
"Set the buffer for `connection' to `buffer'."
(list 'put connection ''dictionary-connection-buffer buffer))
(defun dictionary-connection-create-data (buffer process point)
"Create a new connection data based on `buffer', `process', and `point'."
(let ((connection (make-symbol "connection")))
(put connection 'connection t)
(dictionary-connection-set-read-point connection point)
(dictionary-connection-set-process connection process)
(dictionary-connection-set-buffer connection buffer)
connection))
(defun dictionary-connection-open (server port)
"Open a connection to `server' and `port'.
A data structure identifing the connection is returned"
(let ((process-buffer (generate-new-buffer (format " connection to %s:%s"
server
port)))
(process))
(with-current-buffer process-buffer
(setq process (open-network-stream "connection" process-buffer
server port))
(dictionary-connection-create-data process-buffer process (point-min)))))
(defun dictionary-connection-status (connection)
"Return the status of the connection.
Possible return values are the symbols:
nil: argument is no connection object
'none: argument has no connection
'up: connection is open and buffer is existing
'down: connection is closed
'alone: connection is not associated with a buffer"
(if (dictionary-connection-p connection)
(let ((process (dictionary-connection-process connection))
(buffer (dictionary-connection-buffer connection)))
(if (not process)
'none
(if (not (buffer-live-p buffer))
'alone
(if (not (eq (process-status process) 'open))
'down
'up))))
nil))
(defun dictionary-connection-close (connection)
"Force closing of the connection."
(if (dictionary-connection-p connection)
(progn
(let ((buffer (dictionary-connection-buffer connection))
(process (dictionary-connection-process connection)))
(if process
(delete-process process))
(if buffer
(kill-buffer buffer))
(dictionary-connection-set-process connection nil)
(dictionary-connection-set-buffer connection nil)))))
(defun dictionary-connection-send (connection data)
"Send `data' to the process."
(unless (eq (dictionary-connection-status connection) 'up)
(error "Connection is not up"))
(with-current-buffer (dictionary-connection-buffer connection)
(goto-char (point-max))
(dictionary-connection-set-read-point connection (point))
(process-send-string (dictionary-connection-process connection) data)))
(defun dictionary-connection-send-crlf (connection data)
"Send `data' together with CRLF to the process."
(dictionary-connection-send connection (concat data "\r\n")))
(defun dictionary-connection-read (connection delimiter)
"Read data until `delimiter' is found inside the buffer."
(unless (eq (dictionary-connection-status connection) 'up)
(error "Connection is not up"))
(let ((case-fold-search nil)
match-end)
(with-current-buffer (dictionary-connection-buffer connection)
(goto-char (dictionary-connection-read-point connection))
;; Wait until there is enough data
(while (not (search-forward-regexp delimiter nil t))
(accept-process-output (dictionary-connection-process connection) 3)
(goto-char (dictionary-connection-read-point connection)))
(setq match-end (point))
;; Return the result
(let ((result (buffer-substring (dictionary-connection-read-point connection)
match-end)))
(dictionary-connection-set-read-point connection match-end)
result))))
(defun dictionary-connection-read-crlf (connection)
"Read until a line is completedx with CRLF"
(dictionary-connection-read connection "\015?\012"))
(defun dictionary-connection-read-to-point (connection)
"Read until a line is consisting of a single point"
(dictionary-connection-read connection "\015?\012[.]\015?\012"))
(provide 'dictionary-connection)
;;; dictionary-connection.el ends here

View file

@ -39,7 +39,7 @@
(require 'easymenu) (require 'easymenu)
(require 'custom) (require 'custom)
(require 'connection) (require 'dictionary-connection)
(require 'link) (require 'link)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -60,10 +60,10 @@
(defun dictionary-set-server-var (name value) (defun dictionary-set-server-var (name value)
(if (and (boundp 'dictionary-connection) (if (and (boundp 'dictionary-connection)
dictionary-connection dictionary-connection
(eq (connection-status dictionary-connection) 'up) (eq (dictionary-connection-status dictionary-connection) 'up)
(y-or-n-p (y-or-n-p
(concat "Close existing connection to " dictionary-server "? "))) (concat "Close existing connection to " dictionary-server "? ")))
(connection-close dictionary-connection)) (dictionary-connection-close dictionary-connection))
(set-default name value)) (set-default name value))
(defgroup dictionary nil (defgroup dictionary nil
@ -451,7 +451,7 @@ by the choice value:
(defun dictionary-check-connection () (defun dictionary-check-connection ()
"Check if there is already a connection open" "Check if there is already a connection open"
(if (not (and dictionary-connection (if (not (and dictionary-connection
(eq (connection-status dictionary-connection) 'up))) (eq (dictionary-connection-status dictionary-connection) 'up)))
(let ((wanted 'raw-text) (let ((wanted 'raw-text)
(coding-system nil)) (coding-system nil))
(if (and (fboundp 'coding-system-list) (if (and (fboundp 'coding-system-list)
@ -461,14 +461,14 @@ by the choice value:
(coding-system-for-write coding-system)) (coding-system-for-write coding-system))
(message "Opening connection to %s:%s" dictionary-server (message "Opening connection to %s:%s" dictionary-server
dictionary-port) dictionary-port)
(connection-close dictionary-connection) (dictionary-connection-close dictionary-connection)
(setq dictionary-connection (setq dictionary-connection
(if dictionary-use-http-proxy (if dictionary-use-http-proxy
(connection-open dictionary-proxy-server (dictionary-connection-open dictionary-proxy-server
dictionary-proxy-port) dictionary-proxy-port)
(connection-open dictionary-server dictionary-port))) (dictionary-connection-open dictionary-server dictionary-port)))
(set-process-query-on-exit-flag (set-process-query-on-exit-flag
(connection-process dictionary-connection) (dictionary-connection-process dictionary-connection)
nil) nil)
(when dictionary-use-http-proxy (when dictionary-use-http-proxy
@ -520,7 +520,7 @@ by the choice value:
(progn (progn
(setq major-mode nil) (setq major-mode nil)
(if (<= (decf dictionary-instances) 0) (if (<= (decf dictionary-instances) 0)
(connection-close dictionary-connection)) (dictionary-connection-close dictionary-connection))
(let ((configuration dictionary-window-configuration) (let ((configuration dictionary-window-configuration)
(selected-window dictionary-selected-window)) (selected-window dictionary-selected-window))
(kill-buffer (current-buffer)) (kill-buffer (current-buffer))
@ -535,11 +535,11 @@ by the choice value:
"Send the command `string' to the network connection." "Send the command `string' to the network connection."
(dictionary-check-connection) (dictionary-check-connection)
;;;; ##### ;;;; #####
(connection-send-crlf dictionary-connection string)) (dictionary-connection-send-crlf dictionary-connection string))
(defun dictionary-read-reply () (defun dictionary-read-reply ()
"Read the reply line from the server" "Read the reply line from the server"
(let ((answer (connection-read-crlf dictionary-connection))) (let ((answer (dictionary-connection-read-crlf dictionary-connection)))
(if (string-match "\r?\n" answer) (if (string-match "\r?\n" answer)
(substring answer 0 (match-beginning 0)) (substring answer 0 (match-beginning 0))
answer))) answer)))
@ -574,7 +574,7 @@ This function knows about the special meaning of quotes (\")"
(defun dictionary-read-answer () (defun dictionary-read-answer ()
"Read an answer delimited by a . on a single line" "Read an answer delimited by a . on a single line"
(let ((answer (connection-read-to-point dictionary-connection)) (let ((answer (dictionary-connection-read-to-point dictionary-connection))
(start 0)) (start 0))
(while (string-match "\r\n" answer start) (while (string-match "\r\n" answer start)
(setq answer (replace-match "\n" t t answer)) (setq answer (replace-match "\n" t t answer))
@ -623,7 +623,7 @@ This function knows about the special meaning of quotes (\")"
"Read the first reply from server and check it." "Read the first reply from server and check it."
(let ((reply (dictionary-read-reply-and-split))) (let ((reply (dictionary-read-reply-and-split)))
(unless (dictionary-check-reply reply 220) (unless (dictionary-check-reply reply 220)
(connection-close dictionary-connection) (dictionary-connection-close dictionary-connection)
(error "Server returned: %s" (dictionary-reply reply))))) (error "Server returned: %s" (dictionary-reply reply)))))
;; Store the current state ;; Store the current state