diff options
Diffstat (limited to 'src/pods.js')
-rw-r--r-- | src/pods.js | 89 |
1 files changed, 11 insertions, 78 deletions
diff --git a/src/pods.js b/src/pods.js index e26b3f0ae..9afc6cc96 100644 --- a/src/pods.js +++ b/src/pods.js | |||
@@ -8,6 +8,7 @@ | |||
8 | 8 | ||
9 | var logger = require('./logger') | 9 | var logger = require('./logger') |
10 | var PodsDB = require('./database').PodsDB | 10 | var PodsDB = require('./database').PodsDB |
11 | var poolRequests = require('./poolRequests') | ||
11 | var utils = require('./utils') | 12 | var utils = require('./utils') |
12 | 13 | ||
13 | var pods = {} | 14 | var pods = {} |
@@ -16,13 +17,6 @@ | |||
16 | var host = config.get('webserver.host') | 17 | var host = config.get('webserver.host') |
17 | var port = config.get('webserver.port') | 18 | var port = config.get('webserver.port') |
18 | 19 | ||
19 | // ----------- Constants ----------- | ||
20 | |||
21 | var PODS_SCORE = { | ||
22 | MALUS: -10, | ||
23 | BONUS: 10 | ||
24 | } | ||
25 | |||
26 | // ----------- Private functions ----------- | 20 | // ----------- Private functions ----------- |
27 | 21 | ||
28 | function getForeignPodsList (url, callback) { | 22 | function getForeignPodsList (url, callback) { |
@@ -34,25 +28,6 @@ | |||
34 | }) | 28 | }) |
35 | } | 29 | } |
36 | 30 | ||
37 | function updatePodsScore (good_pods, bad_pods) { | ||
38 | logger.info('Updating %d good pods and %d bad pods scores.', good_pods.length, bad_pods.length) | ||
39 | |||
40 | PodsDB.update({ _id: { $in: good_pods } }, { $inc: { score: PODS_SCORE.BONUS } }, { multi: true }).exec() | ||
41 | PodsDB.update({ _id: { $in: bad_pods } }, { $inc: { score: PODS_SCORE.MALUS } }, { multi: true }, function (err) { | ||
42 | if (err) throw err | ||
43 | removeBadPods() | ||
44 | }) | ||
45 | } | ||
46 | |||
47 | function removeBadPods () { | ||
48 | PodsDB.remove({ score: 0 }, function (err, result) { | ||
49 | if (err) throw err | ||
50 | |||
51 | var number_removed = result.result.n | ||
52 | if (number_removed !== 0) logger.info('Removed %d pod.', number_removed) | ||
53 | }) | ||
54 | } | ||
55 | |||
56 | // ----------- Public functions ----------- | 31 | // ----------- Public functions ----------- |
57 | 32 | ||
58 | pods.list = function (callback) { | 33 | pods.list = function (callback) { |
@@ -93,58 +68,16 @@ | |||
93 | }) | 68 | }) |
94 | } | 69 | } |
95 | 70 | ||
96 | // { path, data } | 71 | pods.addVideoToFriends = function (video) { |
97 | pods.makeSecureRequest = function (data, callback) { | 72 | // To avoid duplicates |
98 | if (callback === undefined) callback = function () {} | 73 | var id = video.name + video.magnetUri |
99 | 74 | poolRequests.addToPoolRequests(id, 'add', video) | |
100 | PodsDB.find({}, { _id: 1, url: 1, publicKey: 1 }).exec(function (err, pods) { | 75 | } |
101 | if (err) { | ||
102 | logger.error('Cannot get the list of the pods.', { error: err }) | ||
103 | return callback(err) | ||
104 | } | ||
105 | |||
106 | logger.debug('Make multiple requests.') | ||
107 | |||
108 | var params = { | ||
109 | encrypt: true, | ||
110 | sign: true, | ||
111 | method: data.method, | ||
112 | path: data.path, | ||
113 | data: data.data | ||
114 | } | ||
115 | |||
116 | var bad_pods = [] | ||
117 | var good_pods = [] | ||
118 | |||
119 | utils.makeMultipleRetryRequest( | ||
120 | params, | ||
121 | |||
122 | pods, | ||
123 | |||
124 | function callbackEachPodFinished (err, response, body, pod, callback_each_pod_finished) { | ||
125 | if (err || response.statusCode !== 200) { | ||
126 | bad_pods.push(pod._id) | ||
127 | logger.error('Error sending secure request to %s/%s pod.', pod.url, data.path, { error: err }) | ||
128 | } else { | ||
129 | good_pods.push(pod._id) | ||
130 | } | ||
131 | |||
132 | return callback_each_pod_finished() | ||
133 | }, | ||
134 | |||
135 | function callbackAllPodsFinished (err) { | ||
136 | if (err) { | ||
137 | logger.error('There was some errors when sending the video meta data.', { error: err }) | ||
138 | return callback(err) | ||
139 | } | ||
140 | |||
141 | logger.debug('Finished') | ||
142 | 76 | ||
143 | updatePodsScore(good_pods, bad_pods) | 77 | pods.removeVideoToFriends = function (video) { |
144 | callback(null) | 78 | // To avoid duplicates |
145 | } | 79 | var id = video.name + video.magnetUri |
146 | ) | 80 | poolRequests.addToPoolRequests(id, 'remove', video) |
147 | }) | ||
148 | } | 81 | } |
149 | 82 | ||
150 | pods.makeFriends = function (callback) { | 83 | pods.makeFriends = function (callback) { |
@@ -214,7 +147,7 @@ | |||
214 | 147 | ||
215 | pods_list, | 148 | pods_list, |
216 | 149 | ||
217 | function eachRequest (err, response, body, pod, callback_each_request) { | 150 | function eachRequest (err, response, body, url, pod, callback_each_request) { |
218 | // We add the pod if it responded correctly with its public certificate | 151 | // We add the pod if it responded correctly with its public certificate |
219 | if (!err && response.statusCode === 200) { | 152 | if (!err && response.statusCode === 200) { |
220 | pods.add({ url: pod.url, publicKey: body.cert, score: global.FRIEND_BASE_SCORE }, function (err) { | 153 | pods.add({ url: pod.url, publicKey: body.cert, score: global.FRIEND_BASE_SCORE }, function (err) { |