From c45f7f84001c2731909db04dd82e1c1f290386eb Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 31 Jan 2016 11:23:52 +0100 Subject: Infile code reorganization --- lib/poolRequests.js | 211 ++++++++++++++++++++++++++------------------------ lib/webTorrentNode.js | 27 +++---- lib/webtorrent.js | 6 +- 3 files changed, 128 insertions(+), 116 deletions(-) (limited to 'lib') diff --git a/lib/poolRequests.js b/lib/poolRequests.js index 9c7f3238b..53f47d629 100644 --- a/lib/poolRequests.js +++ b/lib/poolRequests.js @@ -2,29 +2,114 @@ 'use strict' var async = require('async') + var pluck = require('lodash-node/compat/collection/pluck') var constants = require('../initializers/constants') - var logger = require('../helpers/logger') var database = require('../initializers/database') - var pluck = require('lodash-node/compat/collection/pluck') - var PoolRequestsDB = database.PoolRequestsDB + var logger = require('../helpers/logger') var PodsDB = database.PodsDB + var PoolRequestsDB = database.PoolRequestsDB var utils = require('../helpers/utils') var VideosDB = database.VideosDB - var poolRequests = {} - - // ----------- Private ----------- var timer = null - function removePoolRequestsFromDB (ids) { - PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) { - if (err) { - logger.error('Cannot remove requests from the pool requests database.', { error: err }) - return + var poolRequests = { + activate: activate, + addToPoolRequests: addToPoolRequests, + deactivate: deactivate, + forceSend: forceSend + } + + function deactivate () { + logger.info('Pool requests deactivated.') + clearInterval(timer) + } + + function forceSend () { + logger.info('Force pool requests sending.') + makePoolRequests() + } + + function activate () { + logger.info('Pool requests activated.') + timer = setInterval(makePoolRequests, constants.INTERVAL) + } + + function addToPoolRequests (id, type, request) { + logger.debug('Add request to the pool requests.', { id: id, type: type, request: request }) + + PoolRequestsDB.findOne({ id: id }, function (err, entity) { + if (err) logger.error(err) + + if (entity) { + if (entity.type === type) { + logger.error(new Error('Cannot insert two same requests.')) + return + } + + // Remove the request of the other type + PoolRequestsDB.remove({ id: id }, function (err) { + if (err) logger.error(err) + }) + } else { + PoolRequestsDB.create({ id: id, type: type, request: request }, function (err) { + if (err) logger.error(err) + }) } + }) + } - logger.info('Pool requests flushed.') + // --------------------------------------------------------------------------- + + module.exports = poolRequests + + // --------------------------------------------------------------------------- + + function makePoolRequest (type, requests, callback) { + if (!callback) callback = function () {} + + PodsDB.find({}, { _id: 1, url: 1, publicKey: 1 }).exec(function (err, pods) { + if (err) throw err + + var params = { + encrypt: true, + sign: true, + method: 'POST', + path: null, + data: requests + } + + if (type === 'add') { + params.path = '/api/' + constants.API_VERSION + '/remotevideos/add' + } else if (type === 'remove') { + params.path = '/api/' + constants.API_VERSION + '/remotevideos/remove' + } else { + throw new Error('Unkown pool request type.') + } + + var bad_pods = [] + var good_pods = [] + + utils.makeMultipleRetryRequest(params, pods, callbackEachPodFinished, callbackAllPodsFinished) + + function callbackEachPodFinished (err, response, body, url, pod, callback_each_pod_finished) { + if (err || (response.statusCode !== 200 && response.statusCode !== 204)) { + bad_pods.push(pod._id) + logger.error('Error sending secure request to %s pod.', url, { error: err || new Error('Status code not 20x') }) + } else { + good_pods.push(pod._id) + } + + return callback_each_pod_finished() + } + + function callbackAllPodsFinished (err) { + if (err) return callback(err) + + updatePodsScore(good_pods, bad_pods) + callback(null) + } }) } @@ -81,16 +166,6 @@ }) } - function updatePodsScore (good_pods, bad_pods) { - logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length) - - PodsDB.update({ _id: { $in: good_pods } }, { $inc: { score: constants.PODS_SCORE.BONUS } }, { multi: true }).exec() - PodsDB.update({ _id: { $in: bad_pods } }, { $inc: { score: constants.PODS_SCORE.MALUS } }, { multi: true }, function (err) { - if (err) throw err - removeBadPods() - }) - } - function removeBadPods () { PodsDB.find({ score: 0 }, { _id: 1, url: 1 }, function (err, pods) { if (err) throw err @@ -115,92 +190,24 @@ }) } - function makePoolRequest (type, requests, callback) { - if (!callback) callback = function () {} - - PodsDB.find({}, { _id: 1, url: 1, publicKey: 1 }).exec(function (err, pods) { - if (err) throw err - - var params = { - encrypt: true, - sign: true, - method: 'POST', - path: null, - data: requests - } - - if (type === 'add') { - params.path = '/api/' + constants.API_VERSION + '/remotevideos/add' - } else if (type === 'remove') { - params.path = '/api/' + constants.API_VERSION + '/remotevideos/remove' - } else { - throw new Error('Unkown pool request type.') - } - - var bad_pods = [] - var good_pods = [] - - utils.makeMultipleRetryRequest(params, pods, callbackEachPodFinished, callbackAllPodsFinished) - - function callbackEachPodFinished (err, response, body, url, pod, callback_each_pod_finished) { - if (err || (response.statusCode !== 200 && response.statusCode !== 204)) { - bad_pods.push(pod._id) - logger.error('Error sending secure request to %s pod.', url, { error: err || new Error('Status code not 20x') }) - } else { - good_pods.push(pod._id) - } - - return callback_each_pod_finished() + function removePoolRequestsFromDB (ids) { + PoolRequestsDB.remove({ _id: { $in: ids } }, function (err) { + if (err) { + logger.error('Cannot remove requests from the pool requests database.', { error: err }) + return } - function callbackAllPodsFinished (err) { - if (err) return callback(err) - - updatePodsScore(good_pods, bad_pods) - callback(null) - } + logger.info('Pool requests flushed.') }) } - // ----------- Public ----------- - poolRequests.activate = function () { - logger.info('Pool requests activated.') - timer = setInterval(makePoolRequests, constants.INTERVAL) - } - - poolRequests.addToPoolRequests = function (id, type, request) { - logger.debug('Add request to the pool requests.', { id: id, type: type, request: request }) - - PoolRequestsDB.findOne({ id: id }, function (err, entity) { - if (err) logger.error(err) - - if (entity) { - if (entity.type === type) { - logger.error(new Error('Cannot insert two same requests.')) - return - } + function updatePodsScore (good_pods, bad_pods) { + logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length) - // Remove the request of the other type - PoolRequestsDB.remove({ id: id }, function (err) { - if (err) logger.error(err) - }) - } else { - PoolRequestsDB.create({ id: id, type: type, request: request }, function (err) { - if (err) logger.error(err) - }) - } + PodsDB.update({ _id: { $in: good_pods } }, { $inc: { score: constants.PODS_SCORE.BONUS } }, { multi: true }).exec() + PodsDB.update({ _id: { $in: bad_pods } }, { $inc: { score: constants.PODS_SCORE.MALUS } }, { multi: true }, function (err) { + if (err) throw err + removeBadPods() }) } - - poolRequests.deactivate = function () { - logger.info('Pool requests deactivated.') - clearInterval(timer) - } - - poolRequests.forceSend = function () { - logger.info('Force pool requests sending.') - makePoolRequests() - } - - module.exports = poolRequests })() diff --git a/lib/webTorrentNode.js b/lib/webTorrentNode.js index 8827c68c5..69fa6b012 100644 --- a/lib/webTorrentNode.js +++ b/lib/webTorrentNode.js @@ -10,22 +10,21 @@ var host = config.get('webserver.host') var port = config.get('webserver.port') - var nodeKey = 'webtorrentnode' + port var processKey = 'webtorrent' + port - ipc.config.silent = true ipc.config.id = nodeKey - var webtorrentnode = {} - - // Useful for beautiful tests - webtorrentnode.silent = false - - // Useful to kill it - webtorrentnode.app = null + var webtorrentnode = { + add: add, + app: null, // Pid of the app + create: create, + remove: remove, + seed: seed, + silent: false // Useful for beautiful tests + } - webtorrentnode.create = function (options, callback) { + function create (options, callback) { if (typeof options === 'function') { callback = options options = {} @@ -75,7 +74,7 @@ ipc.server.start() } - webtorrentnode.seed = function (path, callback) { + function seed (path, callback) { var extension = pathUtils.extname(path) var basename = pathUtils.basename(path, extension) var data = { @@ -104,7 +103,7 @@ ipc.server.broadcast(processKey + '.seed', data) } - webtorrentnode.add = function (magnetUri, callback) { + function add (magnetUri, callback) { var data = { _id: magnetUri, args: { @@ -131,7 +130,7 @@ ipc.server.broadcast(processKey + '.add', data) } - webtorrentnode.remove = function (magnetUri, callback) { + function remove (magnetUri, callback) { var data = { _id: magnetUri, args: { @@ -156,5 +155,7 @@ ipc.server.broadcast(processKey + '.remove', data) } + // --------------------------------------------------------------------------- + module.exports = webtorrentnode })() diff --git a/lib/webtorrent.js b/lib/webtorrent.js index b72bc500d..41e60499f 100644 --- a/lib/webtorrent.js +++ b/lib/webtorrent.js @@ -1,7 +1,7 @@ ;(function () { 'use strict' - module.exports = function (args) { + function webtorrent (args) { var WebTorrent = require('webtorrent') var ipc = require('node-ipc') @@ -88,4 +88,8 @@ ipc.of[nodeKey].emit(processKey + '.exception', { exception: e }) }) } + + // --------------------------------------------------------------------------- + + module.exports = webtorrent })() -- cgit v1.2.3