blob: 7338ff68a76e8cb256c61832b323a4cccf8e9300 [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 Sleee74306a2007-02-21 05:38:12 +000020from thrift.Thrift import *
Mark Sleecde2b612006-09-03 21:13:07 +000021
Mark Slee92195ae2007-02-21 05:16:30 +000022class TProtocolException(TException):
23
24 """Custom Protocol Exception class"""
25
26 UNKNOWN = 0
27 INVALID_DATA = 1
28 NEGATIVE_SIZE = 2
29 SIZE_LIMIT = 3
Mark Slee808454e2007-06-20 21:51:57 +000030 BAD_VERSION = 4
Mark Slee92195ae2007-02-21 05:16:30 +000031
32 def __init__(self, type=UNKNOWN, message=None):
33 TException.__init__(self, message)
34 self.type = type
35
Mark Sleecde2b612006-09-03 21:13:07 +000036class TProtocolBase:
37
38 """Base class for Thrift protocol driver."""
39
Aditya Agarwal5c468192007-02-06 01:14:33 +000040 def __init__(self, trans):
41 self.trans = trans
Mark Slee4ac459f2006-10-25 21:39:01 +000042
43 def writeMessageBegin(self, name, type, seqid):
Mark Sleecde2b612006-09-03 21:13:07 +000044 pass
45
Mark Slee4ac459f2006-10-25 21:39:01 +000046 def writeMessageEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000047 pass
48
Mark Slee4ac459f2006-10-25 21:39:01 +000049 def writeStructBegin(self, name):
Mark Sleecde2b612006-09-03 21:13:07 +000050 pass
51
Mark Slee4ac459f2006-10-25 21:39:01 +000052 def writeStructEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000053 pass
54
Mark Slee4ac459f2006-10-25 21:39:01 +000055 def writeFieldBegin(self, name, type, id):
Mark Sleecde2b612006-09-03 21:13:07 +000056 pass
57
Mark Slee4ac459f2006-10-25 21:39:01 +000058 def writeFieldEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000059 pass
60
Mark Slee4ac459f2006-10-25 21:39:01 +000061 def writeFieldStop(self):
Mark Sleecde2b612006-09-03 21:13:07 +000062 pass
63
Mark Slee4ac459f2006-10-25 21:39:01 +000064 def writeMapBegin(self, ktype, vtype, size):
Mark Sleecde2b612006-09-03 21:13:07 +000065 pass
66
Mark Slee4ac459f2006-10-25 21:39:01 +000067 def writeMapEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000068 pass
69
Mark Slee4ac459f2006-10-25 21:39:01 +000070 def writeListBegin(self, etype, size):
Mark Sleecde2b612006-09-03 21:13:07 +000071 pass
72
Mark Slee4ac459f2006-10-25 21:39:01 +000073 def writeListEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000074 pass
75
Mark Slee4ac459f2006-10-25 21:39:01 +000076 def writeSetBegin(self, etype, size):
Mark Sleecde2b612006-09-03 21:13:07 +000077 pass
78
Mark Slee4ac459f2006-10-25 21:39:01 +000079 def writeSetEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000080 pass
81
Mark Slee4ac459f2006-10-25 21:39:01 +000082 def writeBool(self, bool):
Mark Sleecde2b612006-09-03 21:13:07 +000083 pass
84
Mark Slee4ac459f2006-10-25 21:39:01 +000085 def writeByte(self, byte):
Mark Sleecde2b612006-09-03 21:13:07 +000086 pass
87
Mark Slee4ac459f2006-10-25 21:39:01 +000088 def writeI16(self, i16):
Mark Sleecde2b612006-09-03 21:13:07 +000089 pass
90
Mark Slee4ac459f2006-10-25 21:39:01 +000091 def writeI32(self, i32):
Mark Sleecde2b612006-09-03 21:13:07 +000092 pass
93
Mark Slee4ac459f2006-10-25 21:39:01 +000094 def writeI64(self, i64):
Mark Sleecde2b612006-09-03 21:13:07 +000095 pass
96
Mark Slee4ac459f2006-10-25 21:39:01 +000097 def writeDouble(self, dub):
Mark Sleec98d0502006-09-06 02:42:25 +000098 pass
99
Mark Slee4ac459f2006-10-25 21:39:01 +0000100 def writeString(self, str):
Mark Sleecde2b612006-09-03 21:13:07 +0000101 pass
102
Mark Slee4ac459f2006-10-25 21:39:01 +0000103 def readMessageBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000104 pass
105
Mark Slee4ac459f2006-10-25 21:39:01 +0000106 def readMessageEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000107 pass
108
Mark Slee4ac459f2006-10-25 21:39:01 +0000109 def readStructBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000110 pass
111
Mark Slee4ac459f2006-10-25 21:39:01 +0000112 def readStructEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000113 pass
114
Mark Slee4ac459f2006-10-25 21:39:01 +0000115 def readFieldBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000116 pass
117
Mark Slee4ac459f2006-10-25 21:39:01 +0000118 def readFieldEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000119 pass
120
Mark Slee4ac459f2006-10-25 21:39:01 +0000121 def readMapBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000122 pass
123
Mark Slee4ac459f2006-10-25 21:39:01 +0000124 def readMapEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000125 pass
126
Mark Slee4ac459f2006-10-25 21:39:01 +0000127 def readListBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000128 pass
129
Mark Slee4ac459f2006-10-25 21:39:01 +0000130 def readListEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000131 pass
132
Mark Slee4ac459f2006-10-25 21:39:01 +0000133 def readSetBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000134 pass
135
Mark Slee4ac459f2006-10-25 21:39:01 +0000136 def readSetEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000137 pass
138
Mark Slee4ac459f2006-10-25 21:39:01 +0000139 def readBool(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000140 pass
141
Mark Slee4ac459f2006-10-25 21:39:01 +0000142 def readByte(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000143 pass
144
Mark Slee4ac459f2006-10-25 21:39:01 +0000145 def readI16(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000146 pass
147
Mark Slee4ac459f2006-10-25 21:39:01 +0000148 def readI32(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000149 pass
150
Mark Slee4ac459f2006-10-25 21:39:01 +0000151 def readI64(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000152 pass
153
Mark Slee4ac459f2006-10-25 21:39:01 +0000154 def readDouble(self):
Mark Sleec98d0502006-09-06 02:42:25 +0000155 pass
156
Mark Slee4ac459f2006-10-25 21:39:01 +0000157 def readString(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000158 pass
159
Mark Slee4ac459f2006-10-25 21:39:01 +0000160 def skip(self, type):
Mark Sleecde2b612006-09-03 21:13:07 +0000161 if type == TType.STOP:
162 return
163 elif type == TType.BOOL:
Mark Slee4ac459f2006-10-25 21:39:01 +0000164 self.readBool()
Mark Sleecde2b612006-09-03 21:13:07 +0000165 elif type == TType.BYTE:
Mark Slee4ac459f2006-10-25 21:39:01 +0000166 self.readByte()
Mark Sleecde2b612006-09-03 21:13:07 +0000167 elif type == TType.I16:
Mark Slee4ac459f2006-10-25 21:39:01 +0000168 self.readI16()
Mark Sleecde2b612006-09-03 21:13:07 +0000169 elif type == TType.I32:
Mark Slee4ac459f2006-10-25 21:39:01 +0000170 self.readI32()
Mark Sleecde2b612006-09-03 21:13:07 +0000171 elif type == TType.I64:
Mark Slee4ac459f2006-10-25 21:39:01 +0000172 self.readI64()
Mark Sleec98d0502006-09-06 02:42:25 +0000173 elif type == TType.DOUBLE:
Mark Slee4ac459f2006-10-25 21:39:01 +0000174 self.readDouble()
Mark Sleecde2b612006-09-03 21:13:07 +0000175 elif type == TType.STRING:
Mark Slee4ac459f2006-10-25 21:39:01 +0000176 self.readString()
Mark Sleecde2b612006-09-03 21:13:07 +0000177 elif type == TType.STRUCT:
Mark Slee4ac459f2006-10-25 21:39:01 +0000178 name = self.readStructBegin()
Mark Sleecde2b612006-09-03 21:13:07 +0000179 while True:
Mark Slee4ac459f2006-10-25 21:39:01 +0000180 (name, type, id) = self.readFieldBegin()
Mark Sleecde2b612006-09-03 21:13:07 +0000181 if type == TType.STOP:
182 break
Mark Slee4ac459f2006-10-25 21:39:01 +0000183 self.skip(type)
184 self.readFieldEnd()
185 self.readStructEnd()
Mark Sleecde2b612006-09-03 21:13:07 +0000186 elif type == TType.MAP:
Mark Slee4ac459f2006-10-25 21:39:01 +0000187 (ktype, vtype, size) = self.readMapBegin()
Mark Sleecde2b612006-09-03 21:13:07 +0000188 for i in range(size):
Mark Slee4ac459f2006-10-25 21:39:01 +0000189 self.skip(ktype)
190 self.skip(vtype)
191 self.readMapEnd()
Mark Sleecde2b612006-09-03 21:13:07 +0000192 elif type == TType.SET:
Mark Slee4ac459f2006-10-25 21:39:01 +0000193 (etype, size) = self.readSetBegin()
Mark Sleecde2b612006-09-03 21:13:07 +0000194 for i in range(size):
Mark Slee4ac459f2006-10-25 21:39:01 +0000195 self.skip(etype)
196 self.readSetEnd()
Mark Sleecde2b612006-09-03 21:13:07 +0000197 elif type == TType.LIST:
Mark Slee4ac459f2006-10-25 21:39:01 +0000198 (etype, size) = self.readListBegin()
Mark Sleecde2b612006-09-03 21:13:07 +0000199 for i in range(size):
Mark Slee4ac459f2006-10-25 21:39:01 +0000200 self.skip(etype)
201 self.readListEnd()
Mark Sleecde2b612006-09-03 21:13:07 +0000202
Roger Meierf4eec7a2011-09-11 18:16:21 +0000203 # tuple of: ( 'reader method' name, is_container boolean, 'writer_method' name )
204 _TTYPE_HANDLERS = (
205 (None, None, False), # 0 == TType,STOP
206 (None, None, False), # 1 == TType.VOID # TODO: handle void?
207 ('readBool', 'writeBool', False), # 2 == TType.BOOL
208 ('readByte', 'writeByte', False), # 3 == TType.BYTE and I08
209 ('readDouble', 'writeDouble', False), # 4 == TType.DOUBLE
210 (None, None, False), # 5, undefined
211 ('readI16', 'writeI16', False), # 6 == TType.I16
212 (None, None, False), # 7, undefined
213 ('readI32', 'writeI32', False), # 8 == TType.I32
214 (None, None, False), # 9, undefined
215 ('readI64', 'writeI64', False), # 10 == TType.I64
216 ('readString', 'writeString', False), # 11 == TType.STRING and UTF7
217 ('readContainerStruct', 'writeContainerStruct', True), # 12 == TType.STRUCT
218 ('readContainerMap', 'writeContainerMap', True), # 13 == TType.MAP
219 ('readContainerSet', 'writeContainerSet', True), # 14 == TType.SET
220 ('readContainerList', 'writeContainerList', True), # 15 == TType.LIST
221 (None, None, False), # 16 == TType.UTF8 # TODO: handle utf8 types?
222 (None, None, False)# 17 == TType.UTF16 # TODO: handle utf16 types?
223 )
224
225 def readFieldByTType(self, ttype, spec):
226 try:
227 (r_handler, w_handler, is_container) = self._TTYPE_HANDLERS[ttype]
228 except IndexError:
229 raise TProtocolException(type=TProtocolException.INVALID_DATA,
230 message='Invalid field type %d' % (ttype))
231 if r_handler is None:
232 raise TProtocolException(type=TProtocolException.INVALID_DATA,
233 message='Invalid field type %d' % (ttype))
234 reader = getattr(self, r_handler)
235 if not is_container:
236 return reader()
237 return reader(spec)
238
239 def readContainerList(self, spec):
240 results = []
241 ttype, tspec = spec[0], spec[1]
242 r_handler = self._TTYPE_HANDLERS[ttype][0]
243 reader = getattr(self, r_handler)
244 (list_type, list_len) = self.readListBegin()
245 if tspec is None:
246 # list values are simple types
247 for idx in xrange(list_len):
248 results.append(reader())
249 else:
250 # this is like an inlined readFieldByTType
251 container_reader = self._TTYPE_HANDLERS[list_type][0]
252 val_reader = getattr(self, container_reader)
253 for idx in xrange(list_len):
254 val = val_reader(tspec)
255 results.append(val)
256 self.readListEnd()
257 return results
258
259 def readContainerSet(self, spec):
260 results = set()
261 ttype, tspec = spec[0], spec[1]
262 r_handler = self._TTYPE_HANDLERS[ttype][0]
263 reader = getattr(self, r_handler)
264 (set_type, set_len) = self.readSetBegin()
265 if tspec is None:
266 # set members are simple types
267 for idx in xrange(set_len):
268 results.add(reader())
269 else:
270 container_reader = self._TTYPE_HANDLERS[set_type][0]
271 val_reader = getattr(self, container_reader)
272 for idx in xrange(set_len):
273 results.add(val_reader(tspec))
274 self.readSetEnd()
275 return results
276
277 def readContainerStruct(self, spec):
278 (obj_class, obj_spec) = spec
279 obj = obj_class()
280 obj.read(self)
281 return obj
282
283 def readContainerMap(self, spec):
284 results = dict()
285 key_ttype, key_spec = spec[0], spec[1]
286 val_ttype, val_spec = spec[2], spec[3]
287 (map_ktype, map_vtype, map_len) = self.readMapBegin()
288 # TODO: compare types we just decoded with thrift_spec and abort/skip if types disagree
289 key_reader = getattr(self, self._TTYPE_HANDLERS[key_ttype][0])
290 val_reader = getattr(self, self._TTYPE_HANDLERS[val_ttype][0])
291 # list values are simple types
292 for idx in xrange(map_len):
293 if key_spec is None:
294 k_val = key_reader()
295 else:
296 k_val = self.readFieldByTType(key_ttype, key_spec)
297 if val_spec is None:
298 v_val = val_reader()
299 else:
300 v_val = self.readFieldByTType(val_ttype, val_spec)
301 # this raises a TypeError with unhashable keys types. i.e. d=dict(); d[[0,1]] = 2 fails
302 results[k_val] = v_val
303 self.readMapEnd()
304 return results
305
306 def readStruct(self, obj, thrift_spec):
307 self.readStructBegin()
308 while True:
309 (fname, ftype, fid) = self.readFieldBegin()
310 if ftype == TType.STOP:
311 break
312 try:
313 field = thrift_spec[fid]
314 except IndexError:
315 self.skip(ftype)
316 else:
317 if field is not None and ftype == field[1]:
318 fname = field[2]
319 fspec = field[3]
320 val = self.readFieldByTType(ftype, fspec)
321 setattr(obj, fname, val)
322 else:
323 self.skip(ftype)
324 self.readFieldEnd()
325 self.readStructEnd()
326
327 def writeContainerStruct(self, val, spec):
328 val.write(self)
329
330 def writeContainerList(self, val, spec):
331 self.writeListBegin(spec[0], len(val))
332 r_handler, w_handler, is_container = self._TTYPE_HANDLERS[spec[0]]
333 e_writer = getattr(self, w_handler)
334 if not is_container:
335 for elem in val:
336 e_writer(elem)
337 else:
338 for elem in val:
339 e_writer(elem, spec[1])
340 self.writeListEnd()
341
342 def writeContainerSet(self, val, spec):
343 self.writeSetBegin(spec[0], len(val))
344 r_handler, w_handler, is_container = self._TTYPE_HANDLERS[spec[0]]
345 e_writer = getattr(self, w_handler)
346 if not is_container:
347 for elem in val:
348 e_writer(elem)
349 else:
350 for elem in val:
351 e_writer(elem, spec[1])
352 self.writeSetEnd()
353
354 def writeContainerMap(self, val, spec):
355 k_type = spec[0]
356 v_type = spec[2]
357 ignore, ktype_name, k_is_container = self._TTYPE_HANDLERS[k_type]
358 ignore, vtype_name, v_is_container = self._TTYPE_HANDLERS[v_type]
359 k_writer = getattr(self, ktype_name)
360 v_writer = getattr(self, vtype_name)
361 self.writeMapBegin(k_type, v_type, len(val))
362 for m_key, m_val in val.iteritems():
363 if not k_is_container:
364 k_writer(m_key)
365 else:
366 k_writer(m_key, spec[1])
367 if not v_is_container:
368 v_writer(m_val)
369 else:
370 v_writer(m_val, spec[3])
371 self.writeMapEnd()
372
373 def writeStruct(self, obj, thrift_spec):
374 self.writeStructBegin(obj.__class__.__name__)
375 for field in thrift_spec:
376 if field is None:
377 continue
378 fname = field[2]
379 val = getattr(obj, fname)
380 if val is None:
381 # skip writing out unset fields
382 continue
383 fid = field[0]
384 ftype = field[1]
385 fspec = field[3]
386 # get the writer method for this value
387 self.writeFieldBegin(fname, ftype, fid)
388 self.writeFieldByTType(ftype, val, fspec)
389 self.writeFieldEnd()
390 self.writeFieldStop()
391 self.writeStructEnd()
392
393 def writeFieldByTType(self, ttype, val, spec):
394 r_handler, w_handler, is_container = self._TTYPE_HANDLERS[ttype]
395 writer = getattr(self, w_handler)
396 if is_container:
397 writer(val, spec)
398 else:
399 writer(val)
400
Mark Slee4ac459f2006-10-25 21:39:01 +0000401class TProtocolFactory:
Aditya Agarwal5c468192007-02-06 01:14:33 +0000402 def getProtocol(self, trans):
Mark Slee4ac459f2006-10-25 21:39:01 +0000403 pass
Roger Meierf4eec7a2011-09-11 18:16:21 +0000404