fix(search): show torrent page from remote peer

This commit is contained in:
Alexey Kasyanchuk
2018-03-05 04:01:08 +03:00
parent 857bc66c7f
commit 1c626fa2bf
7 changed files with 84 additions and 29 deletions

View File

@ -204,6 +204,13 @@ class p2p {
return
return peers.map(peer => ({address: peer.address, port: peer.port}))
}
find(peer)
{
return this.peersList().find((localPeer) => {
return localPeer.address === peer.address
})
}
}
module.exports = p2p

View File

@ -321,14 +321,31 @@ if(dataDirectory && fs.existsSync(dataDirectory + '/peers.p2p'))
});
});
recive('torrent', function(hash, options, callback)
{
const onTorrent = (hash, options, callback) => {
if(hash.length != 40)
return;
if(typeof callback != 'function')
return;
// remote request
if(options.peer)
{
console.log('remote torrent request to peer')
const peer = p2p.find(options.peer)
if(!peer)
{
callback(undefined)
return;
}
delete options.peer;
peer.emit('torrent', {hash, options}, (data) => {
console.log('remote torrent result')
callback(data)
})
return;
}
sphinx.query('SELECT * FROM `torrents` WHERE `hash` = ?', hash, function (error, rows, fields) {
if(!rows || rows.length == 0) {
callback(undefined)
@ -357,7 +374,15 @@ if(dataDirectory && fs.existsSync(dataDirectory + '/peers.p2p'))
send('downloading', torrent.infoHash)
}
});
});
}
recive('torrent', onTorrent);
p2p.on('torrent', ({hash, options} = {}, callback) => {
if(!hash)
return;
onTorrent(hash, options, (data) => callback(data))
})
const searchTorrentCall = function(text, navigation, callback)
{