blob: ef655ea5579d30f7b9dfa167209d7ad18801a325 [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
Bryan Duxbury69720412012-01-03 17:32:30 +000020
Nobuaki Sukegawab9c859a2015-12-21 01:10:25 +090021class TType(object):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090022 STOP = 0
23 VOID = 1
24 BOOL = 2
25 BYTE = 3
26 I08 = 3
27 DOUBLE = 4
28 I16 = 6
29 I32 = 8
30 I64 = 10
31 STRING = 11
32 UTF7 = 11
33 STRUCT = 12
34 MAP = 13
35 SET = 14
36 LIST = 15
37 UTF8 = 16
38 UTF16 = 17
Mark Sleee74306a2007-02-21 05:38:12 +000039
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090040 _VALUES_TO_NAMES = (
41 'STOP',
42 'VOID',
43 'BOOL',
44 'BYTE',
45 'DOUBLE',
46 None,
47 'I16',
48 None,
49 'I32',
50 None,
51 'I64',
52 'STRING',
53 'STRUCT',
54 'MAP',
55 'SET',
56 'LIST',
57 'UTF8',
58 'UTF16',
59 )
Bryan Duxbury69720412012-01-03 17:32:30 +000060
Roger Meierf4eec7a2011-09-11 18:16:21 +000061
Nobuaki Sukegawab9c859a2015-12-21 01:10:25 +090062class TMessageType(object):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090063 CALL = 1
64 REPLY = 2
65 EXCEPTION = 3
66 ONEWAY = 4
Mark Sleee74306a2007-02-21 05:38:12 +000067
Mark Sleec9676562006-09-05 17:34:52 +000068
Nobuaki Sukegawab9c859a2015-12-21 01:10:25 +090069class TProcessor(object):
James E. King, IIIe7611d02017-10-23 16:44:45 -040070 """Base class for processor, which works on two streams."""
Mark Sleec9676562006-09-05 17:34:52 +000071
James E. King, IIIe7611d02017-10-23 16:44:45 -040072 def process(self, iprot, oprot):
James E. King III393f6c92019-02-09 10:35:44 -050073 """
74 Process a request. The normal behvaior is to have the
75 processor invoke the correct handler and then it is the
76 server's responsibility to write the response to oprot.
77 """
78 pass
79
80 def on_message_begin(self, func):
81 """
82 Install a callback that receives (name, type, seqid)
83 after the message header is read.
84 """
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090085 pass
Mark Slee92195ae2007-02-21 05:16:30 +000086
Mark Slee92195ae2007-02-21 05:16:30 +000087
Bryan Duxbury69720412012-01-03 17:32:30 +000088class TException(Exception):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090089 """Base class for all thrift exceptions."""
Mark Slee92195ae2007-02-21 05:16:30 +000090
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090091 def __init__(self, message=None):
92 Exception.__init__(self, message)
93 self.message = message
Mark Slee92195ae2007-02-21 05:16:30 +000094
Mark Slee92195ae2007-02-21 05:16:30 +000095
Bryan Duxbury69720412012-01-03 17:32:30 +000096class TApplicationException(TException):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090097 """Application level thrift exceptions."""
Mark Slee92195ae2007-02-21 05:16:30 +000098
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +090099 UNKNOWN = 0
100 UNKNOWN_METHOD = 1
101 INVALID_MESSAGE_TYPE = 2
102 WRONG_METHOD_NAME = 3
103 BAD_SEQUENCE_ID = 4
104 MISSING_RESULT = 5
105 INTERNAL_ERROR = 6
106 PROTOCOL_ERROR = 7
107 INVALID_TRANSFORM = 8
108 INVALID_PROTOCOL = 9
109 UNSUPPORTED_CLIENT_TYPE = 10
Mark Slee92195ae2007-02-21 05:16:30 +0000110
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900111 def __init__(self, type=UNKNOWN, message=None):
112 TException.__init__(self, message)
113 self.type = type
Mark Slee92195ae2007-02-21 05:16:30 +0000114
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900115 def __str__(self):
116 if self.message:
117 return self.message
118 elif self.type == self.UNKNOWN_METHOD:
119 return 'Unknown method'
120 elif self.type == self.INVALID_MESSAGE_TYPE:
121 return 'Invalid message type'
122 elif self.type == self.WRONG_METHOD_NAME:
123 return 'Wrong method name'
124 elif self.type == self.BAD_SEQUENCE_ID:
125 return 'Bad sequence ID'
126 elif self.type == self.MISSING_RESULT:
127 return 'Missing result'
128 elif self.type == self.INTERNAL_ERROR:
129 return 'Internal error'
130 elif self.type == self.PROTOCOL_ERROR:
131 return 'Protocol error'
132 elif self.type == self.INVALID_TRANSFORM:
133 return 'Invalid transform'
134 elif self.type == self.INVALID_PROTOCOL:
135 return 'Invalid protocol'
136 elif self.type == self.UNSUPPORTED_CLIENT_TYPE:
137 return 'Unsupported client type'
Mark Slee92195ae2007-02-21 05:16:30 +0000138 else:
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900139 return 'Default (unknown) TApplicationException'
Mark Slee92195ae2007-02-21 05:16:30 +0000140
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900141 def read(self, iprot):
142 iprot.readStructBegin()
143 while True:
144 (fname, ftype, fid) = iprot.readFieldBegin()
145 if ftype == TType.STOP:
146 break
147 if fid == 1:
148 if ftype == TType.STRING:
149 self.message = iprot.readString()
150 else:
151 iprot.skip(ftype)
152 elif fid == 2:
153 if ftype == TType.I32:
154 self.type = iprot.readI32()
155 else:
156 iprot.skip(ftype)
157 else:
158 iprot.skip(ftype)
159 iprot.readFieldEnd()
160 iprot.readStructEnd()
161
162 def write(self, oprot):
163 oprot.writeStructBegin('TApplicationException')
164 if self.message is not None:
165 oprot.writeFieldBegin('message', TType.STRING, 1)
166 oprot.writeString(self.message)
167 oprot.writeFieldEnd()
168 if self.type is not None:
169 oprot.writeFieldBegin('type', TType.I32, 2)
170 oprot.writeI32(self.type)
171 oprot.writeFieldEnd()
172 oprot.writeFieldStop()
173 oprot.writeStructEnd()
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +0900174
175
176class TFrozenDict(dict):
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900177 """A dictionary that is "frozen" like a frozenset"""
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +0900178
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900179 def __init__(self, *args, **kwargs):
180 super(TFrozenDict, self).__init__(*args, **kwargs)
181 # Sort the items so they will be in a consistent order.
182 # XOR in the hash of the class so we don't collide with
183 # the hash of a list of tuples.
184 self.__hashval = hash(TFrozenDict) ^ hash(tuple(sorted(self.items())))
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +0900185
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900186 def __setitem__(self, *args):
187 raise TypeError("Can't modify frozen TFreezableDict")
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +0900188
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900189 def __delitem__(self, *args):
190 raise TypeError("Can't modify frozen TFreezableDict")
Nobuaki Sukegawae841b3d2015-11-17 11:01:17 +0900191
Nobuaki Sukegawa10308cb2016-02-03 01:57:03 +0900192 def __hash__(self):
193 return self.__hashval