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 | ---- namespace thrift |
| 21 | --thrift = {} |
| 22 | --setmetatable(thrift, {__index = _G}) --> perf hit for accessing global methods |
| 23 | --setfenv(1, thrift) |
| 24 | |
| 25 | package.cpath = package.cpath .. ';bin/?.so' -- TODO FIX |
| 26 | function ttype(obj) |
| 27 | if type(obj) == 'table' and |
| 28 | obj.__type and |
| 29 | type(obj.__type) == 'string' then |
| 30 | return obj.__type |
| 31 | end |
| 32 | return type(obj) |
| 33 | end |
| 34 | |
| 35 | function terror(e) |
| 36 | if e and e.__tostring then |
| 37 | error(e:__tostring()) |
| 38 | return |
| 39 | end |
| 40 | error(e) |
| 41 | end |
| 42 | |
| 43 | version = 1.0 |
| 44 | |
| 45 | TType = { |
| 46 | STOP = 0, |
| 47 | VOID = 1, |
| 48 | BOOL = 2, |
| 49 | BYTE = 3, |
| 50 | I08 = 3, |
| 51 | DOUBLE = 4, |
| 52 | I16 = 6, |
| 53 | I32 = 8, |
| 54 | I64 = 10, |
| 55 | STRING = 11, |
| 56 | UTF7 = 11, |
| 57 | STRUCT = 12, |
| 58 | MAP = 13, |
| 59 | SET = 14, |
| 60 | LIST = 15, |
| 61 | UTF8 = 16, |
| 62 | UTF16 = 17 |
| 63 | } |
| 64 | |
| 65 | TMessageType = { |
| 66 | CALL = 1, |
| 67 | REPLY = 2, |
| 68 | EXCEPTION = 3, |
| 69 | ONEWAY = 4 |
| 70 | } |
| 71 | |
| 72 | -- Recursive __index function to achive inheritance |
| 73 | function __tobj_index(self, key) |
| 74 | local v = rawget(self, key) |
| 75 | if v ~= nil then |
| 76 | return v |
| 77 | end |
| 78 | |
| 79 | local p = rawget(self, '__parent') |
| 80 | if p then |
| 81 | return __tobj_index(p, key) |
| 82 | end |
| 83 | |
| 84 | return nil |
| 85 | end |
| 86 | |
| 87 | -- Basic Thrift-Lua Object |
| 88 | __TObject = { |
| 89 | __type = '__TObject', |
| 90 | __mt = { |
| 91 | __index = __tobj_index |
| 92 | } |
| 93 | } |
| 94 | function __TObject:new(init_obj) |
| 95 | local obj = {} |
| 96 | if ttype(obj) == 'table' then |
| 97 | obj = init_obj |
| 98 | end |
| 99 | |
| 100 | -- Use the __parent key and the __index function to achieve inheritance |
| 101 | obj.__parent = self |
| 102 | setmetatable(obj, __TObject.__mt) |
| 103 | return obj |
| 104 | end |
| 105 | |
| 106 | -- Return a string representation of any lua variable |
| 107 | function thrift_print_r(t) |
| 108 | local ret = '' |
| 109 | local ltype = type(t) |
| 110 | if (ltype == 'table') then |
| 111 | ret = ret .. '{ ' |
| 112 | for key,value in pairs(t) do |
| 113 | ret = ret .. tostring(key) .. '=' .. thrift_print_r(value) .. ' ' |
| 114 | end |
| 115 | ret = ret .. '}' |
| 116 | elseif ltype == 'string' then |
| 117 | ret = ret .. "'" .. tostring(t) .. "'" |
| 118 | else |
| 119 | ret = ret .. tostring(t) |
| 120 | end |
| 121 | return ret |
| 122 | end |
| 123 | |
| 124 | -- Basic Exception |
| 125 | TException = __TObject:new{ |
| 126 | message, |
| 127 | errorCode, |
| 128 | __type = 'TException' |
| 129 | } |
| 130 | function TException:__tostring() |
| 131 | if self.message then |
| 132 | return string.format('%s: %s', self.__type, self.message) |
| 133 | else |
| 134 | local message |
| 135 | if self.errorCode and self.__errorCodeToString then |
| 136 | message = string.format('%d: %s', self.errorCode, self:__errorCodeToString()) |
| 137 | else |
| 138 | message = thrift_print_r(self) |
| 139 | end |
| 140 | return string.format('%s:%s', self.__type, message) |
| 141 | end |
| 142 | end |
| 143 | |
| 144 | TApplicationException = TException:new{ |
| 145 | UNKNOWN = 0, |
| 146 | UNKNOWN_METHOD = 1, |
| 147 | INVALID_MESSAGE_TYPE = 2, |
| 148 | WRONG_METHOD_NAME = 3, |
| 149 | BAD_SEQUENCE_ID = 4, |
| 150 | MISSING_RESULT = 5, |
| 151 | INTERNAL_ERROR = 6, |
| 152 | PROTOCOL_ERROR = 7, |
| 153 | INVALID_TRANSFORM = 8, |
| 154 | INVALID_PROTOCOL = 9, |
| 155 | UNSUPPORTED_CLIENT_TYPE = 10, |
| 156 | errorCode = 0, |
| 157 | __type = 'TApplicationException' |
| 158 | } |
| 159 | |
| 160 | function TApplicationException:__errorCodeToString() |
| 161 | if self.errorCode == self.UNKNOWN_METHOD then |
| 162 | return 'Unknown method' |
| 163 | elseif self.errorCode == self.INVALID_MESSAGE_TYPE then |
| 164 | return 'Invalid message type' |
| 165 | elseif self.errorCode == self.WRONG_METHOD_NAME then |
| 166 | return 'Wrong method name' |
| 167 | elseif self.errorCode == self.BAD_SEQUENCE_ID then |
| 168 | return 'Bad sequence ID' |
| 169 | elseif self.errorCode == self.MISSING_RESULT then |
| 170 | return 'Missing result' |
| 171 | elseif self.errorCode == self.INTERNAL_ERROR then |
| 172 | return 'Internal error' |
| 173 | elseif self.errorCode == self.PROTOCOL_ERROR then |
| 174 | return 'Protocol error' |
| 175 | elseif self.errorCode == self.INVALID_TRANSFORM then |
| 176 | return 'Invalid transform' |
| 177 | elseif self.errorCode == self.INVALID_PROTOCOL then |
| 178 | return 'Invalid protocol' |
| 179 | elseif self.errorCode == self.UNSUPPORTED_CLIENT_TYPE then |
| 180 | return 'Unsupported client type' |
| 181 | else |
| 182 | return 'Default (unknown)' |
| 183 | end |
| 184 | end |
| 185 | |
| 186 | function TException:read(iprot) |
| 187 | iprot:readStructBegin() |
| 188 | while true do |
| 189 | local fname, ftype, fid = iprot:readFieldBegin() |
| 190 | if ftype == TType.STOP then |
| 191 | break |
| 192 | elseif fid == 1 then |
| 193 | if ftype == TType.STRING then |
| 194 | self.message = iprot:readString() |
| 195 | else |
| 196 | iprot:skip(ftype) |
| 197 | end |
| 198 | elseif fid == 2 then |
| 199 | if ftype == TType.I32 then |
| 200 | self.errorCode = iprot:readI32() |
| 201 | else |
| 202 | iprot:skip(ftype) |
| 203 | end |
| 204 | else |
| 205 | iprot:skip(ftype) |
| 206 | end |
| 207 | iprot:readFieldEnd() |
| 208 | end |
| 209 | iprot:readStructEnd() |
| 210 | end |
| 211 | |
| 212 | function TException:write(oprot) |
| 213 | oprot:writeStructBegin('TApplicationException') |
| 214 | if self.message then |
| 215 | oprot:writeFieldBegin('message', TType.STRING, 1) |
| 216 | oprot:writeString(self.message) |
| 217 | oprot:writeFieldEnd() |
| 218 | end |
| 219 | if self.errorCode then |
| 220 | oprot:writeFieldBegin('type', TType.I32, 2) |
| 221 | oprot:writeI32(self.errorCode) |
| 222 | oprot:writeFieldEnd() |
| 223 | end |
| 224 | oprot:writeFieldStop() |
| 225 | oprot:writeStructEnd() |
| 226 | end |
| 227 | |
| 228 | -- Basic Client (used in generated lua code) |
| 229 | __TClient = __TObject:new{ |
| 230 | __type = '__TClient', |
| 231 | _seqid = 0 |
| 232 | } |
| 233 | function __TClient:new(obj) |
| 234 | if ttype(obj) ~= 'table' then |
| 235 | error('TClient must be initialized with a table') |
| 236 | end |
| 237 | |
| 238 | -- Set iprot & oprot |
| 239 | if obj.protocol then |
| 240 | obj.iprot = obj.protocol |
| 241 | obj.oprot = obj.protocol |
| 242 | obj.protocol = nil |
| 243 | elseif not obj.iprot then |
| 244 | error('You must provide ' .. ttype(self) .. ' with an iprot') |
| 245 | end |
| 246 | if not obj.oprot then |
| 247 | obj.oprot = obj.iprot |
| 248 | end |
| 249 | |
| 250 | return __TObject.new(self, obj) |
| 251 | end |
| 252 | |
| 253 | function __TClient:close() |
| 254 | self.iprot.trans:close() |
| 255 | self.oprot.trans:close() |
| 256 | end |
| 257 | |
| 258 | -- Basic Processor (used in generated lua code) |
| 259 | __TProcessor = __TObject:new{ |
| 260 | __type = '__TProcessor' |
| 261 | } |
| 262 | function __TProcessor:new(obj) |
| 263 | if ttype(obj) ~= 'table' then |
| 264 | error('TProcessor must be initialized with a table') |
| 265 | end |
| 266 | |
| 267 | -- Ensure a handler is provided |
| 268 | if not obj.handler then |
| 269 | error('You must provide ' .. ttype(self) .. ' with a handler') |
| 270 | end |
| 271 | |
| 272 | return __TObject.new(self, obj) |
| 273 | end |