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