tracker returns warnings for non-fatal conditions

This commit is contained in:
Feross Aboukhadijeh 2014-07-12 18:56:25 -07:00
parent be875c40af
commit 24c8535792

View File

@ -151,28 +151,27 @@ Server.prototype._onHttpRequest = function (req, res) {
case 'started': case 'started':
if (peer) { if (peer) {
warning = 'unexpected `started` event from peer that is already in swarm' warning = 'unexpected `started` event from peer that is already in swarm'
} else { break
var left = Number(params.left)
if (left === 0) {
swarm.complete += 1
} else {
swarm.incomplete += 1
}
peer = swarm.peers[addr] = {
ip: ip,
port: port,
peerId: peerId
}
self.emit('start', addr)
} }
if (Number(params.left) === 0) {
swarm.complete += 1
} else {
swarm.incomplete += 1
}
swarm.peers[addr] = {
ip: ip,
port: port,
peerId: peerId
}
self.emit('start', addr)
break break
case 'stopped': case 'stopped':
if (!peer) { if (!peer) {
return error('unexpected `stopped` event from peer that is not in swarm') warning = 'unexpected `stopped` event from peer that is not in swarm'
break
} }
if (peer.complete) { if (peer.complete) {
@ -182,29 +181,31 @@ Server.prototype._onHttpRequest = function (req, res) {
} }
swarm.peers[addr] = null swarm.peers[addr] = null
self.emit('stop', addr) self.emit('stop', addr)
break break
case 'completed': case 'completed':
if (!peer) { if (!peer) {
return error('unexpected `completed` event from peer that is not in swarm') warning = 'unexpected `completed` event from peer that is not in swarm'
break
} }
if (peer.complete) { if (peer.complete) {
warning = 'unexpected `completed` event from peer that is already marked as completed' warning = 'unexpected `completed` event from peer that is already marked as completed'
break
} }
peer.complete = true
swarm.complete += 1 swarm.complete += 1
swarm.incomplete -= 1 swarm.incomplete -= 1
peer.complete = true
self.emit('complete', addr) self.emit('complete', addr)
break break
case '': // update case '': // update
case undefined: case undefined:
if (!peer) { if (!peer) {
return error('unexpected `update` event from peer that is not in swarm') warning = 'unexpected `update` event from peer that is not in swarm'
break
} }
self.emit('update', addr) self.emit('update', addr)
@ -341,7 +342,8 @@ Server.prototype._onUdpRequest = function (msg, rinfo) {
switch (event) { switch (event) {
case common.EVENTS.started: case common.EVENTS.started:
if (peer) { if (peer) {
return error('unexpected `started` event from peer that is already in swarm') warning = 'unexpected `started` event from peer that is already in swarm'
break
} }
if (left === 0) { if (left === 0) {
@ -350,18 +352,18 @@ Server.prototype._onUdpRequest = function (msg, rinfo) {
swarm.incomplete += 1 swarm.incomplete += 1
} }
peer = swarm.peers[addr] = { swarm.peers[addr] = {
ip: ip, ip: ip,
port: port, port: port,
peerId: peerId peerId: peerId
} }
self.emit('start', addr) self.emit('start', addr)
break break
case common.EVENTS.stopped: case common.EVENTS.stopped:
if (!peer) { if (!peer) {
return error('unexpected `stopped` event from peer that is not in swarm') warning = 'unexpected `stopped` event from peer that is not in swarm'
break
} }
if (peer.complete) { if (peer.complete) {
@ -371,35 +373,37 @@ Server.prototype._onUdpRequest = function (msg, rinfo) {
} }
swarm.peers[addr] = null swarm.peers[addr] = null
self.emit('stop', addr) self.emit('stop', addr)
break break
case common.EVENTS.completed: case common.EVENTS.completed:
if (!peer) { if (!peer) {
return error('unexpected `completed` event from peer that is not in swarm') warning = 'unexpected `completed` event from peer that is not in swarm'
break
} }
if (peer.complete) { if (peer.complete) {
warning = 'unexpected `completed` event from peer that is already marked as completed' warning = 'unexpected `completed` event from peer that is already marked as completed'
break
} }
peer.complete = true
swarm.complete += 1 swarm.complete += 1
swarm.incomplete -= 1 swarm.incomplete -= 1
peer.complete = true
self.emit('complete', addr) self.emit('complete', addr)
break break
case common.EVENTS.update: // update case common.EVENTS.update: // update
if (!peer) { if (!peer) {
return error('unexpected `update` event from peer that is not in swarm') warning = 'unexpected `update` event from peer that is not in swarm'
break
} }
self.emit('update', addr) self.emit('update', addr)
break break
default: default:
return error('unexpected event: ' + event) // early return return error('invalid event') // early return
} }
// send peers // send peers