From 0c1d8de5ed97f33b0f1253e4336f546f4e14e35f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=A1vio=20Ribeiro?= Date: Sun, 12 Oct 2014 15:34:41 -0300 Subject: [PATCH] swarm: move timeoutFor function to playback_info (refs #92) --- src/main.js | 1 - src/playback_info.js | 10 ++++++++++ src/swarm.js | 9 +++++---- src/swarm_utils.js | 10 ---------- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/main.js b/src/main.js index 6720744..775da38 100644 --- a/src/main.js +++ b/src/main.js @@ -68,7 +68,6 @@ class P2PHLS extends HLS { requestResource(url) { this.currentUrl = url this.playbackInfo.addData({'segmentSize': this.getAverageSegmentSize()}) - this.resourceRequester.p2pManager.swarm.avgSegmentSize = this.getAverageSegmentSize() this.resourceRequester.requestResource(url, this.bufferLength, (chunk, method) => this.resourceLoaded(chunk, method)) } diff --git a/src/playback_info.js b/src/playback_info.js index efe7b87..d8cb1bd 100644 --- a/src/playback_info.js +++ b/src/playback_info.js @@ -19,6 +19,16 @@ class PlaybackInfo extends BaseObject { addData(metrics) { this.data = _.extend(this.data, metrics) } + + timeoutFor(command) { + var segmentSize = this.data.segmentSize? this.data.segmentSize * 1000: 2000 + if (command === 'interested') { + var timeout = segmentSize / 3 + return timeout > 2000? 2000: timeout + } else if (command === 'request') { + return segmentSize * 0.6 + } + } } PlaybackInfo.getInstance = function() { diff --git a/src/swarm.js b/src/swarm.js index 7a4c1f2..fabab00 100644 --- a/src/swarm.js +++ b/src/swarm.js @@ -10,15 +10,15 @@ var Settings = require('./settings') var _ = require('underscore') var log = require('./log') var SwarmUtils = require('./swarm_utils') - +var PlaybackInfo = require('./playback_info') class Swarm extends BaseObject { constructor() { + this.playbackInfo = PlaybackInfo.getInstance() this.utils = new SwarmUtils(this) this.peers = [] this.satisfyCandidate = undefined this.chokedClients = 0 - this.avgSegmentSize = 0 this.peersContainsResource = [] } @@ -64,7 +64,7 @@ class Swarm extends BaseObject { this.externalCallbackSuccess = callbackSuccess this.currentResource = resource this.sendTo('contributors', 'interested', resource) - var timeout = this.utils.timeoutFor('interested') + var timeout = this.playbackInfo.timeoutFor('interested') this.interestedFailID = setTimeout(this.callbackFail.bind(this), timeout) } @@ -86,7 +86,8 @@ class Swarm extends BaseObject { } else { this.satisfyCandidate = peer.ident this.clearInterestedFailInterval() - this.requestFailID = setTimeout(this.callbackFail.bind(this), this.utils.timeoutFor('request')) + var timeout = this.playbackInfo.timeoutFor('request') + this.requestFailID = setTimeout(this.callbackFail.bind(this), timeout) this.sendTo(this.satisfyCandidate, 'request', resource) } } diff --git a/src/swarm_utils.js b/src/swarm_utils.js index 24cbd8d..1d55f3d 100644 --- a/src/swarm_utils.js +++ b/src/swarm_utils.js @@ -43,16 +43,6 @@ class SwarmUtils extends BaseObject { getLowestScorePeer() { return _.first(_.sortBy(this.swarm.peers, function(p) { return p.score })) } - - timeoutFor(command) { - var segmentSize = this.swarm.avgSegmentSize > 0? this.swarm.avgSegmentSize * 1000: 1000 - if (command === 'interested') { - var timeout = segmentSize / 3 - return timeout > 2000? 2000: timeout - } else if (command === 'request') { - return segmentSize * 0.6 - } - } } module.exports = SwarmUtils