blob: 224febbc54c9bea93bcded56b1762e207c7b4950 [file] [log] [blame]
iproctor9a41a0c2007-07-16 21:59:24 +00001exception Break;;
2exception Thrift_error;;
3exception Field_empty of string;;
4
5class t_exn =
6object
7 val mutable message = ""
8 method get_message = message
9 method set_message s = message <- s
10end;;
11
12exception TExn of t_exn;;
13
14
15
16
17module Transport =
18struct
19 type exn_type =
20 | UNKNOWN
21 | NOT_OPEN
22 | ALREADY_OPEN
23 | TIMED_OUT
24 | END_OF_FILE;;
25
26 class exn =
27 object
28 inherit t_exn
29 val mutable typ = UNKNOWN
30 method get_type = typ
31 method set_type t = typ <- t
32 end
33 exception TTransportExn of exn
34 let raise_TTransportExn message typ =
35 let e = new exn in
36 e#set_message message;
37 e#set_type typ;
38 raise (TTransportExn e)
39
40 class virtual t =
41 object (self)
42 method virtual isOpen : bool
43 method virtual opn : unit
44 method virtual close : unit
45 method virtual read : string -> int -> int -> int
46 method readAll buf off len =
47 let got = ref 0 in
48 let ret = ref 0 in
49 while !got < len do
50 ret := self#read buf (off+(!got)) (len - (!got));
51 if !ret <= 0 then
52 let e = new exn in
53 e#set_message "Cannot read. Remote side has closed.";
54 raise (TTransportExn e)
55 else ();
56 got := !got + !ret
57 done;
58 !got
59 method virtual write : string -> int -> int -> unit
60 method virtual flush : unit
61 end
62
63 class factory =
64 object
65 method getTransport (t : t) = t
66 end
67
68 class virtual server_t =
69 object (self)
70 method virtual listen : unit
71 method accept = self#acceptImpl
72 method virtual close : unit
73 method virtual acceptImpl : t
74 end
75
76end;;
77
78
79
80module Protocol =
81struct
82 type t_type =
83 | T_STOP
84 | T_VOID
85 | T_BOOL
86 | T_BYTE
87 | T_I08
88 | T_I16
89 | T_I32
90 | T_U64
91 | T_I64
92 | T_DOUBLE
93 | T_STRING
94 | T_UTF7
95 | T_STRUCT
96 | T_MAP
97 | T_SET
98 | T_LIST
99 | T_UTF8
100 | T_UTF16
101
102 let t_type_to_i = function
103 T_STOP -> 0
104 | T_VOID -> 1
105 | T_BOOL -> 2
106 | T_BYTE -> 3
107 | T_I08 -> 3
108 | T_I16 -> 6
109 | T_I32 -> 8
110 | T_U64 -> 9
111 | T_I64 -> 10
112 | T_DOUBLE -> 4
113 | T_STRING -> 11
114 | T_UTF7 -> 11
115 | T_STRUCT -> 12
116 | T_MAP -> 13
117 | T_SET -> 14
118 | T_LIST -> 15
119 | T_UTF8 -> 16
120 | T_UTF16 -> 17
121
122 let t_type_of_i = function
123 0 -> T_STOP
124 | 1 -> T_VOID
125 | 2 -> T_BOOL
126 | 3 -> T_BYTE
127 | 6-> T_I16
128 | 8 -> T_I32
129 | 9 -> T_U64
130 | 10 -> T_I64
131 | 4 -> T_DOUBLE
132 | 11 -> T_STRING
133 | 12 -> T_STRUCT
134 | 13 -> T_MAP
135 | 14 -> T_SET
136 | 15 -> T_LIST
137 | 16 -> T_UTF8
138 | 17 -> T_UTF16
139 | _ -> raise Thrift_error
140
141 type message_type =
142 | CALL
143 | REPLY
144 | EXCEPTION
145
146 let message_type_to_i = function
147 | CALL -> 1
148 | REPLY -> 2
149 | EXCEPTION -> 3
150
151 let message_type_of_i = function
152 | 1 -> CALL
153 | 2 -> REPLY
154 | 3 -> EXCEPTION
155 | _ -> raise Thrift_error
156
157 class virtual t (trans: Transport.t) =
158 object (self)
159 val mutable trans_ = trans
160 method getTransport = trans_
161 (* writing methods *)
162 method virtual writeMessageBegin : string * message_type * int -> unit
163 method virtual writeMessageEnd : unit
164 method virtual writeStructBegin : string -> unit
165 method virtual writeStructEnd : unit
166 method virtual writeFieldBegin : string * t_type * int -> unit
167 method virtual writeFieldEnd : unit
168 method virtual writeFieldStop : unit
169 method virtual writeMapBegin : t_type * t_type * int -> unit
170 method virtual writeMapEnd : unit
171 method virtual writeListBegin : t_type * int -> unit
172 method virtual writeListEnd : unit
173 method virtual writeSetBegin : t_type * int -> unit
174 method virtual writeSetEnd : unit
175 method virtual writeBool : bool -> unit
176 method virtual writeByte : int -> unit
177 method virtual writeI16 : int -> unit
178 method virtual writeI32 : int -> unit
179 method virtual writeI64 : Int64.t -> unit
180 method virtual writeDouble : float -> unit
181 method virtual writeString : string -> unit
182 method virtual writeBinary : string -> unit
183 (* reading methods *)
184 method virtual readMessageBegin : string * message_type * int
185 method virtual readMessageEnd : unit
186 method virtual readStructBegin : string
187 method virtual readStructEnd : unit
188 method virtual readFieldBegin : string * t_type * int
189 method virtual readFieldEnd : unit
190 method virtual readMapBegin : t_type * t_type * int
191 method virtual readMapEnd : unit
192 method virtual readListBegin : t_type * int
193 method virtual readListEnd : unit
194 method virtual readSetBegin : t_type * int
195 method virtual readSetEnd : unit
196 method virtual readBool : bool
197 method virtual readByte : int
198 method virtual readI16 : int
199 method virtual readI32: int
200 method virtual readI64 : Int64.t
201 method virtual readDouble : float
202 method virtual readString : string
203 method virtual readBinary : string
204 (* skippage *)
205 method skip typ =
206 match typ with
207 | T_STOP -> ()
208 | T_VOID -> ()
209 | T_BOOL -> ignore self#readBool
210 | T_BYTE
211 | T_I08 -> ignore self#readByte
212 | T_I16 -> ignore self#readI16
213 | T_I32 -> ignore self#readI32
214 | T_U64
215 | T_I64 -> ignore self#readI64
216 | T_DOUBLE -> ignore self#readDouble
217 | T_STRING -> ignore self#readString
218 | T_UTF7 -> ()
219 | T_STRUCT -> ignore ((ignore self#readStructBegin);
220 (try
221 while true do
222 let (_,t,_) = self#readFieldBegin in
223 if t = T_STOP then
224 raise Break
225 else
226 (self#skip t;
227 self#readFieldEnd)
228 done
229 with Break -> ());
230 self#readStructEnd)
231 | T_MAP -> ignore (let (k,v,s) = self#readMapBegin in
232 for i=0 to s do
233 self#skip k;
234 self#skip v;
235 done;
236 self#readMapEnd)
237 | T_SET -> ignore (let (t,s) = self#readSetBegin in
238 for i=0 to s do
239 self#skip t
240 done;
241 self#readSetEnd)
242 | T_LIST -> ignore (let (t,s) = self#readListBegin in
243 for i=0 to s do
244 self#skip t
245 done;
246 self#readListEnd)
247 | T_UTF8 -> ()
248 | T_UTF16 -> ()
249 end
250
251 class virtual factory =
252 object
253 method virtual getProtocol : Transport.t -> t
254 end
255
256end;;
257
258
259module Processor =
260struct
261 class virtual t =
262 object
263 method virtual process : Protocol.t -> Protocol.t -> bool
264 end;;
265
266 class factory (processor : t) =
267 object
268 val processor_ = processor
269 method getProcessor (trans : Transport.t) = processor_
270 end;;
271end
272
273
274
275module Application_Exn =
276struct
277 type typ=
278 | UNKNOWN
279 | UNKNOWN_METHOD
280 | INVALID_MESSAGE_TYPE
281 | WRONG_METHOD_NAME
282 | BAD_SEQUENCE_ID
283 | MISSING_RESULT
284
285 let typ_of_i = function
286 0 -> UNKNOWN
287 | 1 -> UNKNOWN_METHOD
288 | 2 -> INVALID_MESSAGE_TYPE
289 | 3 -> WRONG_METHOD_NAME
290 | 4 -> BAD_SEQUENCE_ID
291 | 5 -> MISSING_RESULT
292 | _ -> raise Thrift_error;;
293 let typ_to_i = function
294 | UNKNOWN -> 0
295 | UNKNOWN_METHOD -> 1
296 | INVALID_MESSAGE_TYPE -> 2
297 | WRONG_METHOD_NAME -> 3
298 | BAD_SEQUENCE_ID -> 4
299 | MISSING_RESULT -> 5
300
301 class t =
302 object (self)
303 inherit t_exn
304 val mutable typ = UNKNOWN
305 method get_type = typ
306 method set_type t = typ <- t
307 method write (oprot : Protocol.t) =
308 oprot#writeStructBegin "TApplicationExeception";
309 if self#get_message != "" then
310 (oprot#writeFieldBegin ("message",Protocol.T_STRING, 1);
311 oprot#writeString self#get_message;
312 oprot#writeFieldEnd)
313 else ();
314 oprot#writeFieldBegin ("type",Protocol.T_I32,2);
315 oprot#writeI32 (typ_to_i typ);
316 oprot#writeFieldEnd;
317 oprot#writeFieldStop;
318 oprot#writeStructEnd
319 end;;
320
321 let create typ msg =
322 let e = new t in
323 e#set_type typ;
324 e#set_message msg;
325 e
326
327 let read (iprot : Protocol.t) =
328 let msg = ref "" in
329 let typ = ref 0 in
330 iprot#readStructBegin;
331 (try
332 while true do
333 let (name,ft,id) =iprot#readFieldBegin in
334 if ft = Protocol.T_STOP then
335 raise Break
336 else ();
337 (match id with
338 | 1 -> (if ft = Protocol.T_STRING then
339 msg := (iprot#readString)
340 else
341 iprot#skip ft)
342 | 2 -> (if ft = Protocol.T_I32 then
343 typ := iprot#readI32
344 else
345 iprot#skip ft)
346 | _ -> iprot#skip ft);
347 iprot#readFieldEnd
348 done
349 with Break -> ());
350 iprot#readStructEnd;
351 let e = new t in
352 e#set_type (typ_of_i !typ);
353 e#set_message !msg;
354 e;;
355
356 exception E of t
357end;;