@@ -15,20 +15,24 @@ Swarm.prototype.announce = function (params, cb) {
1515 var self = this
1616 var peer = self . peers [ params . addr || params . peer_id ]
1717
18- // Dispatch announce event
19- var fn = '_onAnnounce_' + params . event
20- if ( self [ fn ] ) {
21- self [ fn ] ( params , peer ) // process event
22-
23- var peerType = params . compact === undefined ? 'webrtc' : 'addr'
24- cb ( null , {
25- complete : self . complete ,
26- incomplete : self . incomplete ,
27- peers : self . _getPeers ( params . numwant , peerType )
28- } )
18+ if ( params . event === 'started' ) {
19+ self . _onAnnounceStarted ( params , peer )
20+ } else if ( params . event === 'stopped' ) {
21+ self . _onAnnounceStopped ( params , peer )
22+ } else if ( params . event === 'completed' ) {
23+ self . _onAnnounceCompleted ( params , peer )
24+ } else if ( params . event === 'update' ) {
25+ self . _onAnnounceUpdate ( params , peer )
2926 } else {
3027 cb ( new Error ( 'invalid event' ) )
28+ return
3129 }
30+ console . log ( 'FEROSS' , params . compact )
31+ cb ( null , {
32+ complete : self . complete ,
33+ incomplete : self . incomplete ,
34+ peers : self . _getPeers ( params . numwant , ! ! params . socket )
35+ } )
3236}
3337
3438Swarm . prototype . scrape = function ( params , cb ) {
@@ -38,7 +42,7 @@ Swarm.prototype.scrape = function (params, cb) {
3842 } )
3943}
4044
41- Swarm . prototype . _onAnnounce_started = function ( params , peer ) {
45+ Swarm . prototype . _onAnnounceStarted = function ( params , peer ) {
4246 if ( peer ) {
4347 debug ( 'unexpected `started` event from peer that is already in swarm' )
4448 return this . _onAnnounce_update ( params , peer ) // treat as an update
@@ -48,14 +52,14 @@ Swarm.prototype._onAnnounce_started = function (params, peer) {
4852 else this . incomplete += 1
4953 peer = this . peers [ params . addr || params . peer_id ] = {
5054 complete : params . left === 0 ,
51- ip : params . ip , // only http+udp
5255 peerId : params . peer_id , // as hex
53- port : params . port , // only http+udp
56+ ip : params . ip , // only http, udp
57+ port : params . port , // only http, udp
5458 socket : params . socket // only websocket
5559 }
5660}
5761
58- Swarm . prototype . _onAnnounce_stopped = function ( params , peer ) {
62+ Swarm . prototype . _onAnnounceStopped = function ( params , peer ) {
5963 if ( ! peer ) {
6064 debug ( 'unexpected `stopped` event from peer that is not in swarm' )
6165 return // do nothing
@@ -66,7 +70,7 @@ Swarm.prototype._onAnnounce_stopped = function (params, peer) {
6670 this . peers [ params . addr || params . peer_id ] = null
6771}
6872
69- Swarm . prototype . _onAnnounce_completed = function ( params , peer ) {
73+ Swarm . prototype . _onAnnounceCompleted = function ( params , peer ) {
7074 if ( ! peer ) {
7175 debug ( 'unexpected `completed` event from peer that is not in swarm' )
7276 return this . _onAnnounce_started ( params , peer ) // treat as a start
@@ -81,7 +85,7 @@ Swarm.prototype._onAnnounce_completed = function (params, peer) {
8185 peer . complete = true
8286}
8387
84- Swarm . prototype . _onAnnounce_update = function ( params , peer ) {
88+ Swarm . prototype . _onAnnounceUpdate = function ( params , peer ) {
8589 if ( ! peer ) {
8690 debug ( 'unexpected `update` event from peer that is not in swarm' )
8791 return this . _onAnnounce_started ( params , peer ) // treat as a start
@@ -94,16 +98,14 @@ Swarm.prototype._onAnnounce_update = function (params, peer) {
9498 }
9599}
96100
97- Swarm . prototype . _getPeers = function ( numWant , peerType ) {
101+ Swarm . prototype . _getPeers = function ( numwant , isWebRTC ) {
98102 var peers = [ ]
99103 var ite = randomIterate ( Object . keys ( this . peers ) )
100- while ( true ) {
101- var peerId = ite ( )
102- if ( peers . length >= numWant || peerId == null ) return peers
104+ var peerId
105+ while ( ( peerId = ite ( ) ) && peers . length < numwant ) {
103106 var peer = this . peers [ peerId ]
104- if ( peer &&
105- ( ( peerType === 'webrtc' && peer . socket ) || ( peerType === 'addr' && peer . ip ) ) ) {
106- peers . push ( peer )
107- }
107+ if ( ! peer ) continue
108+ if ( ( isWebRTC && peer . socket ) || ( ! isWebRTC && peer . ip ) ) peers . push ( peer )
108109 }
110+ return peers
109111}
0 commit comments