1- var debug = require ( 'debug' ) ( 'bittorrent-tracker' )
2-
31module . exports = Swarm
42
3+ var debug = require ( 'debug' ) ( 'bittorrent-tracker' )
4+
55// Regard this as the default implementation of an interface that you
66// need to support when overriding Server.getSwarm()
77function Swarm ( infoHash , server ) {
@@ -19,30 +19,27 @@ Swarm.prototype.announce = function (params, cb) {
1919 if ( ! params . event || params . event === 'empty' ) params . event = 'update'
2020 var fn = '_onAnnounce_' + params . event
2121 if ( self [ fn ] ) {
22- self [ fn ] ( params , peer , function ( err ) {
23- // event processed, prepare response:
22+ self [ fn ] ( params , peer ) // process event
2423
25- if ( params . left === 0 && peer ) peer . complete = true
24+ if ( params . left === 0 && peer ) peer . complete = true
2625
27- // send peers
28- var peers = self . _getPeers ( params . numwant )
29-
30- cb ( null , {
31- complete : self . complete ,
32- incomplete : self . incomplete ,
33- peers : peers
34- } )
26+ cb ( null , {
27+ complete : self . complete ,
28+ incomplete : self . incomplete ,
29+ peers : self . _getPeers ( params . numwant )
3530 } )
31+
3632 } else {
3733 cb ( new Error ( 'invalid event' ) )
3834 }
3935}
4036
41- Swarm . prototype . _onAnnounce_started = function ( params , peer , cb ) {
37+ Swarm . prototype . _onAnnounce_started = function ( params , peer ) {
4238 if ( peer ) {
4339 debug ( 'unexpected `started` event from peer that is already in swarm' )
44- return this . _onAnnounce_update ( ) // treat as an update
40+ return this . _onAnnounce_update ( params , peer ) // treat as an update
4541 }
42+
4643 if ( params . left === 0 ) this . complete += 1
4744 else this . incomplete += 1
4845 peer = this . peers [ params . addr ] = {
@@ -51,48 +48,42 @@ Swarm.prototype._onAnnounce_started = function (params, peer, cb) {
5148 peerId : params . peer_id
5249 }
5350 this . emit ( 'start' , params . addr )
54-
55- cb ( )
5651}
5752
58- Swarm . prototype . _onAnnounce_stopped = function ( params , peer , cb ) {
53+ Swarm . prototype . _onAnnounce_stopped = function ( params , peer ) {
5954 if ( ! peer ) {
6055 debug ( 'unexpected `stopped` event from peer that is not in swarm' )
6156 return // do nothing
6257 }
58+
6359 if ( peer . complete ) this . complete -= 1
6460 else this . incomplete -= 1
6561 this . peers [ params . addr ] = null
6662 this . emit ( 'stop' , params . addr )
67-
68- cb ( )
6963}
7064
71- Swarm . prototype . _onAnnounce_completed = function ( params , peer , cb ) {
65+ Swarm . prototype . _onAnnounce_completed = function ( params , peer ) {
7266 if ( ! peer ) {
7367 debug ( 'unexpected `completed` event from peer that is not in swarm' )
74- return start ( ) // treat as a start
68+ return this . _onAnnounce_started ( params , peer ) // treat as a start
7569 }
7670 if ( peer . complete ) {
7771 debug ( 'unexpected `completed` event from peer that is already marked as completed' )
7872 return // do nothing
7973 }
74+
8075 this . complete += 1
8176 this . incomplete -= 1
8277 peer . complete = true
8378 this . emit ( 'complete' , params . addr )
84-
85- cb ( )
8679}
8780
88- Swarm . prototype . _onAnnounce_update = function ( params , peer , cb ) {
81+ Swarm . prototype . _onAnnounce_update = function ( params , peer ) {
8982 if ( ! peer ) {
9083 debug ( 'unexpected `update` event from peer that is not in swarm' )
91- return start ( ) // treat as a start
84+ return this . _onAnnounce_started ( params , peer ) // treat as a start
9285 }
9386 this . emit ( 'update' , params . addr )
94-
95- cb ( )
9687}
9788
9889Swarm . prototype . _getPeers = function ( numwant ) {
@@ -110,7 +101,7 @@ Swarm.prototype._getPeers = function (numwant) {
110101 return peers
111102}
112103
113- Swarm . prototype . scrape = function ( infoHash , params , cb ) {
104+ Swarm . prototype . scrape = function ( params , cb ) {
114105 cb ( null , {
115106 complete : this . complete ,
116107 incomplete : this . incomplete
0 commit comments