mirror of
https://github.com/webtorrent/bittorrent-tracker.git
synced 2025-01-19 04:31: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 self = this
|
||||||
var peer = self.peers[params.addr]
|
var peer = self.peers[params.addr]
|
||||||
|
|
||||||
var start = function () {
|
// Dispatch announce event
|
||||||
if (peer) {
|
if (!params.event || params.event === 'empty') params.event = 'update'
|
||||||
debug('unexpected `started` event from peer that is already in swarm')
|
var fn = '_onAnnounce_' + params.event
|
||||||
return update() // treat as an update
|
if (self[fn]) {
|
||||||
}
|
self[fn](params, peer, function (err) {
|
||||||
if (params.left === 0) self.complete += 1
|
// event processed, prepare response:
|
||||||
else self.incomplete += 1
|
|
||||||
peer = self.peers[params.addr] = {
|
if (params.left === 0 && peer) peer.complete = true
|
||||||
ip: params.ip,
|
|
||||||
port: params.port,
|
// send peers
|
||||||
peerId: params.peer_id
|
var peers = self._getPeers(params.numwant)
|
||||||
}
|
|
||||||
self.emit('start', params.addr)
|
cb(null, {
|
||||||
|
complete: self.complete,
|
||||||
|
incomplete: self.incomplete,
|
||||||
|
peers: peers
|
||||||
|
})
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
cb(new Error('invalid event'))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var stop = function () {
|
Swarm.prototype._onAnnounce_started = function (params, peer, cb) {
|
||||||
if (!peer) {
|
if (peer) {
|
||||||
debug('unexpected `stopped` event from peer that is not in swarm')
|
debug('unexpected `started` event from peer that is already in swarm')
|
||||||
return // do nothing
|
return this._onAnnounce_update() // treat as an update
|
||||||
}
|
|
||||||
if (peer.complete) self.complete -= 1
|
|
||||||
else self.incomplete -= 1
|
|
||||||
self.peers[params.addr] = null
|
|
||||||
self.emit('stop', params.addr)
|
|
||||||
}
|
}
|
||||||
|
if (params.left === 0) this.complete += 1
|
||||||
var complete = function () {
|
else this.incomplete += 1
|
||||||
if (!peer) {
|
peer = this.peers[params.addr] = {
|
||||||
debug('unexpected `completed` event from peer that is not in swarm')
|
ip: params.ip,
|
||||||
return start() // treat as a start
|
port: params.port,
|
||||||
}
|
peerId: params.peer_id
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
this.emit('start', params.addr)
|
||||||
|
|
||||||
var update = function () {
|
cb()
|
||||||
if (!peer) {
|
}
|
||||||
debug('unexpected `update` event from peer that is not in swarm')
|
|
||||||
return start() // treat as a start
|
Swarm.prototype._onAnnounce_stopped = function (params, peer, cb) {
|
||||||
}
|
if (!peer) {
|
||||||
self.emit('update', params.addr)
|
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) {
|
cb()
|
||||||
case 'started':
|
}
|
||||||
start()
|
|
||||||
break
|
Swarm.prototype._onAnnounce_completed = function (params, peer, cb) {
|
||||||
case 'stopped':
|
if (!peer) {
|
||||||
stop()
|
debug('unexpected `completed` event from peer that is not in swarm')
|
||||||
break
|
return start() // treat as a start
|
||||||
case 'completed':
|
|
||||||
complete()
|
|
||||||
break
|
|
||||||
case '': case undefined: case 'empty': case 'update': // update
|
|
||||||
update()
|
|
||||||
break
|
|
||||||
default:
|
|
||||||
return cb(new Error('invalid event')) // early return
|
|
||||||
}
|
}
|
||||||
|
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
|
Swarm.prototype._onAnnounce_update = function (params, peer, cb) {
|
||||||
var peers = self._getPeers(params.numwant)
|
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, {
|
cb()
|
||||||
complete: this.complete,
|
|
||||||
incomplete: this.incomplete,
|
|
||||||
peers: peers
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Swarm.prototype._getPeers = function (numwant) {
|
Swarm.prototype._getPeers = function (numwant) {
|
||||||
|
Loading…
Reference in New Issue
Block a user