| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +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 | /*jshint evil:true*/ | 
|  | 21 |  | 
|  | 22 | /** | 
|  | 23 | * The Thrift namespace houses the Apache Thrift JavaScript library | 
|  | 24 | * elements providing JavaScript bindings for the Apache Thrift RPC | 
| henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 25 | * system. End users will typically only directly make use of the | 
|  | 26 | * Transport (TXHRTransport/TWebSocketTransport) and Protocol | 
|  | 27 | * (TJSONPRotocol/TBinaryProtocol) constructors. | 
|  | 28 | * | 
|  | 29 | * Object methods beginning with a __ (e.g. __onOpen()) are internal | 
|  | 30 | * and should not be called outside of the object's own methods. | 
|  | 31 | * | 
|  | 32 | * This library creates one global object: Thrift | 
|  | 33 | * Code in this library must never create additional global identifiers, | 
|  | 34 | * all features must be scoped within the Thrift namespace. | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 35 | * @namespace | 
|  | 36 | * @example | 
|  | 37 | *     var transport = new Thrift.Transport("http://localhost:8585"); | 
|  | 38 | *     var protocol  = new Thrift.Protocol(transport); | 
|  | 39 | *     var client = new MyThriftSvcClient(protocol); | 
|  | 40 | *     var result = client.MyMethod(); | 
|  | 41 | */ | 
|  | 42 | var Thrift = { | 
|  | 43 | /** | 
|  | 44 | * Thrift JavaScript library version. | 
|  | 45 | * @readonly | 
|  | 46 | * @const {string} Version | 
|  | 47 | * @memberof Thrift | 
|  | 48 | */ | 
|  | 49 | Version: '1.0.0-dev', | 
|  | 50 |  | 
|  | 51 | /** | 
|  | 52 | * Thrift IDL type string to Id mapping. | 
|  | 53 | * @readonly | 
|  | 54 | * @property {number}  STOP   - End of a set of fields. | 
|  | 55 | * @property {number}  VOID   - No value (only legal for return types). | 
|  | 56 | * @property {number}  BOOL   - True/False integer. | 
|  | 57 | * @property {number}  BYTE   - Signed 8 bit integer. | 
|  | 58 | * @property {number}  I08    - Signed 8 bit integer. | 
|  | 59 | * @property {number}  DOUBLE - 64 bit IEEE 854 floating point. | 
|  | 60 | * @property {number}  I16    - Signed 16 bit integer. | 
|  | 61 | * @property {number}  I32    - Signed 32 bit integer. | 
|  | 62 | * @property {number}  I64    - Signed 64 bit integer. | 
|  | 63 | * @property {number}  STRING - Array of bytes representing a string of characters. | 
|  | 64 | * @property {number}  UTF7   - Array of bytes representing a string of UTF7 encoded characters. | 
|  | 65 | * @property {number}  STRUCT - A multifield type. | 
|  | 66 | * @property {number}  MAP    - A collection type (map/associative-array/dictionary). | 
|  | 67 | * @property {number}  SET    - A collection type (unordered and without repeated values). | 
|  | 68 | * @property {number}  LIST   - A collection type (unordered). | 
|  | 69 | * @property {number}  UTF8   - Array of bytes representing a string of UTF8 encoded characters. | 
|  | 70 | * @property {number}  UTF16  - Array of bytes representing a string of UTF16 encoded characters. | 
|  | 71 | */ | 
|  | 72 | Type: { | 
|  | 73 | 'STOP' : 0, | 
|  | 74 | 'VOID' : 1, | 
|  | 75 | 'BOOL' : 2, | 
|  | 76 | 'BYTE' : 3, | 
|  | 77 | 'I08' : 3, | 
|  | 78 | 'DOUBLE' : 4, | 
|  | 79 | 'I16' : 6, | 
|  | 80 | 'I32' : 8, | 
|  | 81 | 'I64' : 10, | 
|  | 82 | 'STRING' : 11, | 
|  | 83 | 'UTF7' : 11, | 
|  | 84 | 'STRUCT' : 12, | 
|  | 85 | 'MAP' : 13, | 
|  | 86 | 'SET' : 14, | 
|  | 87 | 'LIST' : 15, | 
|  | 88 | 'UTF8' : 16, | 
|  | 89 | 'UTF16' : 17 | 
|  | 90 | }, | 
|  | 91 |  | 
|  | 92 | /** | 
|  | 93 | * Thrift RPC message type string to Id mapping. | 
|  | 94 | * @readonly | 
|  | 95 | * @property {number}  CALL      - RPC call sent from client to server. | 
|  | 96 | * @property {number}  REPLY     - RPC call normal response from server to client. | 
|  | 97 | * @property {number}  EXCEPTION - RPC call exception response from server to client. | 
|  | 98 | * @property {number}  ONEWAY    - Oneway RPC call from client to server with no response. | 
|  | 99 | */ | 
|  | 100 | MessageType: { | 
|  | 101 | 'CALL' : 1, | 
|  | 102 | 'REPLY' : 2, | 
|  | 103 | 'EXCEPTION' : 3, | 
|  | 104 | 'ONEWAY' : 4 | 
|  | 105 | }, | 
|  | 106 |  | 
|  | 107 | /** | 
|  | 108 | * Utility function returning the count of an object's own properties. | 
|  | 109 | * @param {object} obj - Object to test. | 
|  | 110 | * @returns {number} number of object's own properties | 
|  | 111 | */ | 
|  | 112 | objectLength: function(obj) { | 
|  | 113 | var length = 0; | 
|  | 114 | for (var k in obj) { | 
|  | 115 | if (obj.hasOwnProperty(k)) { | 
|  | 116 | length++; | 
|  | 117 | } | 
|  | 118 | } | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 119 | return length; | 
|  | 120 | }, | 
|  | 121 |  | 
|  | 122 | /** | 
|  | 123 | * Utility function to establish prototype inheritance. | 
|  | 124 | * @see {@link http://javascript.crockford.com/prototypal.html|Prototypal Inheritance} | 
|  | 125 | * @param {function} constructor - Contstructor function to set as derived. | 
|  | 126 | * @param {function} superConstructor - Contstructor function to set as base. | 
|  | 127 | * @param {string} [name] - Type name to set as name property in derived prototype. | 
|  | 128 | */ | 
|  | 129 | inherits: function(constructor, superConstructor, name) { | 
|  | 130 | function F() {} | 
|  | 131 | F.prototype = superConstructor.prototype; | 
|  | 132 | constructor.prototype = new F(); | 
|  | 133 | constructor.prototype.name = name || ""; | 
|  | 134 | } | 
|  | 135 | }; | 
|  | 136 |  | 
|  | 137 | /** | 
|  | 138 | * Initializes a Thrift TException instance. | 
|  | 139 | * @constructor | 
|  | 140 | * @augments Error | 
|  | 141 | * @param {string} message - The TException message (distinct from the Error message). | 
|  | 142 | * @classdesc TException is the base class for all Thrift exceptions types. | 
|  | 143 | */ | 
|  | 144 | Thrift.TException = function(message) { | 
|  | 145 | this.message = message; | 
|  | 146 | }; | 
|  | 147 | Thrift.inherits(Thrift.TException, Error, 'TException'); | 
|  | 148 |  | 
|  | 149 | /** | 
|  | 150 | * Returns the message set on the exception. | 
|  | 151 | * @readonly | 
|  | 152 | * @returns {string} exception message | 
|  | 153 | */ | 
|  | 154 | Thrift.TException.prototype.getMessage = function() { | 
|  | 155 | return this.message; | 
|  | 156 | }; | 
|  | 157 |  | 
|  | 158 | /** | 
|  | 159 | * Thrift Application Exception type string to Id mapping. | 
|  | 160 | * @readonly | 
|  | 161 | * @property {number}  UNKNOWN                 - Unknown/undefined. | 
|  | 162 | * @property {number}  UNKNOWN_METHOD          - Client attempted to call a method unknown to the server. | 
|  | 163 | * @property {number}  INVALID_MESSAGE_TYPE    - Client passed an unknown/unsupported MessageType. | 
|  | 164 | * @property {number}  WRONG_METHOD_NAME       - Unused. | 
|  | 165 | * @property {number}  BAD_SEQUENCE_ID         - Unused in Thrift RPC, used to flag proprietary sequence number errors. | 
|  | 166 | * @property {number}  MISSING_RESULT          - Raised by a server processor if a handler fails to supply the required return result. | 
|  | 167 | * @property {number}  INTERNAL_ERROR          - Something bad happened. | 
|  | 168 | * @property {number}  PROTOCOL_ERROR          - The protocol layer failed to serialize or deserialize data. | 
|  | 169 | * @property {number}  INVALID_TRANSFORM       - Unused. | 
|  | 170 | * @property {number}  INVALID_PROTOCOL        - The protocol (or version) is not supported. | 
|  | 171 | * @property {number}  UNSUPPORTED_CLIENT_TYPE - Unused. | 
|  | 172 | */ | 
|  | 173 | Thrift.TApplicationExceptionType = { | 
|  | 174 | 'UNKNOWN' : 0, | 
|  | 175 | 'UNKNOWN_METHOD' : 1, | 
|  | 176 | 'INVALID_MESSAGE_TYPE' : 2, | 
|  | 177 | 'WRONG_METHOD_NAME' : 3, | 
|  | 178 | 'BAD_SEQUENCE_ID' : 4, | 
|  | 179 | 'MISSING_RESULT' : 5, | 
|  | 180 | 'INTERNAL_ERROR' : 6, | 
|  | 181 | 'PROTOCOL_ERROR' : 7, | 
|  | 182 | 'INVALID_TRANSFORM' : 8, | 
|  | 183 | 'INVALID_PROTOCOL' : 9, | 
|  | 184 | 'UNSUPPORTED_CLIENT_TYPE' : 10 | 
|  | 185 | }; | 
|  | 186 |  | 
|  | 187 | /** | 
|  | 188 | * Initializes a Thrift TApplicationException instance. | 
|  | 189 | * @constructor | 
|  | 190 | * @augments Thrift.TException | 
|  | 191 | * @param {string} message - The TApplicationException message (distinct from the Error message). | 
|  | 192 | * @param {Thrift.TApplicationExceptionType} [code] - The TApplicationExceptionType code. | 
|  | 193 | * @classdesc TApplicationException is the exception class used to propagate exceptions from an RPC server back to a calling client. | 
|  | 194 | */ | 
|  | 195 | Thrift.TApplicationException = function(message, code) { | 
|  | 196 | this.message = message; | 
|  | 197 | this.code = typeof code === "number" ? code : 0; | 
|  | 198 | }; | 
|  | 199 | Thrift.inherits(Thrift.TApplicationException, Thrift.TException, 'TApplicationException'); | 
|  | 200 |  | 
|  | 201 | /** | 
|  | 202 | * Read a TApplicationException from the supplied protocol. | 
|  | 203 | * @param {object} input - The input protocol to read from. | 
|  | 204 | */ | 
|  | 205 | Thrift.TApplicationException.prototype.read = function(input) { | 
|  | 206 | while (1) { | 
|  | 207 | var ret = input.readFieldBegin(); | 
|  | 208 |  | 
|  | 209 | if (ret.ftype == Thrift.Type.STOP) { | 
|  | 210 | break; | 
|  | 211 | } | 
|  | 212 |  | 
|  | 213 | var fid = ret.fid; | 
|  | 214 |  | 
|  | 215 | switch (fid) { | 
|  | 216 | case 1: | 
|  | 217 | if (ret.ftype == Thrift.Type.STRING) { | 
|  | 218 | ret = input.readString(); | 
|  | 219 | this.message = ret.value; | 
|  | 220 | } else { | 
|  | 221 | ret = input.skip(ret.ftype); | 
|  | 222 | } | 
|  | 223 | break; | 
|  | 224 | case 2: | 
|  | 225 | if (ret.ftype == Thrift.Type.I32) { | 
|  | 226 | ret = input.readI32(); | 
|  | 227 | this.code = ret.value; | 
|  | 228 | } else { | 
|  | 229 | ret = input.skip(ret.ftype); | 
|  | 230 | } | 
|  | 231 | break; | 
|  | 232 | default: | 
|  | 233 | ret = input.skip(ret.ftype); | 
|  | 234 | break; | 
|  | 235 | } | 
|  | 236 |  | 
|  | 237 | input.readFieldEnd(); | 
|  | 238 | } | 
|  | 239 |  | 
|  | 240 | input.readStructEnd(); | 
|  | 241 | }; | 
|  | 242 |  | 
|  | 243 | /** | 
|  | 244 | * Wite a TApplicationException to the supplied protocol. | 
|  | 245 | * @param {object} output - The output protocol to write to. | 
|  | 246 | */ | 
|  | 247 | Thrift.TApplicationException.prototype.write = function(output) { | 
|  | 248 | output.writeStructBegin('TApplicationException'); | 
|  | 249 |  | 
|  | 250 | if (this.message) { | 
|  | 251 | output.writeFieldBegin('message', Thrift.Type.STRING, 1); | 
|  | 252 | output.writeString(this.getMessage()); | 
|  | 253 | output.writeFieldEnd(); | 
|  | 254 | } | 
|  | 255 |  | 
|  | 256 | if (this.code) { | 
|  | 257 | output.writeFieldBegin('type', Thrift.Type.I32, 2); | 
|  | 258 | output.writeI32(this.code); | 
|  | 259 | output.writeFieldEnd(); | 
|  | 260 | } | 
|  | 261 |  | 
|  | 262 | output.writeFieldStop(); | 
|  | 263 | output.writeStructEnd(); | 
|  | 264 | }; | 
|  | 265 |  | 
|  | 266 | /** | 
|  | 267 | * Returns the application exception code set on the exception. | 
|  | 268 | * @readonly | 
|  | 269 | * @returns {Thrift.TApplicationExceptionType} exception code | 
|  | 270 | */ | 
|  | 271 | Thrift.TApplicationException.prototype.getCode = function() { | 
|  | 272 | return this.code; | 
|  | 273 | }; | 
|  | 274 |  | 
|  | 275 | /** | 
| henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 276 | * Constructor Function for the XHR transport. | 
|  | 277 | * If you do not specify a url then you must handle XHR operations on | 
|  | 278 | * your own. This type can also be constructed using the Transport alias | 
|  | 279 | * for backward compatibility. | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 280 | * @constructor | 
|  | 281 | * @param {string} [url] - The URL to connect to. | 
| henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 282 | * @classdesc The Apache Thrift Transport layer performs byte level I/O | 
|  | 283 | * between RPC clients and servers. The JavaScript TXHRTransport object | 
|  | 284 | * uses Http[s]/XHR. Target servers must implement the http[s] transport | 
|  | 285 | * (see: node.js example server_http.js). | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 286 | * @example | 
| henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 287 | *     var transport = new Thrift.TXHRTransport("http://localhost:8585"); | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 288 | */ | 
| Roger Meier | 52744ee | 2014-03-12 09:38:42 +0100 | [diff] [blame] | 289 | Thrift.Transport = Thrift.TXHRTransport = function(url, options) { | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 290 | this.url = url; | 
|  | 291 | this.wpos = 0; | 
|  | 292 | this.rpos = 0; | 
| Roger Meier | 52744ee | 2014-03-12 09:38:42 +0100 | [diff] [blame] | 293 | this.useCORS = (options && options.useCORS); | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 294 | this.send_buf = ''; | 
|  | 295 | this.recv_buf = ''; | 
|  | 296 | }; | 
|  | 297 |  | 
| henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 298 | Thrift.TXHRTransport.prototype = { | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 299 | /** | 
|  | 300 | * Gets the browser specific XmlHttpRequest Object. | 
|  | 301 | * @returns {object} the browser XHR interface object | 
|  | 302 | */ | 
|  | 303 | getXmlHttpRequestObject: function() { | 
|  | 304 | try { return new XMLHttpRequest(); } catch (e1) { } | 
|  | 305 | try { return new ActiveXObject('Msxml2.XMLHTTP'); } catch (e2) { } | 
|  | 306 | try { return new ActiveXObject('Microsoft.XMLHTTP'); } catch (e3) { } | 
|  | 307 |  | 
|  | 308 | throw "Your browser doesn't support XHR."; | 
|  | 309 | }, | 
|  | 310 |  | 
|  | 311 | /** | 
| henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 312 | * Sends the current XRH request if the transport was created with a URL | 
|  | 313 | * and the async parameter is false. If the transport was not created with | 
|  | 314 | * a URL, or the async parameter is True and no callback is provided, or | 
|  | 315 | * the URL is an empty string, the current send buffer is returned. | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 316 | * @param {object} async - If true the current send buffer is returned. | 
| henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 317 | * @param {object} callback - Optional async completion callback | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 318 | * @returns {undefined|string} Nothing or the current send buffer. | 
|  | 319 | * @throws {string} If XHR fails. | 
|  | 320 | */ | 
| henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 321 | flush: function(async, callback) { | 
| henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 322 | var self = this; | 
| henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 323 | if ((async && !callback) || this.url === undefined || this.url === '') { | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 324 | return this.send_buf; | 
|  | 325 | } | 
|  | 326 |  | 
|  | 327 | var xreq = this.getXmlHttpRequestObject(); | 
|  | 328 |  | 
|  | 329 | if (xreq.overrideMimeType) { | 
|  | 330 | xreq.overrideMimeType('application/json'); | 
|  | 331 | } | 
|  | 332 |  | 
| henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 333 | if (callback) { | 
| henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 334 | //Ignore XHR callbacks until the data arrives, then call the | 
|  | 335 | //  client's callback | 
|  | 336 | xreq.onreadystatechange = | 
|  | 337 | (function() { | 
|  | 338 | var clientCallback = callback; | 
|  | 339 | return function() { | 
|  | 340 | if (this.readyState == 4 && this.status == 200) { | 
|  | 341 | self.setRecvBuffer(this.responseText); | 
|  | 342 | clientCallback(); | 
|  | 343 | } | 
|  | 344 | }; | 
|  | 345 | }()); | 
| henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 346 | } | 
|  | 347 |  | 
|  | 348 | xreq.open('POST', this.url, !!async); | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 349 | xreq.send(this.send_buf); | 
| henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 350 | if (async && callback) { | 
|  | 351 | return; | 
|  | 352 | } | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 353 |  | 
|  | 354 | if (xreq.readyState != 4) { | 
|  | 355 | throw 'encountered an unknown ajax ready state: ' + xreq.readyState; | 
|  | 356 | } | 
|  | 357 |  | 
|  | 358 | if (xreq.status != 200) { | 
|  | 359 | throw 'encountered a unknown request status: ' + xreq.status; | 
|  | 360 | } | 
|  | 361 |  | 
|  | 362 | this.recv_buf = xreq.responseText; | 
|  | 363 | this.recv_buf_sz = this.recv_buf.length; | 
|  | 364 | this.wpos = this.recv_buf.length; | 
|  | 365 | this.rpos = 0; | 
|  | 366 | }, | 
|  | 367 |  | 
|  | 368 | /** | 
|  | 369 | * Creates a jQuery XHR object to be used for a Thrift server call. | 
|  | 370 | * @param {object} client - The Thrift Service client object generated by the IDL compiler. | 
|  | 371 | * @param {object} postData - The message to send to the server. | 
| henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 372 | * @param {function} args - The original call arguments with the success call back at the end. | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 373 | * @param {function} recv_method - The Thrift Service Client receive method for the call. | 
|  | 374 | * @returns {object} A new jQuery XHR object. | 
|  | 375 | * @throws {string} If the jQuery version is prior to 1.5 or if jQuery is not found. | 
|  | 376 | */ | 
|  | 377 | jqRequest: function(client, postData, args, recv_method) { | 
|  | 378 | if (typeof jQuery === 'undefined' || | 
|  | 379 | typeof jQuery.Deferred === 'undefined') { | 
|  | 380 | throw 'Thrift.js requires jQuery 1.5+ to use asynchronous requests'; | 
|  | 381 | } | 
|  | 382 |  | 
|  | 383 | var thriftTransport = this; | 
|  | 384 |  | 
|  | 385 | var jqXHR = jQuery.ajax({ | 
|  | 386 | url: this.url, | 
|  | 387 | data: postData, | 
|  | 388 | type: 'POST', | 
|  | 389 | cache: false, | 
|  | 390 | contentType: 'application/json', | 
|  | 391 | dataType: 'text thrift', | 
|  | 392 | converters: { | 
|  | 393 | 'text thrift' : function(responseData) { | 
|  | 394 | thriftTransport.setRecvBuffer(responseData); | 
|  | 395 | var value = recv_method.call(client); | 
|  | 396 | return value; | 
|  | 397 | } | 
|  | 398 | }, | 
|  | 399 | context: client, | 
|  | 400 | success: jQuery.makeArray(args).pop() | 
|  | 401 | }); | 
|  | 402 |  | 
|  | 403 | return jqXHR; | 
|  | 404 | }, | 
|  | 405 |  | 
|  | 406 | /** | 
| henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 407 | * Sets the buffer to provide the protocol when deserializing. | 
|  | 408 | * @param {string} buf - The buffer to supply the protocol. | 
|  | 409 | */ | 
|  | 410 | setRecvBuffer: function(buf) { | 
|  | 411 | this.recv_buf = buf; | 
|  | 412 | this.recv_buf_sz = this.recv_buf.length; | 
|  | 413 | this.wpos = this.recv_buf.length; | 
|  | 414 | this.rpos = 0; | 
|  | 415 | }, | 
|  | 416 |  | 
|  | 417 | /** | 
|  | 418 | * Returns true if the transport is open, XHR always returns true. | 
|  | 419 | * @readonly | 
|  | 420 | * @returns {boolean} Always True. | 
|  | 421 | */ | 
|  | 422 | isOpen: function() { | 
|  | 423 | return true; | 
|  | 424 | }, | 
|  | 425 |  | 
|  | 426 | /** | 
|  | 427 | * Opens the transport connection, with XHR this is a nop. | 
|  | 428 | */ | 
|  | 429 | open: function() {}, | 
|  | 430 |  | 
|  | 431 | /** | 
|  | 432 | * Closes the transport connection, with XHR this is a nop. | 
|  | 433 | */ | 
|  | 434 | close: function() {}, | 
|  | 435 |  | 
|  | 436 | /** | 
|  | 437 | * Returns the specified number of characters from the response | 
|  | 438 | * buffer. | 
|  | 439 | * @param {number} len - The number of characters to return. | 
|  | 440 | * @returns {string} Characters sent by the server. | 
|  | 441 | */ | 
|  | 442 | read: function(len) { | 
|  | 443 | var avail = this.wpos - this.rpos; | 
|  | 444 |  | 
|  | 445 | if (avail === 0) { | 
|  | 446 | return ''; | 
|  | 447 | } | 
|  | 448 |  | 
|  | 449 | var give = len; | 
|  | 450 |  | 
|  | 451 | if (avail < len) { | 
|  | 452 | give = avail; | 
|  | 453 | } | 
|  | 454 |  | 
|  | 455 | var ret = this.read_buf.substr(this.rpos, give); | 
|  | 456 | this.rpos += give; | 
|  | 457 |  | 
|  | 458 | //clear buf when complete? | 
|  | 459 | return ret; | 
|  | 460 | }, | 
|  | 461 |  | 
|  | 462 | /** | 
|  | 463 | * Returns the entire response buffer. | 
|  | 464 | * @returns {string} Characters sent by the server. | 
|  | 465 | */ | 
|  | 466 | readAll: function() { | 
|  | 467 | return this.recv_buf; | 
|  | 468 | }, | 
|  | 469 |  | 
|  | 470 | /** | 
|  | 471 | * Sets the send buffer to buf. | 
|  | 472 | * @param {string} buf - The buffer to send. | 
|  | 473 | */ | 
|  | 474 | write: function(buf) { | 
|  | 475 | this.send_buf = buf; | 
|  | 476 | }, | 
|  | 477 |  | 
|  | 478 | /** | 
|  | 479 | * Returns the send buffer. | 
|  | 480 | * @readonly | 
|  | 481 | * @returns {string} The send buffer. | 
|  | 482 | */ | 
|  | 483 | getSendBuffer: function() { | 
|  | 484 | return this.send_buf; | 
|  | 485 | } | 
|  | 486 |  | 
|  | 487 | }; | 
|  | 488 |  | 
|  | 489 |  | 
|  | 490 | /** | 
|  | 491 | * Constructor Function for the WebSocket transport. | 
|  | 492 | * @constructor | 
|  | 493 | * @param {string} [url] - The URL to connect to. | 
|  | 494 | * @classdesc The Apache Thrift Transport layer performs byte level I/O | 
|  | 495 | * between RPC clients and servers. The JavaScript TWebSocketTransport object | 
|  | 496 | * uses the WebSocket protocol. Target servers must implement WebSocket. | 
|  | 497 | * (see: node.js example server_http.js). | 
|  | 498 | * @example | 
|  | 499 | *   var transport = new Thrift.TWebSocketTransport("http://localhost:8585"); | 
|  | 500 | */ | 
|  | 501 | Thrift.TWebSocketTransport = function(url) { | 
|  | 502 | this.__reset(url); | 
|  | 503 | }; | 
|  | 504 |  | 
|  | 505 | Thrift.TWebSocketTransport.prototype = { | 
|  | 506 | __reset: function(url) { | 
|  | 507 | this.url = url;             //Where to connect | 
|  | 508 | this.socket = null;         //The web socket | 
|  | 509 | this.callbacks = [];        //Pending callbacks | 
|  | 510 | this.send_pending = [];     //Buffers/Callback pairs waiting to be sent | 
|  | 511 | this.send_buf = '';         //Outbound data, immutable until sent | 
|  | 512 | this.recv_buf = '';         //Inbound data | 
|  | 513 | this.rb_wpos = 0;           //Network write position in receive buffer | 
|  | 514 | this.rb_rpos = 0;           //Client read position in receive buffer | 
|  | 515 | }, | 
|  | 516 |  | 
|  | 517 | /** | 
|  | 518 | * Sends the current WS request and registers callback. The async | 
|  | 519 | * parameter is ignored (WS flush is always async) and the callback | 
|  | 520 | * function parameter is required. | 
|  | 521 | * @param {object} async - Ignored. | 
|  | 522 | * @param {object} callback - The client completion callback. | 
|  | 523 | * @returns {undefined|string} Nothing (undefined) | 
|  | 524 | */ | 
|  | 525 | flush: function(async, callback) { | 
|  | 526 | var self = this; | 
|  | 527 | if (this.isOpen()) { | 
|  | 528 | //Send data and register a callback to invoke the client callback | 
|  | 529 | this.socket.send(this.send_buf); | 
|  | 530 | this.callbacks.push((function() { | 
|  | 531 | var clientCallback = callback; | 
|  | 532 | return function(msg) { | 
|  | 533 | self.setRecvBuffer(msg); | 
|  | 534 | clientCallback(); | 
|  | 535 | }; | 
|  | 536 | }())); | 
|  | 537 | } else { | 
|  | 538 | //Queue the send to go out __onOpen | 
|  | 539 | this.send_pending.push({ | 
|  | 540 | buf: this.send_buf, | 
|  | 541 | cb:  callback | 
|  | 542 | }); | 
|  | 543 | } | 
|  | 544 | }, | 
|  | 545 |  | 
|  | 546 | __onOpen: function() { | 
|  | 547 | var self = this; | 
|  | 548 | if (this.send_pending.length > 0) { | 
|  | 549 | //If the user made calls before the connection was fully | 
|  | 550 | //open, send them now | 
|  | 551 | this.send_pending.forEach(function(elem) { | 
|  | 552 | this.socket.send(elem.buf); | 
|  | 553 | this.callbacks.push((function() { | 
|  | 554 | var clientCallback = elem.cb; | 
|  | 555 | return function(msg) { | 
|  | 556 | self.setRecvBuffer(msg); | 
|  | 557 | clientCallback(); | 
|  | 558 | }; | 
|  | 559 | }())); | 
|  | 560 | }); | 
|  | 561 | this.send_pending = []; | 
|  | 562 | } | 
|  | 563 | }, | 
|  | 564 |  | 
|  | 565 | __onClose: function(evt) { | 
|  | 566 | this.__reset(this.url); | 
|  | 567 | }, | 
|  | 568 |  | 
|  | 569 | __onMessage: function(evt) { | 
|  | 570 | if (this.callbacks.length) { | 
|  | 571 | this.callbacks.shift()(evt.data); | 
|  | 572 | } | 
|  | 573 | }, | 
|  | 574 |  | 
|  | 575 | __onError: function(evt) { | 
|  | 576 | console.log("Thrift WebSocket Error: " + evt.toString()); | 
|  | 577 | this.socket.close(); | 
|  | 578 | }, | 
|  | 579 |  | 
|  | 580 | /** | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 581 | * Sets the buffer to use when receiving server responses. | 
|  | 582 | * @param {string} buf - The buffer to receive server responses. | 
|  | 583 | */ | 
|  | 584 | setRecvBuffer: function(buf) { | 
|  | 585 | this.recv_buf = buf; | 
|  | 586 | this.recv_buf_sz = this.recv_buf.length; | 
|  | 587 | this.wpos = this.recv_buf.length; | 
|  | 588 | this.rpos = 0; | 
|  | 589 | }, | 
|  | 590 |  | 
|  | 591 | /** | 
| henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 592 | * Returns true if the transport is open | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 593 | * @readonly | 
| henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 594 | * @returns {boolean} | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 595 | */ | 
|  | 596 | isOpen: function() { | 
| henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 597 | return this.socket && this.socket.readyState == this.socket.OPEN; | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 598 | }, | 
|  | 599 |  | 
|  | 600 | /** | 
| henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 601 | * Opens the transport connection | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 602 | */ | 
| henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 603 | open: function() { | 
|  | 604 | //If OPEN/CONNECTING/CLOSING ignore additional opens | 
|  | 605 | if (this.socket && this.socket.readyState != this.socket.CLOSED) { | 
|  | 606 | return; | 
|  | 607 | } | 
|  | 608 | //If there is no socket or the socket is closed: | 
|  | 609 | this.socket = new WebSocket(this.url); | 
|  | 610 | this.socket.onopen = this.__onOpen.bind(this); | 
|  | 611 | this.socket.onmessage = this.__onMessage.bind(this); | 
|  | 612 | this.socket.onerror = this.__onError.bind(this); | 
|  | 613 | this.socket.onclose = this.__onClose.bind(this); | 
|  | 614 | }, | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 615 |  | 
|  | 616 | /** | 
| henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 617 | * Closes the transport connection | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 618 | */ | 
| henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 619 | close: function() { | 
|  | 620 | this.socket.close(); | 
|  | 621 | }, | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 622 |  | 
|  | 623 | /** | 
|  | 624 | * Returns the specified number of characters from the response | 
|  | 625 | * buffer. | 
|  | 626 | * @param {number} len - The number of characters to return. | 
|  | 627 | * @returns {string} Characters sent by the server. | 
|  | 628 | */ | 
|  | 629 | read: function(len) { | 
|  | 630 | var avail = this.wpos - this.rpos; | 
|  | 631 |  | 
|  | 632 | if (avail === 0) { | 
|  | 633 | return ''; | 
|  | 634 | } | 
|  | 635 |  | 
|  | 636 | var give = len; | 
|  | 637 |  | 
|  | 638 | if (avail < len) { | 
|  | 639 | give = avail; | 
|  | 640 | } | 
|  | 641 |  | 
|  | 642 | var ret = this.read_buf.substr(this.rpos, give); | 
|  | 643 | this.rpos += give; | 
|  | 644 |  | 
|  | 645 | //clear buf when complete? | 
|  | 646 | return ret; | 
|  | 647 | }, | 
|  | 648 |  | 
|  | 649 | /** | 
|  | 650 | * Returns the entire response buffer. | 
|  | 651 | * @returns {string} Characters sent by the server. | 
|  | 652 | */ | 
|  | 653 | readAll: function() { | 
|  | 654 | return this.recv_buf; | 
|  | 655 | }, | 
|  | 656 |  | 
|  | 657 | /** | 
|  | 658 | * Sets the send buffer to buf. | 
|  | 659 | * @param {string} buf - The buffer to send. | 
|  | 660 | */ | 
|  | 661 | write: function(buf) { | 
|  | 662 | this.send_buf = buf; | 
|  | 663 | }, | 
|  | 664 |  | 
|  | 665 | /** | 
|  | 666 | * Returns the send buffer. | 
|  | 667 | * @readonly | 
|  | 668 | * @returns {string} The send buffer. | 
|  | 669 | */ | 
|  | 670 | getSendBuffer: function() { | 
|  | 671 | return this.send_buf; | 
|  | 672 | } | 
|  | 673 |  | 
|  | 674 | }; | 
|  | 675 |  | 
|  | 676 | /** | 
|  | 677 | * Initializes a Thrift JSON protocol instance. | 
|  | 678 | * @constructor | 
|  | 679 | * @param {Thrift.Transport} transport - The transport to serialize to/from. | 
|  | 680 | * @classdesc Apache Thrift Protocols perform serialization which enables cross | 
|  | 681 | * language RPC. The Protocol type is the JavaScript browser implementation | 
|  | 682 | * of the Apache Thrift TJSONProtocol. | 
|  | 683 | * @example | 
|  | 684 | *     var protocol  = new Thrift.Protocol(transport); | 
|  | 685 | */ | 
| Roger Meier | 52744ee | 2014-03-12 09:38:42 +0100 | [diff] [blame] | 686 | Thrift.TJSONProtocol = Thrift.Protocol = function(transport) { | 
| radekg | 1d30558 | 2015-01-01 20:35:01 +0100 | [diff] [blame^] | 687 | this.tstack = []; | 
|  | 688 | this.tpos = []; | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 689 | this.transport = transport; | 
|  | 690 | }; | 
|  | 691 |  | 
|  | 692 | /** | 
|  | 693 | * Thrift IDL type Id to string mapping. | 
|  | 694 | * @readonly | 
|  | 695 | * @see {@link Thrift.Type} | 
|  | 696 | */ | 
|  | 697 | Thrift.Protocol.Type = {}; | 
|  | 698 | Thrift.Protocol.Type[Thrift.Type.BOOL] = '"tf"'; | 
|  | 699 | Thrift.Protocol.Type[Thrift.Type.BYTE] = '"i8"'; | 
|  | 700 | Thrift.Protocol.Type[Thrift.Type.I16] = '"i16"'; | 
|  | 701 | Thrift.Protocol.Type[Thrift.Type.I32] = '"i32"'; | 
|  | 702 | Thrift.Protocol.Type[Thrift.Type.I64] = '"i64"'; | 
|  | 703 | Thrift.Protocol.Type[Thrift.Type.DOUBLE] = '"dbl"'; | 
|  | 704 | Thrift.Protocol.Type[Thrift.Type.STRUCT] = '"rec"'; | 
|  | 705 | Thrift.Protocol.Type[Thrift.Type.STRING] = '"str"'; | 
|  | 706 | Thrift.Protocol.Type[Thrift.Type.MAP] = '"map"'; | 
|  | 707 | Thrift.Protocol.Type[Thrift.Type.LIST] = '"lst"'; | 
|  | 708 | Thrift.Protocol.Type[Thrift.Type.SET] = '"set"'; | 
|  | 709 |  | 
|  | 710 | /** | 
|  | 711 | * Thrift IDL type string to Id mapping. | 
|  | 712 | * @readonly | 
|  | 713 | * @see {@link Thrift.Type} | 
|  | 714 | */ | 
|  | 715 | Thrift.Protocol.RType = {}; | 
|  | 716 | Thrift.Protocol.RType.tf = Thrift.Type.BOOL; | 
|  | 717 | Thrift.Protocol.RType.i8 = Thrift.Type.BYTE; | 
|  | 718 | Thrift.Protocol.RType.i16 = Thrift.Type.I16; | 
|  | 719 | Thrift.Protocol.RType.i32 = Thrift.Type.I32; | 
|  | 720 | Thrift.Protocol.RType.i64 = Thrift.Type.I64; | 
|  | 721 | Thrift.Protocol.RType.dbl = Thrift.Type.DOUBLE; | 
|  | 722 | Thrift.Protocol.RType.rec = Thrift.Type.STRUCT; | 
|  | 723 | Thrift.Protocol.RType.str = Thrift.Type.STRING; | 
|  | 724 | Thrift.Protocol.RType.map = Thrift.Type.MAP; | 
|  | 725 | Thrift.Protocol.RType.lst = Thrift.Type.LIST; | 
|  | 726 | Thrift.Protocol.RType.set = Thrift.Type.SET; | 
|  | 727 |  | 
|  | 728 | /** | 
|  | 729 | * The TJSONProtocol version number. | 
|  | 730 | * @readonly | 
|  | 731 | * @const {number} Version | 
|  | 732 | * @memberof Thrift.Protocol | 
|  | 733 | */ | 
|  | 734 | Thrift.Protocol.Version = 1; | 
|  | 735 |  | 
|  | 736 | Thrift.Protocol.prototype = { | 
|  | 737 | /** | 
|  | 738 | * Returns the underlying transport. | 
|  | 739 | * @readonly | 
|  | 740 | * @returns {Thrift.Transport} The underlying transport. | 
|  | 741 | */ | 
|  | 742 | getTransport: function() { | 
|  | 743 | return this.transport; | 
|  | 744 | }, | 
|  | 745 |  | 
|  | 746 | /** | 
|  | 747 | * Serializes the beginning of a Thrift RPC message. | 
|  | 748 | * @param {string} name - The service method to call. | 
|  | 749 | * @param {Thrift.MessageType} messageType - The type of method call. | 
|  | 750 | * @param {number} seqid - The sequence number of this call (always 0 in Apache Thrift). | 
|  | 751 | */ | 
|  | 752 | writeMessageBegin: function(name, messageType, seqid) { | 
|  | 753 | this.tstack = []; | 
|  | 754 | this.tpos = []; | 
|  | 755 |  | 
|  | 756 | this.tstack.push([Thrift.Protocol.Version, '"' + | 
|  | 757 | name + '"', messageType, seqid]); | 
|  | 758 | }, | 
|  | 759 |  | 
|  | 760 | /** | 
|  | 761 | * Serializes the end of a Thrift RPC message. | 
|  | 762 | */ | 
|  | 763 | writeMessageEnd: function() { | 
|  | 764 | var obj = this.tstack.pop(); | 
|  | 765 |  | 
|  | 766 | this.wobj = this.tstack.pop(); | 
|  | 767 | this.wobj.push(obj); | 
|  | 768 |  | 
|  | 769 | this.wbuf = '[' + this.wobj.join(',') + ']'; | 
|  | 770 |  | 
|  | 771 | this.transport.write(this.wbuf); | 
|  | 772 | }, | 
|  | 773 |  | 
|  | 774 |  | 
|  | 775 | /** | 
|  | 776 | * Serializes the beginning of a struct. | 
|  | 777 | * @param {string} name - The name of the struct. | 
|  | 778 | */ | 
|  | 779 | writeStructBegin: function(name) { | 
|  | 780 | this.tpos.push(this.tstack.length); | 
|  | 781 | this.tstack.push({}); | 
|  | 782 | }, | 
|  | 783 |  | 
|  | 784 | /** | 
|  | 785 | * Serializes the end of a struct. | 
|  | 786 | */ | 
|  | 787 | writeStructEnd: function() { | 
|  | 788 |  | 
|  | 789 | var p = this.tpos.pop(); | 
|  | 790 | var struct = this.tstack[p]; | 
|  | 791 | var str = '{'; | 
|  | 792 | var first = true; | 
|  | 793 | for (var key in struct) { | 
|  | 794 | if (first) { | 
|  | 795 | first = false; | 
|  | 796 | } else { | 
|  | 797 | str += ','; | 
|  | 798 | } | 
|  | 799 |  | 
|  | 800 | str += key + ':' + struct[key]; | 
|  | 801 | } | 
|  | 802 |  | 
|  | 803 | str += '}'; | 
|  | 804 | this.tstack[p] = str; | 
|  | 805 | }, | 
|  | 806 |  | 
|  | 807 | /** | 
|  | 808 | * Serializes the beginning of a struct field. | 
|  | 809 | * @param {string} name - The name of the field. | 
|  | 810 | * @param {Thrift.Protocol.Type} fieldType - The data type of the field. | 
|  | 811 | * @param {number} fieldId - The field's unique identifier. | 
|  | 812 | */ | 
|  | 813 | writeFieldBegin: function(name, fieldType, fieldId) { | 
|  | 814 | this.tpos.push(this.tstack.length); | 
|  | 815 | this.tstack.push({ 'fieldId': '"' + | 
|  | 816 | fieldId + '"', 'fieldType': Thrift.Protocol.Type[fieldType] | 
|  | 817 | }); | 
|  | 818 |  | 
|  | 819 | }, | 
|  | 820 |  | 
|  | 821 | /** | 
|  | 822 | * Serializes the end of a field. | 
|  | 823 | */ | 
|  | 824 | writeFieldEnd: function() { | 
|  | 825 | var value = this.tstack.pop(); | 
|  | 826 | var fieldInfo = this.tstack.pop(); | 
|  | 827 |  | 
|  | 828 | this.tstack[this.tstack.length - 1][fieldInfo.fieldId] = '{' + | 
|  | 829 | fieldInfo.fieldType + ':' + value + '}'; | 
|  | 830 | this.tpos.pop(); | 
|  | 831 | }, | 
|  | 832 |  | 
|  | 833 | /** | 
|  | 834 | * Serializes the end of the set of fields for a struct. | 
|  | 835 | */ | 
|  | 836 | writeFieldStop: function() { | 
|  | 837 | //na | 
|  | 838 | }, | 
|  | 839 |  | 
|  | 840 | /** | 
|  | 841 | * Serializes the beginning of a map collection. | 
|  | 842 | * @param {Thrift.Type} keyType - The data type of the key. | 
|  | 843 | * @param {Thrift.Type} valType - The data type of the value. | 
|  | 844 | * @param {number} [size] - The number of elements in the map (ignored). | 
|  | 845 | */ | 
|  | 846 | writeMapBegin: function(keyType, valType, size) { | 
|  | 847 | this.tpos.push(this.tstack.length); | 
|  | 848 | this.tstack.push([Thrift.Protocol.Type[keyType], | 
|  | 849 | Thrift.Protocol.Type[valType], 0]); | 
|  | 850 | }, | 
|  | 851 |  | 
|  | 852 | /** | 
|  | 853 | * Serializes the end of a map. | 
|  | 854 | */ | 
|  | 855 | writeMapEnd: function() { | 
|  | 856 | var p = this.tpos.pop(); | 
|  | 857 |  | 
|  | 858 | if (p == this.tstack.length) { | 
|  | 859 | return; | 
|  | 860 | } | 
|  | 861 |  | 
|  | 862 | if ((this.tstack.length - p - 1) % 2 !== 0) { | 
|  | 863 | this.tstack.push(''); | 
|  | 864 | } | 
|  | 865 |  | 
|  | 866 | var size = (this.tstack.length - p - 1) / 2; | 
|  | 867 |  | 
|  | 868 | this.tstack[p][this.tstack[p].length - 1] = size; | 
|  | 869 |  | 
|  | 870 | var map = '}'; | 
|  | 871 | var first = true; | 
|  | 872 | while (this.tstack.length > p + 1) { | 
|  | 873 | var v = this.tstack.pop(); | 
|  | 874 | var k = this.tstack.pop(); | 
|  | 875 | if (first) { | 
|  | 876 | first = false; | 
|  | 877 | } else { | 
|  | 878 | map = ',' + map; | 
|  | 879 | } | 
|  | 880 |  | 
|  | 881 | if (! isNaN(k)) { k = '"' + k + '"'; } //json "keys" need to be strings | 
|  | 882 | map = k + ':' + v + map; | 
|  | 883 | } | 
|  | 884 | map = '{' + map; | 
|  | 885 |  | 
|  | 886 | this.tstack[p].push(map); | 
|  | 887 | this.tstack[p] = '[' + this.tstack[p].join(',') + ']'; | 
|  | 888 | }, | 
|  | 889 |  | 
|  | 890 | /** | 
|  | 891 | * Serializes the beginning of a list collection. | 
|  | 892 | * @param {Thrift.Type} elemType - The data type of the elements. | 
|  | 893 | * @param {number} size - The number of elements in the list. | 
|  | 894 | */ | 
|  | 895 | writeListBegin: function(elemType, size) { | 
|  | 896 | this.tpos.push(this.tstack.length); | 
|  | 897 | this.tstack.push([Thrift.Protocol.Type[elemType], size]); | 
|  | 898 | }, | 
|  | 899 |  | 
|  | 900 | /** | 
|  | 901 | * Serializes the end of a list. | 
|  | 902 | */ | 
|  | 903 | writeListEnd: function() { | 
|  | 904 | var p = this.tpos.pop(); | 
|  | 905 |  | 
|  | 906 | while (this.tstack.length > p + 1) { | 
|  | 907 | var tmpVal = this.tstack[p + 1]; | 
|  | 908 | this.tstack.splice(p + 1, 1); | 
|  | 909 | this.tstack[p].push(tmpVal); | 
|  | 910 | } | 
|  | 911 |  | 
|  | 912 | this.tstack[p] = '[' + this.tstack[p].join(',') + ']'; | 
|  | 913 | }, | 
|  | 914 |  | 
|  | 915 | /** | 
|  | 916 | * Serializes the beginning of a set collection. | 
|  | 917 | * @param {Thrift.Type} elemType - The data type of the elements. | 
|  | 918 | * @param {number} size - The number of elements in the list. | 
|  | 919 | */ | 
|  | 920 | writeSetBegin: function(elemType, size) { | 
|  | 921 | this.tpos.push(this.tstack.length); | 
|  | 922 | this.tstack.push([Thrift.Protocol.Type[elemType], size]); | 
|  | 923 | }, | 
|  | 924 |  | 
|  | 925 | /** | 
|  | 926 | * Serializes the end of a set. | 
|  | 927 | */ | 
|  | 928 | writeSetEnd: function() { | 
|  | 929 | var p = this.tpos.pop(); | 
|  | 930 |  | 
|  | 931 | while (this.tstack.length > p + 1) { | 
|  | 932 | var tmpVal = this.tstack[p + 1]; | 
|  | 933 | this.tstack.splice(p + 1, 1); | 
|  | 934 | this.tstack[p].push(tmpVal); | 
|  | 935 | } | 
|  | 936 |  | 
|  | 937 | this.tstack[p] = '[' + this.tstack[p].join(',') + ']'; | 
|  | 938 | }, | 
|  | 939 |  | 
|  | 940 | /** Serializes a boolean */ | 
|  | 941 | writeBool: function(value) { | 
|  | 942 | this.tstack.push(value ? 1 : 0); | 
|  | 943 | }, | 
|  | 944 |  | 
|  | 945 | /** Serializes a number */ | 
|  | 946 | writeByte: function(i8) { | 
|  | 947 | this.tstack.push(i8); | 
|  | 948 | }, | 
|  | 949 |  | 
|  | 950 | /** Serializes a number */ | 
|  | 951 | writeI16: function(i16) { | 
|  | 952 | this.tstack.push(i16); | 
|  | 953 | }, | 
|  | 954 |  | 
|  | 955 | /** Serializes a number */ | 
|  | 956 | writeI32: function(i32) { | 
|  | 957 | this.tstack.push(i32); | 
|  | 958 | }, | 
|  | 959 |  | 
|  | 960 | /** Serializes a number */ | 
|  | 961 | writeI64: function(i64) { | 
|  | 962 | this.tstack.push(i64); | 
|  | 963 | }, | 
|  | 964 |  | 
|  | 965 | /** Serializes a number */ | 
|  | 966 | writeDouble: function(dbl) { | 
|  | 967 | this.tstack.push(dbl); | 
|  | 968 | }, | 
|  | 969 |  | 
|  | 970 | /** Serializes a string */ | 
|  | 971 | writeString: function(str) { | 
|  | 972 | // We do not encode uri components for wire transfer: | 
|  | 973 | if (str === null) { | 
|  | 974 | this.tstack.push(null); | 
|  | 975 | } else { | 
|  | 976 | // concat may be slower than building a byte buffer | 
|  | 977 | var escapedString = ''; | 
|  | 978 | for (var i = 0; i < str.length; i++) { | 
|  | 979 | var ch = str.charAt(i);      // a single double quote: " | 
|  | 980 | if (ch === '\"') { | 
|  | 981 | escapedString += '\\\"'; // write out as: \" | 
| Roger Meier | 52744ee | 2014-03-12 09:38:42 +0100 | [diff] [blame] | 982 | } else if (ch === '\\') {    // a single backslash | 
|  | 983 | escapedString += '\\\\'; // write out as double backslash | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 984 | } else if (ch === '\b') {    // a single backspace: invisible | 
|  | 985 | escapedString += '\\b';  // write out as: \b" | 
|  | 986 | } else if (ch === '\f') {    // a single formfeed: invisible | 
|  | 987 | escapedString += '\\f';  // write out as: \f" | 
|  | 988 | } else if (ch === '\n') {    // a single newline: invisible | 
|  | 989 | escapedString += '\\n';  // write out as: \n" | 
|  | 990 | } else if (ch === '\r') {    // a single return: invisible | 
|  | 991 | escapedString += '\\r';  // write out as: \r" | 
|  | 992 | } else if (ch === '\t') {    // a single tab: invisible | 
|  | 993 | escapedString += '\\t';  // write out as: \t" | 
|  | 994 | } else { | 
|  | 995 | escapedString += ch;     // Else it need not be escaped | 
|  | 996 | } | 
|  | 997 | } | 
|  | 998 | this.tstack.push('"' + escapedString + '"'); | 
|  | 999 | } | 
|  | 1000 | }, | 
|  | 1001 |  | 
|  | 1002 | /** Serializes a string */ | 
|  | 1003 | writeBinary: function(str) { | 
|  | 1004 | this.writeString(str); | 
|  | 1005 | }, | 
|  | 1006 |  | 
|  | 1007 | /** | 
|  | 1008 | @class | 
|  | 1009 | @name AnonReadMessageBeginReturn | 
|  | 1010 | @property {string} fname - The name of the service method. | 
|  | 1011 | @property {Thrift.MessageType} mtype - The type of message call. | 
|  | 1012 | @property {number} rseqid - The sequence number of the message (0 in Thrift RPC). | 
|  | 1013 | */ | 
|  | 1014 | /** | 
|  | 1015 | * Deserializes the beginning of a message. | 
|  | 1016 | * @returns {AnonReadMessageBeginReturn} | 
|  | 1017 | */ | 
|  | 1018 | readMessageBegin: function() { | 
|  | 1019 | this.rstack = []; | 
|  | 1020 | this.rpos = []; | 
|  | 1021 |  | 
| Roger Meier | 52744ee | 2014-03-12 09:38:42 +0100 | [diff] [blame] | 1022 | if (typeof JSON !== 'undefined' && typeof JSON.parse === 'function') { | 
|  | 1023 | this.robj = JSON.parse(this.transport.readAll()); | 
|  | 1024 | } else if (typeof jQuery !== 'undefined') { | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 1025 | this.robj = jQuery.parseJSON(this.transport.readAll()); | 
|  | 1026 | } else { | 
|  | 1027 | this.robj = eval(this.transport.readAll()); | 
|  | 1028 | } | 
|  | 1029 |  | 
|  | 1030 | var r = {}; | 
|  | 1031 | var version = this.robj.shift(); | 
|  | 1032 |  | 
|  | 1033 | if (version != Thrift.Protocol.Version) { | 
|  | 1034 | throw 'Wrong thrift protocol version: ' + version; | 
|  | 1035 | } | 
|  | 1036 |  | 
|  | 1037 | r.fname = this.robj.shift(); | 
|  | 1038 | r.mtype = this.robj.shift(); | 
|  | 1039 | r.rseqid = this.robj.shift(); | 
|  | 1040 |  | 
|  | 1041 |  | 
|  | 1042 | //get to the main obj | 
|  | 1043 | this.rstack.push(this.robj.shift()); | 
|  | 1044 |  | 
|  | 1045 | return r; | 
|  | 1046 | }, | 
|  | 1047 |  | 
|  | 1048 | /** Deserializes the end of a message. */ | 
|  | 1049 | readMessageEnd: function() { | 
|  | 1050 | }, | 
|  | 1051 |  | 
|  | 1052 | /** | 
|  | 1053 | * Deserializes the beginning of a struct. | 
|  | 1054 | * @param {string} [name] - The name of the struct (ignored) | 
|  | 1055 | * @returns {object} - An object with an empty string fname property | 
|  | 1056 | */ | 
|  | 1057 | readStructBegin: function(name) { | 
|  | 1058 | var r = {}; | 
|  | 1059 | r.fname = ''; | 
|  | 1060 |  | 
|  | 1061 | //incase this is an array of structs | 
|  | 1062 | if (this.rstack[this.rstack.length - 1] instanceof Array) { | 
|  | 1063 | this.rstack.push(this.rstack[this.rstack.length - 1].shift()); | 
|  | 1064 | } | 
|  | 1065 |  | 
|  | 1066 | return r; | 
|  | 1067 | }, | 
|  | 1068 |  | 
|  | 1069 | /** Deserializes the end of a struct. */ | 
|  | 1070 | readStructEnd: function() { | 
|  | 1071 | if (this.rstack[this.rstack.length - 2] instanceof Array) { | 
|  | 1072 | this.rstack.pop(); | 
|  | 1073 | } | 
|  | 1074 | }, | 
|  | 1075 |  | 
|  | 1076 | /** | 
|  | 1077 | @class | 
|  | 1078 | @name AnonReadFieldBeginReturn | 
|  | 1079 | @property {string} fname - The name of the field (always ''). | 
|  | 1080 | @property {Thrift.Type} ftype - The data type of the field. | 
|  | 1081 | @property {number} fid - The unique identifier of the field. | 
|  | 1082 | */ | 
|  | 1083 | /** | 
|  | 1084 | * Deserializes the beginning of a field. | 
|  | 1085 | * @returns {AnonReadFieldBeginReturn} | 
|  | 1086 | */ | 
|  | 1087 | readFieldBegin: function() { | 
|  | 1088 | var r = {}; | 
|  | 1089 |  | 
|  | 1090 | var fid = -1; | 
|  | 1091 | var ftype = Thrift.Type.STOP; | 
|  | 1092 |  | 
|  | 1093 | //get a fieldId | 
|  | 1094 | for (var f in (this.rstack[this.rstack.length - 1])) { | 
|  | 1095 | if (f === null) { | 
|  | 1096 | continue; | 
|  | 1097 | } | 
|  | 1098 |  | 
|  | 1099 | fid = parseInt(f, 10); | 
|  | 1100 | this.rpos.push(this.rstack.length); | 
|  | 1101 |  | 
|  | 1102 | var field = this.rstack[this.rstack.length - 1][fid]; | 
|  | 1103 |  | 
|  | 1104 | //remove so we don't see it again | 
|  | 1105 | delete this.rstack[this.rstack.length - 1][fid]; | 
|  | 1106 |  | 
|  | 1107 | this.rstack.push(field); | 
|  | 1108 |  | 
|  | 1109 | break; | 
|  | 1110 | } | 
|  | 1111 |  | 
|  | 1112 | if (fid != -1) { | 
|  | 1113 |  | 
|  | 1114 | //should only be 1 of these but this is the only | 
|  | 1115 | //way to match a key | 
|  | 1116 | for (var i in (this.rstack[this.rstack.length - 1])) { | 
|  | 1117 | if (Thrift.Protocol.RType[i] === null) { | 
|  | 1118 | continue; | 
|  | 1119 | } | 
|  | 1120 |  | 
|  | 1121 | ftype = Thrift.Protocol.RType[i]; | 
|  | 1122 | this.rstack[this.rstack.length - 1] = | 
|  | 1123 | this.rstack[this.rstack.length - 1][i]; | 
|  | 1124 | } | 
|  | 1125 | } | 
|  | 1126 |  | 
|  | 1127 | r.fname = ''; | 
|  | 1128 | r.ftype = ftype; | 
|  | 1129 | r.fid = fid; | 
|  | 1130 |  | 
|  | 1131 | return r; | 
|  | 1132 | }, | 
|  | 1133 |  | 
|  | 1134 | /** Deserializes the end of a field. */ | 
|  | 1135 | readFieldEnd: function() { | 
|  | 1136 | var pos = this.rpos.pop(); | 
|  | 1137 |  | 
|  | 1138 | //get back to the right place in the stack | 
|  | 1139 | while (this.rstack.length > pos) { | 
|  | 1140 | this.rstack.pop(); | 
|  | 1141 | } | 
|  | 1142 |  | 
|  | 1143 | }, | 
|  | 1144 |  | 
|  | 1145 | /** | 
|  | 1146 | @class | 
|  | 1147 | @name AnonReadMapBeginReturn | 
|  | 1148 | @property {Thrift.Type} ktype - The data type of the key. | 
|  | 1149 | @property {Thrift.Type} vtype - The data type of the value. | 
|  | 1150 | @property {number} size - The number of elements in the map. | 
|  | 1151 | */ | 
|  | 1152 | /** | 
|  | 1153 | * Deserializes the beginning of a map. | 
|  | 1154 | * @returns {AnonReadMapBeginReturn} | 
|  | 1155 | */ | 
|  | 1156 | readMapBegin: function() { | 
|  | 1157 | var map = this.rstack.pop(); | 
| Liangliang He | 5d6378f | 2014-08-19 18:25:37 +0800 | [diff] [blame] | 1158 | var first = map.shift(); | 
|  | 1159 | if (first instanceof Array) { | 
|  | 1160 | this.rstack.push(map); | 
|  | 1161 | map = first; | 
|  | 1162 | first = map.shift(); | 
|  | 1163 | } | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 1164 |  | 
|  | 1165 | var r = {}; | 
| Liangliang He | 5d6378f | 2014-08-19 18:25:37 +0800 | [diff] [blame] | 1166 | r.ktype = Thrift.Protocol.RType[first]; | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 1167 | r.vtype = Thrift.Protocol.RType[map.shift()]; | 
|  | 1168 | r.size = map.shift(); | 
|  | 1169 |  | 
|  | 1170 |  | 
|  | 1171 | this.rpos.push(this.rstack.length); | 
|  | 1172 | this.rstack.push(map.shift()); | 
|  | 1173 |  | 
|  | 1174 | return r; | 
|  | 1175 | }, | 
|  | 1176 |  | 
|  | 1177 | /** Deserializes the end of a map. */ | 
|  | 1178 | readMapEnd: function() { | 
|  | 1179 | this.readFieldEnd(); | 
|  | 1180 | }, | 
|  | 1181 |  | 
|  | 1182 | /** | 
|  | 1183 | @class | 
|  | 1184 | @name AnonReadColBeginReturn | 
|  | 1185 | @property {Thrift.Type} etype - The data type of the element. | 
|  | 1186 | @property {number} size - The number of elements in the collection. | 
|  | 1187 | */ | 
|  | 1188 | /** | 
|  | 1189 | * Deserializes the beginning of a list. | 
|  | 1190 | * @returns {AnonReadColBeginReturn} | 
|  | 1191 | */ | 
|  | 1192 | readListBegin: function() { | 
|  | 1193 | var list = this.rstack[this.rstack.length - 1]; | 
|  | 1194 |  | 
|  | 1195 | var r = {}; | 
|  | 1196 | r.etype = Thrift.Protocol.RType[list.shift()]; | 
|  | 1197 | r.size = list.shift(); | 
|  | 1198 |  | 
|  | 1199 | this.rpos.push(this.rstack.length); | 
|  | 1200 | this.rstack.push(list); | 
|  | 1201 |  | 
|  | 1202 | return r; | 
|  | 1203 | }, | 
|  | 1204 |  | 
|  | 1205 | /** Deserializes the end of a list. */ | 
|  | 1206 | readListEnd: function() { | 
|  | 1207 | this.readFieldEnd(); | 
|  | 1208 | }, | 
|  | 1209 |  | 
|  | 1210 | /** | 
|  | 1211 | * Deserializes the beginning of a set. | 
|  | 1212 | * @returns {AnonReadColBeginReturn} | 
|  | 1213 | */ | 
|  | 1214 | readSetBegin: function(elemType, size) { | 
|  | 1215 | return this.readListBegin(elemType, size); | 
|  | 1216 | }, | 
|  | 1217 |  | 
|  | 1218 | /** Deserializes the end of a set. */ | 
|  | 1219 | readSetEnd: function() { | 
|  | 1220 | return this.readListEnd(); | 
|  | 1221 | }, | 
|  | 1222 |  | 
|  | 1223 | /** Returns an object with a value property set to | 
|  | 1224 | *  False unless the next number in the protocol buffer | 
| Konrad Grochowski | 3b5dacb | 2014-11-24 10:55:31 +0100 | [diff] [blame] | 1225 | *  is 1, in which case the value property is True */ | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 1226 | readBool: function() { | 
|  | 1227 | var r = this.readI32(); | 
|  | 1228 |  | 
|  | 1229 | if (r !== null && r.value == '1') { | 
|  | 1230 | r.value = true; | 
|  | 1231 | } else { | 
|  | 1232 | r.value = false; | 
|  | 1233 | } | 
|  | 1234 |  | 
|  | 1235 | return r; | 
|  | 1236 | }, | 
|  | 1237 |  | 
|  | 1238 | /** Returns the an object with a value property set to the | 
|  | 1239 | next value found in the protocol buffer */ | 
|  | 1240 | readByte: function() { | 
|  | 1241 | return this.readI32(); | 
|  | 1242 | }, | 
|  | 1243 |  | 
|  | 1244 | /** Returns the an object with a value property set to the | 
|  | 1245 | next value found in the protocol buffer */ | 
|  | 1246 | readI16: function() { | 
|  | 1247 | return this.readI32(); | 
|  | 1248 | }, | 
|  | 1249 |  | 
|  | 1250 | /** Returns the an object with a value property set to the | 
|  | 1251 | next value found in the protocol buffer */ | 
|  | 1252 | readI32: function(f) { | 
|  | 1253 | if (f === undefined) { | 
|  | 1254 | f = this.rstack[this.rstack.length - 1]; | 
|  | 1255 | } | 
|  | 1256 |  | 
|  | 1257 | var r = {}; | 
|  | 1258 |  | 
|  | 1259 | if (f instanceof Array) { | 
|  | 1260 | if (f.length === 0) { | 
|  | 1261 | r.value = undefined; | 
|  | 1262 | } else { | 
|  | 1263 | r.value = f.shift(); | 
|  | 1264 | } | 
|  | 1265 | } else if (f instanceof Object) { | 
|  | 1266 | for (var i in f) { | 
|  | 1267 | if (i === null) { | 
|  | 1268 | continue; | 
|  | 1269 | } | 
|  | 1270 | this.rstack.push(f[i]); | 
|  | 1271 | delete f[i]; | 
|  | 1272 |  | 
|  | 1273 | r.value = i; | 
|  | 1274 | break; | 
|  | 1275 | } | 
|  | 1276 | } else { | 
|  | 1277 | r.value = f; | 
|  | 1278 | this.rstack.pop(); | 
|  | 1279 | } | 
|  | 1280 |  | 
|  | 1281 | return r; | 
|  | 1282 | }, | 
|  | 1283 |  | 
|  | 1284 | /** Returns the an object with a value property set to the | 
|  | 1285 | next value found in the protocol buffer */ | 
|  | 1286 | readI64: function() { | 
|  | 1287 | return this.readI32(); | 
|  | 1288 | }, | 
|  | 1289 |  | 
|  | 1290 | /** Returns the an object with a value property set to the | 
|  | 1291 | next value found in the protocol buffer */ | 
|  | 1292 | readDouble: function() { | 
|  | 1293 | return this.readI32(); | 
|  | 1294 | }, | 
|  | 1295 |  | 
|  | 1296 | /** Returns the an object with a value property set to the | 
|  | 1297 | next value found in the protocol buffer */ | 
|  | 1298 | readString: function() { | 
|  | 1299 | var r = this.readI32(); | 
|  | 1300 | return r; | 
|  | 1301 | }, | 
|  | 1302 |  | 
|  | 1303 | /** Returns the an object with a value property set to the | 
|  | 1304 | next value found in the protocol buffer */ | 
|  | 1305 | readBinary: function() { | 
|  | 1306 | return this.readString(); | 
|  | 1307 | }, | 
|  | 1308 |  | 
|  | 1309 | /** | 
| Jens Geyer | 329d59a | 2014-06-19 22:11:53 +0200 | [diff] [blame] | 1310 | * Method to arbitrarily skip over data */ | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 1311 | skip: function(type) { | 
| Jens Geyer | 329d59a | 2014-06-19 22:11:53 +0200 | [diff] [blame] | 1312 | var ret, i; | 
|  | 1313 | switch (type) { | 
|  | 1314 | case Thrift.Type.STOP: | 
|  | 1315 | return null; | 
|  | 1316 |  | 
|  | 1317 | case Thrift.Type.BOOL: | 
|  | 1318 | return this.readBool(); | 
|  | 1319 |  | 
|  | 1320 | case Thrift.Type.BYTE: | 
|  | 1321 | return this.readByte(); | 
|  | 1322 |  | 
|  | 1323 | case Thrift.Type.I16: | 
|  | 1324 | return this.readI16(); | 
|  | 1325 |  | 
|  | 1326 | case Thrift.Type.I32: | 
|  | 1327 | return this.readI32(); | 
|  | 1328 |  | 
|  | 1329 | case Thrift.Type.I64: | 
|  | 1330 | return this.readI64(); | 
|  | 1331 |  | 
|  | 1332 | case Thrift.Type.DOUBLE: | 
|  | 1333 | return this.readDouble(); | 
|  | 1334 |  | 
|  | 1335 | case Thrift.Type.STRING: | 
|  | 1336 | return this.readString(); | 
|  | 1337 |  | 
|  | 1338 | case Thrift.Type.STRUCT: | 
|  | 1339 | this.readStructBegin(); | 
|  | 1340 | while (true) { | 
|  | 1341 | ret = this.readFieldBegin(); | 
|  | 1342 | if (ret.ftype == Thrift.Type.STOP) { | 
|  | 1343 | break; | 
|  | 1344 | } | 
|  | 1345 | this.skip(ret.ftype); | 
|  | 1346 | this.readFieldEnd(); | 
|  | 1347 | } | 
|  | 1348 | this.readStructEnd(); | 
|  | 1349 | return null; | 
|  | 1350 |  | 
|  | 1351 | case Thrift.Type.MAP: | 
|  | 1352 | ret = this.readMapBegin(); | 
|  | 1353 | for (i = 0; i < ret.size; i++) { | 
|  | 1354 | if (i > 0) { | 
|  | 1355 | if (this.rstack.length > this.rpos[this.rpos.length - 1] + 1) { | 
|  | 1356 | this.rstack.pop(); | 
|  | 1357 | } | 
|  | 1358 | } | 
|  | 1359 | this.skip(ret.ktype); | 
|  | 1360 | this.skip(ret.vtype); | 
|  | 1361 | } | 
|  | 1362 | this.readMapEnd(); | 
|  | 1363 | return null; | 
|  | 1364 |  | 
|  | 1365 | case Thrift.Type.SET: | 
|  | 1366 | ret = this.readSetBegin(); | 
|  | 1367 | for (i = 0; i < ret.size; i++) { | 
|  | 1368 | this.skip(ret.etype); | 
|  | 1369 | } | 
|  | 1370 | this.readSetEnd(); | 
|  | 1371 | return null; | 
|  | 1372 |  | 
|  | 1373 | case Thrift.Type.LIST: | 
|  | 1374 | ret = this.readListBegin(); | 
|  | 1375 | for (i = 0; i < ret.size; i++) { | 
|  | 1376 | this.skip(ret.etype); | 
|  | 1377 | } | 
|  | 1378 | this.readListEnd(); | 
|  | 1379 | return null; | 
|  | 1380 | } | 
| Henrique Mendonça | 095ddb7 | 2013-09-20 19:38:03 +0200 | [diff] [blame] | 1381 | } | 
|  | 1382 | }; | 
| henrique | 5ba91f2 | 2013-12-20 21:13:13 +0100 | [diff] [blame] | 1383 |  | 
|  | 1384 |  | 
|  | 1385 | /** | 
|  | 1386 | * Initializes a MutilplexProtocol Implementation as a Wrapper for Thrift.Protocol | 
|  | 1387 | * @constructor | 
|  | 1388 | */ | 
|  | 1389 | Thrift.MultiplexProtocol = function (srvName, trans, strictRead, strictWrite) { | 
|  | 1390 | Thrift.Protocol.call(this, trans, strictRead, strictWrite); | 
|  | 1391 | this.serviceName = srvName; | 
|  | 1392 | }; | 
|  | 1393 | Thrift.inherits(Thrift.MultiplexProtocol, Thrift.Protocol, 'multiplexProtocol'); | 
|  | 1394 |  | 
|  | 1395 | /** Override writeMessageBegin method of prototype*/ | 
|  | 1396 | Thrift.MultiplexProtocol.prototype.writeMessageBegin = function (name, type, seqid) { | 
|  | 1397 |  | 
|  | 1398 | if (type === Thrift.MessageType.CALL || type === Thrift.MessageType.ONEWAY) { | 
|  | 1399 | Thrift.Protocol.prototype.writeMessageBegin.call(this, this.serviceName + ":" + name, type, seqid); | 
|  | 1400 | } else { | 
|  | 1401 | Thrift.Protocol.prototype.writeMessageBegin.call(this, name, type, seqid); | 
|  | 1402 | } | 
|  | 1403 | }; | 
|  | 1404 |  | 
|  | 1405 | Thrift.Multiplexer = function () { | 
|  | 1406 | this.seqid = 0; | 
|  | 1407 | }; | 
|  | 1408 |  | 
|  | 1409 | /** Instantiates a multiplexed client for a specific service | 
|  | 1410 | * @constructor | 
|  | 1411 | * @param {String} serviceName - The transport to serialize to/from. | 
|  | 1412 | * @param {Thrift.ServiceClient} SCl - The Service Client Class | 
|  | 1413 | * @param {Thrift.Transport} transport - Thrift.Transport instance which provides remote host:port | 
|  | 1414 | * @example | 
|  | 1415 | *    var mp = new Thrift.Multiplexer(); | 
|  | 1416 | *    var transport = new Thrift.Transport("http://localhost:9090/foo.thrift"); | 
|  | 1417 | *    var protocol = new Thrift.Protocol(transport); | 
|  | 1418 | *    var client = mp.createClient('AuthService', AuthServiceClient, transport); | 
|  | 1419 | */ | 
|  | 1420 | Thrift.Multiplexer.prototype.createClient = function (serviceName, SCl, transport) { | 
|  | 1421 | if (SCl.Client) { | 
|  | 1422 | SCl = SCl.Client; | 
|  | 1423 | } | 
|  | 1424 | var self = this; | 
|  | 1425 | SCl.prototype.new_seqid = function () { | 
|  | 1426 | self.seqid += 1; | 
|  | 1427 | return self.seqid; | 
|  | 1428 | }; | 
|  | 1429 | var client = new SCl(new Thrift.MultiplexProtocol(serviceName, transport)); | 
|  | 1430 |  | 
|  | 1431 | return client; | 
|  | 1432 | }; | 
|  | 1433 |  | 
| henrique | a2de410 | 2014-02-07 14:12:56 +0100 | [diff] [blame] | 1434 |  | 
| henrique | 2a7dccc | 2014-03-07 22:16:51 +0100 | [diff] [blame] | 1435 |  |