@@ -9,7 +9,7 @@ var randomIterate = require('random-iterate')
99function Swarm ( infoHash , server ) {
1010 this . peers = new LRU ( {
1111 max : server . peersCacheLength || 1000 ,
12- maxAge : server . peersCacheTtl || 900 // 900s = 15 minutes
12+ maxAge : server . peersCacheTtl || 900000 // 900 000ms = 15 minutes
1313 } )
1414 this . complete = 0
1515 this . incomplete = 0
@@ -21,18 +21,15 @@ Swarm.prototype.announce = function (params, cb) {
2121 // Mark the source peer as recently used in cache
2222 var peer = self . peers . get ( id )
2323
24- // Get the peer back in swarm if missing
25- if ( params . event === 'started' || ! peer ) {
26- self . _onAnnounceStarted ( params , peer )
27- }
28-
29- if ( params . event === 'stopped' ) {
30- self . _onAnnounceStopped ( params , peer )
24+ if ( params . event === 'started' ) {
25+ self . _onAnnounceStarted ( params , peer , id )
26+ } else if ( params . event === 'stopped' ) {
27+ self . _onAnnounceStopped ( params , peer , id )
3128 } else if ( params . event === 'completed' ) {
32- self . _onAnnounceCompleted ( params , peer )
29+ self . _onAnnounceCompleted ( params , peer , id )
3330 } else if ( params . event === 'update' ) {
34- self . _onAnnounceUpdate ( params , peer )
35- } else if ( params . event !== 'started' ) {
31+ self . _onAnnounceUpdate ( params , peer , id )
32+ } else {
3633 cb ( new Error ( 'invalid event' ) )
3734 return
3835 }
@@ -50,15 +47,14 @@ Swarm.prototype.scrape = function (params, cb) {
5047 } )
5148}
5249
53- Swarm . prototype . _onAnnounceStarted = function ( params , peer ) {
50+ Swarm . prototype . _onAnnounceStarted = function ( params , peer , id ) {
5451 if ( peer ) {
5552 debug ( 'unexpected `started` event from peer that is already in swarm' )
5653 return this . _onAnnounceUpdate ( params , peer ) // treat as an update
5754 }
5855
5956 if ( params . left === 0 ) this . complete += 1
6057 else this . incomplete += 1
61- var id = params . type === 'ws' ? params . peer_id : params . addr
6258 peer = this . peers . set ( id , {
6359 type : params . type ,
6460 complete : params . left === 0 ,
@@ -69,19 +65,18 @@ Swarm.prototype._onAnnounceStarted = function (params, peer) {
6965 } )
7066}
7167
72- Swarm . prototype . _onAnnounceStopped = function ( params , peer ) {
68+ Swarm . prototype . _onAnnounceStopped = function ( params , peer , id ) {
7369 if ( ! peer ) {
7470 debug ( 'unexpected `stopped` event from peer that is not in swarm' )
7571 return // do nothing
7672 }
7773
7874 if ( peer . complete ) this . complete -= 1
7975 else this . incomplete -= 1
80- var id = params . type === 'ws' ? params . peer_id : params . addr
8176 this . peers . remove ( id )
8277}
8378
84- Swarm . prototype . _onAnnounceCompleted = function ( params , peer ) {
79+ Swarm . prototype . _onAnnounceCompleted = function ( params , peer , id ) {
8580 if ( ! peer ) {
8681 debug ( 'unexpected `completed` event from peer that is not in swarm' )
8782 return this . _onAnnounceStarted ( params , peer ) // treat as a start
@@ -94,11 +89,10 @@ Swarm.prototype._onAnnounceCompleted = function (params, peer) {
9489 this . complete += 1
9590 this . incomplete -= 1
9691 peer . complete = true
97- var id = params . type === 'ws' ? params . peer_id : params . addr
9892 this . peers . set ( id , peer )
9993}
10094
101- Swarm . prototype . _onAnnounceUpdate = function ( params , peer ) {
95+ Swarm . prototype . _onAnnounceUpdate = function ( params , peer , id ) {
10296 if ( ! peer ) {
10397 debug ( 'unexpected `update` event from peer that is not in swarm' )
10498 return this . _onAnnounceStarted ( params , peer ) // treat as a start
@@ -108,7 +102,6 @@ Swarm.prototype._onAnnounceUpdate = function (params, peer) {
108102 this . complete += 1
109103 this . incomplete -= 1
110104 peer . complete = true
111- var id = params . type === 'ws' ? params . peer_id : params . addr
112105 this . peers . set ( id , peer )
113106 }
114107}
0 commit comments