forked from webtorrent/bittorrent-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathswarm.js
More file actions
105 lines (90 loc) · 2.79 KB
/
swarm.js
File metadata and controls
105 lines (90 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
module.exports = Swarm
var debug = require('debug')('bittorrent-tracker')
var randomIterate = require('random-iterate')
// 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
var peer = self.peers[params.addr || params.peer_id]
// Dispatch announce event
var fn = '_onAnnounce_' + params.event
if (self[fn]) {
self[fn](params, peer) // process event
cb(null, {
complete: self.complete,
incomplete: self.incomplete,
peers: self._getPeers(params.numwant)
})
} else {
cb(new Error('invalid event'))
}
}
Swarm.prototype.scrape = function (params, cb) {
cb(null, {
complete: this.complete,
incomplete: this.incomplete
})
}
Swarm.prototype._onAnnounce_started = function (params, peer) {
if (peer) {
debug('unexpected `started` event from peer that is already in swarm')
return this._onAnnounce_update(params, peer) // treat as an update
}
if (params.left === 0) this.complete += 1
else this.incomplete += 1
peer = this.peers[params.addr || params.peer_id] = {
complete: params.left === 0,
ip: params.ip, // only http+udp
peerId: params.peer_id, // as hex
port: params.port, // only http+udp
socket: params.socket // only websocket
}
}
Swarm.prototype._onAnnounce_stopped = function (params, peer) {
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 || params.peer_id] = null
}
Swarm.prototype._onAnnounce_completed = function (params, peer) {
if (!peer) {
debug('unexpected `completed` event from peer that is not in swarm')
return this._onAnnounce_started(params, peer) // 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
}
Swarm.prototype._onAnnounce_update = function (params, peer) {
if (!peer) {
debug('unexpected `update` event from peer that is not in swarm')
return this._onAnnounce_started(params, peer) // treat as a start
}
if (!peer.complete && params.left === 0) {
this.complete += 1
this.incomplete -= 1
peer.complete = true
}
}
Swarm.prototype._getPeers = function (numWant) {
var peers = []
var ite = randomIterate(Object.keys(this.peers))
while (true) {
var peerId = ite()
if (peers.length >= numWant || peerId == null) return peers
var peer = this.peers[peerId]
if (peer) peers.push(peer)
}
}