blob: a06cc9a904bddbb02409485eb28798ad6a8c6aa6 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001(*
2 Licensed to the Apache Software Foundation (ASF) under one
3 or more contributor license agreements. See the NOTICE file
4 distributed with this work for additional information
5 regarding copyright ownership. The ASF licenses this file
6 to you under the Apache License, Version 2.0 (the
7 "License"); you may not use this file except in compliance
8 with the License. You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing,
13 software distributed under the License is distributed on an
14 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 KIND, either express or implied. See the License for the
16 specific language governing permissions and limitations
17 under the License.
18*)
19
iproctor9a41a0c2007-07-16 21:59:24 +000020open Thrift
21
22module P = Protocol
23
24let get_byte i b = 255 land (i lsr (8*b))
25let get_byte64 i b = 255 land (Int64.to_int (Int64.shift_right i (8*b)))
26
27
28let tv = P.t_type_to_i
29let vt = P.t_type_of_i
30
31
David Reiss0c90f6f2008-02-06 22:18:40 +000032let comp_int b n =
iproctord4de1e92007-07-24 19:47:55 +000033 let s = ref 0l in
34 let sb = 32 - 8*n in
iproctor9a41a0c2007-07-16 21:59:24 +000035 for i=0 to (n-1) do
iproctord4de1e92007-07-24 19:47:55 +000036 s:= Int32.logor !s (Int32.shift_left (Int32.of_int (int_of_char b.[i])) (8*(n-1-i)))
iproctor9a41a0c2007-07-16 21:59:24 +000037 done;
iproctord4de1e92007-07-24 19:47:55 +000038 Int32.to_int (Int32.shift_right (Int32.shift_left !s sb) sb)
iproctor9a41a0c2007-07-16 21:59:24 +000039
40let comp_int64 b n =
41 let s = ref 0L in
42 for i=0 to (n-1) do
43 s:=Int64.logor !s (Int64.shift_left (Int64.of_int (int_of_char b.[i])) (8*(n-1-i)))
44 done;
45 !s
46
iproctord4de1e92007-07-24 19:47:55 +000047let version_mask = 0xffff0000
48let version_1 = 0x80010000
49
iproctor9a41a0c2007-07-16 21:59:24 +000050class t trans =
51object (self)
52 inherit P.t trans
53 val ibyte = String.create 8
David Reiss0c90f6f2008-02-06 22:18:40 +000054 method writeBool b =
iproctor9a41a0c2007-07-16 21:59:24 +000055 ibyte.[0] <- char_of_int (if b then 1 else 0);
56 trans#write ibyte 0 1
57 method writeByte i =
58 ibyte.[0] <- char_of_int (get_byte i 0);
59 trans#write ibyte 0 1
60 method writeI16 i =
61 let gb = get_byte i in
62 ibyte.[1] <- char_of_int (gb 0);
63 ibyte.[0] <- char_of_int (gb 1);
64 trans#write ibyte 0 2
65 method writeI32 i =
66 let gb = get_byte i in
67 for i=0 to 3 do
68 ibyte.[3-i] <- char_of_int (gb i)
69 done;
70 trans#write ibyte 0 4
71 method writeI64 i=
72 let gb = get_byte64 i in
73 for i=0 to 7 do
74 ibyte.[7-i] <- char_of_int (gb i)
75 done;
76 trans#write ibyte 0 8
77 method writeDouble d =
78 self#writeI64 (Int64.bits_of_float d)
79 method writeString s=
80 let n = String.length s in
81 self#writeI32(n);
82 trans#write s 0 n
83 method writeBinary a = self#writeString a
84 method writeMessageBegin (n,t,s) =
iproctord4de1e92007-07-24 19:47:55 +000085 self#writeI32 (version_1 lor (P.message_type_to_i t));
iproctor9a41a0c2007-07-16 21:59:24 +000086 self#writeString n;
iproctor9a41a0c2007-07-16 21:59:24 +000087 self#writeI32 s
88 method writeMessageEnd = ()
89 method writeStructBegin s = ()
90 method writeStructEnd = ()
91 method writeFieldBegin (n,t,i) =
92 self#writeByte (tv t);
93 self#writeI16 i
94 method writeFieldEnd = ()
95 method writeFieldStop =
96 self#writeByte (tv (Protocol.T_STOP))
97 method writeMapBegin (k,v,s) =
98 self#writeByte (tv k);
99 self#writeByte (tv v);
100 self#writeI32 s
101 method writeMapEnd = ()
102 method writeListBegin (t,s) =
103 self#writeByte (tv t);
104 self#writeI32 s
105 method writeListEnd = ()
106 method writeSetBegin (t,s) =
107 self#writeByte (tv t);
108 self#writeI32 s
109 method writeSetEnd = ()
David Reiss0c90f6f2008-02-06 22:18:40 +0000110 method readByte =
iproctor9a41a0c2007-07-16 21:59:24 +0000111 ignore (trans#readAll ibyte 0 1);
112 (comp_int ibyte 1)
113 method readI16 =
114 ignore (trans#readAll ibyte 0 2);
115 comp_int ibyte 2
116 method readI32 =
117 ignore (trans#readAll ibyte 0 4);
118 comp_int ibyte 4
119 method readI64 =
120 ignore (trans#readAll ibyte 0 8);
121 comp_int64 ibyte 8
122 method readDouble =
123 Int64.float_of_bits (self#readI64)
124 method readBool =
125 self#readByte = 1
126 method readString =
127 let sz = self#readI32 in
128 let buf = String.create sz in
129 ignore (trans#readAll buf 0 sz);
130 buf
131 method readBinary = self#readString
132 method readMessageBegin =
iproctord4de1e92007-07-24 19:47:55 +0000133 let ver = self#readI32 in
134 if (ver land version_mask != version_1) then
135 (print_int ver;
iproctore470aa32007-08-10 20:48:12 +0000136 raise (P.E (P.BAD_VERSION, "Missing version identifier")))
iproctord4de1e92007-07-24 19:47:55 +0000137 else
138 let s = self#readString in
139 let mt = P.message_type_of_i (ver land 0xFF) in
140 (s,mt, self#readI32)
iproctor9a41a0c2007-07-16 21:59:24 +0000141 method readMessageEnd = ()
142 method readStructBegin =
143 ""
144 method readStructEnd = ()
145 method readFieldBegin =
David Reiss0c90f6f2008-02-06 22:18:40 +0000146 let t = (vt (self#readByte))
iproctor9a41a0c2007-07-16 21:59:24 +0000147 in
148 if t != P.T_STOP then
149 ("",t,self#readI16)
150 else ("",t,0);
151 method readFieldEnd = ()
152 method readMapBegin =
153 let kt = vt (self#readByte) in
154 let vt = vt (self#readByte) in
155 (kt,vt, self#readI32)
156 method readMapEnd = ()
157 method readListBegin =
158 let t = vt (self#readByte) in
159 (t,self#readI32)
160 method readListEnd = ()
161 method readSetBegin =
162 let t = vt (self#readByte) in
163 (t, self#readI32);
164 method readSetEnd = ()
165end
166
167class factory =
168object
169 inherit P.factory
170 method getProtocol tr = new t tr
171end