From 9f10b2928df655c3672d9607e864e667d4bc903a Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Sun, 7 Feb 2016 11:23:23 +0100 Subject: Remove useless anonymous functions of files --- lib/friends.js | 356 +++++++++++++++++++++++----------------------- lib/poolRequests.js | 358 +++++++++++++++++++++++------------------------ lib/videos.js | 102 +++++++------- lib/webtorrent.js | 254 +++++++++++++++++---------------- lib/webtorrentProcess.js | 144 ++++++++++--------- 5 files changed, 602 insertions(+), 612 deletions(-) (limited to 'lib') diff --git a/lib/friends.js b/lib/friends.js index c05ccedb1..8cc1a3151 100644 --- a/lib/friends.js +++ b/lib/friends.js @@ -1,224 +1,222 @@ -;(function () { - 'use strict' - - var async = require('async') - var config = require('config') - var fs = require('fs') - var request = require('request') - - var constants = require('../initializers/constants') - var logger = require('../helpers/logger') - var peertubeCrypto = require('../helpers/peertubeCrypto') - var Pods = require('../models/pods') - var poolRequests = require('../lib/poolRequests') - var requests = require('../helpers/requests') - var Videos = require('../models/videos') - - var http = config.get('webserver.https') ? 'https' : 'http' - var host = config.get('webserver.host') - var port = config.get('webserver.port') - - var pods = { - addVideoToFriends: addVideoToFriends, - hasFriends: hasFriends, - makeFriends: makeFriends, - quitFriends: quitFriends, - removeVideoToFriends: removeVideoToFriends - } +'use strict' + +var async = require('async') +var config = require('config') +var fs = require('fs') +var request = require('request') + +var constants = require('../initializers/constants') +var logger = require('../helpers/logger') +var peertubeCrypto = require('../helpers/peertubeCrypto') +var Pods = require('../models/pods') +var poolRequests = require('../lib/poolRequests') +var requests = require('../helpers/requests') +var Videos = require('../models/videos') + +var http = config.get('webserver.https') ? 'https' : 'http' +var host = config.get('webserver.host') +var port = config.get('webserver.port') + +var pods = { + addVideoToFriends: addVideoToFriends, + hasFriends: hasFriends, + makeFriends: makeFriends, + quitFriends: quitFriends, + removeVideoToFriends: removeVideoToFriends +} + +function addVideoToFriends (video) { + // To avoid duplicates + var id = video.name + video.magnetUri + // ensure namePath is null + video.namePath = null + poolRequests.addRequest(id, 'add', video) +} + +function hasFriends (callback) { + Pods.count(function (err, count) { + if (err) return callback(err) + + var has_friends = (count !== 0) + callback(null, has_friends) + }) +} + +function makeFriends (callback) { + var pods_score = {} + + logger.info('Make friends!') + fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { + if (err) { + logger.error('Cannot read public cert.') + return callback(err) + } - function addVideoToFriends (video) { - // To avoid duplicates - var id = video.name + video.magnetUri - // ensure namePath is null - video.namePath = null - poolRequests.addRequest(id, 'add', video) - } + var urls = config.get('network.friends') - function hasFriends (callback) { - Pods.count(function (err, count) { + async.each(urls, computeForeignPodsList, function (err) { if (err) return callback(err) - var has_friends = (count !== 0) - callback(null, has_friends) + logger.debug('Pods scores computed.', { pods_score: pods_score }) + var pods_list = computeWinningPods(urls, pods_score) + logger.debug('Pods that we keep computed.', { pods_to_keep: pods_list }) + + makeRequestsToWinningPods(cert, pods_list) }) - } + }) - function makeFriends (callback) { - var pods_score = {} + // ----------------------------------------------------------------------- - logger.info('Make friends!') - fs.readFile(peertubeCrypto.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { - if (err) { - logger.error('Cannot read public cert.') - return callback(err) - } + function computeForeignPodsList (url, callback) { + // Let's give 1 point to the pod we ask the friends list + pods_score[url] = 1 - var urls = config.get('network.friends') + getForeignPodsList(url, function (err, foreign_pods_list) { + if (err) return callback(err) + if (foreign_pods_list.length === 0) return callback() - async.each(urls, computeForeignPodsList, function (err) { - if (err) return callback(err) + async.each(foreign_pods_list, function (foreign_pod, callback_each) { + var foreign_url = foreign_pod.url - logger.debug('Pods scores computed.', { pods_score: pods_score }) - var pods_list = computeWinningPods(urls, pods_score) - logger.debug('Pods that we keep computed.', { pods_to_keep: pods_list }) + if (pods_score[foreign_url]) pods_score[foreign_url]++ + else pods_score[foreign_url] = 1 - makeRequestsToWinningPods(cert, pods_list) + callback_each() + }, function () { + callback() }) }) + } - // ----------------------------------------------------------------------- - - function computeForeignPodsList (url, callback) { - // Let's give 1 point to the pod we ask the friends list - pods_score[url] = 1 - - getForeignPodsList(url, function (err, foreign_pods_list) { - if (err) return callback(err) - if (foreign_pods_list.length === 0) return callback() - - async.each(foreign_pods_list, function (foreign_pod, callback_each) { - var foreign_url = foreign_pod.url - - if (pods_score[foreign_url]) pods_score[foreign_url]++ - else pods_score[foreign_url] = 1 - - callback_each() - }, function () { - callback() - }) - }) - } + function computeWinningPods (urls, pods_score) { + // Build the list of pods to add + // Only add a pod if it exists in more than a half base pods + var pods_list = [] + var base_score = urls.length / 2 + Object.keys(pods_score).forEach(function (pod) { + if (pods_score[pod] > base_score) pods_list.push({ url: pod }) + }) - function computeWinningPods (urls, pods_score) { - // Build the list of pods to add - // Only add a pod if it exists in more than a half base pods - var pods_list = [] - var base_score = urls.length / 2 - Object.keys(pods_score).forEach(function (pod) { - if (pods_score[pod] > base_score) pods_list.push({ url: pod }) - }) + return pods_list + } - return pods_list - } + function makeRequestsToWinningPods (cert, pods_list) { + // Stop pool requests + poolRequests.deactivate() + // Flush pool requests + poolRequests.forceSend() - function makeRequestsToWinningPods (cert, pods_list) { - // Stop pool requests - poolRequests.deactivate() - // Flush pool requests - poolRequests.forceSend() - - // Get the list of our videos to send to our new friends - Videos.listOwned(function (err, videos_list) { - if (err) { - logger.error('Cannot get the list of videos we own.') - return callback(err) - } + // Get the list of our videos to send to our new friends + Videos.listOwned(function (err, videos_list) { + if (err) { + logger.error('Cannot get the list of videos we own.') + return callback(err) + } - var data = { - url: http + '://' + host + ':' + port, - publicKey: cert, - videos: videos_list - } + var data = { + url: http + '://' + host + ':' + port, + publicKey: cert, + videos: videos_list + } - requests.makeMultipleRetryRequest( - { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data }, + requests.makeMultipleRetryRequest( + { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data }, - pods_list, + pods_list, - function eachRequest (err, response, body, url, pod, callback_each_request) { - // We add the pod if it responded correctly with its public certificate - if (!err && response.statusCode === 200) { - Pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) { - if (err) logger.error('Error with adding %s pod.', pod.url, { error: err }) + function eachRequest (err, response, body, url, pod, callback_each_request) { + // We add the pod if it responded correctly with its public certificate + if (!err && response.statusCode === 200) { + Pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) { + if (err) logger.error('Error with adding %s pod.', pod.url, { error: err }) - Videos.addRemotes(body.videos, function (err) { - if (err) logger.error('Error with adding videos of pod.', pod.url, { error: err }) + Videos.addRemotes(body.videos, function (err) { + if (err) logger.error('Error with adding videos of pod.', pod.url, { error: err }) - logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos }) - return callback_each_request() - }) + logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos }) + return callback_each_request() }) - } else { - logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') }) - return callback_each_request() - } - }, - - function endRequests (err) { - // Now we made new friends, we can re activate the pool of requests - poolRequests.activate() - - if (err) { - logger.error('There was some errors when we wanted to make friends.') - return callback(err) - } - - logger.debug('makeRequestsToWinningPods finished.') - return callback(null) + }) + } else { + logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') }) + return callback_each_request() } - ) - }) - } - } + }, - function quitFriends (callback) { - // Stop pool requests - poolRequests.deactivate() - // Flush pool requests - poolRequests.forceSend() + function endRequests (err) { + // Now we made new friends, we can re activate the pool of requests + poolRequests.activate() - Pods.list(function (err, pods) { - if (err) return callback(err) + if (err) { + logger.error('There was some errors when we wanted to make friends.') + return callback(err) + } - var request = { - method: 'POST', - path: '/api/' + constants.API_VERSION + '/pods/remove', - sign: true, - encrypt: true, - data: { - url: 'me' // Fake data + logger.debug('makeRequestsToWinningPods finished.') + return callback(null) } + ) + }) + } +} + +function quitFriends (callback) { + // Stop pool requests + poolRequests.deactivate() + // Flush pool requests + poolRequests.forceSend() + + Pods.list(function (err, pods) { + if (err) return callback(err) + + var request = { + method: 'POST', + path: '/api/' + constants.API_VERSION + '/pods/remove', + sign: true, + encrypt: true, + data: { + url: 'me' // Fake data } + } - // Announce we quit them - requests.makeMultipleRetryRequest(request, pods, function () { - Pods.removeAll(function (err) { - poolRequests.activate() + // Announce we quit them + requests.makeMultipleRetryRequest(request, pods, function () { + Pods.removeAll(function (err) { + poolRequests.activate() - if (err) return callback(err) + if (err) return callback(err) - logger.info('Broke friends, so sad :(') + logger.info('Broke friends, so sad :(') - Videos.removeAllRemotes(function (err) { - if (err) return callback(err) + Videos.removeAllRemotes(function (err) { + if (err) return callback(err) - logger.info('Removed all remote videos.') - callback(null) - }) + logger.info('Removed all remote videos.') + callback(null) }) }) }) - } + }) +} - function removeVideoToFriends (video) { - // To avoid duplicates - var id = video.name + video.magnetUri - poolRequests.addRequest(id, 'remove', video) - } +function removeVideoToFriends (video) { + // To avoid duplicates + var id = video.name + video.magnetUri + poolRequests.addRequest(id, 'remove', video) +} - // --------------------------------------------------------------------------- +// --------------------------------------------------------------------------- - module.exports = pods +module.exports = pods - // --------------------------------------------------------------------------- +// --------------------------------------------------------------------------- - function getForeignPodsList (url, callback) { - var path = '/api/' + constants.API_VERSION + '/pods' +function getForeignPodsList (url, callback) { + var path = '/api/' + constants.API_VERSION + '/pods' - request.get(url + path, function (err, response, body) { - if (err) return callback(err) + request.get(url + path, function (err, response, body) { + if (err) return callback(err) - callback(null, JSON.parse(body)) - }) - } -})() + callback(null, JSON.parse(body)) + }) +} diff --git a/lib/poolRequests.js b/lib/poolRequests.js index 9ea41f383..f786c3c7a 100644 --- a/lib/poolRequests.js +++ b/lib/poolRequests.js @@ -1,223 +1,221 @@ -;(function () { - '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 Pods = require('../models/pods') - var PoolRequests = require('../models/poolRequests') - var requests = require('../helpers/requests') - var Videos = require('../models/videos') - - var timer = null - - var poolRequests = { - activate: activate, - addRequest: addRequest, - deactivate: deactivate, - forceSend: forceSend - } - - function activate () { - logger.info('Pool requests activated.') - timer = setInterval(makePoolRequests, constants.INTERVAL) - } - - function addRequest (id, type, request) { - logger.debug('Add request to the pool requests.', { id: id, type: type, request: request }) - - PoolRequests.findById(id, function (err, entity) { - if (err) { - logger.error('Cannot find one pool request.', { error: err }) +'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 Pods = require('../models/pods') +var PoolRequests = require('../models/poolRequests') +var requests = require('../helpers/requests') +var Videos = require('../models/videos') + +var timer = null + +var poolRequests = { + activate: activate, + addRequest: addRequest, + deactivate: deactivate, + forceSend: forceSend +} + +function activate () { + logger.info('Pool requests activated.') + timer = setInterval(makePoolRequests, constants.INTERVAL) +} + +function addRequest (id, type, request) { + logger.debug('Add request to the pool requests.', { id: id, type: type, request: request }) + + PoolRequests.findById(id, function (err, entity) { + if (err) { + logger.error('Cannot find one pool request.', { error: err }) + return // Abort + } + + if (entity) { + if (entity.type === type) { + logger.error('Cannot insert two same requests.') return // Abort } - if (entity) { - if (entity.type === type) { - logger.error('Cannot insert two same requests.') + // Remove the request of the other type + PoolRequests.removeRequestById(id, function (err) { + if (err) { + logger.error('Cannot remove a pool request.', { error: err }) return // Abort } + }) + } else { + PoolRequests.create(id, type, request, function (err) { + if (err) logger.error('Cannot create a pool request.', { error: err }) + return // Abort + }) + } + }) +} - // Remove the request of the other type - PoolRequests.removeRequestById(id, function (err) { - if (err) { - logger.error('Cannot remove a pool request.', { error: err }) - return // Abort - } - }) - } else { - PoolRequests.create(id, type, request, function (err) { - if (err) logger.error('Cannot create a pool request.', { error: err }) - return // Abort - }) - } - }) - } +function deactivate () { + logger.info('Pool requests deactivated.') + clearInterval(timer) +} - function deactivate () { - logger.info('Pool requests deactivated.') - clearInterval(timer) - } +function forceSend () { + logger.info('Force pool requests sending.') + makePoolRequests() +} - function forceSend () { - logger.info('Force pool requests sending.') - makePoolRequests() - } +// --------------------------------------------------------------------------- - // --------------------------------------------------------------------------- +module.exports = poolRequests - module.exports = poolRequests +// --------------------------------------------------------------------------- - // --------------------------------------------------------------------------- +function makePoolRequest (type, requests_to_make, callback) { + if (!callback) callback = function () {} - function makePoolRequest (type, requests_to_make, callback) { - if (!callback) callback = function () {} + Pods.list(function (err, pods) { + if (err) return callback(err) - Pods.list(function (err, pods) { - if (err) return callback(err) + var params = { + encrypt: true, + sign: true, + method: 'POST', + path: null, + data: requests_to_make + } - var params = { - encrypt: true, - sign: true, - method: 'POST', - path: null, - data: requests_to_make - } + if (type === 'add') { + params.path = '/api/' + constants.API_VERSION + '/remotevideos/add' + } else if (type === 'remove') { + params.path = '/api/' + constants.API_VERSION + '/remotevideos/remove' + } else { + return callback(new Error('Unkown pool request type.')) + } + + var bad_pods = [] + var good_pods = [] - if (type === 'add') { - params.path = '/api/' + constants.API_VERSION + '/remotevideos/add' - } else if (type === 'remove') { - params.path = '/api/' + constants.API_VERSION + '/remotevideos/remove' + requests.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 { - return callback(new Error('Unkown pool request type.')) + good_pods.push(pod._id) } - var bad_pods = [] - var good_pods = [] - - requests.makeMultipleRetryRequest(params, pods, callbackEachPodFinished, callbackAllPodsFinished) + return callback_each_pod_finished() + } - 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) - } + function callbackAllPodsFinished (err) { + if (err) return callback(err) - return callback_each_pod_finished() + updatePodsScore(good_pods, bad_pods) + callback(null) + } + }) +} + +function makePoolRequests () { + logger.info('Making pool requests to friends.') + + PoolRequests.list(function (err, pool_requests) { + if (err) { + logger.error('Cannot get the list of pool requests.', { err: err }) + return // Abort + } + + if (pool_requests.length === 0) return + + var requests_to_make = { + add: { + ids: [], + requests: [] + }, + remove: { + ids: [], + requests: [] } - - function callbackAllPodsFinished (err) { - if (err) return callback(err) - - updatePodsScore(good_pods, bad_pods) - callback(null) + } + + async.each(pool_requests, function (pool_request, callback_each) { + if (pool_request.type === 'add') { + requests_to_make.add.requests.push(pool_request.request) + requests_to_make.add.ids.push(pool_request._id) + } else if (pool_request.type === 'remove') { + requests_to_make.remove.requests.push(pool_request.request) + requests_to_make.remove.ids.push(pool_request._id) + } else { + logger.error('Unkown pool request type.', { request_type: pool_request.type }) + return // abort } - }) - } - function makePoolRequests () { - logger.info('Making pool requests to friends.') + callback_each() + }, function () { + // Send the add requests + if (requests_to_make.add.requests.length !== 0) { + makePoolRequest('add', requests_to_make.add.requests, function (err) { + if (err) logger.error('Errors when sent add pool requests.', { error: err }) - PoolRequests.list(function (err, pool_requests) { - if (err) { - logger.error('Cannot get the list of pool requests.', { err: err }) - return // Abort + PoolRequests.removeRequests(requests_to_make.add.ids) + }) } - if (pool_requests.length === 0) return + // Send the remove requests + if (requests_to_make.remove.requests.length !== 0) { + makePoolRequest('remove', requests_to_make.remove.requests, function (err) { + if (err) logger.error('Errors when sent remove pool requests.', { error: err }) - var requests_to_make = { - add: { - ids: [], - requests: [] - }, - remove: { - ids: [], - requests: [] - } + PoolRequests.removeRequests(requests_to_make.remove.ids) + }) } + }) + }) +} - async.each(pool_requests, function (pool_request, callback_each) { - if (pool_request.type === 'add') { - requests_to_make.add.requests.push(pool_request.request) - requests_to_make.add.ids.push(pool_request._id) - } else if (pool_request.type === 'remove') { - requests_to_make.remove.requests.push(pool_request.request) - requests_to_make.remove.ids.push(pool_request._id) - } else { - logger.error('Unkown pool request type.', { request_type: pool_request.type }) - return // abort - } - - callback_each() - }, function () { - // Send the add requests - if (requests_to_make.add.requests.length !== 0) { - makePoolRequest('add', requests_to_make.add.requests, function (err) { - if (err) logger.error('Errors when sent add pool requests.', { error: err }) - - PoolRequests.removeRequests(requests_to_make.add.ids) - }) - } +function removeBadPods () { + Pods.findBadPods(function (err, pods) { + if (err) { + logger.error('Cannot find bad pods.', { error: err }) + return // abort + } - // Send the remove requests - if (requests_to_make.remove.requests.length !== 0) { - makePoolRequest('remove', requests_to_make.remove.requests, function (err) { - if (err) logger.error('Errors when sent remove pool requests.', { error: err }) + if (pods.length === 0) return - PoolRequests.removeRequests(requests_to_make.remove.ids) - }) - } - }) - }) - } + var urls = pluck(pods, 'url') + var ids = pluck(pods, '_id') - function removeBadPods () { - Pods.findBadPods(function (err, pods) { + Videos.removeAllRemotesOf(urls, function (err, r) { if (err) { - logger.error('Cannot find bad pods.', { error: err }) - return // abort + logger.error('Cannot remove videos from a pod that we removing.', { error: err }) + } else { + var videos_removed = r.result.n + logger.info('Removed %d videos.', videos_removed) } - if (pods.length === 0) return - - var urls = pluck(pods, 'url') - var ids = pluck(pods, '_id') - - Videos.removeAllRemotesOf(urls, function (err, r) { + Pods.removeAllByIds(ids, function (err, r) { if (err) { - logger.error('Cannot remove videos from a pod that we removing.', { error: err }) + logger.error('Cannot remove bad pods.', { error: err }) } else { - var videos_removed = r.result.n - logger.info('Removed %d videos.', videos_removed) + var pods_removed = r.result.n + logger.info('Removed %d pods.', pods_removed) } - - Pods.removeAllByIds(ids, function (err, r) { - if (err) { - logger.error('Cannot remove bad pods.', { error: err }) - } else { - var pods_removed = r.result.n - logger.info('Removed %d pods.', pods_removed) - } - }) }) }) - } + }) +} - function updatePodsScore (good_pods, bad_pods) { - logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length) +function updatePodsScore (good_pods, bad_pods) { + logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length) - Pods.incrementScores(good_pods, constants.PODS_SCORE.BONUS, function (err) { - if (err) logger.error('Cannot increment scores of good pods.') - }) + Pods.incrementScores(good_pods, constants.PODS_SCORE.BONUS, function (err) { + if (err) logger.error('Cannot increment scores of good pods.') + }) - Pods.incrementScores(bad_pods, constants.PODS_SCORE.MALUS, function (err) { - if (err) logger.error('Cannot increment scores of bad pods.') - removeBadPods() - }) - } -})() + Pods.incrementScores(bad_pods, constants.PODS_SCORE.MALUS, function (err) { + if (err) logger.error('Cannot increment scores of bad pods.') + removeBadPods() + }) +} diff --git a/lib/videos.js b/lib/videos.js index 0e8143351..2d7d9500d 100644 --- a/lib/videos.js +++ b/lib/videos.js @@ -1,52 +1,50 @@ -;(function () { - 'use strict' - - var async = require('async') - var config = require('config') - var path = require('path') - var webtorrent = require('../lib/webtorrent') - - var logger = require('../helpers/logger') - var Videos = require('../models/videos') - - var uploadDir = path.join(__dirname, '..', config.get('storage.uploads')) - - var videos = { - seed: seed, - seedAllExisting: seedAllExisting - } - - function seed (path, callback) { - logger.info('Seeding %s...', path) - - webtorrent.seed(path, function (torrent) { - logger.info('%s seeded (%s).', path, torrent.magnetURI) - - return callback(null, torrent) - }) - } - - function seedAllExisting (callback) { - Videos.listOwned(function (err, videos_list) { - if (err) { - logger.error('Cannot get list of the videos to seed.') - return callback(err) - } - - async.each(videos_list, function (video, each_callback) { - seed(uploadDir + video.namePath, function (err) { - if (err) { - logger.error('Cannot seed this video.') - return callback(err) - } - - each_callback(null) - }) - }, callback) - }) - } - - // --------------------------------------------------------------------------- - - module.exports = videos -})() +'use strict' + +var async = require('async') +var config = require('config') +var path = require('path') +var webtorrent = require('../lib/webtorrent') + +var logger = require('../helpers/logger') +var Videos = require('../models/videos') + +var uploadDir = path.join(__dirname, '..', config.get('storage.uploads')) + +var videos = { + seed: seed, + seedAllExisting: seedAllExisting +} + +function seed (path, callback) { + logger.info('Seeding %s...', path) + + webtorrent.seed(path, function (torrent) { + logger.info('%s seeded (%s).', path, torrent.magnetURI) + + return callback(null, torrent) + }) +} + +function seedAllExisting (callback) { + Videos.listOwned(function (err, videos_list) { + if (err) { + logger.error('Cannot get list of the videos to seed.') + return callback(err) + } + + async.each(videos_list, function (video, each_callback) { + seed(uploadDir + video.namePath, function (err) { + if (err) { + logger.error('Cannot seed this video.') + return callback(err) + } + + each_callback(null) + }) + }, callback) + }) +} + +// --------------------------------------------------------------------------- + +module.exports = videos diff --git a/lib/webtorrent.js b/lib/webtorrent.js index 083e5b77a..5f10322a5 100644 --- a/lib/webtorrent.js +++ b/lib/webtorrent.js @@ -1,161 +1,159 @@ -;(function () { - 'use strict' - - var config = require('config') - var ipc = require('node-ipc') - var pathUtils = require('path') - var spawn = require('electron-spawn') - - var logger = require('../helpers/logger') - - var host = config.get('webserver.host') - var port = config.get('webserver.port') - var nodeKey = 'webtorrentnode' + port - var processKey = 'webtorrentprocess' + port - ipc.config.silent = true - ipc.config.id = nodeKey - - var webtorrent = { - add: add, - app: null, // Pid of the app - create: create, - remove: remove, - seed: seed, - silent: false // Useful for beautiful tests +'use strict' + +var config = require('config') +var ipc = require('node-ipc') +var pathUtils = require('path') +var spawn = require('electron-spawn') + +var logger = require('../helpers/logger') + +var host = config.get('webserver.host') +var port = config.get('webserver.port') +var nodeKey = 'webtorrentnode' + port +var processKey = 'webtorrentprocess' + port +ipc.config.silent = true +ipc.config.id = nodeKey + +var webtorrent = { + add: add, + app: null, // Pid of the app + create: create, + remove: remove, + seed: seed, + silent: false // Useful for beautiful tests +} + +function create (options, callback) { + if (typeof options === 'function') { + callback = options + options = {} } - function create (options, callback) { - if (typeof options === 'function') { - callback = options - options = {} - } - - // Override options - if (options.host) host = options.host - if (options.port) { - port = options.port - nodeKey = 'webtorrentnode' + port - processKey = 'webtorrentprocess' + port - ipc.config.id = nodeKey - } - - ipc.serve(function () { - if (!webtorrent.silent) logger.info('IPC server ready.') + // Override options + if (options.host) host = options.host + if (options.port) { + port = options.port + nodeKey = 'webtorrentnode' + port + processKey = 'webtorrentprocess' + port + ipc.config.id = nodeKey + } - // Run a timeout of 30s after which we exit the process - var timeout_webtorrent_process = setTimeout(function () { - logger.error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt npm package installed with xvfb-run.') - process.exit() - }, 30000) + ipc.serve(function () { + if (!webtorrent.silent) logger.info('IPC server ready.') - ipc.server.on(processKey + '.ready', function () { - if (!webtorrent.silent) logger.info('Webtorrent process ready.') - clearTimeout(timeout_webtorrent_process) - callback() - }) + // Run a timeout of 30s after which we exit the process + var timeout_webtorrent_process = setTimeout(function () { + logger.error('Timeout : cannot run the webtorrent process. Please ensure you have electron-prebuilt npm package installed with xvfb-run.') + process.exit() + }, 30000) - ipc.server.on(processKey + '.exception', function (data) { - logger.error('Received exception error from webtorrent process.', { exception: data.exception }) - process.exit() - }) + ipc.server.on(processKey + '.ready', function () { + if (!webtorrent.silent) logger.info('Webtorrent process ready.') + clearTimeout(timeout_webtorrent_process) + callback() + }) - var webtorrent_process = spawn(pathUtils.join(__dirname, 'webtorrentProcess.js'), host, port, { detached: true }) - webtorrent_process.stderr.on('data', function (data) { - // logger.debug('Webtorrent process stderr: ', data.toString()) - }) + ipc.server.on(processKey + '.exception', function (data) { + logger.error('Received exception error from webtorrent process.', { exception: data.exception }) + process.exit() + }) - webtorrent_process.stdout.on('data', function (data) { - // logger.debug('Webtorrent process:', data.toString()) - }) + var webtorrent_process = spawn(pathUtils.join(__dirname, 'webtorrentProcess.js'), host, port, { detached: true }) + webtorrent_process.stderr.on('data', function (data) { + // logger.debug('Webtorrent process stderr: ', data.toString()) + }) - webtorrent.app = webtorrent_process + webtorrent_process.stdout.on('data', function (data) { + // logger.debug('Webtorrent process:', data.toString()) }) - ipc.server.start() - } + webtorrent.app = webtorrent_process + }) - function seed (path, callback) { - var extension = pathUtils.extname(path) - var basename = pathUtils.basename(path, extension) - var data = { - _id: basename, - args: { - path: path - } + ipc.server.start() +} + +function seed (path, callback) { + var extension = pathUtils.extname(path) + var basename = pathUtils.basename(path, extension) + var data = { + _id: basename, + args: { + path: path } + } - if (!webtorrent.silent) logger.debug('Node wants to seed %s.', data._id) + if (!webtorrent.silent) logger.debug('Node wants to seed %s.', data._id) - // Finish signal - var event_key = nodeKey + '.seedDone.' + data._id - ipc.server.on(event_key, function listener (received) { - if (!webtorrent.silent) logger.debug('Process seeded torrent %s.', received.magnetUri) + // Finish signal + var event_key = nodeKey + '.seedDone.' + data._id + ipc.server.on(event_key, function listener (received) { + if (!webtorrent.silent) logger.debug('Process seeded torrent %s.', received.magnetUri) - // This is a fake object, we just use the magnetUri in this project - var torrent = { - magnetURI: received.magnetUri - } + // This is a fake object, we just use the magnetUri in this project + var torrent = { + magnetURI: received.magnetUri + } - ipc.server.off(event_key) - callback(torrent) - }) + ipc.server.off(event_key) + callback(torrent) + }) - ipc.server.broadcast(processKey + '.seed', data) - } + ipc.server.broadcast(processKey + '.seed', data) +} - function add (magnetUri, callback) { - var data = { - _id: magnetUri, - args: { - magnetUri: magnetUri - } +function add (magnetUri, callback) { + var data = { + _id: magnetUri, + args: { + magnetUri: magnetUri } + } - if (!webtorrent.silent) logger.debug('Node wants to add ' + data._id) + if (!webtorrent.silent) logger.debug('Node wants to add ' + data._id) - // Finish signal - var event_key = nodeKey + '.addDone.' + data._id - ipc.server.on(event_key, function (received) { - if (!webtorrent.silent) logger.debug('Process added torrent.') + // Finish signal + var event_key = nodeKey + '.addDone.' + data._id + ipc.server.on(event_key, function (received) { + if (!webtorrent.silent) logger.debug('Process added torrent.') - // This is a fake object, we just use the magnetUri in this project - var torrent = { - files: received.files - } + // This is a fake object, we just use the magnetUri in this project + var torrent = { + files: received.files + } - ipc.server.off(event_key) - callback(torrent) - }) + ipc.server.off(event_key) + callback(torrent) + }) - ipc.server.broadcast(processKey + '.add', data) - } + ipc.server.broadcast(processKey + '.add', data) +} - function remove (magnetUri, callback) { - var data = { - _id: magnetUri, - args: { - magnetUri: magnetUri - } +function remove (magnetUri, callback) { + var data = { + _id: magnetUri, + args: { + magnetUri: magnetUri } + } - if (!webtorrent.silent) logger.debug('Node wants to stop seeding %s.', data._id) + if (!webtorrent.silent) logger.debug('Node wants to stop seeding %s.', data._id) - // Finish signal - var event_key = nodeKey + '.removeDone.' + data._id - ipc.server.on(event_key, function (received) { - if (!webtorrent.silent) logger.debug('Process removed torrent %s.', data._id) + // Finish signal + var event_key = nodeKey + '.removeDone.' + data._id + ipc.server.on(event_key, function (received) { + if (!webtorrent.silent) logger.debug('Process removed torrent %s.', data._id) - var err = null - if (received.err) err = received.err + var err = null + if (received.err) err = received.err - ipc.server.off(event_key) - callback(err) - }) + ipc.server.off(event_key) + callback(err) + }) - ipc.server.broadcast(processKey + '.remove', data) - } + ipc.server.broadcast(processKey + '.remove', data) +} - // --------------------------------------------------------------------------- +// --------------------------------------------------------------------------- - module.exports = webtorrent -})() +module.exports = webtorrent diff --git a/lib/webtorrentProcess.js b/lib/webtorrentProcess.js index 7dc655f10..96ebf9d02 100644 --- a/lib/webtorrentProcess.js +++ b/lib/webtorrentProcess.js @@ -1,95 +1,93 @@ -;(function () { - 'use strict' +'use strict' - function webtorrent (args) { - var WebTorrent = require('webtorrent') - var ipc = require('node-ipc') +function webtorrent (args) { + var WebTorrent = require('webtorrent') + var ipc = require('node-ipc') - if (args.length !== 3) { - console.log('Wrong arguments number: ' + args.length + '/3') - process.exit(-1) - } - - var host = args[1] - var port = args[2] - var nodeKey = 'webtorrentnode' + port - var processKey = 'webtorrentprocess' + port + if (args.length !== 3) { + console.log('Wrong arguments number: ' + args.length + '/3') + process.exit(-1) + } - ipc.config.silent = true - ipc.config.id = processKey + var host = args[1] + var port = args[2] + var nodeKey = 'webtorrentnode' + port + var processKey = 'webtorrentprocess' + port - if (host === 'client' && port === '1') global.WEBTORRENT_ANNOUNCE = [] - else global.WEBTORRENT_ANNOUNCE = 'ws://' + host + ':' + port + '/tracker/socket' - var wt = new WebTorrent({ dht: false }) + ipc.config.silent = true + ipc.config.id = processKey - function seed (data) { - var args = data.args - var path = args.path - var _id = data._id + if (host === 'client' && port === '1') global.WEBTORRENT_ANNOUNCE = [] + else global.WEBTORRENT_ANNOUNCE = 'ws://' + host + ':' + port + '/tracker/socket' + var wt = new WebTorrent({ dht: false }) - wt.seed(path, { announceList: '' }, function (torrent) { - var to_send = { - magnetUri: torrent.magnetURI - } + function seed (data) { + var args = data.args + var path = args.path + var _id = data._id - ipc.of[nodeKey].emit(nodeKey + '.seedDone.' + _id, to_send) - }) - } + wt.seed(path, { announceList: '' }, function (torrent) { + var to_send = { + magnetUri: torrent.magnetURI + } - function add (data) { - var args = data.args - var magnetUri = args.magnetUri - var _id = data._id + ipc.of[nodeKey].emit(nodeKey + '.seedDone.' + _id, to_send) + }) + } - wt.add(magnetUri, function (torrent) { - var to_send = { - files: [] - } + function add (data) { + var args = data.args + var magnetUri = args.magnetUri + var _id = data._id - torrent.files.forEach(function (file) { - to_send.files.push({ path: file.path }) - }) + wt.add(magnetUri, function (torrent) { + var to_send = { + files: [] + } - ipc.of[nodeKey].emit(nodeKey + '.addDone.' + _id, to_send) + torrent.files.forEach(function (file) { + to_send.files.push({ path: file.path }) }) - } - function remove (data) { - var args = data.args - var magnetUri = args.magnetUri - var _id = data._id + ipc.of[nodeKey].emit(nodeKey + '.addDone.' + _id, to_send) + }) + } - try { - wt.remove(magnetUri, callback) - } catch (err) { - console.log('Cannot remove the torrent from WebTorrent.') - return callback(null) - } + function remove (data) { + var args = data.args + var magnetUri = args.magnetUri + var _id = data._id - function callback () { - var to_send = {} - ipc.of[nodeKey].emit(nodeKey + '.removeDone.' + _id, to_send) - } + try { + wt.remove(magnetUri, callback) + } catch (err) { + console.log('Cannot remove the torrent from WebTorrent.') + return callback(null) + } + + function callback () { + var to_send = {} + ipc.of[nodeKey].emit(nodeKey + '.removeDone.' + _id, to_send) } + } - console.log('Configuration: ' + host + ':' + port) - console.log('Connecting to IPC...') + console.log('Configuration: ' + host + ':' + port) + console.log('Connecting to IPC...') - ipc.connectTo(nodeKey, function () { - ipc.of[nodeKey].on(processKey + '.seed', seed) - ipc.of[nodeKey].on(processKey + '.add', add) - ipc.of[nodeKey].on(processKey + '.remove', remove) + ipc.connectTo(nodeKey, function () { + ipc.of[nodeKey].on(processKey + '.seed', seed) + ipc.of[nodeKey].on(processKey + '.add', add) + ipc.of[nodeKey].on(processKey + '.remove', remove) - ipc.of[nodeKey].emit(processKey + '.ready') - console.log('Ready.') - }) + ipc.of[nodeKey].emit(processKey + '.ready') + console.log('Ready.') + }) - process.on('uncaughtException', function (e) { - ipc.of[nodeKey].emit(processKey + '.exception', { exception: e }) - }) - } + process.on('uncaughtException', function (e) { + ipc.of[nodeKey].emit(processKey + '.exception', { exception: e }) + }) +} - // --------------------------------------------------------------------------- +// --------------------------------------------------------------------------- - module.exports = webtorrent -})() +module.exports = webtorrent -- cgit v1.2.3