michael@0: var scheduler = exports; michael@0: michael@0: // michael@0: // ### function Scheduler (connection) michael@0: // #### @connection {spdy.Connection} active connection michael@0: // Connection's streams scheduler michael@0: // michael@0: function Scheduler(connection) { michael@0: this.connection = connection; michael@0: this.priorities = [[], [], [], [], [], [], [], []]; michael@0: this._tickListener = null; michael@0: } michael@0: michael@0: // michael@0: // ### function create (connection) michael@0: // #### @connection {spdy.Connection} active connection michael@0: // michael@0: exports.create = function create(connection) { michael@0: return new Scheduler(connection); michael@0: }; michael@0: michael@0: // michael@0: // ### function schedule (stream, data) michael@0: // #### @stream {spdy.Stream} Source stream michael@0: // #### @data {Buffer} data to write on tick michael@0: // Use stream priority to invoke callbacks in right order michael@0: // michael@0: Scheduler.prototype.schedule = function schedule(stream, data) { michael@0: this.priorities[stream.priority].push(data); michael@0: }; michael@0: michael@0: // michael@0: // ### function tick () michael@0: // Add .nextTick callback if not already present michael@0: // michael@0: Scheduler.prototype.tick = function tick() { michael@0: if (this._tickListener !== null) return; michael@0: var self = this; michael@0: this._tickListener = function() { michael@0: var priorities = self.priorities; michael@0: michael@0: self._tickListener = null; michael@0: self.priorities = [[], [], [], [], [], [], [], []]; michael@0: michael@0: // Run all priorities michael@0: for (var i = 0; i < 8; i++) { michael@0: for (var j = 0; j < priorities[i].length; j++) { michael@0: self.connection.write( michael@0: priorities[i][j] michael@0: ); michael@0: } michael@0: } michael@0: }; michael@0: michael@0: if (this.connection.parser.drained) { michael@0: process.nextTick(this._tickListener); michael@0: } else { michael@0: this.connection.parser.once('drain', this._tickListener); michael@0: } michael@0: };