-
Notifications
You must be signed in to change notification settings - Fork 7
/
protocol.lisp
1291 lines (1071 loc) · 59 KB
/
protocol.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
;;; -*- Mode: lisp; Syntax: ansi-common-lisp; Base: 10; Package: org.apache.thrift.implementation; -*-
(in-package :org.apache.thrift.implementation)
;;; This file defines the abstract '`protocol` layer for the `org.apache.thrift` library.
;;;
;;; copyright 2010 [james anderson]([email protected])
;;;
;;; Licensed to the Apache Software Foundation (ASF) under one
;;; or more contributor license agreements. See the NOTICE file
;;; distributed with this work for additional information
;;; regarding copyright ownership. The ASF licenses this file
;;; to you under the Apache License, Version 2.0 (the
;;; "License"); you may not use this file except in compliance
;;; with the License. You may obtain a copy of the License at
;;;
;;; http://www.apache.org/licenses/LICENSE-2.0
;;;
;;; Unless required by applicable law or agreed to in writing,
;;; software distributed under the License is distributed on an
;;; "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
;;; KIND, either express or implied. See the License for the
;;; specific language governing permissions and limitations
;;; under the License.
;;; The protocol class is the abstract root for comminucation protocol implementations.
;;; It is specialized for each message structure
;;;
;;; protocol
;;; - encoded-protocol
;;; - binary-protocol (see binary-protocol.lisp)
;;;
;;; The abstract class determines the abstract representation of message components in terms of
;;; and arrangement of Thrift data types. Each concrete protocol class implements the codec for
;;; base data types in terms of signed bytes and unsigned byte sequences. It then delegates to
;;; its input/output transports to decode and encode that data in terms of the transport's
;;; representation.
;;; nb. there is a bnf, protocols are observed to eliminate and/or reorder fields at will. whatever.
;;; The stream interface operators are implemented in two forms. A generic interface is specialized
;;; by protocol and/or actual data argument type. In addition a compiler-macro complement performs
;;; compile-time in-line codec expansion when the data type is statically specified. As Thrift
;;; requires all types to be declared statically, this compiles IDL files to in-line codecs.
;;;
;;; Type comparisons - both at compile-time and as run-time validation, are according to nominal equality.
;;; As the Thrift type system permits no sub-typing, primtive types are a finite set and the struct/exception
;;; classes permit no super-types.
;;; The only variation would be to to permit integer subtypes for integer container elements, eg i08 sent
;;; where i32 was declared, but that would matter only if supporting a compact protocol.
;;;
;;; Names exists in two domains:
;;; - An 'identifier' is a string. They are used when the package is unknown, which is the situation at
;;; the start of a message. After that, a package is imputed from the association between a found message
;;; and its service context
;;; - A 'name' uis a symbol. These are interned into a services 'namespace' when the idl is compiled.
;;; These interned names are compiled onto request/response functions and the struct codecs.
;;; When messages are read, the respective service's namespace package applies to intern identifiers
;;; to match them against decoding type constraints.
;;;
;;; interface
(defgeneric stream-read-type (protocol))
(defgeneric stream-read-message-type (protocol))
(defgeneric stream-read-bool (protocol))
(defgeneric stream-read-i08 (protocol))
(defgeneric stream-read-i16 (protocol))
(defgeneric stream-read-i32 (protocol))
(defgeneric stream-read-i64 (protocol))
(defgeneric stream-read-double (protocol))
(defgeneric stream-read-string (protocol))
(defgeneric stream-read-binary (protocol))
(defgeneric stream-read-message-begin (protocol))
(defgeneric stream-read-message (protocol))
(defgeneric stream-read-message-end (protocol))
(defgeneric stream-read-struct-begin (protocol))
(defgeneric stream-read-struct (protocol &optional type))
(defgeneric stream-read-struct-end (protocol))
(defgeneric stream-read-field-begin (protocol))
(defgeneric stream-read-field (protocol &optional type))
(defgeneric stream-read-field-end (protocol))
(defgeneric stream-read-map-begin (protocol))
(defgeneric stream-read-map (protocol &optional key-type value-type))
(defgeneric stream-read-map-end (protocol))
(defgeneric stream-read-list-begin (protocol))
(defgeneric stream-read-list (protocol &optional type))
(defgeneric stream-read-list-end (protocol))
(defgeneric stream-read-set-begin (protocol))
(defgeneric stream-read-set (protocol &optional type))
(defgeneric stream-read-set-end (protocol))
(defgeneric stream-write-type (protocol type-name))
(defgeneric stream-write-message-type (protocol type-name))
(defgeneric stream-write-bool (protocol value))
(defgeneric stream-write-i08 (protocol value))
(defgeneric stream-write-i16 (protocol value))
(defgeneric stream-write-i32 (protocol value))
(defgeneric stream-write-i64 (protocol value))
(defgeneric stream-write-double (protocol value))
#+digitool ;;digitools stream-write-string signature requires four arguments. leave it to be shadowed
(defgeneric stream-write-string (protocol value &optional start end))
(defgeneric stream-write-binary (protocol value))
(defgeneric stream-write-message-begin (protocol identifier type seq))
(defgeneric stream-write-message (protocol struct message-type &key type sequence-number))
(defgeneric stream-write-message-end (protocol))
(defgeneric stream-write-struct-begin (protocol identifier))
(defgeneric stream-write-struct (protocol value &optional type))
(defgeneric stream-write-struct-end (protocol))
(defgeneric stream-write-field-begin (protocol identifier-name type identifier-number))
(defgeneric stream-write-field (protocol value &key identifier-name identifier-number type))
(defgeneric stream-write-field-end (protocol))
(defgeneric stream-write-field-stop (protocol))
(defgeneric stream-write-map-begin (protocol key-type value-type size))
(defgeneric stream-write-map (protocol value &optional key-type value-type))
(defgeneric stream-write-map-end (protocol))
(defgeneric stream-write-list-begin (protocol etype size))
(defgeneric stream-write-list (protocol value &optional type))
(defgeneric stream-write-list-end (protocol))
(defgeneric stream-write-set-begin (protocol etype size))
(defgeneric stream-write-set (protocol value &optional type))
(defgeneric stream-write-set-end (protocol))
;;;
;;; macros
;;; nb. this does not interact at all nicely with redefined macros.
(defmacro trace-macro (&rest names)
`(progn ,@(loop for name in names collect `(%trace-macro ',name))))
(defmacro untrace-macro (&rest names)
`(progn ,@(loop for name in names collect `(%untrace-macro ',name))))
(defun %trace-macro (name)
(let ((function nil))
(flet ((make-traced-expander (name function)
#'(lambda (form environment)
(let ((expansion (funcall function form environment)))
(unless (eq form expansion)
(%format-macro-trace name form expansion))
expansion))))
(cond ((or (get name 'macro-function) (get name 'compiler-macro-function))
name)
((setf function (macro-function name))
(setf (get name 'macro-function) function)
(setf (macro-function name) (make-traced-expander name function))
name)
((setf function (compiler-macro-function name))
(setf (get name 'compiler-macro-function) function)
(setf (compiler-macro-function name) (make-traced-expander name function))
name)
(t
(error "~a has no macro definition." name))))))
(defun %untrace-macro (name)
(let ((function nil))
(cond ((setf function (get name 'macro-function))
(setf (macro-function name) function)
(setf (get name 'macro-function) nil)
name)
((setf function (get name 'compiler-macro-function))
(setf (compiler-macro-function name) function)
(setf (get name 'compiler-macro-function) nil)
name)
(t
nil))))
(defun %format-macro-trace (name form expansion)
(declare (ignore name))
(format *trace-output* "~&~%~:w~%==>~%~:w" form expansion))
(defmacro expand-iff-constant-types (type-variables form &body body)
"Used in the codec compiler macros to conditionalize expansion on constant types."
`(cond ((and ,@(loop for tv in type-variables collect `(typep ,tv '(cons (eql quote)))))
,@(loop for tv in type-variables collect `(setf ,tv (second , tv)))
,@body)
(t
,form)))
#+digitool (setf (ccl:assq 'expand-iff-constant-types ccl:*fred-special-indent-alist*) 2)
;;;
;;; classes
(defclass protocol (#+ccl stream #+sbcl sb-gray:fundamental-stream)
((input-transport
:initform (error "transport is required.") :initarg :input-transport :initarg :transport
:reader protocol-input-transport)
(output-transport
:initform (error "transport is required.") :initarg :output-transport :initarg :transport
:reader protocol-output-transport)
(direction
:initform (error "direction is required.") :initarg :direction
:reader stream-direction)
(version-id :initarg :version-id :reader protocol-version-id)
(version-number :initarg :version-number :reader protocol-version-number)
(sequence-number :initform 0 :accessor protocol-sequence-number)
(field-id-mode :initarg :field-key :reader protocol-field-id-mode
:type (member :identifier-number :identifier-name))
(struct-id-mode :initarg :struct-id-mode :reader protocol-struct-id-mode
:type (member :identifier-name :none))))
(defclass encoded-protocol (protocol)
((string-encoder :initarg :string-encoder :reader transport-string-encoder)
(string-decoder :initarg :string-decoder :reader transport-string-decoder))
(:default-initargs :charset :utf8))
;;;
;;; protocol operators
(defmethod initialize-instance ((protocol encoded-protocol) &rest initargs &key (charset nil))
(declare (dynamic-extent initargs))
(multiple-value-bind (decoder encoder)
(ecase charset
((nil) (values #'(lambda (string) (map 'vector #'char-code string))
#'(lambda (bytes) (map 'string #'code-char bytes))))
(:utf8 (values #'trivial-utf-8:utf-8-bytes-to-string
#'trivial-utf-8:string-to-utf-8-bytes)))
(apply #'call-next-method protocol
:string-encoder encoder
:string-decoder decoder
initargs)))
#-mcl ;; mcl defines a plain function in terms of stream-direction
(defmethod open-stream-p ((protocol protocol))
(with-slots (input-transport output-transport) protocol
(or (open-stream-p input-transport)
(open-stream-p output-transport))))
(defun protocol-close (protocol &key abort)
"The protocol close implementation is used by whichever interface the runtime presents for extensions.
as per the gray interface, close is replaced with a generic function. in other cases, stream-close
is a generic operator."
(with-slots (input-transport output-transport stream-direction) protocol
(when (open-stream-p protocol)
(close input-transport :abort abort)
(close output-transport :abort abort)
(setf (slot-value protocol 'direction) :closed))))
(when (fboundp 'stream-close)
(defmethod stream-close ((protocol protocol))
(when (next-method-p) (call-next-method))
(protocol-close protocol)))
(when (typep #'close 'generic-function)
(defmethod close ((stream protocol) &rest args)
(when (next-method-p) (call-next-method))
(apply #'protocol-close stream args)))
(defgeneric protocol-version (protocol)
(:method ((protocol protocol))
(cons (protocol-version-id protocol) (protocol-version-number protocol))))
(defgeneric protocol-find-thrift-class (protocol name)
(:method ((protocol protocol) (name string))
(or (find-thrift-class (str-sym name) nil)
(class-not-found protocol name))))
(defgeneric protocol-next-sequence-number (protocol)
(:method ((protocol protocol))
(incf (protocol-sequence-number protocol))))
(defmethod stream-position ((protocol protocol) &optional new-position)
(if new-position
(stream-position (protocol-input-transport protocol) new-position)
(stream-position (protocol-input-transport protocol))))
;;;
;;; type code <-> name operators are specific to each protocol
(defgeneric type-code-name (protocol code)
)
(defgeneric type-name-code (protocol name)
)
(defgeneric message-type-code (protocol message-name)
)
(defgeneric message-type-name (protocol type-code)
)
;;;
;;; input implementation
;;; nb. defined in this sequence to ensure compile-macro presence is whether loading one or
;;; reloading while developing
(defmethod stream-read-field-begin ((protocol protocol))
"Read the field header as per protocol schema and encoding.
PROTOCOL : protocol
VALUES = symbol : the field name
= i16 : the field id number
= symbol : the field type
The protocol's field-id-mode determines which id form to expect.
:identifier-number : decodes a type, and unless the type is STOP a subsequent id number
:identifier-name : decodes an identifier name and a type."
(let ((type nil)
(id-number 0)
(identifier nil))
(ecase (protocol-field-id-mode protocol)
(:identifier-number
(setf type (stream-read-type protocol))
(unless (eq type 'stop)
(setf id-number (stream-read-i16 protocol))))
(:identifier-name
(setf identifier (str-sym (stream-read-string protocol))
;; NB the bnf is broke here, as it says "T_STOP | <field_name> <field_type> <field_id>",
;; by which there is no way to distinguish the count field of a string from the stop code
;; so perhaps this is excluded for a binary protocol and works only if the
;; protocol's field are themselves self-describing
type (stream-read-type protocol))))
(values identifier id-number type)))
(defmethod stream-read-field-end ((protocol protocol))
"The base method does nothing.")
(defmethod stream-read-field((protocol protocol) &optional type)
"Read a typed field as per protocol scheme and encoding.
PROTOCOL : protocol
VALUES = t : the decoded field value
= symbol : the field name
= i16 : the field identifier number"
(multiple-value-bind (identifier idnr read-type)
(stream-read-field-begin protocol)
(if (eq read-type 'stop)
(values nil nil 0 'stop)
;; constraint the read type to match the expected if one is provided.
;; allow for the ambiguity between string and binary types as there is just one type code
(let ((field-value (cond ((and (eq type 'binary) (eq read-type 'string))
(stream-read-binary protocol))
((or (null type) (equal (type-category type) read-type))
(stream-read-value-as protocol read-type))
(t
;; signal an error, but use whatever it returns
(invalid-field-type protocol nil idnr identifier type
(stream-read-value-as protocol read-type))))))
(stream-read-field-end protocol)
(values field-value identifier idnr)))))
;;; a compiler macro would find no use, since the macro expansion for reading a struct already
;;; incorporates dispatches on field id to an inline-able call stream-read-value-as, while stream-read-field
;;; never itself knows the type at compile time.
(defmethod stream-read-struct-begin ((protocol protocol))
(ecase (protocol-struct-id-mode protocol)
(:identifier-name
(let ((name (stream-read-string protocol)))
(protocol-find-thrift-class protocol name)))
(:none
nil)))
(defmethod stream-read-struct-end ((protocol protocol)))
(defmethod stream-read-struct ((protocol protocol) &optional expected-type)
"Interpret an encoded structure as either an expcetion or a struct depending on the specified class.
Decode each field in turn. When decoding exceptions, build the initargs list and construct it as the
last step. Otherwise allocate an instacen and bind each value in succession.
Should the field fail to correspond to a known slot, delegate unknown-field to the class for a field
defintion. If it supplies none, then resort to the class."
;; Were it slot classes only, a better protocol would be (setf slot-value-using-class), but that does not
;; apply to exceptions. Given both cases, this is coded to stay symmetric.
(let* ((class (stream-read-struct-begin protocol))
(type (when class (struct-name class))))
(when expected-type
(if type
(unless (eq type expected-type)
(invalid-struct-type protocol expected-type type))
(setf type expected-type
class (find-thrift-class expected-type))))
(cond ((null type) ; anonymous struct
(let ((struct ()))
(loop (multiple-value-bind (value name id field-type)
(stream-read-field protocol)
(cond ((eq field-type 'stop)
(stream-read-struct-end protocol)
(return (nreverse struct)))
(t
(setf struct (acons (or name id) value struct))))))))
((subtypep type 'condition)
;; allocation-instance and setf slot-value) are not standard for conditions
;; if class-slots (as required by class-field-definitions) is not defined, this will need changes
(let ((initargs ())
(fields (class-field-definitions class))
(fd nil))
(loop (multiple-value-bind (value name id field-type)
(stream-read-field protocol)
(cond ((eq field-type 'stop)
(stream-read-struct-end protocol)
(return (apply #'make-condition type initargs)))
((setf fd (or (find id fields :key #'field-definition-identifier-number :test #'eql)
(unknown-field class id name field-type value)))
(setf (getf initargs (field-definition-initarg fd)) value))
(t
(unknown-field protocol id name field-type value)))))))
(t
(let* ((instance (allocate-instance class))
(fields (class-field-definitions class))
(fd nil))
(loop (multiple-value-bind (value name id field-type)
(stream-read-field protocol)
(cond ((eq field-type 'stop)
(stream-read-struct-end protocol)
(return instance))
((setf fd (or (find id fields :key #'field-definition-identifier-number :test #'eql)
(unknown-field class id name field-type value)))
(setf (slot-value instance (field-definition-name fd))
value))
(t
(unknown-field protocol id name field-type value))))))))))
(define-compiler-macro stream-read-struct (&whole form prot &optional type instance &environment env)
"Iff the type is a constant, compile the decoder inline. If class is not defined, signal an error.
The intended use is to compile IDL files, for which the code generator and the definition macros
arrange that structure definitions preceed references."
(expand-iff-constant-types (type) form
(with-gensyms (expected-class)
(with-optional-gensyms (prot) env
(with-gensyms (initargs)
(let* ((class (find-thrift-class type))
(field-definitions (class-field-definitions class))
(struct (gensym)))
(if (subtypep type 'condition)
`(let ((,initargs nil)
(,expected-class (find-thrift-class ',type)))
,(generate-struct-decoder prot expected-class
(loop for fd in field-definitions
collect `((getf ,initargs ',(field-definition-initarg fd)) nil
:id ,(field-definition-identifier-number fd)
:type ,(field-definition-type fd)))
initargs)
(apply #'make-struct ',type ,initargs))
`(let* ((,initargs nil)
(,expected-class (find-thrift-class ',type))
(,struct ,(if instance instance `(allocate-instance ,expected-class))))
,(generate-struct-decoder prot expected-class
(loop for fd in field-definitions
collect `((slot-value ,struct ',(field-definition-name fd)) nil
:id ,(field-definition-identifier-number fd)
:type ,(field-definition-type fd)))
initargs)
(when ,initargs
(apply #'reinitialize-instance ,struct ,initargs))
,struct))))))))
#+(or) ; alternative version with make instance
(define-compiler-macro stream-read-struct (&whole form prot &optional type &environment env)
"Iff the type is a constant, compile the decoder inline. If class is not defined, signal an error.
The intended use is to compile IDL files, for which the code generator and the definition macros
arrange that structure definitions preceed references."
(expand-iff-constant-types (type) form
(with-gensyms (expected-class)
(with-optional-gensyms (prot) env
(with-gensyms (extra-initargs)
(let* ((class (find-thrift-class type))
(field-definitions (class-field-definitions class)))
`(let (,@(loop for fd in field-definitions
collect (list (field-definition-name fd) nil))
(,extra-initargs nil)
(,expected-class (find-thrift-class ',type)))
,(generate-struct-decoder prot expected-class field-definitions extra-initargs)
(apply #'make-struct ,expected-class
,@(loop for fd in field-definitions
nconc (list (field-definition-initarg fd) (field-definition-name fd)))
,extra-initargs))))))))
(defmethod stream-read-message-begin ((protocol protocol))
"Read a message header strictly.
PROTOCOL : protocol
VALUES = string : the message identifier name
= symbol : the message type
= i32 : sequence number
A backwards compatible implementation would read the entire I32, in order
to use a value w/o the #x80000000 tag bit as a string length, but that is not necessary when reading strictly.
The jira [issue](https://issues.apache.org/jira/browse/THRIFT-254) indicates that all implementions write
strict by default, and that a 'next' release should treat non-strict messages as bugs.
This version recognizes the layout established by the compact protocol, whereby the first byte is the
protocol id and subsequent to that is specific to the protocol."
(let* ((id (logand (stream-read-i08 protocol) #xff)) ; actually unsigned
(ver (logand (stream-read-i08 protocol) #xff)) ; actually unsigned
(type-name (stream-read-message-type protocol)))
(unless (and (= (protocol-version-id protocol) id) (= (protocol-version-number protocol) ver))
(invalid-protocol-version protocol id ver))
(let ((name (stream-read-string protocol))
(sequence (stream-read-i32 protocol)))
(values name type-name sequence))))
(defmethod stream-read-message ((protocol protocol))
"Perform a generic 'read' of a complete message.
PROTOCOL : protocol
VALUES : symbol : the message identifer interned in the current package
= i32 : the sequence number
= symbol : the message type (eg, call, reply)
= thrift-object : the message body object
This is here for dynamic processing and testing only. Service requests/reponses messages are not first-class
entities. The request/response operators interpret message content on-the-fly, as side-effects against
specially tailored lexical contexts. A response, eg, is a 'struct' in which the '0:success' field carries
the (single) result and other fields capture exceptions. The expansions for def-request-method and
def-response-method generate the codecs to interpret/generate the data stream without an intermediate
reified object."
(multiple-value-bind (message-identifier type sequence)
(stream-read-message-begin protocol)
(let* ((message-name (str-sym message-identifier))
(body (stream-read-struct protocol message-name)))
(stream-read-message-end protocol)
(values message-name type sequence body))))
(defmethod stream-read-message-end ((protocol protocol)))
(defmethod stream-read-map-begin ((protocol protocol))
; t_key t_val size
(values (stream-read-type protocol)
(stream-read-type protocol)
(stream-read-i32 protocol)))
(defmethod stream-read-map-end ((protocol protocol)))
(defmethod stream-read-map((protocol protocol) &optional key-type value-type)
(let ((map ()))
(multiple-value-bind (read-key-type read-value-type size) (stream-read-map-begin protocol)
(unless (or (null key-type) (equal read-key-type (type-category key-type)))
(invalid-element-type protocol 'thrift:map key-type read-key-type))
(unless (or (null value-type) (equal read-value-type (type-category value-type)))
(invalid-element-type protocol 'thrift:map value-type read-value-type))
(unless (typep size 'field-size)
(invalid-field-size protocol 0 "" 'field-size size))
(dotimes (i size)
;; no type check - presume the respective reader is correct.
(setf map (acons (stream-read-value-as protocol read-key-type)
(stream-read-value-as protocol read-value-type)
map)))
(stream-read-map-end protocol)
(nreverse map))))
(define-compiler-macro stream-read-map (&whole form prot &optional key-type value-type &environment env)
(expand-iff-constant-types (key-type value-type) form
(with-gensyms (map)
(with-optional-gensyms (prot) env
`(let ((,map ()))
(multiple-value-bind (key-type value-type size) (stream-read-map-begin ,prot)
(unless (equal key-type ',(type-category key-type))
(invalid-element-type ,prot 'thrift:map ',key-type key-type))
(unless (equal value-type ',(type-category value-type))
(invalid-element-type ,prot 'thrift:map ',value-type value-type))
(unless (typep size 'field-size)
(invalid-field-size ,prot 0 "" 'field-size size))
(dotimes (i size)
;; no type check - presume the respective reader is correct.
(setf ,map (acons (stream-read-value-as ,prot ',key-type)
(stream-read-value-as ,prot ',value-type)
,map)))
(stream-read-map-end ,prot)
(nreverse ,map)))))))
(defmethod stream-read-list-begin ((protocol protocol))
; t_elt size
(values (stream-read-type protocol)
(stream-read-i32 protocol)))
(defmethod stream-read-list-end ((protocol protocol)))
(defmethod stream-read-list((protocol protocol) &optional type)
(multiple-value-bind (read-type size)
(stream-read-list-begin protocol)
(when type
(unless (equal read-type (type-category type))
(invalid-element-type protocol 'thrift:list type read-type)))
(unless (typep size 'field-size)
(invalid-field-size protocol 0 "" 'field-size size))
(prog1 (loop for i from 0 below size
collect (stream-read-value-as protocol read-type))
(stream-read-list-end protocol))))
(define-compiler-macro stream-read-list (&whole form prot &optional type &environment env)
(expand-iff-constant-types (type) form
(with-optional-gensyms (prot) env
`(multiple-value-bind (type size)
(stream-read-list-begin ,prot)
(unless (equal type ',(type-category type))
(invalid-element-type ,prot 'thrift:list ',type type))
(unless (typep size 'field-size)
(invalid-field-size ,prot 0 "" 'field-size size))
(prog1 (loop for i from 0 below size
collect (stream-read-value-as ,prot ',type))
(stream-read-list-end ,prot))))))
(defmethod stream-read-set-begin ((protocol protocol))
(values (stream-read-type protocol)
(stream-read-i32 protocol)))
(defmethod stream-read-set-end ((protocol protocol)))
(defmethod stream-read-set((protocol protocol) &optional type)
(multiple-value-bind (read-type size)
(stream-read-set-begin protocol)
(when type
(unless (equal read-type (type-category type))
(invalid-element-type protocol 'thrift:set type read-type)))
(unless (typep size 'field-size)
(invalid-field-size protocol 0 "" 'field-size size))
(prog1 (loop for i from 0 below size
collect (stream-read-value-as protocol read-type))
(stream-read-set-end protocol))))
(define-compiler-macro stream-read-set (&whole form prot &optional type &environment env)
(expand-iff-constant-types (type) form
(with-optional-gensyms (prot) env
`(multiple-value-bind (type size)
(stream-read-set-begin ,prot)
(unless (equal type ',(type-category type))
(invalid-element-type ,prot 'thrift:set ',type type))
(unless (typep size 'field-size)
(invalid-field-size ,prot 0 "" 'field-size size))
(prog1 (loop for i from 0 below size
collect (stream-read-value-as ,prot ',type))
(stream-read-set-end ,prot))))))
(defmethod stream-read-enum ((protocol protocol) type)
"Read an i32 and verify type"
(let ((value (stream-read-i32 protocol)))
(unless (typep value type)
(invalid-enum protocol type value))
value))
(define-compiler-macro stream-read-enum (&whole form prot type)
(expand-iff-constant-types (type) form
#+thrift-check-types
`(let ((value (stream-read-i32 ,prot)))
(unless (typep value ',type)
(invalid-enum protocol ',type value))
value)
`(stream-read-i32 ,prot)))
(defgeneric stream-read-value-as (protocol type)
(:documentation "Read a value if a specified type.")
(:method ((protocol protocol) (type-code fixnum))
(stream-read-value-as protocol (type-code-name protocol type-code)))
(:method ((protocol protocol) (type cons))
(ecase (first type)
(thrift:map (stream-read-map protocol (str-sym (second type)) (str-sym (third type))))
(thrift:list (stream-read-list protocol (str-sym (second type))))
(thrift:set (stream-read-set protocol (str-sym (second type))))
(struct (stream-read-struct protocol (str-sym (second type))))
(enum (stream-read-map protocol (str-sym (second type))))))
(:method ((protocol protocol) (type-code (eql 'bool)))
(stream-read-bool protocol))
(:method ((protocol protocol) (type-code (eql 'thrift:byte)))
;; call through the i08 methods as byte ops are transport, not protocol methods
(stream-read-i08 protocol))
(:method ((protocol protocol) (type-code (eql 'i08)))
(stream-read-i08 protocol))
(:method ((protocol protocol) (type-code (eql 'i16)))
(stream-read-i16 protocol))
(:method ((protocol protocol) (type-code (eql 'enum)))
;; as a fall-back
(stream-read-i16 protocol))
(:method ((protocol protocol) (type-code (eql 'i32)))
(stream-read-i32 protocol))
(:method ((protocol protocol) (type-code (eql 'i64)))
(stream-read-i64 protocol))
(:method ((protocol protocol) (type-code (eql 'double)))
(stream-read-double protocol))
(:method ((protocol protocol) (type-code (eql 'string)))
(stream-read-string protocol))
(:method ((protocol protocol) (type-code (eql 'binary)))
(stream-read-binary protocol))
(:method ((protocol protocol) (type-code (eql 'struct)))
(stream-read-struct protocol))
(:method ((protocol protocol) (type-code (eql 'thrift:map)))
(stream-read-map protocol))
(:method ((protocol protocol) (type-code (eql 'thrift:list)))
(stream-read-list protocol))
(:method ((protocol protocol) (type-code (eql 'thrift:set)))
(stream-read-set protocol)))
(define-compiler-macro stream-read-value-as (&whole form protocol type)
"Given a constant type, generate the respective read operations.
Recognizes all thrift types, container x element type combinations
and struct classes. A void specification yields no values.
If the type is not constant, declare to expand, which leaves the dispatch
to run-time interpretation."
(typecase type
;; just in case
(fixnum (setf type (type-name-class type)))
((cons (eql quote)) (setf type (second type)))
;; if it's not constante, decline to expand
(t (return-from stream-read-value-as form)))
;; given a constant type, attempt an expansion
(etypecase type
((eql void)
(values))
(base-type
`(,(cons-symbol :org.apache.thrift.implementation
:stream-read- type) ,protocol))
((member thrift:set thrift:list thrift:map)
(warn "Compiling generic container decoder: ~s." type)
`(,(cons-symbol :org.apache.thrift.implementation :stream-read- type)
,protocol))
(container-type
(destructuring-bind (type . element-types) type
`(,(cons-symbol :org.apache.thrift.implementation :stream-read- type)
,protocol ,@(mapcar #'(lambda (type) `(quote ,type)) element-types))))
(struct-type
`(stream-read-struct ,protocol ',(str-sym (second type))))
(enum-type
`(stream-read-enum ,protocol ',(str-sym (second type))))))
(defgeneric stream-read-typed-value (protocol)
(:documentation "Given a PROTOCOL instance, decode the value's type and then the value itself.
This is used to decode adhoc data for exchange as a binary value.")
(:method ((protocol protocol))
(let ((type-name (stream-read-type protocol)))
(stream-read-value-as protocol type-name))))
;;; output implementation
;;; nb. defined in this sequence to ensure compile-macro presence is whether loading one or
;;; reloading while developing
(defmethod stream-write-field-begin ((protocol protocol) (identifier string) type identifier-number)
(ecase (protocol-field-id-mode protocol)
(:identifier-number (+ (stream-write-type protocol type)
(stream-write-i16 protocol identifier-number)))
(:identifier-name (+ (stream-write-string protocol identifier)
(stream-write-type protocol type)))))
(defmethod stream-write-field-end ((protocol protocol)))
(defmethod stream-write-field-stop ((protocol protocol))
(stream-write-type protocol 'stop))
(defmethod stream-write-field ((protocol protocol) (value t) &key identifier-name identifier-number (type (thrift:type-of value)))
(stream-write-field-begin protocol identifier-name type identifier-number)
(stream-write-value-as protocol value type)
(stream-write-field-end protocol))
(define-compiler-macro stream-write-field (&whole form prot value &key identifier-name identifier-number type &environment env)
(expand-iff-constant-types (type) form
(with-optional-gensyms (prot) env
`(progn (stream-write-field-begin ,prot ,identifier-name ',type ,identifier-number)
(stream-write-value-as ,prot ,value ',type)
(stream-write-field-end ,prot)))))
(defmethod stream-write-struct-begin ((protocol protocol) (name string))
(ecase (protocol-struct-id-mode protocol)
(:identifier-name
(stream-write-string protocol name))
(:none
nil)))
(defmethod stream-write-struct-end ((protocol protocol)))
(defmethod stream-write-struct ((protocol protocol) (value standard-object) &optional (type (type-of value)))
"Given a VALUE, encode it as per PROTOCOL.
PROTOCOL : protocol
VALUE : standard-object : the object's class must be a thrift-class to provide field metadata."
(let ((class (find-thrift-class type)))
(stream-write-struct-begin protocol (class-identifier class))
(dolist (fd (class-field-definitions class))
(let ((name (field-definition-name fd))
(optional (field-definition-optional fd)))
;; if a slot is optional, then skip unbound slots
;; otherwise, require the value, possibly signalling the unbound situation
(unless (and optional (not (slot-boundp value name)))
(let ((slot-value (slot-value value name)))
(stream-write-field protocol slot-value
:identifier-number (field-definition-identifier-number fd)
:identifier-name (field-definition-identifier fd)
:type (field-definition-type fd))))))
(stream-write-field-stop protocol)
(stream-write-struct-end protocol)))
(defmethod stream-write-struct ((protocol protocol) (value list) &optional type)
(let* ((class (find-thrift-class type))
(fields (class-field-definitions class)))
(stream-write-struct-begin protocol (class-identifier class))
(loop for (id . field-value) in value
do (etypecase id
(fixnum
(let ((fd (or (find id fields :key #'field-definition-identifier-number)
(error 'unknown-field-error :protocol protocol :number id :name nil
:structure-type type :datum field-value))))
(stream-write-field protocol field-value
:identifier-number id
:identifier-name (field-definition-identifier fd)
:type (field-definition-type fd))))
(string
(let ((fd (or (find id fields :key #'field-definition-identifier)
(error 'unknown-field-error :protocol protocol :number nil :name id
:structure-type type :datum field-value))))
(stream-write-field protocol field-value
:identifier-number (field-definition-identifier-number fd)
:identifier-name id
:type (field-definition-type fd))))))
(stream-write-field-stop protocol)
(stream-write-struct-end protocol)))
(define-compiler-macro stream-write-struct (&whole form prot value &optional type &environment env)
"Iff the type is a constant, emit the structure in-line. In this case allow also the variation, that the
structure itself is a thrift:list a-list of (id-number . place)."
(expand-iff-constant-types (type) form
(etypecase type
(symbol )
(struct-type (setf type (second type))))
(let* ((class (find-thrift-class type))
(identifier (class-identifier class))
(field-definitions (class-field-definitions class)))
(if (typep value '(cons (eql thrift:list)))
;; if it's a literal environment, expand it in-line
(with-optional-gensyms (prot) env
`(progn (stream-write-struct-begin ,prot ,identifier)
,@(loop for (nil id place) in (rest value) ; ignore the 'cons'
for fd = (or (or (find id field-definitions :key #'field-definition-identifier-number)
(find id field-definitions :key #'field-definition-name :test #'string-equal))
(error "Field id not found: ~s, ~s" id identifier))
do (list id place fd)
collect `(stream-write-field ,prot ,place
:identifier-number ,(field-definition-identifier-number fd)
:identifier-name ,(field-definition-identifier fd)
:type ',(field-definition-type fd)))
(stream-write-field-stop ,prot)
(stream-write-struct-end ,prot)))
;; otherwise expand with instance field refences
(with-optional-gensyms (prot value) env
`(progn
(typecase ,value
(,type
(stream-write-struct-begin ,prot ,identifier)
,@(loop for fd in field-definitions
collect (if (field-definition-optional fd)
`(when (slot-boundp ,value ',(field-definition-name fd))
(let ((slot-value (,(field-definition-reader fd) ,value)))
(stream-write-field ,prot slot-value
:identifier-number ,(field-definition-identifier-number fd)
:identifier-name ,(field-definition-identifier fd)
:type ',(field-definition-type fd))))
`(stream-write-field ,prot (,(field-definition-reader fd) ,value)
:identifier-number ,(field-definition-identifier-number fd)
:identifier-name ,(field-definition-identifier fd)
:type ',(field-definition-type fd))))
(stream-write-field-stop ,prot)
(stream-write-struct-end ,prot))
(list ; allow s-exp encoded structs
(let ((type ',type))
(stream-write-struct ,prot ,value type)))
(t
(assert (typep ,value ',type) ()
"Attempt to serialize ~s as ~s." ,value ',type)))))))))
(defmethod stream-write-message-begin ((protocol protocol) name type sequence)
(stream-write-i08 protocol (protocol-version-id protocol))
(stream-write-i08 protocol (protocol-version-number protocol))
(stream-write-message-type protocol type)
(stream-write-string protocol name)
(stream-write-i32 protocol sequence))
(defmethod stream-write-message ((protocol protocol) (object standard-object) (message-type (eql 'call))
&key (type (type-of object))
(sequence-number (protocol-next-sequence-number protocol)))
(stream-write-message-begin protocol (class-identifier type) message-type sequence-number)
(stream-write-struct protocol object type)
(stream-write-message-end protocol))
(defmethod stream-write-message ((protocol protocol) (object standard-object) (message-type (eql 'oneway))
&key (type (type-of object))
(sequence-number (protocol-next-sequence-number protocol)))
(stream-write-message-begin protocol (class-identifier type) message-type sequence-number)
(stream-write-struct protocol object type)
(stream-write-message-end protocol))
(defmethod stream-write-message ((protocol protocol) (object standard-object) (message-type t)
&key (type (type-of object))
(sequence-number (protocol-sequence-number protocol)))
(stream-write-message-begin protocol (class-identifier type) message-type sequence-number)
(stream-write-struct protocol object type)
(stream-write-message-end protocol))
(defmethod stream-write-message-end ((protocol protocol))
(stream-force-output (protocol-output-transport protocol)))
(defgeneric stream-write-exception (protocol exception)
(:method ((protocol protocol) (exception thrift-error))
(stream-write-message protocol exception 'exception
:identifier (class-identifier exception)))
(:method ((protocol protocol) (exception condition))
(stream-write-message protocol
(make-instance 'application-error :condition exception)
'exception)))
(defmethod stream-write-map-begin ((protocol protocol) key-type value-type size)
(stream-write-type protocol key-type)
(stream-write-type protocol value-type)
(stream-write-i32 protocol size))
(defmethod stream-write-map-end ((protocol protocol)))
(defmethod stream-write-map ((protocol protocol) (value list) &optional key-type value-type)
(let ((size (map-size value)))
;; nb. no need to check size as the map size is constrained by array size limits.
(unless key-type (setf key-type (thrift:type-of (caar value))))
(unless value-type (setf value-type (thrift:type-of (cdar value))))
(stream-write-map-begin protocol key-type value-type size)
(loop for (element-key . element-value) in value
do (progn (stream-write-value-as protocol element-key key-type)
(stream-write-value-as protocol element-value value-type)))
(stream-write-map-end protocol)))
(define-compiler-macro stream-write-map (&whole form prot value &optional key-type value-type &environment env)
(expand-iff-constant-types (key-type value-type) form
(with-optional-gensyms (prot value) env
`(let ((size (map-size ,value)))
;; nb. no need to check size as the map size is constrained by array size limits.
(stream-write-map-begin ,prot ',key-type ',value-type size)
(loop for (element-key . element-value) in ,value
do (progn (stream-write-value-as ,prot element-key ',key-type)
(stream-write-value-as ,prot element-value ',value-type)))
(stream-write-map-end ,prot)))))
(defmethod stream-write-list-begin ((protocol protocol) (type t) length)
(stream-write-type protocol type)
(stream-write-i32 protocol length))
(defmethod stream-write-list-end ((protocol protocol)))
(defmethod stream-write-list ((protocol protocol) (value list) &optional
(type (if value (thrift:type-of (first value)) (error "The element type is required."))))
(let ((size (list-length value)))
(unless (typep size 'field-size)
(invalid-field-size protocol 0 "" 'field-size size))
(stream-write-list-begin protocol type size)
(dolist (elt value)
(stream-write-value-as protocol elt type))
(stream-write-list-end protocol)))
(define-compiler-macro stream-write-list (&whole form prot value &optional element-type &environment env)