var logger = require('./logger')
var PodsDB = require('./database').PodsDB
+ var poolRequests = require('./poolRequests')
var utils = require('./utils')
var pods = {}
var params = {
url: data.url,
- publicKey: data.publicKey
+ publicKey: data.publicKey,
+ score: global.FRIEND_BASE_SCORE
}
PodsDB.create(params, function (err, pod) {
})
}
- // { path, data }
- pods.makeSecureRequest = function (data, callback) {
- PodsDB.find({}, { url: 1, publicKey: 1 }).exec(function (err, urls) {
- if (err) {
- logger.error('Cannot get the list of the pods.', { error: err })
- return callback(err)
- }
-
- logger.debug('Make multiple requests.')
-
- var params = {
- encrypt: true,
- sign: true,
- method: data.method,
- path: data.path,
- data: data.data
- }
-
- utils.makeMultipleRetryRequest(
- params,
-
- urls,
-
- function callbackEachPodFinished (err, response, body, url) {
- if (err || response.statusCode !== 200) {
- logger.error('Error sending secure request to %s/%s pod.', url, data.path, { error: err })
- }
- },
-
- function callbackAllPodsFinished (err) {
- if (err) {
- logger.error('There was some errors when sending the video meta data.', { error: err })
- return callback(err)
- }
+ pods.addVideoToFriends = function (video) {
+ // To avoid duplicates
+ var id = video.name + video.magnetUri
+ poolRequests.addToPoolRequests(id, 'add', video)
+ }
- logger.debug('Finished')
- callback(null)
- }
- )
- })
+ pods.removeVideoToFriends = function (video) {
+ // To avoid duplicates
+ var id = video.name + video.magnetUri
+ poolRequests.addToPoolRequests(id, 'remove', video)
}
pods.makeFriends = function (callback) {
var pods_list = computeWinningPods(urls, pods_score)
logger.debug('Pods that we keep computed.', { pods_to_keep: pods_list })
- logger.debug('Make requests...')
makeRequestsToWinningPods(cert, pods_list)
})
})
// -----------------------------------------------------------------------
function computeForeignPodsList (url, callback) {
- // Always add a trust pod
- pods_score[url] = Infinity
+ // Let's give 1 point to the pod we ask the friends list
+ pods_score[url] = 1
getForeignPodsList(url, function (foreign_pods_list) {
if (foreign_pods_list.length === 0) return callback()
pods_list,
- function eachRequest (err, response, body, url) {
+ 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: url, publicKey: body.cert }, function (err) {
+ pods.add({ url: pod.url, publicKey: body.cert, score: global.FRIEND_BASE_SCORE }, function (err) {
if (err) {
- logger.error('Error with adding %s pod.', url, { error: err })
+ logger.error('Error with adding %s pod.', pod.url, { error: err })
}
+
+ return callback_each_request()
})
} else {
- logger.error('Error with adding %s pod.', url, { error: err || new Error('Status not 200') })
+ logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') })
+ return callback_each_request()
}
},