Initial Commit
[packages] / xemacs-packages / dictionary / connection.el
1 ;;; connection.el -- handling a tcp based connection
2
3 ;; Author: Torsten Hilbrich <dictionary@myrkr.in-berlin.de>
4 ;; Keywords: network
5 ;; $Id: connection.el,v 1.9 2001-11-24 12:18:26 torsten Exp $
6
7 ;; This file is free software; you can redistribute it and/or modify
8 ;; it under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation; either version 2, or (at your option)
10 ;; any later version.
11
12 ;; This file is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 ;; GNU General Public License for more details.
16
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING.  If not, write to
19 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 ;; Boston, MA 02111-1307, USA.
21
22 (eval-when-compile
23   (require 'cl))
24
25 (defmacro connection-p (connection)
26   "Returns non-nil if `connection' is a connection object"
27   (list 'get connection ''connection))
28
29 (defmacro connection-read-point (connection)
30   "Return the read point of the connection object."
31   (list 'get connection ''connection-read-point))
32
33 (defmacro connection-process (connection)
34   "Return the process of the connection object."
35   (list 'get connection ''connection-process))
36
37 (defmacro connection-buffer (connection)
38   "Return the buffer of the connection object."
39   (list 'get connection ''connection-buffer))
40
41 (defmacro connection-set-read-point (connection point)
42   "Set the read-point for `connection' to `point'."
43   (list 'put connection ''connection-read-point point))
44
45 (defmacro connection-set-process (connection process)
46   "Set the process for `connection' to `process'."
47   (list 'put connection ''connection-process process))
48
49 (defmacro connection-set-buffer (connection buffer)
50   "Set the buffer for `connection' to `buffer'."
51   (list 'put connection ''connection-buffer buffer))
52
53 (defun connection-create-data (buffer process point)
54   "Create a new connection data based on `buffer', `process', and `point'."
55   (let ((connection (make-symbol "connection")))
56     (put connection 'connection t)
57     (connection-set-read-point connection point)
58     (connection-set-process connection process)
59     (connection-set-buffer connection buffer)
60     connection))
61
62 (defun connection-open (server port)
63   "Open a connection to `server' and `port'.
64 A data structure identifing the connection is returned"
65
66   (let ((process-buffer (generate-new-buffer (format " connection to %s:%s"
67                                                      server
68                                                      port)))
69         (process))
70     (save-excursion
71       (set-buffer process-buffer)
72       (setq process (open-network-stream "connection" process-buffer
73                                          server port))
74       (connection-create-data process-buffer process (point-min)))))
75
76 (defun connection-status (connection)
77   "Return the status of the connection.
78 Possible return values are the symbols:
79 nil: argument is no connection object
80 'none: argument has no connection
81 'up: connection is open and buffer is existing
82 'down: connection is closed
83 'alone: connection is not associated with a buffer"
84   (if (connection-p connection)
85       (let ((process (connection-process connection))
86             (buffer (connection-buffer connection)))
87         (if (not process)
88             'none
89           (if (not (buffer-live-p buffer))
90               'alone
91             (if (not (eq (process-status process) 'open))
92                 'down
93               'up))))
94     nil))
95
96 (defun connection-close (connection)
97   "Force closing of the connection."
98   (if (connection-p connection)
99       (progn
100         (let ((buffer (connection-buffer connection))
101               (process (connection-process connection)))
102           (if process
103               (delete-process process))
104           (if buffer
105               (kill-buffer buffer))
106           
107           (connection-set-process connection nil)
108           (connection-set-buffer connection nil)))))
109
110 (defun connection-send (connection data)
111   "Send `data' to the process."
112   (unless (eq (connection-status connection) 'up)
113     (error "Connection is not up"))
114   (save-excursion
115     (set-buffer (connection-buffer connection))
116     (goto-char (point-max))
117     (connection-set-read-point connection (point))
118     (process-send-string (connection-process connection) data)))
119
120 (defun connection-send-crlf (connection data)
121   "Send `data' together with CRLF to the process."
122   (connection-send connection (concat data "\r\n")))
123
124 (defun connection-read (connection delimiter)
125   "Read data until `delimiter' is found inside the buffer."
126   (unless (eq (connection-status connection) 'up)
127     (error "Connection is not up"))
128   (let ((case-fold-search nil)
129         match-end)
130     (save-excursion
131       (set-buffer (connection-buffer connection))
132       (goto-char (connection-read-point connection))
133       ;; Wait until there is enough data 
134       (while (not (search-forward-regexp delimiter nil t))
135         (accept-process-output (connection-process connection) 3)
136         (goto-char (connection-read-point connection)))
137       (setq match-end (point))
138       ;; Return the result
139       (let ((result (buffer-substring (connection-read-point connection)
140                                       match-end)))
141         (connection-set-read-point connection match-end)
142         result))))
143
144 (defun connection-read-crlf (connection)
145   "Read until a line is completedx with CRLF"
146   (connection-read connection "\015?\012"))
147
148 (defun connection-read-to-point (connection)
149   "Read until a line is consisting of a single point"
150   (connection-read connection "\015?\012[.]\015?\012"))
151
152 (provide 'connection)