blob: b2f07081539110b4fdc1f05c6217792d26210a73 [file] [log] [blame]
Mark Slee89e2bb82007-03-01 00:20:36 +00001# Copyright (c) 2006- Facebook
2# Distributed under the Thrift Software License
3#
4# See accompanying file LICENSE or visit the Thrift site at:
5# http://developers.facebook.com/thrift/
6
Mark Sleee74306a2007-02-21 05:38:12 +00007from thrift.Thrift import *
Mark Sleecde2b612006-09-03 21:13:07 +00008
Mark Slee92195ae2007-02-21 05:16:30 +00009class TProtocolException(TException):
10
11 """Custom Protocol Exception class"""
12
13 UNKNOWN = 0
14 INVALID_DATA = 1
15 NEGATIVE_SIZE = 2
16 SIZE_LIMIT = 3
Mark Slee808454e2007-06-20 21:51:57 +000017 BAD_VERSION = 4
Mark Slee92195ae2007-02-21 05:16:30 +000018
19 def __init__(self, type=UNKNOWN, message=None):
20 TException.__init__(self, message)
21 self.type = type
22
Mark Sleecde2b612006-09-03 21:13:07 +000023class TProtocolBase:
24
25 """Base class for Thrift protocol driver."""
26
Aditya Agarwal5c468192007-02-06 01:14:33 +000027 def __init__(self, trans):
28 self.trans = trans
Mark Slee4ac459f2006-10-25 21:39:01 +000029
30 def writeMessageBegin(self, name, type, seqid):
Mark Sleecde2b612006-09-03 21:13:07 +000031 pass
32
Mark Slee4ac459f2006-10-25 21:39:01 +000033 def writeMessageEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000034 pass
35
Mark Slee4ac459f2006-10-25 21:39:01 +000036 def writeStructBegin(self, name):
Mark Sleecde2b612006-09-03 21:13:07 +000037 pass
38
Mark Slee4ac459f2006-10-25 21:39:01 +000039 def writeStructEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000040 pass
41
Mark Slee4ac459f2006-10-25 21:39:01 +000042 def writeFieldBegin(self, name, type, id):
Mark Sleecde2b612006-09-03 21:13:07 +000043 pass
44
Mark Slee4ac459f2006-10-25 21:39:01 +000045 def writeFieldEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000046 pass
47
Mark Slee4ac459f2006-10-25 21:39:01 +000048 def writeFieldStop(self):
Mark Sleecde2b612006-09-03 21:13:07 +000049 pass
50
Mark Slee4ac459f2006-10-25 21:39:01 +000051 def writeMapBegin(self, ktype, vtype, size):
Mark Sleecde2b612006-09-03 21:13:07 +000052 pass
53
Mark Slee4ac459f2006-10-25 21:39:01 +000054 def writeMapEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000055 pass
56
Mark Slee4ac459f2006-10-25 21:39:01 +000057 def writeListBegin(self, etype, size):
Mark Sleecde2b612006-09-03 21:13:07 +000058 pass
59
Mark Slee4ac459f2006-10-25 21:39:01 +000060 def writeListEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000061 pass
62
Mark Slee4ac459f2006-10-25 21:39:01 +000063 def writeSetBegin(self, etype, size):
Mark Sleecde2b612006-09-03 21:13:07 +000064 pass
65
Mark Slee4ac459f2006-10-25 21:39:01 +000066 def writeSetEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000067 pass
68
Mark Slee4ac459f2006-10-25 21:39:01 +000069 def writeBool(self, bool):
Mark Sleecde2b612006-09-03 21:13:07 +000070 pass
71
Mark Slee4ac459f2006-10-25 21:39:01 +000072 def writeByte(self, byte):
Mark Sleecde2b612006-09-03 21:13:07 +000073 pass
74
Mark Slee4ac459f2006-10-25 21:39:01 +000075 def writeI16(self, i16):
Mark Sleecde2b612006-09-03 21:13:07 +000076 pass
77
Mark Slee4ac459f2006-10-25 21:39:01 +000078 def writeI32(self, i32):
Mark Sleecde2b612006-09-03 21:13:07 +000079 pass
80
Mark Slee4ac459f2006-10-25 21:39:01 +000081 def writeI64(self, i64):
Mark Sleecde2b612006-09-03 21:13:07 +000082 pass
83
Mark Slee4ac459f2006-10-25 21:39:01 +000084 def writeDouble(self, dub):
Mark Sleec98d0502006-09-06 02:42:25 +000085 pass
86
Mark Slee4ac459f2006-10-25 21:39:01 +000087 def writeString(self, str):
Mark Sleecde2b612006-09-03 21:13:07 +000088 pass
89
Mark Slee4ac459f2006-10-25 21:39:01 +000090 def readMessageBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +000091 pass
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):
Mark Sleecde2b612006-09-03 21:13:07 +0000103 pass
104
Mark Slee4ac459f2006-10-25 21:39:01 +0000105 def readFieldEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000106 pass
107
Mark Slee4ac459f2006-10-25 21:39:01 +0000108 def readMapBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000109 pass
110
Mark Slee4ac459f2006-10-25 21:39:01 +0000111 def readMapEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000112 pass
113
Mark Slee4ac459f2006-10-25 21:39:01 +0000114 def readListBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000115 pass
116
Mark Slee4ac459f2006-10-25 21:39:01 +0000117 def readListEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000118 pass
119
Mark Slee4ac459f2006-10-25 21:39:01 +0000120 def readSetBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000121 pass
122
Mark Slee4ac459f2006-10-25 21:39:01 +0000123 def readSetEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000124 pass
125
Mark Slee4ac459f2006-10-25 21:39:01 +0000126 def readBool(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000127 pass
128
Mark Slee4ac459f2006-10-25 21:39:01 +0000129 def readByte(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000130 pass
131
Mark Slee4ac459f2006-10-25 21:39:01 +0000132 def readI16(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000133 pass
134
Mark Slee4ac459f2006-10-25 21:39:01 +0000135 def readI32(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000136 pass
137
Mark Slee4ac459f2006-10-25 21:39:01 +0000138 def readI64(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000139 pass
140
Mark Slee4ac459f2006-10-25 21:39:01 +0000141 def readDouble(self):
Mark Sleec98d0502006-09-06 02:42:25 +0000142 pass
143
Mark Slee4ac459f2006-10-25 21:39:01 +0000144 def readString(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000145 pass
146
Mark Slee4ac459f2006-10-25 21:39:01 +0000147 def skip(self, type):
Mark Sleecde2b612006-09-03 21:13:07 +0000148 if type == TType.STOP:
149 return
150 elif type == TType.BOOL:
Mark Slee4ac459f2006-10-25 21:39:01 +0000151 self.readBool()
Mark Sleecde2b612006-09-03 21:13:07 +0000152 elif type == TType.BYTE:
Mark Slee4ac459f2006-10-25 21:39:01 +0000153 self.readByte()
Mark Sleecde2b612006-09-03 21:13:07 +0000154 elif type == TType.I16:
Mark Slee4ac459f2006-10-25 21:39:01 +0000155 self.readI16()
Mark Sleecde2b612006-09-03 21:13:07 +0000156 elif type == TType.I32:
Mark Slee4ac459f2006-10-25 21:39:01 +0000157 self.readI32()
Mark Sleecde2b612006-09-03 21:13:07 +0000158 elif type == TType.I64:
Mark Slee4ac459f2006-10-25 21:39:01 +0000159 self.readI64()
Mark Sleec98d0502006-09-06 02:42:25 +0000160 elif type == TType.DOUBLE:
Mark Slee4ac459f2006-10-25 21:39:01 +0000161 self.readDouble()
Mark Sleecde2b612006-09-03 21:13:07 +0000162 elif type == TType.STRING:
Mark Slee4ac459f2006-10-25 21:39:01 +0000163 self.readString()
Mark Sleecde2b612006-09-03 21:13:07 +0000164 elif type == TType.STRUCT:
Mark Slee4ac459f2006-10-25 21:39:01 +0000165 name = self.readStructBegin()
Mark Sleecde2b612006-09-03 21:13:07 +0000166 while True:
Mark Slee4ac459f2006-10-25 21:39:01 +0000167 (name, type, id) = self.readFieldBegin()
Mark Sleecde2b612006-09-03 21:13:07 +0000168 if type == TType.STOP:
169 break
Mark Slee4ac459f2006-10-25 21:39:01 +0000170 self.skip(type)
171 self.readFieldEnd()
172 self.readStructEnd()
Mark Sleecde2b612006-09-03 21:13:07 +0000173 elif type == TType.MAP:
Mark Slee4ac459f2006-10-25 21:39:01 +0000174 (ktype, vtype, size) = self.readMapBegin()
Mark Sleecde2b612006-09-03 21:13:07 +0000175 for i in range(size):
Mark Slee4ac459f2006-10-25 21:39:01 +0000176 self.skip(ktype)
177 self.skip(vtype)
178 self.readMapEnd()
Mark Sleecde2b612006-09-03 21:13:07 +0000179 elif type == TType.SET:
Mark Slee4ac459f2006-10-25 21:39:01 +0000180 (etype, size) = self.readSetBegin()
Mark Sleecde2b612006-09-03 21:13:07 +0000181 for i in range(size):
Mark Slee4ac459f2006-10-25 21:39:01 +0000182 self.skip(etype)
183 self.readSetEnd()
Mark Sleecde2b612006-09-03 21:13:07 +0000184 elif type == TType.LIST:
Mark Slee4ac459f2006-10-25 21:39:01 +0000185 (etype, size) = self.readListBegin()
Mark Sleecde2b612006-09-03 21:13:07 +0000186 for i in range(size):
Mark Slee4ac459f2006-10-25 21:39:01 +0000187 self.skip(etype)
188 self.readListEnd()
Mark Sleecde2b612006-09-03 21:13:07 +0000189
Mark Slee4ac459f2006-10-25 21:39:01 +0000190class TProtocolFactory:
Aditya Agarwal5c468192007-02-06 01:14:33 +0000191 def getProtocol(self, trans):
Mark Slee4ac459f2006-10-25 21:39:01 +0000192 pass