blob: 146a8024b06c85cbb51a492b954002c18240efca [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 Sleee74306a2007-02-21 05:38:12 +00009from thrift.Thrift import *
Mark Sleecde2b612006-09-03 21:13:07 +000010
Mark Slee92195ae2007-02-21 05:16:30 +000011class TProtocolException(TException):
12
13 """Custom Protocol Exception class"""
14
15 UNKNOWN = 0
16 INVALID_DATA = 1
17 NEGATIVE_SIZE = 2
18 SIZE_LIMIT = 3
Mark Slee808454e2007-06-20 21:51:57 +000019 BAD_VERSION = 4
Mark Slee92195ae2007-02-21 05:16:30 +000020
21 def __init__(self, type=UNKNOWN, message=None):
22 TException.__init__(self, message)
23 self.type = type
24
Mark Sleecde2b612006-09-03 21:13:07 +000025class TProtocolBase:
26
27 """Base class for Thrift protocol driver."""
28
Aditya Agarwal5c468192007-02-06 01:14:33 +000029 def __init__(self, trans):
30 self.trans = trans
Mark Slee4ac459f2006-10-25 21:39:01 +000031
32 def writeMessageBegin(self, name, type, seqid):
Mark Sleecde2b612006-09-03 21:13:07 +000033 pass
34
Mark Slee4ac459f2006-10-25 21:39:01 +000035 def writeMessageEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000036 pass
37
Mark Slee4ac459f2006-10-25 21:39:01 +000038 def writeStructBegin(self, name):
Mark Sleecde2b612006-09-03 21:13:07 +000039 pass
40
Mark Slee4ac459f2006-10-25 21:39:01 +000041 def writeStructEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000042 pass
43
Mark Slee4ac459f2006-10-25 21:39:01 +000044 def writeFieldBegin(self, name, type, id):
Mark Sleecde2b612006-09-03 21:13:07 +000045 pass
46
Mark Slee4ac459f2006-10-25 21:39:01 +000047 def writeFieldEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000048 pass
49
Mark Slee4ac459f2006-10-25 21:39:01 +000050 def writeFieldStop(self):
Mark Sleecde2b612006-09-03 21:13:07 +000051 pass
52
Mark Slee4ac459f2006-10-25 21:39:01 +000053 def writeMapBegin(self, ktype, vtype, size):
Mark Sleecde2b612006-09-03 21:13:07 +000054 pass
55
Mark Slee4ac459f2006-10-25 21:39:01 +000056 def writeMapEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000057 pass
58
Mark Slee4ac459f2006-10-25 21:39:01 +000059 def writeListBegin(self, etype, size):
Mark Sleecde2b612006-09-03 21:13:07 +000060 pass
61
Mark Slee4ac459f2006-10-25 21:39:01 +000062 def writeListEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000063 pass
64
Mark Slee4ac459f2006-10-25 21:39:01 +000065 def writeSetBegin(self, etype, size):
Mark Sleecde2b612006-09-03 21:13:07 +000066 pass
67
Mark Slee4ac459f2006-10-25 21:39:01 +000068 def writeSetEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000069 pass
70
Mark Slee4ac459f2006-10-25 21:39:01 +000071 def writeBool(self, bool):
Mark Sleecde2b612006-09-03 21:13:07 +000072 pass
73
Mark Slee4ac459f2006-10-25 21:39:01 +000074 def writeByte(self, byte):
Mark Sleecde2b612006-09-03 21:13:07 +000075 pass
76
Mark Slee4ac459f2006-10-25 21:39:01 +000077 def writeI16(self, i16):
Mark Sleecde2b612006-09-03 21:13:07 +000078 pass
79
Mark Slee4ac459f2006-10-25 21:39:01 +000080 def writeI32(self, i32):
Mark Sleecde2b612006-09-03 21:13:07 +000081 pass
82
Mark Slee4ac459f2006-10-25 21:39:01 +000083 def writeI64(self, i64):
Mark Sleecde2b612006-09-03 21:13:07 +000084 pass
85
Mark Slee4ac459f2006-10-25 21:39:01 +000086 def writeDouble(self, dub):
Mark Sleec98d0502006-09-06 02:42:25 +000087 pass
88
Mark Slee4ac459f2006-10-25 21:39:01 +000089 def writeString(self, str):
Mark Sleecde2b612006-09-03 21:13:07 +000090 pass
91
Mark Slee4ac459f2006-10-25 21:39:01 +000092 def readMessageBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +000093 pass
94
Mark Slee4ac459f2006-10-25 21:39:01 +000095 def readMessageEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +000096 pass
97
Mark Slee4ac459f2006-10-25 21:39:01 +000098 def readStructBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +000099 pass
100
Mark Slee4ac459f2006-10-25 21:39:01 +0000101 def readStructEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000102 pass
103
Mark Slee4ac459f2006-10-25 21:39:01 +0000104 def readFieldBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000105 pass
106
Mark Slee4ac459f2006-10-25 21:39:01 +0000107 def readFieldEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000108 pass
109
Mark Slee4ac459f2006-10-25 21:39:01 +0000110 def readMapBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000111 pass
112
Mark Slee4ac459f2006-10-25 21:39:01 +0000113 def readMapEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000114 pass
115
Mark Slee4ac459f2006-10-25 21:39:01 +0000116 def readListBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000117 pass
118
Mark Slee4ac459f2006-10-25 21:39:01 +0000119 def readListEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000120 pass
121
Mark Slee4ac459f2006-10-25 21:39:01 +0000122 def readSetBegin(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000123 pass
124
Mark Slee4ac459f2006-10-25 21:39:01 +0000125 def readSetEnd(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000126 pass
127
Mark Slee4ac459f2006-10-25 21:39:01 +0000128 def readBool(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000129 pass
130
Mark Slee4ac459f2006-10-25 21:39:01 +0000131 def readByte(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000132 pass
133
Mark Slee4ac459f2006-10-25 21:39:01 +0000134 def readI16(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000135 pass
136
Mark Slee4ac459f2006-10-25 21:39:01 +0000137 def readI32(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000138 pass
139
Mark Slee4ac459f2006-10-25 21:39:01 +0000140 def readI64(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000141 pass
142
Mark Slee4ac459f2006-10-25 21:39:01 +0000143 def readDouble(self):
Mark Sleec98d0502006-09-06 02:42:25 +0000144 pass
145
Mark Slee4ac459f2006-10-25 21:39:01 +0000146 def readString(self):
Mark Sleecde2b612006-09-03 21:13:07 +0000147 pass
148
Mark Slee4ac459f2006-10-25 21:39:01 +0000149 def skip(self, type):
Mark Sleecde2b612006-09-03 21:13:07 +0000150 if type == TType.STOP:
151 return
152 elif type == TType.BOOL:
Mark Slee4ac459f2006-10-25 21:39:01 +0000153 self.readBool()
Mark Sleecde2b612006-09-03 21:13:07 +0000154 elif type == TType.BYTE:
Mark Slee4ac459f2006-10-25 21:39:01 +0000155 self.readByte()
Mark Sleecde2b612006-09-03 21:13:07 +0000156 elif type == TType.I16:
Mark Slee4ac459f2006-10-25 21:39:01 +0000157 self.readI16()
Mark Sleecde2b612006-09-03 21:13:07 +0000158 elif type == TType.I32:
Mark Slee4ac459f2006-10-25 21:39:01 +0000159 self.readI32()
Mark Sleecde2b612006-09-03 21:13:07 +0000160 elif type == TType.I64:
Mark Slee4ac459f2006-10-25 21:39:01 +0000161 self.readI64()
Mark Sleec98d0502006-09-06 02:42:25 +0000162 elif type == TType.DOUBLE:
Mark Slee4ac459f2006-10-25 21:39:01 +0000163 self.readDouble()
Mark Sleecde2b612006-09-03 21:13:07 +0000164 elif type == TType.STRING:
Mark Slee4ac459f2006-10-25 21:39:01 +0000165 self.readString()
Mark Sleecde2b612006-09-03 21:13:07 +0000166 elif type == TType.STRUCT:
Mark Slee4ac459f2006-10-25 21:39:01 +0000167 name = self.readStructBegin()
Mark Sleecde2b612006-09-03 21:13:07 +0000168 while True:
Mark Slee4ac459f2006-10-25 21:39:01 +0000169 (name, type, id) = self.readFieldBegin()
Mark Sleecde2b612006-09-03 21:13:07 +0000170 if type == TType.STOP:
171 break
Mark Slee4ac459f2006-10-25 21:39:01 +0000172 self.skip(type)
173 self.readFieldEnd()
174 self.readStructEnd()
Mark Sleecde2b612006-09-03 21:13:07 +0000175 elif type == TType.MAP:
Mark Slee4ac459f2006-10-25 21:39:01 +0000176 (ktype, vtype, size) = self.readMapBegin()
Mark Sleecde2b612006-09-03 21:13:07 +0000177 for i in range(size):
Mark Slee4ac459f2006-10-25 21:39:01 +0000178 self.skip(ktype)
179 self.skip(vtype)
180 self.readMapEnd()
Mark Sleecde2b612006-09-03 21:13:07 +0000181 elif type == TType.SET:
Mark Slee4ac459f2006-10-25 21:39:01 +0000182 (etype, size) = self.readSetBegin()
Mark Sleecde2b612006-09-03 21:13:07 +0000183 for i in range(size):
Mark Slee4ac459f2006-10-25 21:39:01 +0000184 self.skip(etype)
185 self.readSetEnd()
Mark Sleecde2b612006-09-03 21:13:07 +0000186 elif type == TType.LIST:
Mark Slee4ac459f2006-10-25 21:39:01 +0000187 (etype, size) = self.readListBegin()
Mark Sleecde2b612006-09-03 21:13:07 +0000188 for i in range(size):
Mark Slee4ac459f2006-10-25 21:39:01 +0000189 self.skip(etype)
190 self.readListEnd()
Mark Sleecde2b612006-09-03 21:13:07 +0000191
Mark Slee4ac459f2006-10-25 21:39:01 +0000192class TProtocolFactory:
Aditya Agarwal5c468192007-02-06 01:14:33 +0000193 def getProtocol(self, trans):
Mark Slee4ac459f2006-10-25 21:39:01 +0000194 pass