blob: 8275e347aa0030d5f063957a86e79c8b4d3d84c5 [file] [log] [blame]
Mark Sleecde2b612006-09-03 21:13:07 +00001from TProtocol import *
2from struct import pack, unpack
3
4class TBinaryProtocol(TProtocolBase):
5
6 """Binary implementation of the Thrift protocol driver."""
7
Mark Slee4ac459f2006-10-25 21:39:01 +00008 def __init__(self, itrans, otrans=None):
9 TProtocolBase.__init__(self, itrans, otrans)
Mark Sleecde2b612006-09-03 21:13:07 +000010
Mark Slee4ac459f2006-10-25 21:39:01 +000011 def writeMessageBegin(self, name, type, seqid):
12 self.writeString(name)
13 self.writeByte(type)
14 self.writeI32(seqid)
15
16 def writeMessageEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000017 pass
18
Mark Slee4ac459f2006-10-25 21:39:01 +000019 def writeStructBegin(self, name):
Mark Sleecde2b612006-09-03 21:13:07 +000020 pass
21
Mark Slee4ac459f2006-10-25 21:39:01 +000022 def writeStructEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000023 pass
24
Mark Slee4ac459f2006-10-25 21:39:01 +000025 def writeFieldBegin(self, name, type, id):
26 self.writeByte(type)
27 self.writeI16(id)
Mark Sleecde2b612006-09-03 21:13:07 +000028
Mark Slee4ac459f2006-10-25 21:39:01 +000029 def writeFieldEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000030 pass
31
Mark Slee4ac459f2006-10-25 21:39:01 +000032 def writeFieldStop(self):
33 self.writeByte(TType.STOP);
Mark Sleecde2b612006-09-03 21:13:07 +000034
Mark Slee4ac459f2006-10-25 21:39:01 +000035 def writeMapBegin(self, ktype, vtype, size):
36 self.writeByte(ktype)
37 self.writeByte(vtype)
38 self.writeI32(size)
Mark Sleecde2b612006-09-03 21:13:07 +000039
Mark Slee4ac459f2006-10-25 21:39:01 +000040 def writeMapEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000041 pass
42
Mark Slee4ac459f2006-10-25 21:39:01 +000043 def writeListBegin(self, etype, size):
44 self.writeByte(etype)
45 self.writeI32(size)
Mark Sleecde2b612006-09-03 21:13:07 +000046
Mark Slee4ac459f2006-10-25 21:39:01 +000047 def writeListEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000048 pass
49
Mark Slee4ac459f2006-10-25 21:39:01 +000050 def writeSetBegin(self, etype, size):
51 self.writeByte(etype)
52 self.writeI32(size)
Mark Sleecde2b612006-09-03 21:13:07 +000053
Mark Slee4ac459f2006-10-25 21:39:01 +000054 def writeSetEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000055 pass
56
Mark Slee4ac459f2006-10-25 21:39:01 +000057 def writeBool(self, bool):
Mark Sleecde2b612006-09-03 21:13:07 +000058 if bool:
Mark Slee4ac459f2006-10-25 21:39:01 +000059 self.writeByte(1)
Mark Sleecde2b612006-09-03 21:13:07 +000060 else:
Mark Slee4ac459f2006-10-25 21:39:01 +000061 self.writeByte(0)
Mark Sleecde2b612006-09-03 21:13:07 +000062
Mark Slee4ac459f2006-10-25 21:39:01 +000063 def writeByte(self, byte):
Mark Sleecde2b612006-09-03 21:13:07 +000064 buff = pack("!b", byte)
Mark Slee4ac459f2006-10-25 21:39:01 +000065 self.otrans.write(buff)
Mark Sleecde2b612006-09-03 21:13:07 +000066
Mark Slee4ac459f2006-10-25 21:39:01 +000067 def writeI16(self, i16):
Mark Sleecde2b612006-09-03 21:13:07 +000068 buff = pack("!h", i16)
Mark Slee4ac459f2006-10-25 21:39:01 +000069 self.otrans.write(buff)
Mark Sleecde2b612006-09-03 21:13:07 +000070
Mark Slee4ac459f2006-10-25 21:39:01 +000071 def writeI32(self, i32):
Mark Sleecde2b612006-09-03 21:13:07 +000072 buff = pack("!i", i32)
Mark Slee4ac459f2006-10-25 21:39:01 +000073 self.otrans.write(buff)
Mark Sleecde2b612006-09-03 21:13:07 +000074
Mark Slee4ac459f2006-10-25 21:39:01 +000075 def writeI64(self, i64):
Mark Sleec9676562006-09-05 17:34:52 +000076 buff = pack("!q", i64)
Mark Slee4ac459f2006-10-25 21:39:01 +000077 self.otrans.write(buff)
Mark Sleecde2b612006-09-03 21:13:07 +000078
Mark Slee4ac459f2006-10-25 21:39:01 +000079 def writeDouble(self, dub):
Mark Sleec98d0502006-09-06 02:42:25 +000080 buff = pack("!d", dub)
Mark Slee4ac459f2006-10-25 21:39:01 +000081 self.otrans.write(buff)
Mark Sleec98d0502006-09-06 02:42:25 +000082
Mark Slee4ac459f2006-10-25 21:39:01 +000083 def writeString(self, str):
84 self.writeI32(len(str))
85 self.otrans.write(str)
Mark Sleecde2b612006-09-03 21:13:07 +000086
Mark Slee4ac459f2006-10-25 21:39:01 +000087 def readMessageBegin(self):
88 name = self.readString()
89 type = self.readByte()
90 seqid = self.readI32()
Mark Sleecde2b612006-09-03 21:13:07 +000091 return (name, type, seqid)
92
Mark Slee4ac459f2006-10-25 21:39:01 +000093 def readMessageEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000094 pass
95
Mark Slee4ac459f2006-10-25 21:39:01 +000096 def readStructBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +000097 pass
98
Mark Slee4ac459f2006-10-25 21:39:01 +000099 def readStructEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000100 pass
101
Mark Slee4ac459f2006-10-25 21:39:01 +0000102 def readFieldBegin(self):
103 type = self.readByte()
Mark Sleecde2b612006-09-03 21:13:07 +0000104 if type == TType.STOP:
105 return (None, type, 0)
Mark Slee4ac459f2006-10-25 21:39:01 +0000106 id = self.readI16()
Mark Sleecde2b612006-09-03 21:13:07 +0000107 return (None, type, id)
108
Mark Slee4ac459f2006-10-25 21:39:01 +0000109 def readFieldEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000110 pass
111
Mark Slee4ac459f2006-10-25 21:39:01 +0000112 def readMapBegin(self):
113 ktype = self.readByte()
114 vtype = self.readByte()
115 size = self.readI32()
Mark Sleecde2b612006-09-03 21:13:07 +0000116 return (ktype, vtype, size)
117
Mark Slee4ac459f2006-10-25 21:39:01 +0000118 def readMapEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000119 pass
120
Mark Slee4ac459f2006-10-25 21:39:01 +0000121 def readListBegin(self):
122 etype = self.readByte()
123 size = self.readI32()
Mark Sleecde2b612006-09-03 21:13:07 +0000124 return (etype, size)
125
Mark Slee4ac459f2006-10-25 21:39:01 +0000126 def readListEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000127 pass
128
Mark Slee4ac459f2006-10-25 21:39:01 +0000129 def readSetBegin(self):
130 etype = self.readByte()
131 size = self.readI32()
Mark Sleecde2b612006-09-03 21:13:07 +0000132 return (etype, size)
133
Mark Slee4ac459f2006-10-25 21:39:01 +0000134 def readSetEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000135 pass
136
Mark Slee4ac459f2006-10-25 21:39:01 +0000137 def readBool(self):
138 byte = self.readByte()
Mark Sleecde2b612006-09-03 21:13:07 +0000139 if byte == 0:
140 return False
141 return True
142
Mark Slee4ac459f2006-10-25 21:39:01 +0000143 def readByte(self):
144 buff = self.itrans.readAll(1)
Mark Sleecde2b612006-09-03 21:13:07 +0000145 val, = unpack('!b', buff)
146 return val
147
Mark Slee4ac459f2006-10-25 21:39:01 +0000148 def readI16(self):
149 buff = self.itrans.readAll(2)
Mark Sleecde2b612006-09-03 21:13:07 +0000150 val, = unpack('!h', buff)
151 return val
152
Mark Slee4ac459f2006-10-25 21:39:01 +0000153 def readI32(self):
154 buff = self.itrans.readAll(4)
Mark Sleecde2b612006-09-03 21:13:07 +0000155 val, = unpack('!i', buff)
156 return val
157
Mark Slee4ac459f2006-10-25 21:39:01 +0000158 def readI64(self):
159 buff = self.itrans.readAll(8)
Mark Sleec9676562006-09-05 17:34:52 +0000160 val, = unpack('!q', buff)
Mark Sleecde2b612006-09-03 21:13:07 +0000161 return val
162
Mark Slee4ac459f2006-10-25 21:39:01 +0000163 def readDouble(self):
164 buff = self.itrans.readAll(8)
Mark Sleec98d0502006-09-06 02:42:25 +0000165 val, = unpack('!d', buff)
166 return val
167
Mark Slee4ac459f2006-10-25 21:39:01 +0000168 def readString(self):
169 len = self.readI32()
170 str = self.itrans.readAll(len)
Mark Sleecde2b612006-09-03 21:13:07 +0000171 return str
Mark Slee4ac459f2006-10-25 21:39:01 +0000172
173class TBinaryProtocolFactory:
174 def getIOProtocols(self, itrans, otrans):
175 prot = TBinaryProtocol(itrans, otrans)
176 return (prot, prot)