blob: be3cb1403c891f3923089dde1fc652e7514286f0 [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
Mark Slee4ac459f2006-10-25 21:39:01 +0000203class TProtocolFactory:
Aditya Agarwal5c468192007-02-06 01:14:33 +0000204 def getProtocol(self, trans):
Mark Slee4ac459f2006-10-25 21:39:01 +0000205 pass