@@ -167,110 +167,131 @@ class Server extends EventEmitter {
167167 } )
168168 }
169169
170- async getSwarm ( infoHash ) {
170+ getSwarm ( infoHash , cb ) {
171171 if ( Buffer . isBuffer ( infoHash ) ) infoHash = infoHash . toString ( "hex" ) ;
172172
173- return new Promise ( done => {
174- const returnSwarm = ( ) => {
175- done ( this . torrents [ infoHash ] ) ;
176- } ;
177-
178- process . nextTick ( returnSwarm ) ;
179- } )
173+ process . nextTick ( ( ) => {
174+ cb ( null , this . torrents [ infoHash ] ) ;
175+ } ) ;
180176 }
181177
182178 // Get existing swarm, or create one if one does not exist
183- async getOrCreateSwarm ( params ) {
184- try {
185- const swarm = this . getSwarm ( params . info_hash )
186- || await this . createSwarm ( params . info_hash )
187-
188- }
189- catch ( err ) { return err }
190-
191- return swarm
179+ getOrCreateSwarm ( params , cb ) {
180+ this . getSwarm ( params . info_hash , ( err , swarm ) => {
181+ if ( err ) return cb ( err ) ;
182+ if ( swarm ) return cb ( null , swarm ) ;
183+ this . createSwarm ( params . info_hash , ( err , swarm ) => {
184+ if ( err ) return cb ( err ) ;
185+ cb ( null , swarm ) ;
186+ } ) ;
187+ } ) ;
192188 }
193189
194- async _Request ( params ) {
190+ // async _Request(params) {
191+ // if (params && params.action === common.ACTIONS.CONNECT) {
192+ // return { action: common.ACTIONS.CONNECT }
193+ // } else if (params && params.action === common.ACTIONS.ANNOUNCE) {
194+ // return this._onAnnounce(params)
195+ // } else if (params && params.action === common.ACTIONS.SCRAPE) {
196+ // return this._onScrape(params)
197+ // } else {
198+ // return new Error("Invalid action")
199+ // }
200+ // }
201+
202+ _onRequest ( params , cb ) {
203+ console . log ( 'request received' )
195204 if ( params && params . action === common . ACTIONS . CONNECT ) {
196- return { action : common . ACTIONS . CONNECT }
205+ cb ( null , { action : common . ACTIONS . CONNECT } ) ;
197206 } else if ( params && params . action === common . ACTIONS . ANNOUNCE ) {
198- return await this . _onAnnounce ( params )
207+ this . _onAnnounce ( params , cb ) ;
199208 } else if ( params && params . action === common . ACTIONS . SCRAPE ) {
200- return this . _onScrape ( params )
209+ this . _onScrape ( params , cb ) ;
201210 } else {
202- return new Error ( "Invalid action" )
211+ cb ( new Error ( "Invalid action" ) ) ;
203212 }
204213 }
205214
206- fimdIpv4peers ( peers ) {
207- return string2compact (
208- peers
209- . filter ( peer => common . IPV4_RE . test ( peer . ip ) )
210- . map ( peer => `${ peer . ip } :${ peer . port } ` )
211- ) ;
212- }
213-
214- findIpv6Peers ( peers ) {
215- string2compact (
216- peers
217- . filter ( peer => common . IPV6_RE . test ( peer . ip ) )
218- . map ( peer => `[${ peer . ip } ]:${ peer . port } ` )
219- ) ;
220- }
221-
222- announce ( swarm ) {
223- if ( ! params . event || params . event === "empty" ) params . event = "update" ;
224-
225- const onAnnouced = ( err , response ) => {
226- if ( err ) return cb ( err ) ;
227-
228- if ( ! response . action ) response . action = common . ACTIONS . ANNOUNCE ;
229- if ( ! response . interval )
230- response . interval = Math . ceil ( this . intervalMs / 1000 ) ;
231-
232- if ( params . compact === 1 ) {
233- const peers = response . peers ;
215+ _onAnnounce ( params , cb ) {
216+ const self = this ;
234217
235- response . peers = this . findIpv4Peers ( peers )
236- response . peers6 = this . findIpv6Peers ( peers )
218+ if ( this . _filter ) {
219+ this . _filter ( params . info_hash , params , ( err ) => {
220+ // Presence of `err` means that this announce request is disallowed
221+ if ( err ) return cb ( err ) ;
237222
238- } else if ( params . compact === 0 ) {
239- // IPv6 peers are not separate for non-compact responses
240- response . peers = response . peers . map ( ( peer ) => {
241- return {
242- "peer id" : common . hexToBinary ( peer . peerId ) ,
243- ip : peer . ip ,
244- port : peer . port ,
245- } ;
223+ getOrCreateSwarm ( ( err , swarm ) => {
224+ if ( err ) return cb ( err ) ;
225+ announce ( swarm ) ;
246226 } ) ;
247- } // else, return full peer objects (used for websocket responses)
248-
249- cb ( null , response ) ;
250- } ;
251-
252- swarm . announce ( params , onAnnouced ) ;
253- }
254-
255- async _onAsyncAnnounce ( params ) {
256- let _params = params ;
227+ } ) ;
228+ } else {
229+ getOrCreateSwarm ( ( err , swarm ) => {
230+ if ( err ) return cb ( err ) ;
231+ announce ( swarm ) ;
232+ } ) ;
233+ }
257234
258- if ( this . _filter ) {
259- _params = this . _filter ( params . info_hash , params ) ;
235+ // Get existing swarm, or create one if one does not exist
236+ function getOrCreateSwarm ( cb ) {
237+ self . getSwarm ( params . info_hash , ( err , swarm ) => {
238+ if ( err ) return cb ( err ) ;
239+ if ( swarm ) return cb ( null , swarm ) ;
240+ self . createSwarm ( params . info_hash , ( err , swarm ) => {
241+ if ( err ) return cb ( err ) ;
242+ cb ( null , swarm ) ;
243+ } ) ;
244+ } ) ;
260245 }
261246
262- let swarm ;
247+ function announce ( swarm ) {
248+ if ( ! params . event || params . event === "empty" ) params . event = "update" ;
249+ swarm . announce ( params , ( err , response ) => {
250+ if ( err ) return cb ( err ) ;
263251
264- try {
265- swarm = await getOrCreateSwarm ( params ) ;
266- } catch ( err ) {
267- return err ;
268- }
252+ if ( ! response . action ) response . action = common . ACTIONS . ANNOUNCE ;
253+ if ( ! response . interval )
254+ response . interval = Math . ceil ( self . intervalMs / 1000 ) ;
255+
256+ if ( params . compact === 1 ) {
257+ const peers = response . peers ;
258+
259+ // Find IPv4 peers
260+ response . peers = string2compact (
261+ peers
262+ . filter ( ( peer ) => {
263+ return common . IPV4_RE . test ( peer . ip ) ;
264+ } )
265+ . map ( ( peer ) => {
266+ return `${ peer . ip } :${ peer . port } ` ;
267+ } )
268+ ) ;
269+ // Find IPv6 peers
270+ response . peers6 = string2compact (
271+ peers
272+ . filter ( ( peer ) => {
273+ return common . IPV6_RE . test ( peer . ip ) ;
274+ } )
275+ . map ( ( peer ) => {
276+ return `[${ peer . ip } ]:${ peer . port } ` ;
277+ } )
278+ ) ;
279+ } else if ( params . compact === 0 ) {
280+ // IPv6 peers are not separate for non-compact responses
281+ response . peers = response . peers . map ( ( peer ) => {
282+ return {
283+ "peer id" : common . hexToBinary ( peer . peerId ) ,
284+ ip : peer . ip ,
285+ port : peer . port ,
286+ } ;
287+ } ) ;
288+ } // else, return full peer objects (used for websocket responses)
269289
270- this . announce ( swarm ) ;
290+ cb ( null , response ) ;
291+ } ) ;
292+ }
271293 }
272294
273-
274295 _onScrape ( params , cb ) {
275296 if ( params . info_hash == null ) {
276297 // if info_hash param is omitted, stats for all torrents are returned
0 commit comments