diff options
Diffstat (limited to 'server/lib/friends.js')
-rw-r--r-- | server/lib/friends.js | 72 |
1 files changed, 35 insertions, 37 deletions
diff --git a/server/lib/friends.js b/server/lib/friends.js index 3b8a52060..f4f2ada87 100644 --- a/server/lib/friends.js +++ b/server/lib/friends.js | |||
@@ -39,8 +39,8 @@ function hasFriends (callback) { | |||
39 | Pods.count(function (err, count) { | 39 | Pods.count(function (err, count) { |
40 | if (err) return callback(err) | 40 | if (err) return callback(err) |
41 | 41 | ||
42 | const has_friends = (count !== 0) | 42 | const hasFriends = (count !== 0) |
43 | callback(null, has_friends) | 43 | callback(null, hasFriends) |
44 | }) | 44 | }) |
45 | } | 45 | } |
46 | 46 | ||
@@ -49,7 +49,7 @@ function getMyCertificate (callback) { | |||
49 | } | 49 | } |
50 | 50 | ||
51 | function makeFriends (callback) { | 51 | function makeFriends (callback) { |
52 | const pods_score = {} | 52 | const podsScore = {} |
53 | 53 | ||
54 | logger.info('Make friends!') | 54 | logger.info('Make friends!') |
55 | getMyCertificate(function (err, cert) { | 55 | getMyCertificate(function (err, cert) { |
@@ -60,16 +60,16 @@ function makeFriends (callback) { | |||
60 | 60 | ||
61 | const urls = config.get('network.friends') | 61 | const urls = config.get('network.friends') |
62 | 62 | ||
63 | async.each(urls, function (url, callback_each) { | 63 | async.each(urls, function (url, callbackEach) { |
64 | computeForeignPodsList(url, pods_score, callback_each) | 64 | computeForeignPodsList(url, podsScore, callbackEach) |
65 | }, function (err) { | 65 | }, function (err) { |
66 | if (err) return callback(err) | 66 | if (err) return callback(err) |
67 | 67 | ||
68 | logger.debug('Pods scores computed.', { pods_score: pods_score }) | 68 | logger.debug('Pods scores computed.', { podsScore: podsScore }) |
69 | const pods_list = computeWinningPods(urls, pods_score) | 69 | const podsList = computeWinningPods(urls, podsScore) |
70 | logger.debug('Pods that we keep.', { pods_to_keep: pods_list }) | 70 | logger.debug('Pods that we keep.', { podsToKeep: podsList }) |
71 | 71 | ||
72 | makeRequestsToWinningPods(cert, pods_list, callback) | 72 | makeRequestsToWinningPods(cert, podsList, callback) |
73 | }) | 73 | }) |
74 | }) | 74 | }) |
75 | } | 75 | } |
@@ -102,10 +102,10 @@ function quitFriends (callback) { | |||
102 | 102 | ||
103 | logger.info('Broke friends, so sad :(') | 103 | logger.info('Broke friends, so sad :(') |
104 | 104 | ||
105 | Videos.listFromRemotes(function (err, videos_list) { | 105 | Videos.listFromRemotes(function (err, videosList) { |
106 | if (err) return callback(err) | 106 | if (err) return callback(err) |
107 | 107 | ||
108 | videos.removeRemoteVideos(videos_list, function (err) { | 108 | videos.removeRemoteVideos(videosList, function (err) { |
109 | if (err) { | 109 | if (err) { |
110 | logger.error('Cannot remove remote videos.', { error: err }) | 110 | logger.error('Cannot remove remote videos.', { error: err }) |
111 | return callback(err) | 111 | return callback(err) |
@@ -132,35 +132,35 @@ module.exports = pods | |||
132 | 132 | ||
133 | // --------------------------------------------------------------------------- | 133 | // --------------------------------------------------------------------------- |
134 | 134 | ||
135 | function computeForeignPodsList (url, pods_score, callback) { | 135 | function computeForeignPodsList (url, podsScore, callback) { |
136 | // Let's give 1 point to the pod we ask the friends list | 136 | // Let's give 1 point to the pod we ask the friends list |
137 | pods_score[url] = 1 | 137 | podsScore[url] = 1 |
138 | 138 | ||
139 | getForeignPodsList(url, function (err, foreign_pods_list) { | 139 | getForeignPodsList(url, function (err, foreignPodsList) { |
140 | if (err) return callback(err) | 140 | if (err) return callback(err) |
141 | if (foreign_pods_list.length === 0) return callback() | 141 | if (foreignPodsList.length === 0) return callback() |
142 | 142 | ||
143 | foreign_pods_list.forEach(function (foreign_pod) { | 143 | foreignPodsList.forEach(function (foreignPod) { |
144 | const foreign_url = foreign_pod.url | 144 | const foreignUrl = foreignPod.url |
145 | 145 | ||
146 | if (pods_score[foreign_url]) pods_score[foreign_url]++ | 146 | if (podsScore[foreignUrl]) podsScore[foreignUrl]++ |
147 | else pods_score[foreign_url] = 1 | 147 | else podsScore[foreignUrl] = 1 |
148 | }) | 148 | }) |
149 | 149 | ||
150 | callback() | 150 | callback() |
151 | }) | 151 | }) |
152 | } | 152 | } |
153 | 153 | ||
154 | function computeWinningPods (urls, pods_score) { | 154 | function computeWinningPods (urls, podsScore) { |
155 | // Build the list of pods to add | 155 | // Build the list of pods to add |
156 | // Only add a pod if it exists in more than a half base pods | 156 | // Only add a pod if it exists in more than a half base pods |
157 | const pods_list = [] | 157 | const podsList = [] |
158 | const base_score = urls.length / 2 | 158 | const baseScore = urls.length / 2 |
159 | Object.keys(pods_score).forEach(function (pod) { | 159 | Object.keys(baseScore).forEach(function (pod) { |
160 | if (pods_score[pod] > base_score) pods_list.push({ url: pod }) | 160 | if (podsScore[pod] > baseScore) podsList.push({ url: pod }) |
161 | }) | 161 | }) |
162 | 162 | ||
163 | return pods_list | 163 | return podsList |
164 | } | 164 | } |
165 | 165 | ||
166 | function getForeignPodsList (url, callback) { | 166 | function getForeignPodsList (url, callback) { |
@@ -173,14 +173,14 @@ function getForeignPodsList (url, callback) { | |||
173 | }) | 173 | }) |
174 | } | 174 | } |
175 | 175 | ||
176 | function makeRequestsToWinningPods (cert, pods_list, callback) { | 176 | function makeRequestsToWinningPods (cert, podsList, callback) { |
177 | // Stop pool requests | 177 | // Stop pool requests |
178 | requestsScheduler.deactivate() | 178 | requestsScheduler.deactivate() |
179 | // Flush pool requests | 179 | // Flush pool requests |
180 | requestsScheduler.forceSend() | 180 | requestsScheduler.forceSend() |
181 | 181 | ||
182 | // Get the list of our videos to send to our new friends | 182 | // Get the list of our videos to send to our new friends |
183 | Videos.listOwned(function (err, videos_list) { | 183 | Videos.listOwned(function (err, videosList) { |
184 | if (err) { | 184 | if (err) { |
185 | logger.error('Cannot get the list of videos we own.') | 185 | logger.error('Cannot get the list of videos we own.') |
186 | return callback(err) | 186 | return callback(err) |
@@ -189,38 +189,36 @@ function makeRequestsToWinningPods (cert, pods_list, callback) { | |||
189 | const data = { | 189 | const data = { |
190 | url: http + '://' + host + ':' + port, | 190 | url: http + '://' + host + ':' + port, |
191 | publicKey: cert, | 191 | publicKey: cert, |
192 | videos: videos_list | 192 | videos: videosList |
193 | } | 193 | } |
194 | 194 | ||
195 | requests.makeMultipleRetryRequest( | 195 | requests.makeMultipleRetryRequest( |
196 | { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data }, | 196 | { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data }, |
197 | 197 | ||
198 | pods_list, | 198 | podsList, |
199 | 199 | ||
200 | function eachRequest (err, response, body, url, pod, callback_each_request) { | 200 | function eachRequest (err, response, body, url, pod, callbackEachRequest) { |
201 | // We add the pod if it responded correctly with its public certificate | 201 | // We add the pod if it responded correctly with its public certificate |
202 | if (!err && response.statusCode === 200) { | 202 | if (!err && response.statusCode === 200) { |
203 | Pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) { | 203 | Pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) { |
204 | if (err) { | 204 | if (err) { |
205 | logger.error('Error with adding %s pod.', pod.url, { error: err }) | 205 | logger.error('Error with adding %s pod.', pod.url, { error: err }) |
206 | return callback_each_request() | 206 | return callbackEachRequest() |
207 | } | 207 | } |
208 | console.log('hihi') | 208 | |
209 | videos.createRemoteVideos(body.videos, function (err) { | 209 | videos.createRemoteVideos(body.videos, function (err) { |
210 | if (err) { | 210 | if (err) { |
211 | logger.error('Error with adding videos of pod.', pod.url, { error: err }) | 211 | logger.error('Error with adding videos of pod.', pod.url, { error: err }) |
212 | return callback_each_request() | 212 | return callbackEachRequest() |
213 | } | 213 | } |
214 | 214 | ||
215 | console.log('kik') | ||
216 | |||
217 | logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos }) | 215 | logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos }) |
218 | return callback_each_request() | 216 | return callbackEachRequest() |
219 | }) | 217 | }) |
220 | }) | 218 | }) |
221 | } else { | 219 | } else { |
222 | logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') }) | 220 | logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') }) |
223 | return callback_each_request() | 221 | return callbackEachRequest() |
224 | } | 222 | } |
225 | }, | 223 | }, |
226 | 224 | ||