server swarm: split out announce events

This commit is contained in:
Astro 2014-12-10 17:01:34 +01:00
parent 8439300df9
commit e234f14d56

View File

@ -15,33 +15,60 @@ 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 (!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'))
}
}
Swarm.prototype._onAnnounce_started = function (params, peer, cb) {
if (peer) { if (peer) {
debug('unexpected `started` event from peer that is already in swarm') debug('unexpected `started` event from peer that is already in swarm')
return update() // treat as an update return this._onAnnounce_update() // treat as an update
} }
if (params.left === 0) self.complete += 1 if (params.left === 0) this.complete += 1
else self.incomplete += 1 else this.incomplete += 1
peer = self.peers[params.addr] = { peer = this.peers[params.addr] = {
ip: params.ip, ip: params.ip,
port: params.port, port: params.port,
peerId: params.peer_id peerId: params.peer_id
} }
self.emit('start', params.addr) this.emit('start', params.addr)
cb()
} }
var stop = function () { Swarm.prototype._onAnnounce_stopped = function (params, peer, cb) {
if (!peer) { if (!peer) {
debug('unexpected `stopped` event from peer that is not in swarm') debug('unexpected `stopped` event from peer that is not in swarm')
return // do nothing return // do nothing
} }
if (peer.complete) self.complete -= 1 if (peer.complete) this.complete -= 1
else self.incomplete -= 1 else this.incomplete -= 1
self.peers[params.addr] = null this.peers[params.addr] = null
self.emit('stop', params.addr) this.emit('stop', params.addr)
cb()
} }
var complete = function () { Swarm.prototype._onAnnounce_completed = function (params, peer, cb) {
if (!peer) { if (!peer) {
debug('unexpected `completed` event from peer that is not in swarm') debug('unexpected `completed` event from peer that is not in swarm')
return start() // treat as a start return start() // treat as a start
@ -50,47 +77,22 @@ Swarm.prototype.announce = function (params, cb) {
debug('unexpected `completed` event from peer that is already marked as completed') debug('unexpected `completed` event from peer that is already marked as completed')
return // do nothing return // do nothing
} }
self.complete += 1 this.complete += 1
self.incomplete -= 1 this.incomplete -= 1
peer.complete = true peer.complete = true
self.emit('complete', params.addr) this.emit('complete', params.addr)
cb()
} }
var update = function () { Swarm.prototype._onAnnounce_update = function (params, peer, cb) {
if (!peer) { if (!peer) {
debug('unexpected `update` event from peer that is not in swarm') debug('unexpected `update` event from peer that is not in swarm')
return start() // treat as a start return start() // treat as a start
} }
self.emit('update', params.addr) this.emit('update', params.addr)
}
switch (params.event) { cb()
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
}
if (params.left === 0 && peer) peer.complete = true
// send peers
var peers = self._getPeers(params.numwant)
cb(null, {
complete: this.complete,
incomplete: this.incomplete,
peers: peers
})
} }
Swarm.prototype._getPeers = function (numwant) { Swarm.prototype._getPeers = function (numwant) {