]>
Commit | Line | Data |
---|---|---|
1 | ;(function () { | |
2 | 'use strict' | |
3 | ||
4 | var async = require('async') | |
5 | var config = require('config') | |
6 | var fs = require('fs') | |
7 | var request = require('request') | |
8 | ||
9 | var constants = require('../initializers/constants') | |
10 | var logger = require('../helpers/logger') | |
11 | var PodsDB = require('../initializers/database').PodsDB | |
12 | var poolRequests = require('../lib/poolRequests') | |
13 | var utils = require('../helpers/utils') | |
14 | ||
15 | var http = config.get('webserver.https') ? 'https' : 'http' | |
16 | var host = config.get('webserver.host') | |
17 | var port = config.get('webserver.port') | |
18 | ||
19 | var pods = { | |
20 | add: add, | |
21 | addVideoToFriends: addVideoToFriends, | |
22 | list: list, | |
23 | hasFriends: hasFriends, | |
24 | makeFriends: makeFriends, | |
25 | quitFriends: quitFriends, | |
26 | remove: remove, | |
27 | removeVideoToFriends | |
28 | } | |
29 | ||
30 | // TODO: check if the pod is not already a friend | |
31 | function add (data, callback) { | |
32 | var videos = require('./videos') | |
33 | logger.info('Adding pod: %s', data.url) | |
34 | ||
35 | var params = { | |
36 | url: data.url, | |
37 | publicKey: data.publicKey, | |
38 | score: constants.FRIEND_BASE_SCORE | |
39 | } | |
40 | ||
41 | PodsDB.create(params, function (err, pod) { | |
42 | if (err) { | |
43 | logger.error('Cannot insert the pod.', { error: err }) | |
44 | return callback(err) | |
45 | } | |
46 | ||
47 | videos.addRemotes(data.videos) | |
48 | ||
49 | fs.readFile(utils.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { | |
50 | if (err) { | |
51 | logger.error('Cannot read cert file.', { error: err }) | |
52 | return callback(err) | |
53 | } | |
54 | ||
55 | videos.listOwned(function (err, videos_list) { | |
56 | if (err) { | |
57 | logger.error('Cannot get the list of owned videos.', { error: err }) | |
58 | return callback(err) | |
59 | } | |
60 | ||
61 | return callback(null, { cert: cert, videos: videos_list }) | |
62 | }) | |
63 | }) | |
64 | }) | |
65 | } | |
66 | ||
67 | function addVideoToFriends (video) { | |
68 | // To avoid duplicates | |
69 | var id = video.name + video.magnetUri | |
70 | poolRequests.addToPoolRequests(id, 'add', video) | |
71 | } | |
72 | ||
73 | function list (callback) { | |
74 | PodsDB.find(function (err, pods_list) { | |
75 | if (err) { | |
76 | logger.error('Cannot get the list of the pods.', { error: err }) | |
77 | return callback(err) | |
78 | } | |
79 | ||
80 | return callback(null, pods_list) | |
81 | }) | |
82 | } | |
83 | ||
84 | function hasFriends (callback) { | |
85 | PodsDB.count(function (err, count) { | |
86 | if (err) return callback(err) | |
87 | ||
88 | var has_friends = (count !== 0) | |
89 | callback(null, has_friends) | |
90 | }) | |
91 | } | |
92 | ||
93 | function makeFriends (callback) { | |
94 | var videos = require('./videos') | |
95 | var pods_score = {} | |
96 | ||
97 | logger.info('Make friends!') | |
98 | fs.readFile(utils.getCertDir() + 'peertube.pub', 'utf8', function (err, cert) { | |
99 | if (err) { | |
100 | logger.error('Cannot read public cert.', { error: err }) | |
101 | return callback(err) | |
102 | } | |
103 | ||
104 | var urls = config.get('network.friends') | |
105 | ||
106 | async.each(urls, computeForeignPodsList, function () { | |
107 | logger.debug('Pods scores computed.', { pods_score: pods_score }) | |
108 | var pods_list = computeWinningPods(urls, pods_score) | |
109 | logger.debug('Pods that we keep computed.', { pods_to_keep: pods_list }) | |
110 | ||
111 | makeRequestsToWinningPods(cert, pods_list) | |
112 | }) | |
113 | }) | |
114 | ||
115 | // ----------------------------------------------------------------------- | |
116 | ||
117 | function computeForeignPodsList (url, callback) { | |
118 | // Let's give 1 point to the pod we ask the friends list | |
119 | pods_score[url] = 1 | |
120 | ||
121 | getForeignPodsList(url, function (foreign_pods_list) { | |
122 | if (foreign_pods_list.length === 0) return callback() | |
123 | ||
124 | async.each(foreign_pods_list, function (foreign_pod, callback_each) { | |
125 | var foreign_url = foreign_pod.url | |
126 | ||
127 | if (pods_score[foreign_url]) pods_score[foreign_url]++ | |
128 | else pods_score[foreign_url] = 1 | |
129 | ||
130 | callback_each() | |
131 | }, function () { | |
132 | callback() | |
133 | }) | |
134 | }) | |
135 | } | |
136 | ||
137 | function computeWinningPods (urls, pods_score) { | |
138 | // Build the list of pods to add | |
139 | // Only add a pod if it exists in more than a half base pods | |
140 | var pods_list = [] | |
141 | var base_score = urls.length / 2 | |
142 | Object.keys(pods_score).forEach(function (pod) { | |
143 | if (pods_score[pod] > base_score) pods_list.push({ url: pod }) | |
144 | }) | |
145 | ||
146 | return pods_list | |
147 | } | |
148 | ||
149 | function makeRequestsToWinningPods (cert, pods_list) { | |
150 | // Stop pool requests | |
151 | poolRequests.deactivate() | |
152 | // Flush pool requests | |
153 | poolRequests.forceSend() | |
154 | ||
155 | // Get the list of our videos to send to our new friends | |
156 | videos.listOwned(function (err, videos_list) { | |
157 | if (err) throw err | |
158 | ||
159 | var data = { | |
160 | url: http + '://' + host + ':' + port, | |
161 | publicKey: cert, | |
162 | videos: videos_list | |
163 | } | |
164 | ||
165 | utils.makeMultipleRetryRequest( | |
166 | { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data }, | |
167 | ||
168 | pods_list, | |
169 | ||
170 | function eachRequest (err, response, body, url, pod, callback_each_request) { | |
171 | // We add the pod if it responded correctly with its public certificate | |
172 | if (!err && response.statusCode === 200) { | |
173 | add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) { | |
174 | if (err) logger.error('Error with adding %s pod.', pod.url, { error: err }) | |
175 | ||
176 | videos.addRemotes(body.videos, function (err) { | |
177 | if (err) logger.error('Error with adding videos of pod.', pod.url, { error: err }) | |
178 | ||
179 | logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos }) | |
180 | return callback_each_request() | |
181 | }) | |
182 | }) | |
183 | } else { | |
184 | logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') }) | |
185 | return callback_each_request() | |
186 | } | |
187 | }, | |
188 | ||
189 | function endRequests (err) { | |
190 | // Now we made new friends, we can re activate the pool of requests | |
191 | poolRequests.activate() | |
192 | ||
193 | if (err) { | |
194 | logger.error('There was some errors when we wanted to make friends.', { error: err }) | |
195 | return callback(err) | |
196 | } | |
197 | ||
198 | logger.debug('makeRequestsToWinningPods finished.') | |
199 | return callback(null) | |
200 | } | |
201 | ) | |
202 | }) | |
203 | } | |
204 | } | |
205 | ||
206 | function quitFriends (callback) { | |
207 | // Stop pool requests | |
208 | poolRequests.deactivate() | |
209 | // Flush pool requests | |
210 | poolRequests.forceSend() | |
211 | ||
212 | PodsDB.find(function (err, pods) { | |
213 | if (err) return callback(err) | |
214 | ||
215 | var request = { | |
216 | method: 'POST', | |
217 | path: '/api/' + constants.API_VERSION + '/pods/remove', | |
218 | sign: true, | |
219 | encrypt: true, | |
220 | data: { | |
221 | url: 'me' // Fake data | |
222 | } | |
223 | } | |
224 | ||
225 | // Announce we quit them | |
226 | utils.makeMultipleRetryRequest(request, pods, function () { | |
227 | PodsDB.remove(function (err) { | |
228 | poolRequests.activate() | |
229 | ||
230 | if (err) return callback(err) | |
231 | ||
232 | logger.info('Broke friends, so sad :(') | |
233 | ||
234 | var videos = require('./videos') | |
235 | videos.removeAllRemotes(function (err) { | |
236 | if (err) return callback(err) | |
237 | ||
238 | logger.info('Removed all remote videos.') | |
239 | callback(null) | |
240 | }) | |
241 | }) | |
242 | }) | |
243 | }) | |
244 | } | |
245 | ||
246 | function remove (url, callback) { | |
247 | var videos = require('./videos') | |
248 | logger.info('Removing %s pod.', url) | |
249 | ||
250 | videos.removeAllRemotesOf(url, function (err) { | |
251 | if (err) logger.error('Cannot remove all remote videos of %s.', url) | |
252 | ||
253 | PodsDB.remove({ url: url }, function (err) { | |
254 | if (err) return callback(err) | |
255 | ||
256 | logger.info('%s pod removed.', url) | |
257 | callback(null) | |
258 | }) | |
259 | }) | |
260 | } | |
261 | ||
262 | function removeVideoToFriends (video) { | |
263 | // To avoid duplicates | |
264 | var id = video.name + video.magnetUri | |
265 | poolRequests.addToPoolRequests(id, 'remove', video) | |
266 | } | |
267 | ||
268 | // --------------------------------------------------------------------------- | |
269 | ||
270 | module.exports = pods | |
271 | ||
272 | // --------------------------------------------------------------------------- | |
273 | ||
274 | function getForeignPodsList (url, callback) { | |
275 | var path = '/api/' + constants.API_VERSION + '/pods' | |
276 | ||
277 | request.get(url + path, function (err, response, body) { | |
278 | if (err) throw err | |
279 | callback(JSON.parse(body)) | |
280 | }) | |
281 | } | |
282 | })() |