-
Notifications
You must be signed in to change notification settings - Fork 9
/
stream.lisp
526 lines (410 loc) · 20.8 KB
/
stream.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
;;; -*- Package: de.setf.amqp.implementation; -*-
(in-package :de.setf.amqp.implementation)
(:documentation "This file defines a stream interface for AMQP channel and connection instances for the
'de.setf.amqp' library."
(copyright
"Copyright 2010 [james anderson](mailto:[email protected]) All Rights Reserved"
"'de.setf.amqp' is free software: you can redistribute it and/or modify it under the terms of version 3
of the GNU Affero General Public License as published by the Free Software Foundation.
'setf.amqp' 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 Affero General Public License for more details.
A copy of the GNU Affero General Public License should be included with 'de.setf.amqp' as `AMQP:agpl.txt`.
If not, see the GNU [site](http://www.gnu.org/licenses/).")
(long-description "The goal here is to hook the channel/connection stream operations into standard
stream operations. this file implements
- gray stream compatibility
- stream-ty* support
the original "gray streams" proposal source[1] has not been forthcoming, but there is a copy on the clozure
ftp site.[2] kmp has notes[3], corman lisp publishes an implementation[4], and clim discusses in interim gray
streams implementation[5], to be used in the interim, until a real implementation is available.
the stream uses a channel's the dual-channel-simple-stream device implementation to manage body length,
position and buffering together with the its operators for character coding. the interface
supports (vector (unsigned byte 8)) and string element types and (vector (unsigned byte 8))
i/o buffers. codecs are always present - even for ascii, and are used to implement unread-char.
the protocol is
on input:
buffer is the buffer
buf-len is the full buffer size
buffer-ptr is the length of read data, which is given by the frame payload size
buffpos is the position from which to take the next byte
body-position is that position wrt the entire body
body-length is the length of the entire body
the decoders maintain buffpos and body-position, and use the relation between buffpos
and buffer-ptr to refresh the buffer iff needed. if buff-pr is negative, that indicates that eof
has already been reached. the relation between body-position and body-size can indicate
the end of the current content, depends on whether the channel expects chunked content.
(see device-read)
on output:
out-buffer is the buffer
max-out-pos is the full buffer size
outpos is the position at which to put the next byte
body-position is that position wrt the entire body
body-length is the length of the entire body
the encoders maintian outpos and body-position and use the relation between outpos and
max-out-pos to flush the buffer iff needed. the relation between body-position and
body-size indicates the end of intended content and is used to effect chunking. (see device-write)
body-position and body-length can be multiplexed between input and output so long as the channels
remain half-duplex. the application must adhere to the single-channel per process constraint and
limit stream i/o to device-read/write-content or the respetive request-publish/get. should interleaved
input and output be required, the channel will need distinct state for each direction.
---
[1]: ftp://parcftp.xerox.com/pub/cl/cleanup/mail/stream-definition-by-user.mail
[2]: ftp://ftp.clozure.com/pub/stream-definition-by-user.mail
[3]: http://www.nhplace.com/kent/CL/Issues/stream-definition-by-user-notes.html
[4]: http://www.grumblesmurf.org/corman-patches/modules/gray-streams.lisp
[5]: http://www.cs.cmu.edu/afs/cs/project/clisp/hackers/phg/clim/src/utils/cl-streams.lisp"))
;;;
;;; input
(defun amqp-stream-read-char (stream)
"Read a character from an open channel on an amqp-device according to the channel's current encoding.
At EOF return the stream-eof-marker."
(with-slots (decoder) stream
(flet ((buffer-extract-byte (stream)
(with-slots (buffer buffpos buffer-ptr body-position body-length) stream
(when (or (< buffpos buffer-ptr)
(and (not (minusp buffer-ptr))
; resets buff-pos / renews buffer content unless eof
(plusp (device-read stream nil 0 nil t))))
(incf body-position)
(prog1 (aref buffer buffpos)
(incf buffpos))))))
(or (funcall decoder #'buffer-extract-byte stream) (stream-eof-marker stream)))))
(defmethod stream-read-char ((stream amqp-device))
(amqp-stream-read-char stream))
;;;!!! for multi-byte encodings the content-encoding needs to supply this
;;;!!! in order that the position is properly maintained.
;;;!!! it's also not clear what happens when this backs up over a frame boundary
(defmethod stream-unread-char ((stream amqp-device) char)
(with-slots (buffpos body-position) stream
(let ((old-decoder (device-decoder stream)))
(flet ((unread-decoder (byte-decoder stream)
(declare (ignore byte-decoder))
(with-slots (buffpos body-position) stream
(setf-device-decoder old-decoder stream)
(incf body-position)
(incf buffpos)
char)))
(decf body-position)
(decf buffpos)
(setf-device-decoder #'unread-decoder stream)
nil))))
(defmethod stream-read-char-no-hang ((stream amqp-device))
"If input is already available from an open channel on an amqp-device read the next character according
to the channel's current encoding. If none is available, return NIL. At EOF return the stream-eof-marker."
(with-slots (body-position body-length) stream
(if (< body-position body-length)
(when (stream-listen (stream-input-handle stream))
(stream-read-char stream))
(stream-eof-marker stream))))
(defmethod stream-peek-char ((stream amqp-device))
(with-slots (decoder) stream
;; cannot do this manipulating buffer position, as a multi-byte encoding
;; might cross a frame boundary
(flet ((buffer-extract-byte (stream)
(with-slots (buffer buffpos buffer-ptr body-position body-length) stream
(when (or (< buffpos buffer-ptr)
(and (not (minusp buffer-ptr))
; resets buff-pos / renews buffer content unless eof
(plusp (device-read stream nil 0 nil t))))
(incf body-position)
(prog1 (aref buffer buffpos)
(incf buffpos))))))
(let ((char (funcall decoder #'buffer-extract-byte stream)))
(cond (char
(stream-unread-char stream char)
char)
(t
(stream-eof-marker stream)))))))
(defmethod stream-listen ((stream amqp-device))
;; sbcl has no stream-listen for the input stream
(device-listen stream))
(defmethod stream-read-line ((stream amqp-device))
"Read a line of characters from an open channel on an amqp-device according to the channel's current encoding.
Returns those up to the next stream-eol-marker as a new string. Iff the line is terminated by EOF, returns
a second value, the stream-eof-marker."
(with-slots (decoder) stream
(let ((eol-char (stream-eol-marker stream))
(line (stream-line-buffer stream)))
(setf (fill-pointer line) 0)
(flet ((buffer-extract-byte (stream)
(with-slots (buffer buffpos buffer-ptr body-position body-length) stream
(when (or (< buffpos buffer-ptr)
(and (not (minusp buffer-ptr))
; resets buff-pos / renews buffer content unless eof
(plusp (device-read stream nil 0 nil t))))
(incf body-position)
(prog1 (aref buffer buffpos)
(incf buffpos))))))
(loop (let ((char (funcall decoder #'buffer-extract-byte stream)))
(cond ((eql char eol-char)
(return (copy-seq line)))
(char
(vector-push-extend char line))
(t
(return (values (copy-seq line) (stream-eof-marker stream)))))))))))
(defmethod stream-clear-input ((stream amqp-device))
"Clear any pending buffered input from the stream's device."
(device-clear-input stream t))
(defmethod stream-line-column ((stream amqp-device))
"Constantly nil."
nil)
(defmethod stream-start-line-p ((stream amqp-device))
"Constantly nil."
nil)
(defun amqp-stream-write-char (stream character)
(with-slots (encoder) stream
(flet ((buffer-insert-byte (stream byte)
(with-slots (out-buffer outpos max-out-pos body-position body-length encoder) stream
(setf (aref out-buffer outpos) byte)
(incf body-position)
(incf outpos)
(when (>= outpos max-out-pos)
;; resets outpos / flushes out-buffer content
(device-write stream nil 0 nil t)))))
(funcall encoder character #'buffer-insert-byte stream))
character))
(defmethod stream-write-char ((stream amqp-device) character)
"Write a character to an open channel on an amqp-device according to the channel's current encoding."
(amqp-stream-write-char stream character))
(defun amqp-stream-write-string (stream string start end)
(with-slots (encoder) stream
(flet ((buffer-insert-byte (stream byte)
(with-slots (out-buffer outpos max-out-pos body-position body-length) stream
(setf (aref out-buffer outpos) byte)
(incf body-position)
(incf outpos)
(when (>= outpos max-out-pos)
(device-write stream nil 0 nil t)))))
(unless start (setf start 0))
(unless end (setf end (length string)))
(do ((i start (1+ i)))
((>= i end))
(funcall encoder (char string i) #'buffer-insert-byte stream)))
string))
(defmethod stream-write-string ((stream amqp-device) string #-mcl &optional start end)
"Write a string to an open channel on an amqp-device according to the channel's current encoding."
(amqp-stream-write-string stream string start end))
(defmethod stream-terpri ((stream amqp-device))
(stream-write-char stream (stream-eol-marker stream)))
;; stream-fresh-line :
;; default suffices
(defmethod stream-finish-output ((stream amqp-device))
"Force output and delegate the finish to the output stream."
(stream-force-output stream)
(stream-finish-output (stream-output-handle stream)))
(defmethod stream-force-output ((stream amqp-device))
"Flush the current buffer, w/o checking for content. (see device-flush)"
(device-flush stream))
(defmethod stream-clear-output ((stream amqp-device))
"Discard anything in the present output buffer."
(with-slots (outpos body-position) stream
(when (plusp outpos)
;; back up in the global stream
(decf body-position outpos)
;; reset the current frame
(setf outpos 0))))
(defmethod stream-advance-to-column ((stream amqp-device) (column t))
nil)
;;; close :
;;; see extremely-simple-streams.lisp
#-mcl
(defmethod open-stream-p ((stream amqp-device))
"Return true iff the stream's channel is open."
;; This replicates the mcl definition, so that s single stream-direction suffices.
(not (eql (stream-direction stream) :closed)))
(defmethod stream-direction ((stream amqp-device))
"A device's direction depends on the state of its handles."
(if (stream-input-handle stream)
(if (stream-output-handle stream)
:io
:input)
(if (stream-output-handle stream)
:output
:closed)))
#+ccl
(defmethod stream-eofp ((stream amqp-device))
(with-slots (body-length body-position) stream
(>= body-position body-length)))
;; input-stream-p :
;; mcl version is not generic, it is based on direction, which suffices
;; output-stream-p :
;; mcl version is not generic, it is based on direction, which suffices
(defmethod stream-element-type ((stream amqp-device))
(device-element-type stream))
(defmethod stream-position ((stream amqp-device) &optional new)
(when (null new)
(device-file-position stream)))
#-lispworks
(defmethod stream-file-position ((stream amqp-device) &optional new)
(when (null new)
(device-file-position stream)))
#+lispworks
(defmethod stream-file-position ((stream amqp-device))
(device-file-position stream))
;; pathname : NYI
;; truename : NYI
(defun amqp-stream-read-byte (stream)
"Read a byte from an open channel on an amqp-device. Manage buffer positions and refresh
buffers from read frames as required. AAt EOF return the stream-eof-marker."
(with-slots (buffer buffpos buffer-ptr body-position) stream
(if (or (< buffpos buffer-ptr)
(and (not (minusp buffer-ptr))
; resets buff-pos / buffer content unless eof
(plusp (device-read stream nil 0 nil t))))
(prog1 (aref buffer buffpos)
(incf buffpos)
(incf body-position))
(stream-eof-marker stream))))
(defmethod stream-read-byte ((stream amqp-device))
(amqp-stream-read-byte stream))
(defun amqp-stream-write-byte (stream byte)
"Write a byte to an open channel on an amqp-device. Add the byte at the current buffer position.
If either the buffer is full, or the stream length is reached, write the buffer."
(with-slots (out-buffer outpos max-out-pos body-position body-length) stream
(setf (aref out-buffer outpos) byte)
(incf body-position)
(incf outpos)
(when (>= outpos max-out-pos)
(device-write stream nil 0 nil t))
byte))
(defmethod stream-write-byte ((stream amqp-device) byte)
(amqp-stream-write-byte stream byte))
(defmethod stream-read-sequence
#+ccl ((stream amqp-device) (sequence vector) &key (start 0) end)
#+lispworks ((stream amqp-device) (sequence vector) start end)
#-(or ccl lispworks) ((stream amqp-device) (sequence vector) #-ccl &optional (start 0) end)
"Read a byte sequence from an open channel on an amqp-device. Invokes device-read to read and transfer data
directly via the device frame buffers."
(setf end (or end (length sequence)))
(let ((count (device-read stream sequence start end nil)))
(when (plusp count) (+ start count))))
(defmethod stream-read-sequence
#+ccl ((stream amqp-device) (sequence string) &key (start 0) end)
#+lispworks ((stream amqp-device) (sequence string) start end)
#-(or ccl lispworks) ((stream amqp-device) (sequence string) #-ccl &optional (start 0) end)
"Read a character sequence from an open channel on an amqp-device. Arrange to read bytes from the device buffer,
construct characters, and return the the next position. Iff the first byte read shows eof, return nil."
(setf end (or end (length sequence)))
(with-slots (decoder) stream
(flet ((buffer-extract-byte (stream)
(with-slots (buffer buffpos buffer-ptr body-position) stream
(when (or (< buffpos buffer-ptr)
(and (not (minusp buffer-ptr))
; resets buff-pos / buffer content unless eof
(plusp (device-read stream nil 0 nil t))))
(prog1 (aref buffer buffpos)
(incf body-position)
(incf buffpos))))))
(if (> end start)
(let ((char (funcall decoder #'buffer-extract-byte stream)))
(when char
(setf (char sequence start) char)
(do ((i (1+ start) (1+ i)))
((>= i end) end)
(if (setf char (funcall decoder #'buffer-extract-byte stream))
(setf (char sequence i) char)
(return i)))))
end))))
(defmethod stream-write-sequence
#+ccl ((stream amqp-device) (sequence vector) &key (start 0) end)
#+lispworks ((stream amqp-device) (sequence vector) start end)
#-(or ccl lispworks) ((stream amqp-device) (sequence vector) #-ccl &optional (start 0) end)
"Write a byte sequence to an open channel on an amqp-device. Invokes device-read to transfer data write
directly via the device frame buffers."
(setf end (or end (length sequence)))
(device-write stream sequence start end 0))
(defmethod stream-write-sequence
#+ccl ((stream amqp-device) (sequence string) &key (start 0) end)
#+lispworks ((stream amqp-device) (sequence string) start end)
#-(or ccl lispworks) ((stream amqp-device) (sequence string) #-ccl &optional (start 0) end)
"Write a character sequence from an open channel on an amqp-device. Arrange to read bytes from the device buffer,
construct characters, and return the the next position."
(setf end (or end (length sequence)))
(stream-write-string stream sequence start end))
#+clozure
(progn
(defmethod ccl:stream-read-vector ((stream amqp-device) (sequence vector) start end)
(let ((count (device-read stream sequence (or start 0) (or end (length sequence)) nil)))
(when (plusp count) (+ start count))))
(defmethod ccl:stream-read-vector ((stream amqp-device) (sequence string) start end)
(stream-read-sequence stream sequence :start (or start 0) :end (or end (length sequence))))
(defmethod ccl:stream-write-vector ((stream amqp-device) (sequence vector) start end)
(device-write stream sequence (or start 0) (or end (length sequence)) t))
(defmethod ccl:stream-write-vector ((stream amqp-device) (sequence string) start end)
(stream-write-sequence stream sequence :start (or start 0) :end (or end (length sequence)))))
(defmethod stream-tyo ((stream amqp-device) character)
;; (stream-tyo *trace-output* character)
(stream-write-char stream character))
(defmethod stream-tyi ((stream amqp-device))
(let ((char (stream-read-char stream)))
(typecase char
(character char)
(t nil))))
(defmethod stream-untyi ((stream amqp-device) character)
(stream-unread-char stream character))
;;;
;;; fu interface
(defmethod device-allocate-buffer ((stream amqp-device) &key
(length (device-buffer-length stream))
(initial-contents nil))
;; the description does not make this obvious, but it
;; makes sense for thi initial contents to be in the application domain
;; - that is, decoded
(let ((new-buffer (make-frame-buffer length)))
(when initial-contents
(assert (= (length initial-contents) length) ()
"Inconsistent lengths: ~d, ~d" length (length initial-contents))
(with-slots (out-buffer outpos max-out-pos body-length body-position) stream
(let ((.out-buffer out-buffer)
(.outpos outpos)
(.max-out-pos max-out-pos)
(.body-length body-length)
(.body-position body-position))
(unwind-protect
(progn (setf out-buffer new-buffer
outpos 0
max-out-pos (1+ length) ; prevent writing
body-length (1+ length)
body-position 0)
(stream-write-sequence stream initial-contents))
(setf out-buffer .out-buffer
outpos .outpos
max-out-pos .max-out-pos
body-length .body-length
body-position .body-position)))))
new-buffer))
(defmethod device-input-element-type ((stream amqp-device))
;; one only
(device-element-type stream))
(defmethod device-output-element-type ((stream amqp-device))
;; one only
(device-element-type stream))
(defmethod device-encoded-length ((stream amqp-device) buffer &optional start end)
(declare (ignore buffer start end))
nil)
;;;
;;; the description does not make this obvious, but it
;;; [http://paste.lisp.org/display/65229], and the names do imply that
;;; the buffers are in the device domain - that is the content is encoded
;;; thus device-write rather than stream-write-sequence
(defmethod device-write-buffers ((stream amqp-device) &rest buffer-specs)
(loop for (buffer start end) in buffer-specs by #'cddr
do (device-write stream buffer start end t)))
(defmethod device-read-buffers ((stream amqp-device) &rest buffer-specs)
(loop for (buffer start end) in buffer-specs by #'cddr
do (device-read stream buffer start end t)))
;;;
;;; stream-reader/writer
(defmethod stream-reader ((stream amqp-device))
(typecase (amqp.u:channel-content-type stream)
(mime:binary
(values #'amqp-stream-read-byte stream))
(t
(values #'amqp-stream-read-char stream))))
(defmethod stream-writer ((stream amqp-device))
(typecase (amqp.u:channel-content-type stream)
(mime:binary
(values #'amqp-stream-write-byte stream))
(t
(values #'amqp-stream-write-char stream))))