Roger Meier | 6cf0ffc | 2014-04-05 00:45:42 +0200 | [diff] [blame^] | 1 | -- |
| 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 | |
| 20 | require 'Thrift' |
| 21 | |
| 22 | TProtocolException = 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, |
| 29 | MISSING_REQUIRED_FIELD = 6, |
| 30 | errorCode = 0, |
| 31 | __type = 'TProtocolException' |
| 32 | } |
| 33 | function 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' |
| 44 | elseif self.errorCode == self.MISSING_REQUIRED_FIELD then |
| 45 | return 'Missing required field' |
| 46 | else |
| 47 | return 'Default (unknown)' |
| 48 | end |
| 49 | end |
| 50 | |
| 51 | TProtocolBase = __TObject:new{ |
| 52 | __type = 'TProtocolBase', |
| 53 | trans |
| 54 | } |
| 55 | |
| 56 | function 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) |
| 67 | end |
| 68 | |
| 69 | function TProtocolBase:writeMessageBegin(name, ttype, seqid) end |
| 70 | function TProtocolBase:writeMessageEnd() end |
| 71 | function TProtocolBase:writeStructBegin(name) end |
| 72 | function TProtocolBase:writeStructEnd() end |
| 73 | function TProtocolBase:writeFieldBegin(name, ttype, id) end |
| 74 | function TProtocolBase:writeFieldEnd() end |
| 75 | function TProtocolBase:writeFieldStop() end |
| 76 | function TProtocolBase:writeMapBegin(ktype, vtype, size) end |
| 77 | function TProtocolBase:writeMapEnd() end |
| 78 | function TProtocolBase:writeListBegin(ttype, size) end |
| 79 | function TProtocolBase:writeListEnd() end |
| 80 | function TProtocolBase:writeSetBegin(ttype, size) end |
| 81 | function TProtocolBase:writeSetEnd() end |
| 82 | function TProtocolBase:writeBool(bool) end |
| 83 | function TProtocolBase:writeByte(byte) end |
| 84 | function TProtocolBase:writeI16(i16) end |
| 85 | function TProtocolBase:writeI32(i32) end |
| 86 | function TProtocolBase:writeI64(i64) end |
| 87 | function TProtocolBase:writeDouble(dub) end |
| 88 | function TProtocolBase:writeString(str) end |
| 89 | function TProtocolBase:readMessageBegin() end |
| 90 | function TProtocolBase:readMessageEnd() end |
| 91 | function TProtocolBase:readStructBegin() end |
| 92 | function TProtocolBase:readStructEnd() end |
| 93 | function TProtocolBase:readFieldBegin() end |
| 94 | function TProtocolBase:readFieldEnd() end |
| 95 | function TProtocolBase:readMapBegin() end |
| 96 | function TProtocolBase:readMapEnd() end |
| 97 | function TProtocolBase:readListBegin() end |
| 98 | function TProtocolBase:readListEnd() end |
| 99 | function TProtocolBase:readSetBegin() end |
| 100 | function TProtocolBase:readSetEnd() end |
| 101 | function TProtocolBase:readBool() end |
| 102 | function TProtocolBase:readByte() end |
| 103 | function TProtocolBase:readI16() end |
| 104 | function TProtocolBase:readI32() end |
| 105 | function TProtocolBase:readI64() end |
| 106 | function TProtocolBase:readDouble() end |
| 107 | function TProtocolBase:readString() end |
| 108 | |
| 109 | function TProtocolBase:skip(ttype) |
| 110 | if type == TType.STOP then |
| 111 | return |
| 112 | elseif ttype == TType.BOOL then |
| 113 | self:readBool() |
| 114 | elseif ttype == TType.BYTE then |
| 115 | self:readByte() |
| 116 | elseif ttype == TType.I16 then |
| 117 | self:readI16() |
| 118 | elseif ttype == TType.I32 then |
| 119 | self:readI32() |
| 120 | elseif ttype == TType.I64 then |
| 121 | self:readI64() |
| 122 | elseif ttype == TType.DOUBLE then |
| 123 | self:readDouble() |
| 124 | elseif ttype == TType.STRING then |
| 125 | self:readString() |
| 126 | elseif ttype == TType.STRUCT then |
| 127 | local name = self:readStructBegin() |
| 128 | while true do |
| 129 | local name, ttype, id = self:readFieldBegin() |
| 130 | if ttype == TType.STOP then |
| 131 | break |
| 132 | end |
| 133 | self:skip(ttype) |
| 134 | self:readFieldEnd() |
| 135 | end |
| 136 | self:readStructEnd() |
| 137 | elseif ttype == TType.MAP then |
| 138 | local kttype, vttype, size = self:readMapBegin() |
| 139 | for i = 1, size, 1 do |
| 140 | self:skip(kttype) |
| 141 | self:skip(vttype) |
| 142 | end |
| 143 | self:readMapEnd() |
| 144 | elseif ttype == TType.SET then |
| 145 | local ettype, size = self:readSetBegin() |
| 146 | for i = 1, size, 1 do |
| 147 | self:skip(ettype) |
| 148 | end |
| 149 | self:readSetEnd() |
| 150 | elseif ttype == TType.LIST then |
| 151 | local ettype, size = self:readListBegin() |
| 152 | for i = 1, size, 1 do |
| 153 | self:skip(ettype) |
| 154 | end |
| 155 | self:readListEnd() |
| 156 | end |
| 157 | end |
| 158 | |
| 159 | TProtocolFactory = __TObject:new{ |
| 160 | __type = 'TProtocolFactory', |
| 161 | } |
| 162 | function TProtocolFactory:getProtocol(trans) end |