blob: 93734d4b0681e26dec3042ac0101c9bcd70a26ea [file] [log] [blame]
Mark Slee89e2bb82007-03-01 00:20:36 +00001#!/usr/bin/env python
2#
3# Copyright (c) 2006- Facebook
4# Distributed under the Thrift Software License
5#
6# See accompanying file LICENSE or visit the Thrift site at:
7# http://developers.facebook.com/thrift/
8
Mark Sleecde2b612006-09-03 21:13:07 +00009from TProtocol import *
10from struct import pack, unpack
11
12class TBinaryProtocol(TProtocolBase):
13
14 """Binary implementation of the Thrift protocol driver."""
15
Aditya Agarwal5c468192007-02-06 01:14:33 +000016 def __init__(self, trans):
17 TProtocolBase.__init__(self, trans)
Mark Sleecde2b612006-09-03 21:13:07 +000018
Mark Slee4ac459f2006-10-25 21:39:01 +000019 def writeMessageBegin(self, name, type, seqid):
20 self.writeString(name)
21 self.writeByte(type)
22 self.writeI32(seqid)
23
24 def writeMessageEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000025 pass
26
Mark Slee4ac459f2006-10-25 21:39:01 +000027 def writeStructBegin(self, name):
Mark Sleecde2b612006-09-03 21:13:07 +000028 pass
29
Mark Slee4ac459f2006-10-25 21:39:01 +000030 def writeStructEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000031 pass
32
Mark Slee4ac459f2006-10-25 21:39:01 +000033 def writeFieldBegin(self, name, type, id):
34 self.writeByte(type)
35 self.writeI16(id)
Mark Sleecde2b612006-09-03 21:13:07 +000036
Mark Slee4ac459f2006-10-25 21:39:01 +000037 def writeFieldEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000038 pass
39
Mark Slee4ac459f2006-10-25 21:39:01 +000040 def writeFieldStop(self):
41 self.writeByte(TType.STOP);
Mark Sleecde2b612006-09-03 21:13:07 +000042
Mark Slee4ac459f2006-10-25 21:39:01 +000043 def writeMapBegin(self, ktype, vtype, size):
44 self.writeByte(ktype)
45 self.writeByte(vtype)
46 self.writeI32(size)
Mark Sleecde2b612006-09-03 21:13:07 +000047
Mark Slee4ac459f2006-10-25 21:39:01 +000048 def writeMapEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000049 pass
50
Mark Slee4ac459f2006-10-25 21:39:01 +000051 def writeListBegin(self, etype, size):
52 self.writeByte(etype)
53 self.writeI32(size)
Mark Sleecde2b612006-09-03 21:13:07 +000054
Mark Slee4ac459f2006-10-25 21:39:01 +000055 def writeListEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000056 pass
57
Mark Slee4ac459f2006-10-25 21:39:01 +000058 def writeSetBegin(self, etype, size):
59 self.writeByte(etype)
60 self.writeI32(size)
Mark Sleecde2b612006-09-03 21:13:07 +000061
Mark Slee4ac459f2006-10-25 21:39:01 +000062 def writeSetEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000063 pass
64
Mark Slee4ac459f2006-10-25 21:39:01 +000065 def writeBool(self, bool):
Mark Sleecde2b612006-09-03 21:13:07 +000066 if bool:
Mark Slee4ac459f2006-10-25 21:39:01 +000067 self.writeByte(1)
Mark Sleecde2b612006-09-03 21:13:07 +000068 else:
Mark Slee4ac459f2006-10-25 21:39:01 +000069 self.writeByte(0)
Mark Sleecde2b612006-09-03 21:13:07 +000070
Mark Slee4ac459f2006-10-25 21:39:01 +000071 def writeByte(self, byte):
Mark Sleecde2b612006-09-03 21:13:07 +000072 buff = pack("!b", byte)
Aditya Agarwal5c468192007-02-06 01:14:33 +000073 self.trans.write(buff)
Mark Sleecde2b612006-09-03 21:13:07 +000074
Mark Slee4ac459f2006-10-25 21:39:01 +000075 def writeI16(self, i16):
Mark Sleecde2b612006-09-03 21:13:07 +000076 buff = pack("!h", i16)
Aditya Agarwal5c468192007-02-06 01:14:33 +000077 self.trans.write(buff)
Mark Sleecde2b612006-09-03 21:13:07 +000078
Mark Slee4ac459f2006-10-25 21:39:01 +000079 def writeI32(self, i32):
Mark Sleecde2b612006-09-03 21:13:07 +000080 buff = pack("!i", i32)
Aditya Agarwal5c468192007-02-06 01:14:33 +000081 self.trans.write(buff)
Mark Sleecde2b612006-09-03 21:13:07 +000082
Mark Slee4ac459f2006-10-25 21:39:01 +000083 def writeI64(self, i64):
Mark Sleec9676562006-09-05 17:34:52 +000084 buff = pack("!q", i64)
Aditya Agarwal5c468192007-02-06 01:14:33 +000085 self.trans.write(buff)
Mark Sleecde2b612006-09-03 21:13:07 +000086
Mark Slee4ac459f2006-10-25 21:39:01 +000087 def writeDouble(self, dub):
Mark Sleec98d0502006-09-06 02:42:25 +000088 buff = pack("!d", dub)
Aditya Agarwal5c468192007-02-06 01:14:33 +000089 self.trans.write(buff)
Mark Sleec98d0502006-09-06 02:42:25 +000090
Mark Slee4ac459f2006-10-25 21:39:01 +000091 def writeString(self, str):
92 self.writeI32(len(str))
Aditya Agarwal5c468192007-02-06 01:14:33 +000093 self.trans.write(str)
Mark Sleecde2b612006-09-03 21:13:07 +000094
Mark Slee4ac459f2006-10-25 21:39:01 +000095 def readMessageBegin(self):
96 name = self.readString()
97 type = self.readByte()
98 seqid = self.readI32()
Mark Sleecde2b612006-09-03 21:13:07 +000099 return (name, type, seqid)
100
Mark Slee4ac459f2006-10-25 21:39:01 +0000101 def readMessageEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000102 pass
103
Mark Slee4ac459f2006-10-25 21:39:01 +0000104 def readStructBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000105 pass
106
Mark Slee4ac459f2006-10-25 21:39:01 +0000107 def readStructEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000108 pass
109
Mark Slee4ac459f2006-10-25 21:39:01 +0000110 def readFieldBegin(self):
111 type = self.readByte()
Mark Sleecde2b612006-09-03 21:13:07 +0000112 if type == TType.STOP:
113 return (None, type, 0)
Mark Slee4ac459f2006-10-25 21:39:01 +0000114 id = self.readI16()
Mark Sleecde2b612006-09-03 21:13:07 +0000115 return (None, type, id)
116
Mark Slee4ac459f2006-10-25 21:39:01 +0000117 def readFieldEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000118 pass
119
Mark Slee4ac459f2006-10-25 21:39:01 +0000120 def readMapBegin(self):
121 ktype = self.readByte()
122 vtype = self.readByte()
123 size = self.readI32()
Mark Sleecde2b612006-09-03 21:13:07 +0000124 return (ktype, vtype, size)
125
Mark Slee4ac459f2006-10-25 21:39:01 +0000126 def readMapEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000127 pass
128
Mark Slee4ac459f2006-10-25 21:39:01 +0000129 def readListBegin(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 readListEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000135 pass
136
Mark Slee4ac459f2006-10-25 21:39:01 +0000137 def readSetBegin(self):
138 etype = self.readByte()
139 size = self.readI32()
Mark Sleecde2b612006-09-03 21:13:07 +0000140 return (etype, size)
141
Mark Slee4ac459f2006-10-25 21:39:01 +0000142 def readSetEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000143 pass
144
Mark Slee4ac459f2006-10-25 21:39:01 +0000145 def readBool(self):
146 byte = self.readByte()
Mark Sleecde2b612006-09-03 21:13:07 +0000147 if byte == 0:
148 return False
149 return True
150
Mark Slee4ac459f2006-10-25 21:39:01 +0000151 def readByte(self):
Aditya Agarwal5c468192007-02-06 01:14:33 +0000152 buff = self.trans.readAll(1)
Mark Sleecde2b612006-09-03 21:13:07 +0000153 val, = unpack('!b', buff)
154 return val
155
Mark Slee4ac459f2006-10-25 21:39:01 +0000156 def readI16(self):
Aditya Agarwal5c468192007-02-06 01:14:33 +0000157 buff = self.trans.readAll(2)
Mark Sleecde2b612006-09-03 21:13:07 +0000158 val, = unpack('!h', buff)
159 return val
160
Mark Slee4ac459f2006-10-25 21:39:01 +0000161 def readI32(self):
Aditya Agarwal5c468192007-02-06 01:14:33 +0000162 buff = self.trans.readAll(4)
Mark Sleecde2b612006-09-03 21:13:07 +0000163 val, = unpack('!i', buff)
164 return val
165
Mark Slee4ac459f2006-10-25 21:39:01 +0000166 def readI64(self):
Aditya Agarwal5c468192007-02-06 01:14:33 +0000167 buff = self.trans.readAll(8)
Mark Sleec9676562006-09-05 17:34:52 +0000168 val, = unpack('!q', buff)
Mark Sleecde2b612006-09-03 21:13:07 +0000169 return val
170
Mark Slee4ac459f2006-10-25 21:39:01 +0000171 def readDouble(self):
Aditya Agarwal5c468192007-02-06 01:14:33 +0000172 buff = self.trans.readAll(8)
Mark Sleec98d0502006-09-06 02:42:25 +0000173 val, = unpack('!d', buff)
174 return val
175
Mark Slee4ac459f2006-10-25 21:39:01 +0000176 def readString(self):
177 len = self.readI32()
Aditya Agarwal5c468192007-02-06 01:14:33 +0000178 str = self.trans.readAll(len)
Mark Sleecde2b612006-09-03 21:13:07 +0000179 return str
Mark Slee4ac459f2006-10-25 21:39:01 +0000180
181class TBinaryProtocolFactory:
Aditya Agarwal5c468192007-02-06 01:14:33 +0000182 def getProtocol(self, trans):
183 prot = TBinaryProtocol(trans)
184 return prot