blob: 1306fb3d8c318d07befb76ce926ef95aa8cf631b [file] [log] [blame]
Roger Meier6cf0ffc2014-04-05 00:45:42 +02001--
2-- 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--
19
20require 'Thrift'
21
22TProtocolException = TException:new {
23 UNKNOWN = 0,
24 INVALID_DATA = 1,
25 NEGATIVE_SIZE = 2,
26 SIZE_LIMIT = 3,
27 BAD_VERSION = 4,
28 INVALID_PROTOCOL = 5,
Jens Geyer6d1a83a2014-05-03 00:49:05 +020029 DEPTH_LIMIT = 6,
Roger Meier6cf0ffc2014-04-05 00:45:42 +020030 errorCode = 0,
31 __type = 'TProtocolException'
32}
33function TProtocolException:__errorCodeToString()
34 if self.errorCode == self.INVALID_DATA then
35 return 'Invalid data'
36 elseif self.errorCode == self.NEGATIVE_SIZE then
37 return 'Negative size'
38 elseif self.errorCode == self.SIZE_LIMIT then
39 return 'Size limit'
40 elseif self.errorCode == self.BAD_VERSION then
41 return 'Bad version'
42 elseif self.errorCode == self.INVALID_PROTOCOL then
43 return 'Invalid protocol'
Jens Geyer6d1a83a2014-05-03 00:49:05 +020044 elseif self.errorCode == self.DEPTH_LIMIT then
45 return 'Exceeded size limit'
Roger Meier6cf0ffc2014-04-05 00:45:42 +020046 else
47 return 'Default (unknown)'
48 end
49end
50
51TProtocolBase = __TObject:new{
52 __type = 'TProtocolBase',
53 trans
54}
55
56function TProtocolBase:new(obj)
57 if ttype(obj) ~= 'table' then
58 error(ttype(self) .. 'must be initialized with a table')
59 end
60
61 -- Ensure a transport is provided
62 if not obj.trans then
63 error('You must provide ' .. ttype(self) .. ' with a trans')
64 end
65
66 return __TObject.new(self, obj)
67end
68
69function TProtocolBase:writeMessageBegin(name, ttype, seqid) end
70function TProtocolBase:writeMessageEnd() end
71function TProtocolBase:writeStructBegin(name) end
72function TProtocolBase:writeStructEnd() end
73function TProtocolBase:writeFieldBegin(name, ttype, id) end
74function TProtocolBase:writeFieldEnd() end
75function TProtocolBase:writeFieldStop() end
76function TProtocolBase:writeMapBegin(ktype, vtype, size) end
77function TProtocolBase:writeMapEnd() end
78function TProtocolBase:writeListBegin(ttype, size) end
79function TProtocolBase:writeListEnd() end
80function TProtocolBase:writeSetBegin(ttype, size) end
81function TProtocolBase:writeSetEnd() end
82function TProtocolBase:writeBool(bool) end
83function TProtocolBase:writeByte(byte) end
84function TProtocolBase:writeI16(i16) end
85function TProtocolBase:writeI32(i32) end
86function TProtocolBase:writeI64(i64) end
87function TProtocolBase:writeDouble(dub) end
88function TProtocolBase:writeString(str) end
89function TProtocolBase:readMessageBegin() end
90function TProtocolBase:readMessageEnd() end
91function TProtocolBase:readStructBegin() end
92function TProtocolBase:readStructEnd() end
93function TProtocolBase:readFieldBegin() end
94function TProtocolBase:readFieldEnd() end
95function TProtocolBase:readMapBegin() end
96function TProtocolBase:readMapEnd() end
97function TProtocolBase:readListBegin() end
98function TProtocolBase:readListEnd() end
99function TProtocolBase:readSetBegin() end
100function TProtocolBase:readSetEnd() end
101function TProtocolBase:readBool() end
102function TProtocolBase:readByte() end
103function TProtocolBase:readI16() end
104function TProtocolBase:readI32() end
105function TProtocolBase:readI64() end
106function TProtocolBase:readDouble() end
107function TProtocolBase:readString() end
108
109function TProtocolBase:skip(ttype)
James E. King IIIdbc1f8d2019-02-14 16:46:38 -0500110 if ttype == TType.BOOL then
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200111 self:readBool()
112 elseif ttype == TType.BYTE then
113 self:readByte()
114 elseif ttype == TType.I16 then
115 self:readI16()
116 elseif ttype == TType.I32 then
117 self:readI32()
118 elseif ttype == TType.I64 then
119 self:readI64()
120 elseif ttype == TType.DOUBLE then
121 self:readDouble()
122 elseif ttype == TType.STRING then
123 self:readString()
124 elseif ttype == TType.STRUCT then
125 local name = self:readStructBegin()
126 while true do
127 local name, ttype, id = self:readFieldBegin()
128 if ttype == TType.STOP then
129 break
130 end
131 self:skip(ttype)
132 self:readFieldEnd()
133 end
134 self:readStructEnd()
135 elseif ttype == TType.MAP then
136 local kttype, vttype, size = self:readMapBegin()
137 for i = 1, size, 1 do
138 self:skip(kttype)
139 self:skip(vttype)
140 end
141 self:readMapEnd()
142 elseif ttype == TType.SET then
143 local ettype, size = self:readSetBegin()
144 for i = 1, size, 1 do
145 self:skip(ettype)
146 end
147 self:readSetEnd()
148 elseif ttype == TType.LIST then
149 local ettype, size = self:readListBegin()
150 for i = 1, size, 1 do
151 self:skip(ettype)
152 end
153 self:readListEnd()
James E. King IIIdbc1f8d2019-02-14 16:46:38 -0500154 else
155 terror(TProtocolException:new{
156 message = 'Invalid data'
157 })
Roger Meier6cf0ffc2014-04-05 00:45:42 +0200158 end
159end
160
161TProtocolFactory = __TObject:new{
162 __type = 'TProtocolFactory',
163}
164function TProtocolFactory:getProtocol(trans) end