-
Notifications
You must be signed in to change notification settings - Fork 9
/
device-level.lisp
1521 lines (1374 loc) · 81.7 KB
/
device-level.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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; -*- Package: de.setf.amqp.implementation; -*-
(in-package :de.setf.amqp.implementation)
(:documentation "This file implements device-level support for streams based on AMQP connections as part of 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 "Device-level operations are managed by channel instances, which delegete in turn to
connection-wrapped tcp sockets. The AMQP standards permit this combination to behave in either of (at
least) two ways. On one hand, one can use all allowed protocoal variability and treat the socket
connection as a multiplexed multi-channel stream. In this mode each thread would instantiate its own
channel on the shared connection. On the other hand, is also possible to constrain the connection's use
to dedicate it to a single channel - and thereby to a single thread.
The multiplexed mechanism sets a much lower demand for system resources, but increases the processing
for a single channel and constrains the i/o operations to block mode. A dedicated mechanims requires
more sockets, but can dedicate the buffering to a single thread, which permits more efficient i/o
operations.
When the socket is multiplexed, then it is possible that content frames for one channel will be
interleaved with frames for another channel, which means it is not possible to guarantee that a given
socket read operation will contain an expected content frame. The multiplexing requires per-channel
queuing and processing upon delivery, whereby the actual buffer is not to be predicted. When the socket
is dedicated, read operatation can target a given buffer, since the intra-channel ordering constraints
require that content be delivered in-order and uninterrupted. The protocol meta-data can be read and
processed in-line ro parameterize the read and recognize exceptions, while data i/o targets given
buffers. The 1.0pr documentintroduces additional session and context for frame processing, but those are
not considered here. (see amqp0-8.pdf,p.56 amqp0-9.pdf,p.35)
In the implementation below, the two amqp classes - connection and channel, are each specialized from
simple-stream and implement the standard interface. The required device operators are implemented for
both amqp:channel and amqp:connection, despite that stream operations are limited to channels while
connections are to be used only to consolidate frame-based operations with the network stream. The
principle operators are
device-open (stream #-sbcl slots initargs)
device-close (stream abort)
device-read (stream buffer start end blocking)
device-clear-input (stream buffer-only)
device-write (stream buffer start end blocking)
device-clear-output (stream)
device-flush (device)
device-read-content (device &rest content-arguments)
device-write-content (device body &rest content-arguments)
The device-open operator returns an instance which can be used directly. In the case of a channel, it
connects to the respective connection and prepares any exchange/queue specified in uri initialization
argument. In that state it can be supplied to publish, consume, or get operations to initiate stream
i/o, or to transfer isolated objects. In the case of a connection, a socket stream is opened to the
designated server and the protocol handshake and open-connection phases are completed. (see
open-connection and negotiate-client-connection) In that state, one can construct and use channels. In the
case of a connection, it should be used only to make channels. Note that the connection class is adjusted to
match that of the protocol version which it negotiates with the remote broker. That is, a
(make-instance 'amqp:connection :uri \"amqp://guest:guest@localhost/\")
will return a connection sub-class. This places constraints on the effective methods for `device-open` and the
various constituents of the standard instantiation protocol for `simple-stream`.
The respective effective methods for the base implementation look something like
initialize-instance : (CONNECTION) (STANDARD-OBJECT)
shared-initialize (in mcl) : (CONNECTION T) (AMQP-DEVICE T) :AFTER (SIMPLE-STREAM T) (STANDARD-OBJECT T)
reinitialize-instance : (STANDARD-OBJECT)
device-open (in sbcl) : (CONNECTION T) (AMQP-SOCKET-DEVICE T) (AMQP-DEVICE T) (SIMPLE-STREAM T) (SLOT-OBJECT T)
The exact form depends on the run-time (cf. `standard-object` v/s `sloto-object`), but all share the topology,
that no next method call follows the class change. Should protocol-specific specialization be necessary, any
specialized operation subsequent to the change would need to be independent of these effective methods.
The device-read and device-write operators are defined at the respective levels to delegate a channel's
operations to the respective connection and to perform connection i/o through the socket stream. When
channel operations with an explicit buffer, are intended to implement data stream-based data transfer
for a channel which has already initiated a message exchange and observe the body size constraints
specified fro the message. Where the size is known at the outset, the body-size and body-position govern
eof behaviour. Where the size was unkown, the channel implemented chunked transfer in terms of maxmial
sized messages, and relies on device-flush and device-clear-input to indicate and manage the effective
eof.
The channels position operators track the position during stream read/write operations only. They combine
the frame position with the current buffer position to yield the byte offset in the effective stream.
The device-read/write-content operators reset the position and subsequent body frame read/write operators
accumulate the last frame's buffer size.
For connections, the frame position accumulates the start position of all frames (including header frames)
as they are read and written over the connection, but neither accounts for partial buffer lengths nor
distinguishes between header and body frames. (see read/write-frame)
The position operators have no effect on channel state.
For more information on simple streams, see Franz's documentation[3] and the sbcl implementation[4] of same,
as well as the discussions of the the alternative fu interface.[5]
---
[1]: https://jira.amqp.org/confluence/download/attachments/720900/amqp0-8.pdf?version=1
[2]: https://jira.amqp.org/confluence/download/attachments/720900/amqp0-9-1.pdf?version=1
[3]: http://www.franz.com/support/documentation/current/doc/streams.htm
[4]: http://sbcl.cvs.sourceforge.net/viewvc/sbcl/sbcl/contrib/sb-simple-streams/
[5]: http://paste.lisp.org/display/65229"))
(defmacro assert-device-state (device state &optional op)
(let ((state (or (find-symbol (string state) :amqp.s)
(error "invalid state indicator: ~s." state)))
(device-state-var (gensym)))
`(let ((,device-state-var (device-state ,device)))
(assert (typep ,device-state-var ',state) ()
"~@[~a: ~]Required device state ~a is not satisfied by ~a."
',op ',state ,device-state-var))))
;;;
;;; channel operations
(defmethod device-open ((device amqp:channel) #-sbcl (slots t) initargs)
"Prepare a channel by binding it locally to a connection and declaring its
exchange and queue on the connection's server. The connection may be provided
as an instance to be re-used, or as a server designator, in which case a
news connection is opened."
(etypecase (device-state device)
(amqp.s:open-channel
(if (or (stream-input-handle device)
(stream-output-handle device))
(call-next-method)
(destructuring-bind (&key (connection (or (channel-connection device)
(error "connection required.")))
(input-handle connection)
(output-handle connection)
&allow-other-keys)
initargs
(call-next-method)
(setf (stream-input-handle device) input-handle)
(setf (stream-output-handle device) output-handle)
;; once that has been done, adjust the channel class
(let ((connection-channel-class (class-find-object-class connection 'amqp:channel)))
(unless (typep device connection-channel-class)
(change-class device connection-channel-class)))
;; bind the channel to the connection to obtain the queues and initialize buffers
(connect-channel connection device)
;; unless it's the connection's own channel,
;; open the channel w/ the broker
(unless (zerop (channel-number device))
;; resolve the channel's identifer relative to the connection - with
;; non-strict handling to allow a scheme.
(let* (;; (uri (merge-uris (channel-uri device) (connection-uri connection) nil nil))
;; don't merge again here. iinitialize-instance already does it to the initialization argument
;; and the channel should never move from one connection to another.
(uri (channel-uri device))
(host (uri-host uri)))
(setf-device-uri uri device)
;; if the connection is 'for real', that is, if it specifies a host, open the channel
(unless (zerop (length host))
(amqp:open device)
#+(or ) ;; don't do this here:
;; the 0.8r0 channel specializes device-open to get a ticket, which would need to
;; happen before this, as the ticket is an argument.
;; could go in initialize instance
(let ((exchange (uri-exchange uri))
(queue (uri-queue uri))
(object-path (uri-path uri))
(cond (exchange
(let ((queue (amqp:channel.queue device :queue queue))
(exchange (amqp:channel.exchange device :exchange exchange :type "direct")))
(amqp:declare queue)
(amqp:declare exchange)
(amqp:bind queue :exchange exchange :queue queue
:routing-key object-path)))
(queue
;; if there is no exchange, allow input only
(assert (eq (stream-direction device) :input) ()
"An exchange must be provided for channel output.")
(amqp:declare (amqp:channel.queue device :queue queue)))))))))
(setf (device-state device) amqp.s:use-channel)
device)))
(amqp.s:use-channel
(call-next-method))))
(defmethod device-close ((device amqp:channel) (abort t))
"remove the channel from the connection."
(amqp:log :debug device "Close in state: ~s" (channel-state device))
(if (zerop (channel-number device))
(amqp:log :warn device "Attempt to close channel zero.")
(when (open-stream-p device)
(cond (abort
(setf (channel-state device) amqp.s:close-channel)
(call-next-method))
(t
(let ((initial-state (shiftf (channel-state device) amqp.s:close-channel)))
(typecase initial-state
;; if in use, send the close request, the flush it
(amqp.s:use-channel
(amqp:request-close device)
;; complete and flush the content.
(device-flush device t)))
(call-next-method))))
;; in any case disconnect
(disconnect-channel (channel-connection device) device))))
#+zero-frame-eoc-marker
(defmethod device-read ((device amqp:channel) buffer-arg start end blocking)
"Channels read a frame at a time through a connection.
the connection manages the actual stream and makes frames available as
they appear. the specified 'blocking' mode determines whether to
wait if there is nothing present."
(assert-device-state device use-channel.body.input device-read)
(with-slots (buffer buffpos buffer-ptr buf-len body-position body-length) device
(cond ((< buffer-ptr 0)
-1)
((eql start end) ; interpret blocking
(if blocking
;; nothing is read anyway
0
;; iff not blocking , see if anything is present or in the read queue
(if (or (< buffpos buffer-ptr) (not (collection-empty-p (device-read-frames device))))
-3
0)))
((>= body-position body-length)
(typecase (device-state device)
(amqp.s:use-channel.body.input.chunked
;; chunked => start the next message
;; - if the last was a deliver, wait for the next
;; - if it was a get-ok, ask for the next
(command-case (device)
((or amqp:get-ok amqp:deliver) ((basic amqp:basic) &rest args)
(amqp:log :debug device "chunk continuation: (~s ~s) . ~s"
(type-of amqp:method) (type-of basic) args)
t)
(t ((class t) &rest args)
(amqp:log :error device "Unexpected chunk continuation: (~s ~s) . ~s"
(type-of amqp:method) (type-of class) args)
t))
(let ((result (device-read-content-header device)))
(amqp:log :debug device "read chunk header: ~s" result))
;; if that succeeds, read the
(if (< body-position body-length)
;; just try again
(device-read device buffer-arg start end blocking)
-1))
(t
; not chunked => mark eof
(setf buffer-ptr -1))))
(buffer-arg
;; if a buffer is provided, use it+bounds together with the devices buffer+bounds
;; to iteratively fill the argument buffer. recurse for more input
;; maintain body-position since the buffer is accepting the content.
(let ((total-count 0)
(last-device-read 0))
(unless end (setf end (length buffer-arg)))
(unless start (setf start 0))
(loop (unless (> start end) (return))
(when (>= buffpos buffer-ptr)
(unless (and (< body-position body-length)
(plusp (setf last-device-read (device-read device nil 0 nil blocking))))
(return)))
(let* ((count (min (- end start) (- buffer-ptr buffpos)))
(end1 (+ start count))
(end2 (+ buffpos count)))
(replace buffer-arg buffer :start1 start :end1 end1 :start2 buffpos :end2 end2)
(setf start end1
buffpos end2)
(incf total-count count)
(incf body-position count)))
(if (zerop total-count)
last-device-read
total-count)))
(t
;; otherwise read a frame buffer
(assert (and (zerop start) (or (null end) (= end (length buffer)))) ()
"Frame buffer i/o permitted for entire buffers only.")
(let* ((frame nil))
(loop (setf frame (get-read-frame device :wait blocking))
(amqp:log :debug device "device-read: next read frame: state: ~a, size: ~d, body: ~s/~s, buffer: ~s/~s/~s."
(type-of (device-state device)) (frame-size frame) body-position body-length buffpos buffer-ptr buf-len)
;; if non-blocking, maybe no frame
(if frame
(cond ((plusp (frame-size frame))
(let* ((data (frame-data frame))
(length (frame-size frame)))
(rotatef buffer data)
(setf-frame-data data frame)
(release-frame frame)
(incf (device-frame-position device) buffpos)
(setf buffpos 0)
(setf buffer-ptr length)
(setf buf-len (length buffer)) ; could change iff possible to re-tune
(assert (<= buffer-ptr buf-len) ()
"Invalid buffer sizes: ptr ~d, len ~d." buffer-ptr buf-len)
(amqp:log :debug device "device-read: adjusted pointers: state: ~a, body: ~s/~s, buffer: ~s/~s/~s."
(type-of (device-state device)) body-position body-length buffpos buffer-ptr buf-len)
(return length)))
(t
;; if the frame is a zero-length frame, always skip it
(release-frame frame)
;; iff chunking, also end chunking, indicate end of the body and
;; skip next padding frame as well.
(typecase (device-state device)
(amqp.s:use-channel.body.input.chunked
(amqp:log :debug device "device-read: skip zero chunk: state: ~a, body: ~s/~s, buffer: ~s/~s/~s."
(type-of (device-state device)) body-position body-length buffpos buffer-ptr buf-len)
(setf (device-state device) amqp.s:use-channel.body.input)
(cond ((setf frame (get-read-frame device :wait t)) ; resolve it now
(assert (and (eq (frame-type-class-name frame) 'amqp:body)
(= (frame-size frame) (- body-length body-position))) ()
"Invalid pad frame: ~s." frame)
(setf body-length body-position) ; 'truncate' the body
(release-frame frame)
(return (setf buffer-ptr -1)))
(t
(return (setf buffer-ptr -1))))))))
(return (when blocking (setf buffer-ptr -1))))))))))
#-zero-frame-eoc-marker
(defmethod device-read ((device amqp:channel) buffer-arg start end blocking)
"Channels read a frame at a time through a connection.
the connection manages the actual stream and makes frames available as
they appear. the specified 'blocking' mode determines whether to
wait if there is nothing present."
(assert-device-state device use-channel.body.input device-read)
(with-slots (buffer buffpos buffer-ptr buf-len body-position body-length) device
(cond ((< buffer-ptr 0)
-1)
((eql start end) ; interpret blocking
(if blocking
;; nothing is read anyway
0
;; iff not blocking , see if anything is present or in the read queue
(if (or (< buffpos buffer-ptr) (not (collection-empty-p (device-read-frames device))))
-3
0)))
((>= body-position body-length)
(typecase (device-state device)
(amqp.s:use-channel.body.input.chunked
;; chunked => start the next message
;; - if the last was a deliver, wait for the next
;; - if it was a get-ok, ask for the next
(command-case (device)
((or amqp:get-ok amqp:deliver) ((basic amqp:basic) &rest args)
(amqp:log :debug device "chunk continuation: (~s ~s) . ~s"
(type-of amqp:method) (type-of basic) args)
t)
(t ((class t) &rest args)
(amqp:log :error device "Unexpected chunk continuation: (~s ~s) . ~s"
(type-of amqp:method) (type-of class) args)
t))
(let ((basic (device-read-content-header device)))
(amqp:log :debug device "read chunk header: ~s" basic)
(cond ((string-equal (getf (amqp:basic-headers basic) :transfer-encoding) "chunked")
;; if that succeeded, try again
(if (< body-position body-length)
(device-read device buffer-arg start end blocking)
(setf buffer-ptr -1)))
(t
;; no more chunks
(setf (device-state device) amqp.s:use-channel.body.input)
(setf buffer-ptr -1)))))
(t
; not chunked => mark eof
(setf buffer-ptr -1))))
(buffer-arg
;; if a buffer is provided, use it+bounds together with the devices buffer+bounds
;; to iteratively fill the argument buffer. recurse for more input
;; maintain body-position since the buffer is accepting the content.
(let ((total-count 0)
(last-device-read 0))
(unless end (setf end (length buffer-arg)))
(unless start (setf start 0))
(loop (unless (> start end) (return))
(when (>= buffpos buffer-ptr)
(unless (and (< body-position body-length)
(plusp (setf last-device-read (device-read device nil 0 nil blocking))))
(return)))
(let* ((count (min (- end start) (- buffer-ptr buffpos)))
(end1 (+ start count))
(end2 (+ buffpos count)))
(replace buffer-arg buffer :start1 start :end1 end1 :start2 buffpos :end2 end2)
(setf start end1
buffpos end2)
(incf total-count count)
(incf body-position count)))
(if (zerop total-count)
last-device-read
total-count)))
(t
;; otherwise read a frame buffer
(assert (and (zerop start) (or (null end) (= end (length buffer)))) ()
"Frame buffer i/o permitted for entire buffers only.")
(let* ((frame nil))
(loop (setf frame (get-read-frame device :wait blocking))
(amqp:log :debug device "device-read: next read frame: state: ~a, size: ~d, body: ~s/~s, buffer: ~s/~s/~s."
(type-of (device-state device)) (frame-size frame) body-position body-length buffpos buffer-ptr buf-len)
;; if non-blocking, maybe no frame
(if frame
(cond ((not (eq (frame-type-class-name frame) 'amqp:body))
(unget-read-frame device frame)
(return (setf buffer-ptr -1)))
((plusp (frame-size frame))
(let* ((data (frame-data frame))
(length (frame-size frame)))
(rotatef buffer data)
(setf-frame-data data frame)
(release-frame frame)
(incf (device-frame-position device) buffpos)
(setf buffpos 0)
(setf buffer-ptr length)
(setf buf-len (length buffer)) ; could change iff possible to re-tune
(assert (<= buffer-ptr buf-len) ()
"Invalid buffer sizes: ptr ~d, len ~d." buffer-ptr buf-len)
(amqp:log :debug device "device-read: adjusted pointers: state: ~a, body: ~s/~s, buffer: ~s/~s/~s."
(type-of (device-state device)) body-position body-length buffpos buffer-ptr buf-len)
(return length)))
(t
;; if the frame is a zero-length frame, skip it
(release-frame frame)))
(return (when blocking (setf buffer-ptr -1))))))))))
(defmethod device-write ((device amqp:channel) buffer-arg start end (blocking t))
"Channels write a frame at a time through a connection.
The connection manages the actual stream and writes frames as required. The
specified 'blocking' mode has no affect on output."
(assert-device-state device use-channel.body device-write)
(with-slots (out-buffer outpos max-out-pos) device
(cond ((vectorp buffer-arg)
;; if a buffer is provided, use it+bounds together with the devices buffer+bounds
;; to iteratively empty the argument buffer. recurse for progressive output
(let ((total-count 0))
(loop (when (>= start end)
(return))
(let* ((count (min (- end start) (- max-out-pos outpos)))
(source-start start) (source-end (+ source-start count))
(buffer-start outpos) (buffer-end (+ outpos count)))
(setf start source-end
outpos buffer-end)
(when (> (+ (device-body-position device) count)
(device-body-length device))
(error "DEVICE-WRITE: output exceeds content length: ~s, ~s."
(+ (device-body-position device) count)
(device-body-length device)))
(replace out-buffer buffer-arg :start1 buffer-start :end1 buffer-end :start2 source-start :end2 source-end)
(incf total-count count)
(if (>= outpos max-out-pos)
;; flush the buffer
(let ((result (device-write device nil 0 nil blocking)))
(when (minusp result)
(return-from device-write result)))
(incf (device-body-position device) count))))
total-count))
(buffer-arg ; sbcl calls w/ :flush do it only if there is output
(when (plusp outpos)
(device-flush device)))
(t
(assert (and (zerop start) (or (null end) (= end (length out-buffer)))) ()
"Frame buffer i/o permitted for entire buffers only.")
(device-flush device)))))
#+zero-frame-eoc-marker
(defmethod device-flush ((device amqp:channel) &optional (complete nil))
"Push data to the channel's connection.
DEVICE : amqp:channel : an open channel
COMPLETE : boolean : iff true, an in-progress chunked body stream is closed.
Given a buffer, its content is passed through the channel's
frame buffers. Lacking a buffer, the exisitng frame buffer is sent. The effect is to support
single-frame commands, sequence-based stream io, and buffer based stream io. The device state is checked to
confirm an operation makes sense.
On framed content, streams, and chunking:
there are three aspects:
* is there buffered content: (zerop outpos) ?
* is the content complete?
* was the content length known ahead of time: .output or .output.chunked ?
if there is content, wrap up the body frame and send it.
given the now empty buffer, if the content body is now complete, then it matters whether the length was
predetermined. if it was, that is, in state .output, since this is not an abort, sufficient
frames must be sent to achieve the content length. if the length was not predetermined, then the
state is .output.chunked and the end of the sequence of commands is indicated by sending a
zero-length body frame and then a frame padded to fill the content length. should this have been an
empty buffer, then the zero-length frame will be followed by a full length pad."
(let ((result-length 0))
(typecase (device-state device)
(amqp.s:use-channel.body.output
(with-slots (out-buffer outpos max-out-pos body-position body-length) device
(amqp:log :debug device "device-flush (~a) ~d, ~d, ~d"
(device-state device) body-length body-position max-out-pos)
(flet ((flush-frame ()
(let* ((frame (claim-output-frame device))
(length outpos))
(rotatef out-buffer (frame-data frame))
(setf-frame-type-class-name 'amqp:body frame)
(setf-frame-cycle 0 frame)
(setf-frame-channel-number (channel-number device) frame)
(setf-frame-track-number (channel-track device) frame)
(setf-frame-size outpos frame)
;; update start of the next frame -
;; nb. here v/s when frames are written as only body/content frames contribute
(incf (device-frame-position device) outpos)
(put-encoded-frame device frame)
(setf outpos 0)
(setf max-out-pos (length out-buffer))
length)))
;; check whether there is anything to flush
(when (or (plusp outpos) ) ; (zerop body-length)) ; always at least one frame
;; if there is content, send the frame out.
(setf result-length (flush-frame)))
;; check completion
(cond (complete
(amqp:log :debug device "Complete flush in state ~s." (device-state device))
(typecase (device-state device)
(amqp.s:use-channel.body.output.chunked
;; if the content was chunked, send a zero-length frame and revert to .output
(amqp:log :debug device "Ending chunking. padding ~d v/s ~d bytes..."
(- body-length body-position) max-out-pos)
(flush-frame)
(setf (device-state device) amqp.s:use-channel.body.output)))
;; now send frames to fill the difference between content-position and
;; content-length - as many as needed
(do ((count (min (- body-length body-position) max-out-pos)
(min (- body-length body-position) max-out-pos)))
((<= count 0))
(fill out-buffer 0 :start 0 :end count)
(setf outpos count)
(flush-frame)
(incf body-position count))
(amqp:log :debug device "Ended padding."))
(t
(typecase (device-state device)
(amqp.s:use-channel.body.output.chunked
;; if so, need to send a new command:
;; send a new publish, reset the buffer positions, and continue streaming
(let ((basic (channel.basic device)))
(amqp:log :debug basic "Starting next chunk: publish...")
(amqp:send-publish basic :exchange (amqp:basic-exchange basic))
(setf outpos 0
max-out-pos (length out-buffer)
;; start a new body
body-length (class-body-size basic))
(setf body-position 0)
(amqp:log :debug basic "Starting next chunk: header...")
(send-header basic)
(amqp:log :debug basic "Starting next chunk: done."))))))
result-length))))))
#-zero-frame-eoc-marker
(defmethod device-flush ((device amqp:channel) &optional (complete nil))
"Push data to the channel's connection.
DEVICE : amqp:channel : an open channel
COMPLETE : boolean : iff true, this is the final flush on a stream of messsages. if the channel was
sending chunks, send an additional final marker message.
Given a buffer, its content is passed through the channel's
frame buffers. Lacking a buffer, the exisitng frame buffer is sent. The effect is to support
single-frame commands, sequence-based stream io, and buffer based stream io. The device state is checked to
confirm an operation makes sense.
On framed content, streams, and chunking:
there are three aspects:
* is there buffered content: (zerop outpos) ?
* is the content complete?
* was the content length known ahead of time: .output or .output.chunked ?
if there is content, wrap up the body frame and send it.
given the now empty buffer, if the content body is now complete, then it matters whether the length was
predetermined. if it was, that is, in state .output (which includes .output.chunked), since this is not an
abort, sufficient frames must be sent to achieve the content length.if the state is .output.chunked, the end
of the sequence of commands is indicated by sending an additional zero-length message."
(let ((result-length 0)
(basic (channel.basic device)))
(typecase (device-state device)
(amqp.s:use-channel.body.output
(with-slots (out-buffer outpos max-out-pos body-position body-length) device
(amqp:log :debug device "device-flush (~a/~d) ~d/~d, ~d/~d"
(device-state device) (class-body-size basic)
body-position body-length outpos max-out-pos
)
(flet ((flush-frame ()
(let* ((frame (claim-output-frame device))
(length outpos))
;; update start of the next frame -
;; nb. here v/s when frames are written as only body/content frames contribute
(incf (device-frame-position device) outpos)
(rotatef out-buffer (frame-data frame))
(setf-frame-type-class-name 'amqp:body frame)
(setf-frame-cycle 0 frame)
(setf-frame-channel-number (channel-number device) frame)
(setf-frame-track-number (channel-track device) frame)
(setf-frame-size outpos frame)
(put-encoded-frame device frame)
(amqp:log :debug device "flushed frame: ~a" frame)
(setf outpos 0)
(setf max-out-pos (length out-buffer))
length)))
(cond (complete
(amqp:log :debug device "device-flush, complete (~a/~d) ~d/~d, ~d/~d"
(device-state device) (class-body-size basic)
body-position body-length outpos max-out-pos)
(setf result-length outpos)
(let ((pad-length (max 0 (min (- body-length body-position) (- max-out-pos outpos)))))
(when (> pad-length 0)
;; pad the current frame
(fill out-buffer 0 :start outpos :end (+ outpos pad-length))
(incf body-position pad-length)
(incf outpos pad-length)
(amqp:log :debug device "device-flush, complete, padded frame to ~d, ~d" body-position outpos)))
(when (or (plusp outpos) ) ;; (zerop body-length))
;; if there is content, send the frame out.
;; if there is none, always send at least one frame
(flush-frame))
;; send any additional frames to fill the difference between body-position and body-length
(do ((count (min (- body-length body-position) max-out-pos)
(min (- body-length body-position) max-out-pos))
(first t nil))
((<= count 0))
(amqp:log :debug device "device-flush, additional pad frame (~a/~d) ~d/~d, ~d/~d"
(device-state device) (class-body-size basic)
body-position body-length outpos max-out-pos)
(when first (fill out-buffer 0 :start 0 :end count))
(setf outpos count)
(flush-frame)
(incf body-position count))
(amqp:log :debug device "device-flush, complete after any padding (~a/~d) ~d/~d, ~d/~d"
(device-state device) (class-body-size basic)
body-position body-length outpos max-out-pos)
(typecase (device-state device)
(amqp.s:use-channel.body.output.chunked
;; if the content was chunked, send non-chunked termination message
(setf (device-state device) amqp.s:use-channel.body.output)
(let* ((headers (copy-list (amqp:basic-headers basic))))
(remf headers :transfer-encoding)
(amqp:log :debug basic "Starting end non-chunk: publish...")
(amqp:publish device :body ""
:exchange (amqp:basic-exchange basic)
:routing-key (amqp:basic-routing-key basic)
:headers headers))
#+(or)
(let* ((headers (amqp:basic-headers basic)))
(remf headers :transfer-encoding)
(setf (amqp:basic-headers basic) headers)
;; set up for a zero-length message
(setf outpos 0
max-out-pos 0
body-length 0
body-position 0)
(setf (class-body-size basic) 0)
;; (setf (aref out-buffer 0) 0)
(amqp:log :debug basic "Starting end non-chunk: publish...")
(amqp:send-publish basic :exchange (amqp:basic-exchange basic))
(amqp:log :debug basic "Sending EOC message: header...")
(send-header basic)
;; rabbitmq balks on the zero-length body frame
(amqp:log :debug basic "Sending EOC message: zero frame...")
;;(flush-frame)
))))
(t
;; if the output is to continue,
;; - flush the frame
;; - interpose a new message if chunking
;; check whether there is anything to flush
(setf result-length outpos)
(when (or (plusp outpos) ) ;; (zerop body-length))
;; if there is content, send the frame out.
;; if there is none, always send at least one frame
(flush-frame))
(typecase (device-state device)
(amqp.s:use-channel.body.output.chunked
;; if so, need to send a new command:
;; send a new publish, reset the buffer positions, and continue streaming
(let ()
(amqp:log :debug basic "Starting next chunk: publish...")
(amqp:send-publish basic :exchange (amqp:basic-exchange basic))
(setf outpos 0
max-out-pos (length out-buffer)
;; start a new body
body-length (class-body-size basic))
(setf body-position 0)
(amqp:log :debug basic "Starting next chunk: header...")
(send-header basic)
(amqp:log :debug basic "Starting next chunk: done."))))))
result-length))))))
#-zero-frame-eoc-marker ;; with lazy method/header writes
(defmethod device-flush ((device amqp:channel) &optional (complete nil))
"Push data to the channel's connection.
DEVICE : amqp:channel : an open channel
COMPLETE : boolean : iff true, this is the final flush on a stream of messsages. if the channel was
sending chunks, send an additional final marker message.
Given a buffer, its content is passed through the channel's
frame buffers. Lacking a buffer, the exisitng frame buffer is sent. The effect is to support
single-frame commands, sequence-based stream io, and buffer based stream io. The device state is checked to
confirm an operation makes sense.
On framed content, streams, and chunking:
there are three aspects:
* is there buffered content: (zerop outpos) ?
* is the content complete?
* was the content length known ahead of time: .output or .output.chunked ?
if there is content, wrap up the body frame and send it.
given the now empty buffer, if the content body is now complete, then it matters whether the length was
predetermined. if it was, that is, in state .output (which includes .output.chunked), since this is not an
abort, sufficient frames must be sent to achieve the content length.if the state is .output.chunked, the end
of the sequence of commands is indicated by sending an additional zero-length message."
(let ((result-length 0)
(basic (channel.basic device)))
(typecase (device-state device)
(amqp.s:use-channel.body.output
(with-slots (out-buffer outpos max-out-pos body-position body-length) device
(amqp:log :debug device "device-flush (~a/~d) ~d/~d, ~d/~d"
(device-state device) (class-body-size basic)
body-position body-length outpos max-out-pos
)
(flet ((flush-frame ()
(let* ((frame (claim-output-frame device))
(length outpos))
;; update start of the next frame -
;; nb. here v/s when frames are written as only body/content frames contribute
(incf (device-frame-position device) outpos)
(rotatef out-buffer (frame-data frame))
(setf-frame-type-class-name 'amqp:body frame)
(setf-frame-cycle 0 frame)
(setf-frame-channel-number (channel-number device) frame)
(setf-frame-track-number (channel-track device) frame)
(setf-frame-size outpos frame)
(put-encoded-frame device frame)
(amqp:log :debug device "flushed frame: ~a" frame)
(setf outpos 0)
(setf max-out-pos (length out-buffer))
length))
(write-method-and-header ()
;; write the initial header - also the final one if complete
(amqp:log :debug basic "Sending method/header on demand...")
(amqp::send-publish basic)
(send-header basic)))
(when (<= body-position max-out-pos)
;; if this is the first frame, write a header w/o chunking
(when complete
(setf body-length body-position
(class-body-size basic) body-position)
(setf (device-state device) amqp.s:use-channel.body.output)
(remf (amqp:basic-headers basic) :transfer-encoding))
(write-method-and-header))
(cond (complete
(amqp:log :debug device "device-flush, complete (~a/~d) ~d/~d, ~d/~d"
(device-state device) (class-body-size basic)
body-position body-length outpos max-out-pos)
(setf result-length outpos)
(let ((pad-length (max 0 (min (- body-length body-position) (- max-out-pos outpos)))))
(when (> pad-length 0)
;; pad the current frame
(fill out-buffer 0 :start outpos :end (+ outpos pad-length))
(incf body-position pad-length)
(incf outpos pad-length)
(amqp:log :debug device "device-flush, complete, padded frame to ~d, ~d" body-position outpos)))
(when (or (plusp outpos) ) ;; (zerop body-length))
;; if there is content, send the frame out.
;; if there is none, (no !!!always send at least one frame)
(flush-frame))
;; send any additional frames to fill the difference between body-position and body-length
(do ((count (min (- body-length body-position) max-out-pos)
(min (- body-length body-position) max-out-pos))
(first t nil))
((<= count 0))
(amqp:log :debug device "device-flush, additional pad frame (~a/~d) ~d/~d, ~d/~d"
(device-state device) (class-body-size basic)
body-position body-length outpos max-out-pos)
(when first (fill out-buffer 0 :start 0 :end count))
(setf outpos count)
(flush-frame)
(incf body-position count))
(amqp:log :debug device "device-flush, complete after any padding (~a/~d) ~d/~d, ~d/~d"
(device-state device) (class-body-size basic)
body-position body-length outpos max-out-pos)
(typecase (device-state device)
(amqp.s:use-channel.body.output.chunked
;; if the content was chunked, and the header has already been sent,
;; send a non-chunked termination message
(setf (device-state device) amqp.s:use-channel.body.output)
(let* ((headers (amqp:basic-headers basic)))
(remf headers :transfer-encoding)
(setf (amqp:basic-headers basic) headers)
;; set up for a zero-length message
(setf outpos 0
max-out-pos 0
body-length 0
body-position 0)
(setf (class-body-size basic) 0)
;; (setf (aref out-buffer 0) 0)
(amqp:log :debug basic "Starting end non-chunk: publish...")
(amqp:send-publish basic :exchange (amqp:basic-exchange basic))
(amqp:log :debug basic "Sending EOC message: header...")
(send-header basic)
;; rabbitmq balks on the zero-length body frame
(amqp:log :debug basic "Sending EOC message: zero frame...")
(flush-frame))))
;; force output on the base stream
(device-flush (channel-connection device))
;; return the channel to a passive state
(setf (channel-state device) amqp.s:use-channel))
(t
;; if the output is to continue, flush the frame and
;; reset counts
(setf result-length outpos)
(when (or (plusp outpos) (zerop body-length))
;; if there is content, send the frame out.
;; if there is none, always send at least one frame
(flush-frame))
(setf body-position 0)))
result-length))))))
(defmethod device-clear-input ((device amqp:channel) buffer-only)
;;; call the decoder to clear a possible pushed character and correct position
;;; then "empty" the buffer.
;;; then, unless buffer-only, also flush any not yet read frames until the end of the body is reached
(with-slots (decoder buffer buffpos buffer-ptr buf-len body-position body-length) device
(when decoder ; maybe not present for binary streams
(funcall decoder #'(lambda (s) (declare (ignore s)) 0) device))
;; skip over anything already in the buffer
(amqp:log :debug device "device-clear-input: skip current-frame: state: ~a, body: ~s/~s, buffer: ~s/~s/~s."
(type-of (device-state device)) body-position body-length buffpos buffer-ptr buf-len)
(setf body-position (+ body-position (- buffer-ptr buffpos)))
(setf buffpos buffer-ptr)
;; optionally drain pending input
(unless buffer-only
;; flush input
(loop
(amqp:log :debug device "device-clear-input: drain expected frames: state: ~a, at ~s of ~s"
(device-state device) body-position body-length)
(when (>= body-position body-length)
(return))
(unless (plusp (device-read device nil 0 nil t))
(return))
(incf body-position buffer-ptr)))
nil))
(defmethod device-buffer-length ((device amqp:channel))
(let ((connection (channel-connection device)))
(if connection
(device-buffer-length connection)
0)))
(defmethod (setf device-file-position) (position (device amqp:channel))
(declare (ignore position))
(device-body-length device))
(defmethod device-file-length ((device amqp:channel))
(device-body-length device))
(defmethod (setf device-file-length) (length (device amqp:channel))
(declare (ignore length))
nil)
;;;
;;; connection operations
(defmethod device-open ((device amqp:connection) #-sbcl (slots t) initargs)
"Prepare a connection by opening a socket to broker, negotiating the
protocol parameter, and opening the virutal host."
(typecase (device-state device)
(amqp.s:open-connection
(if (or (stream-input-handle device)
(stream-output-handle device))
(call-next-method)
(destructuring-bind (&key (uri (connection-uri device))
(version (class-protocol-version device))
(direction :io)
&allow-other-keys)
initargs
;; merge the host and port information from/to the uri
(let ((remote-host (uri-host uri))
(remote-port (or (uri-port uri) *standard-port*)))
(ecase direction
(:probe (call-next-method device #-sbcl slots
(list* :remote-host remote-host
:remote-port remote-port
:direction :probe
initargs)))
(:io (when (call-next-method device #-sbcl slots
(list* :remote-host remote-host
:remote-port remote-port
:direction :io
initargs))
(when (open-connection device :version version)
;; once the concrete class is fixed, initialize the buffer
(device-initialize-buffers device)
(negotiate-client-connection device)
t))))))))
(amqp.s:use-connection
(call-next-method))
(t
;; have observed it called from shared-initialize to update an obsolete instance
;; when terminating it in for gc !!
nil)))
(defmethod device-close ((device amqp:connection) (abort t))
(map nil #'(lambda (c)
(when (and c (plusp (channel-number c)))
(device-close c abort)))
(get-connection-channels device))
(if abort
(call-next-method)
(typecase (shiftf (device-state device) amqp.s:close-connection)
;; never succeeded to open
(amqp.s:open-connection
(call-next-method))
;; if it's in use, perform a protocol close, then flush that data
;; close the stream, and reset to the initial state
(amqp.s:use-connection
(amqp:request-close device)
(device-flush device t)
(multiple-value-prog1 (call-next-method)
(setf (connection-state device) amqp.s:open-connection)))
;; otherwise, the protocol close is already in progress;
;; just flush and close the stream
(t
(device-flush device t)
(call-next-method)))))
(defmethod device-read ((device amqp:connection) buffer-arg start end (blocking t))
"Connections permit stream io only if there is just one channel read a frame at a time through a connection.
the connection manages the actual stream and makes frames available as
they appear. the specified 'blocking' mode determines whether to
wait of there is noting present."
(assert-device-state device use-connection amqp.s:use-channel.body.input)
(with-slots (buffer buffpos buffer-ptr buf-len) device
(if (or (< buffer-ptr 0)
(>= (device-body-position device) (device-body-length device)))
-1
(cond (buffer-arg
;; if a buffer is provided, use it+bounds together with the devices buffer+bounds
;; to iteratively fill the argument buffer. recurse for more input
(let ((total-count 0))
(loop (let* ((count (min (- end start) (- buffer-ptr buffpos)))
(start1 start) (end1 (+ start1 count))
(start2 buffpos) (end2 (+ buffpos count)))
(setf start end1
buffpos end2)
(replace buffer-arg buffer :start1 start1 :end1 end1 :start2 start2 :end2 end2)
(incf total-count count)
(when (>= start end)
(incf (device-body-position device) count)
(return))
;; read more
(let ((result (device-read device nil 0 nil blocking)))
(when (minusp result)
(return-from device-read result)))))
total-count))
(t
;; otherwise read a frame buffer
(assert (and (zerop start) (null end)) ()
"Frame buffer i/o permitted for entire buffers only.")
(loop (let ((frame (get-read-frame device :wait blocking)))
(if frame
(when (plusp (length frame))
(let* ((data (frame-data frame))
(length (frame-size frame)))
(rotatef buffer data)
(setf-frame-data data frame)