Rev 30 | Blame | Compare with Previous | Last modification | View Log | RSS feed
## Licensed to the Apache Software Foundation (ASF) under one# or more contributor license agreements. See the NOTICE file# distributed with this work for additional information# regarding copyright ownership. The ASF licenses this file# to you under the Apache License, Version 2.0 (the# "License"); you may not use this file except in compliance# with the License. You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing,# software distributed under the License is distributed on an# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY# KIND, either express or implied. See the License for the# specific language governing permissions and limitations# under the License.#from TProtocol import *from struct import pack, unpackclass TBinaryProtocol(TProtocolBase):"""Binary implementation of the Thrift protocol driver."""# NastyHaxx. Python 2.4+ on 32-bit machines forces hex constants to be# positive, converting this into a long. If we hardcode the int value# instead it'll stay in 32 bit-land.# VERSION_MASK = 0xffff0000VERSION_MASK = -65536# VERSION_1 = 0x80010000VERSION_1 = -2147418112TYPE_MASK = 0x000000ffdef __init__(self, trans, strictRead=False, strictWrite=True):TProtocolBase.__init__(self, trans)self.strictRead = strictReadself.strictWrite = strictWritedef writeMessageBegin(self, name, type, seqid):if self.strictWrite:self.writeI32(TBinaryProtocol.VERSION_1 | type)self.writeString(name)self.writeI32(seqid)else:self.writeString(name)self.writeByte(type)self.writeI32(seqid)def writeMessageEnd(self):passdef writeStructBegin(self, name):passdef writeStructEnd(self):passdef writeFieldBegin(self, name, type, id):self.writeByte(type)self.writeI16(id)def writeFieldEnd(self):passdef writeFieldStop(self):self.writeByte(TType.STOP);def writeMapBegin(self, ktype, vtype, size):self.writeByte(ktype)self.writeByte(vtype)self.writeI32(size)def writeMapEnd(self):passdef writeListBegin(self, etype, size):self.writeByte(etype)self.writeI32(size)def writeListEnd(self):passdef writeSetBegin(self, etype, size):self.writeByte(etype)self.writeI32(size)def writeSetEnd(self):passdef writeBool(self, bool):if bool:self.writeByte(1)else:self.writeByte(0)def writeByte(self, byte):buff = pack("!b", byte)self.trans.write(buff)def writeI16(self, i16):buff = pack("!h", i16)self.trans.write(buff)def writeI32(self, i32):buff = pack("!i", i32)self.trans.write(buff)def writeI64(self, i64):buff = pack("!q", i64)self.trans.write(buff)def writeDouble(self, dub):buff = pack("!d", dub)self.trans.write(buff)def writeString(self, str):self.writeI32(len(str))self.trans.write(str)def readMessageBegin(self):sz = self.readI32()if sz < 0:version = sz & TBinaryProtocol.VERSION_MASKif version != TBinaryProtocol.VERSION_1:raise TProtocolException(type=TProtocolException.BAD_VERSION, message='Bad version in readMessageBegin: %d' % (sz))type = sz & TBinaryProtocol.TYPE_MASKname = self.readString()seqid = self.readI32()else:if self.strictRead:raise TProtocolException(type=TProtocolException.BAD_VERSION, message='No protocol version header')name = self.trans.readAll(sz)type = self.readByte()seqid = self.readI32()return (name, type, seqid)def readMessageEnd(self):passdef readStructBegin(self):passdef readStructEnd(self):passdef readFieldBegin(self):type = self.readByte()if type == TType.STOP:return (None, type, 0)id = self.readI16()return (None, type, id)def readFieldEnd(self):passdef readMapBegin(self):ktype = self.readByte()vtype = self.readByte()size = self.readI32()return (ktype, vtype, size)def readMapEnd(self):passdef readListBegin(self):etype = self.readByte()size = self.readI32()return (etype, size)def readListEnd(self):passdef readSetBegin(self):etype = self.readByte()size = self.readI32()return (etype, size)def readSetEnd(self):passdef readBool(self):byte = self.readByte()if byte == 0:return Falsereturn Truedef readByte(self):buff = self.trans.readAll(1)val, = unpack('!b', buff)return valdef readI16(self):buff = self.trans.readAll(2)val, = unpack('!h', buff)return valdef readI32(self):buff = self.trans.readAll(4)val, = unpack('!i', buff)return valdef readI64(self):buff = self.trans.readAll(8)val, = unpack('!q', buff)return valdef readDouble(self):buff = self.trans.readAll(8)val, = unpack('!d', buff)return valdef readString(self):len = self.readI32()str = self.trans.readAll(len)return strclass TBinaryProtocolFactory:def __init__(self, strictRead=False, strictWrite=True):self.strictRead = strictReadself.strictWrite = strictWritedef getProtocol(self, trans):prot = TBinaryProtocol(trans, self.strictRead, self.strictWrite)return protclass TBinaryProtocolAccelerated(TBinaryProtocol):"""C-Accelerated version of TBinaryProtocol.This class does not override any of TBinaryProtocol's methods,but the generated code recognizes it directly and will call intoour C module to do the encoding, bypassing this object entirely.We inherit from TBinaryProtocol so that the normal TBinaryProtocolencoding can happen if the fastbinary module doesn't work for somereason. (TODO(dreiss): Make this happen sanely in more cases.)In order to take advantage of the C module, just useTBinaryProtocolAccelerated instead of TBinaryProtocol.NOTE: This code was contributed by an external developer.The internal Thrift team has reviewed and tested it,but we cannot guarantee that it is production-ready.Please feel free to report bugs and/or success storiesto the public mailing list."""passclass TBinaryProtocolAcceleratedFactory:def getProtocol(self, trans):return TBinaryProtocolAccelerated(trans)