From c33f927a7d394e41b6b316784e34d9fe1bb5f88b Mon Sep 17 00:00:00 2001 From: Alexey Kasyanchuk Date: Sat, 17 Feb 2018 20:04:54 +0300 Subject: [PATCH] feat(p2p): files search also --- package.json | 1 + src/app/search.js | 14 +++++++++++++- src/background/bt/spider.js | 2 +- src/background/spider.js | 22 +++++++++++++++++----- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index d17a53c..e46ffaa 100644 --- a/package.json +++ b/package.json @@ -87,6 +87,7 @@ "iconv-lite": "^0.4.19", "ipaddr.js": "^1.5.4", "json-socket": "^0.3.0", + "lodash": "^4.17.5", "material-ui": "^0.20.0", "moment": "^2.20.1", "mysql": "^2.15.0", diff --git a/src/app/search.js b/src/app/search.js index eecfd2d..be15d8c 100644 --- a/src/app/search.js +++ b/src/app/search.js @@ -17,6 +17,8 @@ import MenuItem from 'material-ui/MenuItem'; import formatBytes from './format-bytes' +import _ from 'lodash' + let session; class TorrentsStatistic extends Component { @@ -217,9 +219,16 @@ export default class Search extends Component { window.torrentSocket.on('newStatistic', this.newStatisticFunc); this.remoteSearchTorrent = (torrents) => { - console.log(torrents) + this.searchTorrents = _.unionBy(this.searchTorrents, torrents, 'hash') + this.forceUpdate(); } window.torrentSocket.on('remoteSearchTorrent', this.remoteSearchTorrent); + + this.remoteSearchFiles = (torrents) => { + this.searchFiles = _.unionBy(this.searchFiles, torrents, 'hash') + this.forceUpdate(); + } + window.torrentSocket.on('remoteSearchFiles', this.remoteSearchFiles); } componentWillUnmount() { @@ -229,6 +238,9 @@ export default class Search extends Component { if(this.remoteSearchTorrent) window.torrentSocket.off('remoteSearchTorrent', this.remoteSearchTorrent); + if(this.remoteSearchFiles) + window.torrentSocket.off('remoteSearchFiles', this.remoteSearchFiles); + session = { searchTorrents: this.searchTorrents, searchFiles: this.searchFiles, diff --git a/src/background/bt/spider.js b/src/background/bt/spider.js index 5eee04f..63dc0e7 100644 --- a/src/background/bt/spider.js +++ b/src/background/bt/spider.js @@ -239,7 +239,7 @@ class Spider extends Emiter { if(this.client && !this.ignore) { cpuDebug('cpu usage:' + cpuUsage()) if(this.cpuLimit <= 0 || cpuUsage() <= this.cpuLimit + this.cpuInterval) { - // this.client.add(addressPair, infohash); + this.client.add(addressPair, infohash); } } } diff --git a/src/background/spider.js b/src/background/spider.js index cb14b04..6b8510d 100644 --- a/src/background/spider.js +++ b/src/background/spider.js @@ -264,7 +264,6 @@ tcpServer.listen(config.spiderPort); tcpServer.on('connection', (socket) => { socket = new JsonSocket(socket); socket.on('message', (message) => { - console.log(message) if(message.type && messageHandlers[message.type]) { messageHandlers[message.type](message.data, (data) => { @@ -411,7 +410,6 @@ tcpServer.on('connection', (socket) => { if(navigation.files.min > 0) where += ' and files > ' + sphinx.escape(navigation.files.min) + ' '; } - console.log(navigation, where) let searchList = []; //args.splice(orderBy && orderBy.length > 0 ? 1 : 0, 1); @@ -432,20 +430,19 @@ tcpServer.on('connection', (socket) => { recive('searchTorrent', (text, navigation, callback) => { searchTorrentCall(text, navigation, callback) p2p.emit('searchTorrent', {text, navigation}, (remote) => { - console.log('remote search responce') + console.log('remote search results', remote && remote.length) send('remoteSearchTorrent', remote) }) }); onSocketMessage('searchTorrent', ({text, navigation} = {}, callback) => { - console.log('search remote', text) if(!text) return; searchTorrentCall(text, navigation, (data) => callback(data)) }) - recive('searchFiles', function(text, navigation, callback) + const searchFilesCall = function(text, navigation, callback) { if(typeof callback != 'function') return; @@ -532,8 +529,23 @@ tcpServer.on('connection', (socket) => { callback(Object.values(search)); }) }); + } + + recive('searchFiles', (text, navigation, callback) => { + searchFilesCall(text, navigation, callback) + p2p.emit('searchFiles', {text, navigation}, (remote) => { + console.log('remote search files results', remote && remote.length) + send('remoteSearchFiles', remote) + }) }); + onSocketMessage('searchFiles', ({text, navigation} = {}, callback) => { + if(!text) + return; + + searchFilesCall(text, navigation, (data) => callback(data)) + }) + recive('checkTrackers', function(hash) { if(hash.length != 40)