David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 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 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 20 | open Thrift |
| 21 | |
| 22 | module P = Protocol |
| 23 | |
| 24 | let get_byte i b = 255 land (i lsr (8*b)) |
Bryan Duxbury | fad8d6b | 2011-01-12 18:41:52 +0000 | [diff] [blame] | 25 | let get_byte32 i b = 255 land (Int32.to_int (Int32.shift_right i (8*b))) |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 26 | let get_byte64 i b = 255 land (Int64.to_int (Int64.shift_right i (8*b))) |
| 27 | |
| 28 | |
| 29 | let tv = P.t_type_to_i |
| 30 | let vt = P.t_type_of_i |
| 31 | |
| 32 | |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 33 | let comp_int b n = |
iproctor | d4de1e9 | 2007-07-24 19:47:55 +0000 | [diff] [blame] | 34 | let s = ref 0l in |
| 35 | let sb = 32 - 8*n in |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 36 | for i=0 to (n-1) do |
iproctor | d4de1e9 | 2007-07-24 19:47:55 +0000 | [diff] [blame] | 37 | s:= Int32.logor !s (Int32.shift_left (Int32.of_int (int_of_char b.[i])) (8*(n-1-i))) |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 38 | done; |
Bryan Duxbury | fad8d6b | 2011-01-12 18:41:52 +0000 | [diff] [blame] | 39 | Int32.shift_right (Int32.shift_left !s sb) sb |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 40 | |
| 41 | let comp_int64 b n = |
| 42 | let s = ref 0L in |
| 43 | for i=0 to (n-1) do |
| 44 | s:=Int64.logor !s (Int64.shift_left (Int64.of_int (int_of_char b.[i])) (8*(n-1-i))) |
| 45 | done; |
| 46 | !s |
| 47 | |
Bryan Duxbury | fad8d6b | 2011-01-12 18:41:52 +0000 | [diff] [blame] | 48 | let version_mask = 0xffff0000l |
| 49 | let version_1 = 0x80010000l |
iproctor | d4de1e9 | 2007-07-24 19:47:55 +0000 | [diff] [blame] | 50 | |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 51 | class t trans = |
| 52 | object (self) |
| 53 | inherit P.t trans |
| 54 | val ibyte = String.create 8 |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 55 | method writeBool b = |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 56 | ibyte.[0] <- char_of_int (if b then 1 else 0); |
| 57 | trans#write ibyte 0 1 |
| 58 | method writeByte i = |
| 59 | ibyte.[0] <- char_of_int (get_byte i 0); |
| 60 | trans#write ibyte 0 1 |
| 61 | method writeI16 i = |
| 62 | let gb = get_byte i in |
| 63 | ibyte.[1] <- char_of_int (gb 0); |
| 64 | ibyte.[0] <- char_of_int (gb 1); |
| 65 | trans#write ibyte 0 2 |
| 66 | method writeI32 i = |
Bryan Duxbury | fad8d6b | 2011-01-12 18:41:52 +0000 | [diff] [blame] | 67 | let gb = get_byte32 i in |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 68 | for i=0 to 3 do |
| 69 | ibyte.[3-i] <- char_of_int (gb i) |
| 70 | done; |
| 71 | trans#write ibyte 0 4 |
| 72 | method writeI64 i= |
| 73 | let gb = get_byte64 i in |
| 74 | for i=0 to 7 do |
| 75 | ibyte.[7-i] <- char_of_int (gb i) |
| 76 | done; |
| 77 | trans#write ibyte 0 8 |
| 78 | method writeDouble d = |
| 79 | self#writeI64 (Int64.bits_of_float d) |
| 80 | method writeString s= |
| 81 | let n = String.length s in |
Bryan Duxbury | fad8d6b | 2011-01-12 18:41:52 +0000 | [diff] [blame] | 82 | self#writeI32 (Int32.of_int n); |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 83 | trans#write s 0 n |
| 84 | method writeBinary a = self#writeString a |
| 85 | method writeMessageBegin (n,t,s) = |
Bryan Duxbury | fad8d6b | 2011-01-12 18:41:52 +0000 | [diff] [blame] | 86 | self#writeI32 (Int32.logor version_1 (Int32.of_int (P.message_type_to_i t))); |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 87 | self#writeString n; |
Bryan Duxbury | fad8d6b | 2011-01-12 18:41:52 +0000 | [diff] [blame] | 88 | self#writeI32 (Int32.of_int s) |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 89 | method writeMessageEnd = () |
| 90 | method writeStructBegin s = () |
| 91 | method writeStructEnd = () |
| 92 | method writeFieldBegin (n,t,i) = |
| 93 | self#writeByte (tv t); |
| 94 | self#writeI16 i |
| 95 | method writeFieldEnd = () |
| 96 | method writeFieldStop = |
Bryan Duxbury | fad8d6b | 2011-01-12 18:41:52 +0000 | [diff] [blame] | 97 | self#writeByte (tv (P.T_STOP)) |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 98 | method writeMapBegin (k,v,s) = |
| 99 | self#writeByte (tv k); |
| 100 | self#writeByte (tv v); |
Bryan Duxbury | fad8d6b | 2011-01-12 18:41:52 +0000 | [diff] [blame] | 101 | self#writeI32 (Int32.of_int s) |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 102 | method writeMapEnd = () |
| 103 | method writeListBegin (t,s) = |
| 104 | self#writeByte (tv t); |
Bryan Duxbury | fad8d6b | 2011-01-12 18:41:52 +0000 | [diff] [blame] | 105 | self#writeI32 (Int32.of_int s) |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 106 | method writeListEnd = () |
| 107 | method writeSetBegin (t,s) = |
| 108 | self#writeByte (tv t); |
Bryan Duxbury | fad8d6b | 2011-01-12 18:41:52 +0000 | [diff] [blame] | 109 | self#writeI32 (Int32.of_int s) |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 110 | method writeSetEnd = () |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 111 | method readByte = |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 112 | ignore (trans#readAll ibyte 0 1); |
Bryan Duxbury | fad8d6b | 2011-01-12 18:41:52 +0000 | [diff] [blame] | 113 | Int32.to_int (comp_int ibyte 1) |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 114 | method readI16 = |
| 115 | ignore (trans#readAll ibyte 0 2); |
Bryan Duxbury | fad8d6b | 2011-01-12 18:41:52 +0000 | [diff] [blame] | 116 | Int32.to_int (comp_int ibyte 2) |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 117 | method readI32 = |
| 118 | ignore (trans#readAll ibyte 0 4); |
| 119 | comp_int ibyte 4 |
| 120 | method readI64 = |
| 121 | ignore (trans#readAll ibyte 0 8); |
| 122 | comp_int64 ibyte 8 |
| 123 | method readDouble = |
| 124 | Int64.float_of_bits (self#readI64) |
| 125 | method readBool = |
| 126 | self#readByte = 1 |
| 127 | method readString = |
Bryan Duxbury | fad8d6b | 2011-01-12 18:41:52 +0000 | [diff] [blame] | 128 | let sz = Int32.to_int (self#readI32) in |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 129 | let buf = String.create sz in |
| 130 | ignore (trans#readAll buf 0 sz); |
| 131 | buf |
| 132 | method readBinary = self#readString |
| 133 | method readMessageBegin = |
iproctor | d4de1e9 | 2007-07-24 19:47:55 +0000 | [diff] [blame] | 134 | let ver = self#readI32 in |
Bryan Duxbury | fad8d6b | 2011-01-12 18:41:52 +0000 | [diff] [blame] | 135 | if Int32.compare (Int32.logand ver version_mask) version_1 != 0 then |
| 136 | raise (P.E (P.BAD_VERSION, "Missing version identifier")) |
iproctor | d4de1e9 | 2007-07-24 19:47:55 +0000 | [diff] [blame] | 137 | else |
| 138 | let s = self#readString in |
Bryan Duxbury | fad8d6b | 2011-01-12 18:41:52 +0000 | [diff] [blame] | 139 | let mt = P.message_type_of_i (Int32.to_int (Int32.logand ver 0xFFl)) in |
| 140 | (s,mt, Int32.to_int self#readI32) |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 141 | method readMessageEnd = () |
| 142 | method readStructBegin = |
| 143 | "" |
| 144 | method readStructEnd = () |
| 145 | method readFieldBegin = |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 146 | let t = (vt (self#readByte)) |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 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 |
Bryan Duxbury | fad8d6b | 2011-01-12 18:41:52 +0000 | [diff] [blame] | 155 | (kt,vt, Int32.to_int self#readI32) |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 156 | method readMapEnd = () |
| 157 | method readListBegin = |
| 158 | let t = vt (self#readByte) in |
Bryan Duxbury | fad8d6b | 2011-01-12 18:41:52 +0000 | [diff] [blame] | 159 | (t, Int32.to_int self#readI32) |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 160 | method readListEnd = () |
| 161 | method readSetBegin = |
| 162 | let t = vt (self#readByte) in |
Bryan Duxbury | fad8d6b | 2011-01-12 18:41:52 +0000 | [diff] [blame] | 163 | (t, Int32.to_int self#readI32); |
iproctor | 9a41a0c | 2007-07-16 21:59:24 +0000 | [diff] [blame] | 164 | method readSetEnd = () |
| 165 | end |
| 166 | |
| 167 | class factory = |
| 168 | object |
| 169 | inherit P.factory |
| 170 | method getProtocol tr = new t tr |
| 171 | end |