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