2014-12-10 15:47:41 +00:00
|
|
|
module.exports = Swarm
|
|
|
|
|
2014-12-11 20:53:39 +00:00
|
|
|
var debug = require('debug')('bittorrent-tracker')
|
|
|
|
|
2014-12-10 15:47:41 +00:00
|
|
|
// Regard this as the default implementation of an interface that you
|
|
|
|
// need to support when overriding Server.getSwarm()
|
|
|
|
function Swarm (infoHash, server) {
|
|
|
|
this.peers = {}
|
|
|
|
this.complete = 0
|
|
|
|
this.incomplete = 0
|
|
|
|
}
|
|
|
|
|
|
|
|
Swarm.prototype.announce = function (params, cb) {
|
|
|
|
var self = this
|
2015-03-27 03:19:06 +00:00
|
|
|
var peer = self.peers[params.addr || params.peer_id]
|
2014-12-10 15:47:41 +00:00
|
|
|
|
2014-12-10 16:01:34 +00:00
|
|
|
// Dispatch announce event
|
|
|
|
var fn = '_onAnnounce_' + params.event
|
|
|
|
if (self[fn]) {
|
2014-12-11 21:43:11 +00:00
|
|
|
self[fn](params, peer) // process event
|
2014-12-10 16:01:34 +00:00
|
|
|
|
2014-12-11 21:43:11 +00:00
|
|
|
if (params.left === 0 && peer) peer.complete = true
|
2014-12-10 15:47:41 +00:00
|
|
|
|
2014-12-11 21:43:11 +00:00
|
|
|
cb(null, {
|
|
|
|
complete: self.complete,
|
|
|
|
incomplete: self.incomplete,
|
2015-03-27 03:19:06 +00:00
|
|
|
peers: self._getPeers(params.numwant, params.peer_id)
|
2014-12-10 16:01:34 +00:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
cb(new Error('invalid event'))
|
2014-12-10 15:47:41 +00:00
|
|
|
}
|
2014-12-10 16:01:34 +00:00
|
|
|
}
|
2014-12-10 15:47:41 +00:00
|
|
|
|
2015-03-27 03:19:06 +00:00
|
|
|
Swarm.prototype.scrape = function (params, cb) {
|
|
|
|
cb(null, {
|
|
|
|
complete: this.complete,
|
|
|
|
incomplete: this.incomplete
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-12-11 21:43:11 +00:00
|
|
|
Swarm.prototype._onAnnounce_started = function (params, peer) {
|
2014-12-10 16:01:34 +00:00
|
|
|
if (peer) {
|
|
|
|
debug('unexpected `started` event from peer that is already in swarm')
|
2014-12-11 21:43:11 +00:00
|
|
|
return this._onAnnounce_update(params, peer) // treat as an update
|
2014-12-10 16:01:34 +00:00
|
|
|
}
|
2014-12-11 21:43:11 +00:00
|
|
|
|
2014-12-10 16:01:34 +00:00
|
|
|
if (params.left === 0) this.complete += 1
|
|
|
|
else this.incomplete += 1
|
2015-03-27 03:19:06 +00:00
|
|
|
peer = this.peers[params.addr || params.peer_id] = {
|
|
|
|
complete: false,
|
|
|
|
ip: params.ip, // only http+udp
|
2015-03-29 08:08:26 +00:00
|
|
|
peerId: params.peer_id, // as hex
|
2015-03-27 03:19:06 +00:00
|
|
|
port: params.port, // only http+udp
|
|
|
|
socket: params.socket // only websocket
|
2014-12-10 15:47:41 +00:00
|
|
|
}
|
2014-12-10 16:01:34 +00:00
|
|
|
}
|
2014-12-10 15:47:41 +00:00
|
|
|
|
2014-12-11 21:43:11 +00:00
|
|
|
Swarm.prototype._onAnnounce_stopped = function (params, peer) {
|
2014-12-10 16:01:34 +00:00
|
|
|
if (!peer) {
|
|
|
|
debug('unexpected `stopped` event from peer that is not in swarm')
|
|
|
|
return // do nothing
|
2014-12-10 15:47:41 +00:00
|
|
|
}
|
2014-12-11 21:43:11 +00:00
|
|
|
|
2014-12-10 16:01:34 +00:00
|
|
|
if (peer.complete) this.complete -= 1
|
|
|
|
else this.incomplete -= 1
|
2015-03-27 03:19:06 +00:00
|
|
|
this.peers[params.addr || params.peer_id] = null
|
2014-12-10 16:01:34 +00:00
|
|
|
}
|
|
|
|
|
2014-12-11 21:43:11 +00:00
|
|
|
Swarm.prototype._onAnnounce_completed = function (params, peer) {
|
2014-12-10 16:01:34 +00:00
|
|
|
if (!peer) {
|
|
|
|
debug('unexpected `completed` event from peer that is not in swarm')
|
2014-12-11 21:43:11 +00:00
|
|
|
return this._onAnnounce_started(params, peer) // treat as a start
|
2014-12-10 15:47:41 +00:00
|
|
|
}
|
2014-12-10 16:01:34 +00:00
|
|
|
if (peer.complete) {
|
|
|
|
debug('unexpected `completed` event from peer that is already marked as completed')
|
|
|
|
return // do nothing
|
|
|
|
}
|
2014-12-11 21:43:11 +00:00
|
|
|
|
2014-12-10 16:01:34 +00:00
|
|
|
this.complete += 1
|
|
|
|
this.incomplete -= 1
|
|
|
|
peer.complete = true
|
|
|
|
}
|
2014-12-10 15:47:41 +00:00
|
|
|
|
2014-12-11 21:43:11 +00:00
|
|
|
Swarm.prototype._onAnnounce_update = function (params, peer) {
|
2014-12-10 16:01:34 +00:00
|
|
|
if (!peer) {
|
|
|
|
debug('unexpected `update` event from peer that is not in swarm')
|
2014-12-11 21:43:11 +00:00
|
|
|
return this._onAnnounce_started(params, peer) // treat as a start
|
2014-12-10 16:01:34 +00:00
|
|
|
}
|
2014-12-10 15:47:41 +00:00
|
|
|
}
|
|
|
|
|
2015-03-27 03:19:06 +00:00
|
|
|
// TODO: randomize the peers that are given out
|
|
|
|
Swarm.prototype._getPeers = function (numWant, fromPeerId) {
|
2014-12-10 15:47:41 +00:00
|
|
|
var peers = []
|
|
|
|
for (var peerId in this.peers) {
|
2015-03-27 03:19:06 +00:00
|
|
|
if (peers.length >= numWant) break
|
|
|
|
if (peerId === fromPeerId) continue // skip self
|
|
|
|
|
2014-12-10 15:47:41 +00:00
|
|
|
var peer = this.peers[peerId]
|
|
|
|
if (!peer) continue // ignore null values
|
2015-03-27 03:19:06 +00:00
|
|
|
peers.push(peer)
|
2014-12-10 15:47:41 +00:00
|
|
|
}
|
|
|
|
return peers
|
|
|
|
}
|