Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
swarm: move timeoutFor function to playback_info (refs #92)
Browse files Browse the repository at this point in the history
  • Loading branch information
flavioribeiro committed Oct 12, 2014
1 parent ab3708d commit 0c1d8de
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
1 change: 0 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

Expand Down
10 changes: 10 additions & 0 deletions src/playback_info.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
9 changes: 5 additions & 4 deletions src/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
}

Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}
}
Expand Down
10 changes: 0 additions & 10 deletions src/swarm_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 0c1d8de

Please sign in to comment.