blob: 50c6aa8963ca95ef4fdd77ea7b4226f46c8dfe0f [file] [log] [blame]
Mark Slee89e2bb82007-03-01 00:20:36 +00001#
David Reissea2cba82009-03-30 21:35:00 +00002# 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#
Mark Slee89e2bb82007-03-01 00:20:36 +000019
Mark Sleecde2b612006-09-03 21:13:07 +000020from TProtocol import *
21from struct import pack, unpack
22
23class TBinaryProtocol(TProtocolBase):
24
25 """Binary implementation of the Thrift protocol driver."""
26
Mark Slee9b36ef32007-10-02 04:44:48 +000027 # NastyHaxx. Python 2.4+ on 32-bit machines forces hex constants to be
28 # positive, converting this into a long. If we hardcode the int value
29 # instead it'll stay in 32 bit-land.
30
31 # VERSION_MASK = 0xffff0000
32 VERSION_MASK = -65536
33
34 # VERSION_1 = 0x80010000
35 VERSION_1 = -2147418112
36
37 TYPE_MASK = 0x000000ff
Mark Slee808454e2007-06-20 21:51:57 +000038
39 def __init__(self, trans, strictRead=False, strictWrite=True):
Aditya Agarwal5c468192007-02-06 01:14:33 +000040 TProtocolBase.__init__(self, trans)
Mark Slee808454e2007-06-20 21:51:57 +000041 self.strictRead = strictRead
42 self.strictWrite = strictWrite
Mark Sleecde2b612006-09-03 21:13:07 +000043
Mark Slee4ac459f2006-10-25 21:39:01 +000044 def writeMessageBegin(self, name, type, seqid):
Mark Slee808454e2007-06-20 21:51:57 +000045 if self.strictWrite:
Mark Slee552410c2007-06-22 01:03:55 +000046 self.writeI32(TBinaryProtocol.VERSION_1 | type)
Mark Slee808454e2007-06-20 21:51:57 +000047 self.writeString(name)
48 self.writeI32(seqid)
49 else:
50 self.writeString(name)
51 self.writeByte(type)
52 self.writeI32(seqid)
Mark Slee4ac459f2006-10-25 21:39:01 +000053
54 def writeMessageEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000055 pass
56
Mark Slee4ac459f2006-10-25 21:39:01 +000057 def writeStructBegin(self, name):
Mark Sleecde2b612006-09-03 21:13:07 +000058 pass
59
Mark Slee4ac459f2006-10-25 21:39:01 +000060 def writeStructEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000061 pass
62
Mark Slee4ac459f2006-10-25 21:39:01 +000063 def writeFieldBegin(self, name, type, id):
64 self.writeByte(type)
65 self.writeI16(id)
Mark Sleecde2b612006-09-03 21:13:07 +000066
Mark Slee4ac459f2006-10-25 21:39:01 +000067 def writeFieldEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000068 pass
69
Mark Slee4ac459f2006-10-25 21:39:01 +000070 def writeFieldStop(self):
71 self.writeByte(TType.STOP);
Mark Sleecde2b612006-09-03 21:13:07 +000072
Mark Slee4ac459f2006-10-25 21:39:01 +000073 def writeMapBegin(self, ktype, vtype, size):
74 self.writeByte(ktype)
75 self.writeByte(vtype)
76 self.writeI32(size)
Mark Sleecde2b612006-09-03 21:13:07 +000077
Mark Slee4ac459f2006-10-25 21:39:01 +000078 def writeMapEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000079 pass
80
Mark Slee4ac459f2006-10-25 21:39:01 +000081 def writeListBegin(self, etype, size):
82 self.writeByte(etype)
83 self.writeI32(size)
Mark Sleecde2b612006-09-03 21:13:07 +000084
Mark Slee4ac459f2006-10-25 21:39:01 +000085 def writeListEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000086 pass
87
Mark Slee4ac459f2006-10-25 21:39:01 +000088 def writeSetBegin(self, etype, size):
89 self.writeByte(etype)
90 self.writeI32(size)
Mark Sleecde2b612006-09-03 21:13:07 +000091
Mark Slee4ac459f2006-10-25 21:39:01 +000092 def writeSetEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000093 pass
94
Mark Slee4ac459f2006-10-25 21:39:01 +000095 def writeBool(self, bool):
Mark Sleecde2b612006-09-03 21:13:07 +000096 if bool:
Mark Slee4ac459f2006-10-25 21:39:01 +000097 self.writeByte(1)
Mark Sleecde2b612006-09-03 21:13:07 +000098 else:
Mark Slee4ac459f2006-10-25 21:39:01 +000099 self.writeByte(0)
David Reiss382fc302007-08-25 18:01:30 +0000100
Mark Slee4ac459f2006-10-25 21:39:01 +0000101 def writeByte(self, byte):
Mark Sleecde2b612006-09-03 21:13:07 +0000102 buff = pack("!b", byte)
Aditya Agarwal5c468192007-02-06 01:14:33 +0000103 self.trans.write(buff)
Mark Sleecde2b612006-09-03 21:13:07 +0000104
Mark Slee4ac459f2006-10-25 21:39:01 +0000105 def writeI16(self, i16):
Mark Sleecde2b612006-09-03 21:13:07 +0000106 buff = pack("!h", i16)
Aditya Agarwal5c468192007-02-06 01:14:33 +0000107 self.trans.write(buff)
Mark Sleecde2b612006-09-03 21:13:07 +0000108
Mark Slee4ac459f2006-10-25 21:39:01 +0000109 def writeI32(self, i32):
Mark Sleecde2b612006-09-03 21:13:07 +0000110 buff = pack("!i", i32)
Aditya Agarwal5c468192007-02-06 01:14:33 +0000111 self.trans.write(buff)
David Reiss382fc302007-08-25 18:01:30 +0000112
Mark Slee4ac459f2006-10-25 21:39:01 +0000113 def writeI64(self, i64):
Mark Sleec9676562006-09-05 17:34:52 +0000114 buff = pack("!q", i64)
Aditya Agarwal5c468192007-02-06 01:14:33 +0000115 self.trans.write(buff)
Mark Sleecde2b612006-09-03 21:13:07 +0000116
Mark Slee4ac459f2006-10-25 21:39:01 +0000117 def writeDouble(self, dub):
Mark Sleec98d0502006-09-06 02:42:25 +0000118 buff = pack("!d", dub)
Aditya Agarwal5c468192007-02-06 01:14:33 +0000119 self.trans.write(buff)
Mark Sleec98d0502006-09-06 02:42:25 +0000120
Mark Slee4ac459f2006-10-25 21:39:01 +0000121 def writeString(self, str):
122 self.writeI32(len(str))
Aditya Agarwal5c468192007-02-06 01:14:33 +0000123 self.trans.write(str)
Mark Sleecde2b612006-09-03 21:13:07 +0000124
Mark Slee4ac459f2006-10-25 21:39:01 +0000125 def readMessageBegin(self):
Mark Slee808454e2007-06-20 21:51:57 +0000126 sz = self.readI32()
127 if sz < 0:
Mark Slee552410c2007-06-22 01:03:55 +0000128 version = sz & TBinaryProtocol.VERSION_MASK
129 if version != TBinaryProtocol.VERSION_1:
Esteve Fernandezbff2a352009-09-24 10:22:00 +0000130 raise TProtocolException(type=TProtocolException.BAD_VERSION, message='Bad version in readMessageBegin: %d' % (sz))
Mark Slee9b36ef32007-10-02 04:44:48 +0000131 type = sz & TBinaryProtocol.TYPE_MASK
Mark Slee808454e2007-06-20 21:51:57 +0000132 name = self.readString()
133 seqid = self.readI32()
134 else:
135 if self.strictRead:
Esteve Fernandezbff2a352009-09-24 10:22:00 +0000136 raise TProtocolException(type=TProtocolException.BAD_VERSION, message='No protocol version header')
Mark Slee808454e2007-06-20 21:51:57 +0000137 name = self.trans.readAll(sz)
138 type = self.readByte()
139 seqid = self.readI32()
Mark Sleecde2b612006-09-03 21:13:07 +0000140 return (name, type, seqid)
141
Mark Slee4ac459f2006-10-25 21:39:01 +0000142 def readMessageEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000143 pass
144
Mark Slee4ac459f2006-10-25 21:39:01 +0000145 def readStructBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000146 pass
147
Mark Slee4ac459f2006-10-25 21:39:01 +0000148 def readStructEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000149 pass
150
Mark Slee4ac459f2006-10-25 21:39:01 +0000151 def readFieldBegin(self):
152 type = self.readByte()
Mark Sleecde2b612006-09-03 21:13:07 +0000153 if type == TType.STOP:
154 return (None, type, 0)
Mark Slee4ac459f2006-10-25 21:39:01 +0000155 id = self.readI16()
Mark Sleecde2b612006-09-03 21:13:07 +0000156 return (None, type, id)
157
Mark Slee4ac459f2006-10-25 21:39:01 +0000158 def readFieldEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000159 pass
160
Mark Slee4ac459f2006-10-25 21:39:01 +0000161 def readMapBegin(self):
162 ktype = self.readByte()
163 vtype = self.readByte()
164 size = self.readI32()
Mark Sleecde2b612006-09-03 21:13:07 +0000165 return (ktype, vtype, size)
166
Mark Slee4ac459f2006-10-25 21:39:01 +0000167 def readMapEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000168 pass
169
Mark Slee4ac459f2006-10-25 21:39:01 +0000170 def readListBegin(self):
171 etype = self.readByte()
172 size = self.readI32()
Mark Sleecde2b612006-09-03 21:13:07 +0000173 return (etype, size)
174
Mark Slee4ac459f2006-10-25 21:39:01 +0000175 def readListEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000176 pass
177
Mark Slee4ac459f2006-10-25 21:39:01 +0000178 def readSetBegin(self):
179 etype = self.readByte()
180 size = self.readI32()
Mark Sleecde2b612006-09-03 21:13:07 +0000181 return (etype, size)
182
Mark Slee4ac459f2006-10-25 21:39:01 +0000183 def readSetEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000184 pass
185
Mark Slee4ac459f2006-10-25 21:39:01 +0000186 def readBool(self):
187 byte = self.readByte()
Mark Sleecde2b612006-09-03 21:13:07 +0000188 if byte == 0:
189 return False
190 return True
191
Mark Slee4ac459f2006-10-25 21:39:01 +0000192 def readByte(self):
Aditya Agarwal5c468192007-02-06 01:14:33 +0000193 buff = self.trans.readAll(1)
Mark Sleecde2b612006-09-03 21:13:07 +0000194 val, = unpack('!b', buff)
195 return val
196
Mark Slee4ac459f2006-10-25 21:39:01 +0000197 def readI16(self):
Aditya Agarwal5c468192007-02-06 01:14:33 +0000198 buff = self.trans.readAll(2)
Mark Sleecde2b612006-09-03 21:13:07 +0000199 val, = unpack('!h', buff)
200 return val
201
Mark Slee4ac459f2006-10-25 21:39:01 +0000202 def readI32(self):
Aditya Agarwal5c468192007-02-06 01:14:33 +0000203 buff = self.trans.readAll(4)
Mark Sleecde2b612006-09-03 21:13:07 +0000204 val, = unpack('!i', buff)
205 return val
206
Mark Slee4ac459f2006-10-25 21:39:01 +0000207 def readI64(self):
Aditya Agarwal5c468192007-02-06 01:14:33 +0000208 buff = self.trans.readAll(8)
Mark Sleec9676562006-09-05 17:34:52 +0000209 val, = unpack('!q', buff)
Mark Sleecde2b612006-09-03 21:13:07 +0000210 return val
211
Mark Slee4ac459f2006-10-25 21:39:01 +0000212 def readDouble(self):
Aditya Agarwal5c468192007-02-06 01:14:33 +0000213 buff = self.trans.readAll(8)
Mark Sleec98d0502006-09-06 02:42:25 +0000214 val, = unpack('!d', buff)
215 return val
216
Mark Slee4ac459f2006-10-25 21:39:01 +0000217 def readString(self):
218 len = self.readI32()
Aditya Agarwal5c468192007-02-06 01:14:33 +0000219 str = self.trans.readAll(len)
Mark Sleecde2b612006-09-03 21:13:07 +0000220 return str
Mark Slee4ac459f2006-10-25 21:39:01 +0000221
David Reiss382fc302007-08-25 18:01:30 +0000222
Mark Slee4ac459f2006-10-25 21:39:01 +0000223class TBinaryProtocolFactory:
Mark Slee808454e2007-06-20 21:51:57 +0000224 def __init__(self, strictRead=False, strictWrite=True):
225 self.strictRead = strictRead
226 self.strictWrite = strictWrite
227
Aditya Agarwal5c468192007-02-06 01:14:33 +0000228 def getProtocol(self, trans):
Mark Slee808454e2007-06-20 21:51:57 +0000229 prot = TBinaryProtocol(trans, self.strictRead, self.strictWrite)
Aditya Agarwal5c468192007-02-06 01:14:33 +0000230 return prot
David Reiss382fc302007-08-25 18:01:30 +0000231
232
233class TBinaryProtocolAccelerated(TBinaryProtocol):
234
235 """C-Accelerated version of TBinaryProtocol.
236
237 This class does not override any of TBinaryProtocol's methods,
238 but the generated code recognizes it directly and will call into
239 our C module to do the encoding, bypassing this object entirely.
240 We inherit from TBinaryProtocol so that the normal TBinaryProtocol
241 encoding can happen if the fastbinary module doesn't work for some
David Reiss5db3e922007-08-30 23:07:45 +0000242 reason. (TODO(dreiss): Make this happen sanely in more cases.)
David Reiss382fc302007-08-25 18:01:30 +0000243
244 In order to take advantage of the C module, just use
245 TBinaryProtocolAccelerated instead of TBinaryProtocol.
246
247 NOTE: This code was contributed by an external developer.
248 The internal Thrift team has reviewed and tested it,
249 but we cannot guarantee that it is production-ready.
250 Please feel free to report bugs and/or success stories
251 to the public mailing list.
252 """
253
254 pass
255
256
257class TBinaryProtocolAcceleratedFactory:
258 def getProtocol(self, trans):
259 return TBinaryProtocolAccelerated(trans)