mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2025-01-18 20:21:36 +00:00
server swarm: split out announce events
This commit is contained in:
parent
8439300df9
commit
e234f14d56
132
lib/swarm.js
132
lib/swarm.js
@ -15,82 +15,84 @@ Swarm.prototype.announce = function (params, cb) {
|
||||
var self = this
|
||||
var peer = self.peers[params.addr]
|
||||
|
||||
var start = function () {
|
||||
if (peer) {
|
||||
debug('unexpected `started` event from peer that is already in swarm')
|
||||
return update() // treat as an update
|
||||
}
|
||||
if (params.left === 0) self.complete += 1
|
||||
else self.incomplete += 1
|
||||
peer = self.peers[params.addr] = {
|
||||
ip: params.ip,
|
||||
port: params.port,
|
||||
peerId: params.peer_id
|
||||
}
|
||||
self.emit('start', params.addr)
|
||||
// Dispatch announce event
|
||||
if (!params.event || params.event === 'empty') params.event = 'update'
|
||||
var fn = '_onAnnounce_' + params.event
|
||||
if (self[fn]) {
|
||||
self[fn](params, peer, function (err) {
|
||||
// event processed, prepare response:
|
||||
|
||||
if (params.left === 0 && peer) peer.complete = true
|
||||
|
||||
// send peers
|
||||
var peers = self._getPeers(params.numwant)
|
||||
|
||||
cb(null, {
|
||||
complete: self.complete,
|
||||
incomplete: self.incomplete,
|
||||
peers: peers
|
||||
})
|
||||
})
|
||||
} else {
|
||||
cb(new Error('invalid event'))
|
||||
}
|
||||
}
|
||||
|
||||
var stop = function () {
|
||||
if (!peer) {
|
||||
debug('unexpected `stopped` event from peer that is not in swarm')
|
||||
return // do nothing
|
||||
}
|
||||
if (peer.complete) self.complete -= 1
|
||||
else self.incomplete -= 1
|
||||
self.peers[params.addr] = null
|
||||
self.emit('stop', params.addr)
|
||||
Swarm.prototype._onAnnounce_started = function (params, peer, cb) {
|
||||
if (peer) {
|
||||
debug('unexpected `started` event from peer that is already in swarm')
|
||||
return this._onAnnounce_update() // treat as an update
|
||||
}
|
||||
|
||||
var complete = function () {
|
||||
if (!peer) {
|
||||
debug('unexpected `completed` event from peer that is not in swarm')
|
||||
return start() // treat as a start
|
||||
}
|
||||
if (peer.complete) {
|
||||
debug('unexpected `completed` event from peer that is already marked as completed')
|
||||
return // do nothing
|
||||
}
|
||||
self.complete += 1
|
||||
self.incomplete -= 1
|
||||
peer.complete = true
|
||||
self.emit('complete', params.addr)
|
||||
if (params.left === 0) this.complete += 1
|
||||
else this.incomplete += 1
|
||||
peer = this.peers[params.addr] = {
|
||||
ip: params.ip,
|
||||
port: params.port,
|
||||
peerId: params.peer_id
|
||||
}
|
||||
this.emit('start', params.addr)
|
||||
|
||||
var update = function () {
|
||||
if (!peer) {
|
||||
debug('unexpected `update` event from peer that is not in swarm')
|
||||
return start() // treat as a start
|
||||
}
|
||||
self.emit('update', params.addr)
|
||||
cb()
|
||||
}
|
||||
|
||||
Swarm.prototype._onAnnounce_stopped = function (params, peer, cb) {
|
||||
if (!peer) {
|
||||
debug('unexpected `stopped` event from peer that is not in swarm')
|
||||
return // do nothing
|
||||
}
|
||||
if (peer.complete) this.complete -= 1
|
||||
else this.incomplete -= 1
|
||||
this.peers[params.addr] = null
|
||||
this.emit('stop', params.addr)
|
||||
|
||||
switch (params.event) {
|
||||
case 'started':
|
||||
start()
|
||||
break
|
||||
case 'stopped':
|
||||
stop()
|
||||
break
|
||||
case 'completed':
|
||||
complete()
|
||||
break
|
||||
case '': case undefined: case 'empty': case 'update': // update
|
||||
update()
|
||||
break
|
||||
default:
|
||||
return cb(new Error('invalid event')) // early return
|
||||
cb()
|
||||
}
|
||||
|
||||
Swarm.prototype._onAnnounce_completed = function (params, peer, cb) {
|
||||
if (!peer) {
|
||||
debug('unexpected `completed` event from peer that is not in swarm')
|
||||
return start() // treat as a start
|
||||
}
|
||||
if (peer.complete) {
|
||||
debug('unexpected `completed` event from peer that is already marked as completed')
|
||||
return // do nothing
|
||||
}
|
||||
this.complete += 1
|
||||
this.incomplete -= 1
|
||||
peer.complete = true
|
||||
this.emit('complete', params.addr)
|
||||
|
||||
if (params.left === 0 && peer) peer.complete = true
|
||||
cb()
|
||||
}
|
||||
|
||||
// send peers
|
||||
var peers = self._getPeers(params.numwant)
|
||||
Swarm.prototype._onAnnounce_update = function (params, peer, cb) {
|
||||
if (!peer) {
|
||||
debug('unexpected `update` event from peer that is not in swarm')
|
||||
return start() // treat as a start
|
||||
}
|
||||
this.emit('update', params.addr)
|
||||
|
||||
cb(null, {
|
||||
complete: this.complete,
|
||||
incomplete: this.incomplete,
|
||||
peers: peers
|
||||
})
|
||||
cb()
|
||||
}
|
||||
|
||||
Swarm.prototype._getPeers = function (numwant) {
|
||||
|
Loading…
Reference in New Issue
Block a user