| 30 |
ashish |
1 |
(*
|
|
|
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 |
|
|
|
20 |
open Thrift
|
|
|
21 |
|
|
|
22 |
module P = Protocol
|
|
|
23 |
|
|
|
24 |
let get_byte i b = 255 land (i lsr (8*b))
|
|
|
25 |
let get_byte64 i b = 255 land (Int64.to_int (Int64.shift_right i (8*b)))
|
|
|
26 |
|
|
|
27 |
|
|
|
28 |
let tv = P.t_type_to_i
|
|
|
29 |
let vt = P.t_type_of_i
|
|
|
30 |
|
|
|
31 |
|
|
|
32 |
let comp_int b n =
|
|
|
33 |
let s = ref 0l in
|
|
|
34 |
let sb = 32 - 8*n in
|
|
|
35 |
for i=0 to (n-1) do
|
|
|
36 |
s:= Int32.logor !s (Int32.shift_left (Int32.of_int (int_of_char b.[i])) (8*(n-1-i)))
|
|
|
37 |
done;
|
|
|
38 |
Int32.to_int (Int32.shift_right (Int32.shift_left !s sb) sb)
|
|
|
39 |
|
|
|
40 |
let 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 |
|
|
|
47 |
let version_mask = 0xffff0000
|
|
|
48 |
let version_1 = 0x80010000
|
|
|
49 |
|
|
|
50 |
class t trans =
|
|
|
51 |
object (self)
|
|
|
52 |
inherit P.t trans
|
|
|
53 |
val ibyte = String.create 8
|
|
|
54 |
method writeBool b =
|
|
|
55 |
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) =
|
|
|
85 |
self#writeI32 (version_1 lor (P.message_type_to_i t));
|
|
|
86 |
self#writeString n;
|
|
|
87 |
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 = ()
|
|
|
110 |
method readByte =
|
|
|
111 |
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 =
|
|
|
133 |
let ver = self#readI32 in
|
|
|
134 |
if (ver land version_mask != version_1) then
|
|
|
135 |
(print_int ver;
|
|
|
136 |
raise (P.E (P.BAD_VERSION, "Missing version identifier")))
|
|
|
137 |
else
|
|
|
138 |
let s = self#readString in
|
|
|
139 |
let mt = P.message_type_of_i (ver land 0xFF) in
|
|
|
140 |
(s,mt, self#readI32)
|
|
|
141 |
method readMessageEnd = ()
|
|
|
142 |
method readStructBegin =
|
|
|
143 |
""
|
|
|
144 |
method readStructEnd = ()
|
|
|
145 |
method readFieldBegin =
|
|
|
146 |
let t = (vt (self#readByte))
|
|
|
147 |
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 = ()
|
|
|
165 |
end
|
|
|
166 |
|
|
|
167 |
class factory =
|
|
|
168 |
object
|
|
|
169 |
inherit P.factory
|
|
|
170 |
method getProtocol tr = new t tr
|
|
|
171 |
end
|