From: henrique Date: Fri, 20 Dec 2013 20:13:13 +0000 (+0100) Subject: THRIFT-2271 JavaScript: Support for Multiplexing Services X-Git-Url: https://source.supwisdom.com/gerrit/gitweb?a=commitdiff_plain;h=5ba91f23bdf627bd869410c53a3d9181c8cfb526;p=common%2Fthrift.git THRIFT-2271 JavaScript: Support for Multiplexing Services Patch: David Sautter + jslint fixes --- diff --git a/lib/js/src/thrift.js b/lib/js/src/thrift.js index 3b489e57..4e1f20fd 100644 --- a/lib/js/src/thrift.js +++ b/lib/js/src/thrift.js @@ -1101,3 +1101,54 @@ Thrift.Protocol.prototype = { throw 'skip not supported yet'; } }; + + +/** + * Initializes a MutilplexProtocol Implementation as a Wrapper for Thrift.Protocol + * @constructor + */ +Thrift.MultiplexProtocol = function (srvName, trans, strictRead, strictWrite) { + Thrift.Protocol.call(this, trans, strictRead, strictWrite); + this.serviceName = srvName; +}; +Thrift.inherits(Thrift.MultiplexProtocol, Thrift.Protocol, 'multiplexProtocol'); + +/** Override writeMessageBegin method of prototype*/ +Thrift.MultiplexProtocol.prototype.writeMessageBegin = function (name, type, seqid) { + + if (type === Thrift.MessageType.CALL || type === Thrift.MessageType.ONEWAY) { + Thrift.Protocol.prototype.writeMessageBegin.call(this, this.serviceName + ":" + name, type, seqid); + } else { + Thrift.Protocol.prototype.writeMessageBegin.call(this, name, type, seqid); + } +}; + +Thrift.Multiplexer = function () { + this.seqid = 0; +}; + +/** Instantiates a multiplexed client for a specific service + * @constructor + * @param {String} serviceName - The transport to serialize to/from. + * @param {Thrift.ServiceClient} SCl - The Service Client Class + * @param {Thrift.Transport} transport - Thrift.Transport instance which provides remote host:port + * @example + * var mp = new Thrift.Multiplexer(); + * var transport = new Thrift.Transport("http://localhost:9090/foo.thrift"); + * var protocol = new Thrift.Protocol(transport); + * var client = mp.createClient('AuthService', AuthServiceClient, transport); +*/ +Thrift.Multiplexer.prototype.createClient = function (serviceName, SCl, transport) { + if (SCl.Client) { + SCl = SCl.Client; + } + var self = this; + SCl.prototype.new_seqid = function () { + self.seqid += 1; + return self.seqid; + }; + var client = new SCl(new Thrift.MultiplexProtocol(serviceName, transport)); + + return client; +}; +