aboutsummaryrefslogtreecommitdiffhomepage
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/friends.js198
1 files changed, 102 insertions, 96 deletions
diff --git a/lib/friends.js b/lib/friends.js
index 8cc1a3151..006a64404 100644
--- a/lib/friends.js
+++ b/lib/friends.js
@@ -54,110 +54,18 @@ function makeFriends (callback) {
54 54
55 var urls = config.get('network.friends') 55 var urls = config.get('network.friends')
56 56
57 async.each(urls, computeForeignPodsList, function (err) { 57 async.each(urls, function (url, callback) {
58 computeForeignPodsList(url, pods_score, callback)
59 }, function (err) {
58 if (err) return callback(err) 60 if (err) return callback(err)
59 61
60 logger.debug('Pods scores computed.', { pods_score: pods_score }) 62 logger.debug('Pods scores computed.', { pods_score: pods_score })
61 var pods_list = computeWinningPods(urls, pods_score) 63 var pods_list = computeWinningPods(urls, pods_score)
62 logger.debug('Pods that we keep computed.', { pods_to_keep: pods_list }) 64 logger.debug('Pods that we keep computed.', { pods_to_keep: pods_list })
63 65
64 makeRequestsToWinningPods(cert, pods_list) 66 makeRequestsToWinningPods(cert, pods_list, callback)
65 }) 67 })
66 }) 68 })
67
68 // -----------------------------------------------------------------------
69
70 function computeForeignPodsList (url, callback) {
71 // Let's give 1 point to the pod we ask the friends list
72 pods_score[url] = 1
73
74 getForeignPodsList(url, function (err, foreign_pods_list) {
75 if (err) return callback(err)
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) {
112 logger.error('Cannot get the list of videos we own.')
113 return callback(err)
114 }
115
116 var data = {
117 url: http + '://' + host + ':' + port,
118 publicKey: cert,
119 videos: videos_list
120 }
121
122 requests.makeMultipleRetryRequest(
123 { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data },
124
125 pods_list,
126
127 function eachRequest (err, response, body, url, pod, callback_each_request) {
128 // We add the pod if it responded correctly with its public certificate
129 if (!err && response.statusCode === 200) {
130 Pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) {
131 if (err) logger.error('Error with adding %s pod.', pod.url, { error: err })
132
133 Videos.addRemotes(body.videos, function (err) {
134 if (err) logger.error('Error with adding videos of pod.', pod.url, { error: err })
135
136 logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos })
137 return callback_each_request()
138 })
139 })
140 } else {
141 logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') })
142 return callback_each_request()
143 }
144 },
145
146 function endRequests (err) {
147 // Now we made new friends, we can re activate the pool of requests
148 poolRequests.activate()
149
150 if (err) {
151 logger.error('There was some errors when we wanted to make friends.')
152 return callback(err)
153 }
154
155 logger.debug('makeRequestsToWinningPods finished.')
156 return callback(null)
157 }
158 )
159 })
160 }
161} 69}
162 70
163function quitFriends (callback) { 71function quitFriends (callback) {
@@ -211,6 +119,39 @@ module.exports = pods
211 119
212// --------------------------------------------------------------------------- 120// ---------------------------------------------------------------------------
213 121
122function computeForeignPodsList (url, pods_score, callback) {
123 // Let's give 1 point to the pod we ask the friends list
124 pods_score[url] = 1
125
126 getForeignPodsList(url, function (err, foreign_pods_list) {
127 if (err) return callback(err)
128 if (foreign_pods_list.length === 0) return callback()
129
130 async.each(foreign_pods_list, function (foreign_pod, callback_each) {
131 var foreign_url = foreign_pod.url
132
133 if (pods_score[foreign_url]) pods_score[foreign_url]++
134 else pods_score[foreign_url] = 1
135
136 callback_each()
137 }, function () {
138 callback()
139 })
140 })
141}
142
143function computeWinningPods (urls, pods_score) {
144 // Build the list of pods to add
145 // Only add a pod if it exists in more than a half base pods
146 var pods_list = []
147 var base_score = urls.length / 2
148 Object.keys(pods_score).forEach(function (pod) {
149 if (pods_score[pod] > base_score) pods_list.push({ url: pod })
150 })
151
152 return pods_list
153}
154
214function getForeignPodsList (url, callback) { 155function getForeignPodsList (url, callback) {
215 var path = '/api/' + constants.API_VERSION + '/pods' 156 var path = '/api/' + constants.API_VERSION + '/pods'
216 157
@@ -220,3 +161,68 @@ function getForeignPodsList (url, callback) {
220 callback(null, JSON.parse(body)) 161 callback(null, JSON.parse(body))
221 }) 162 })
222} 163}
164
165function makeRequestsToWinningPods (cert, pods_list, callback) {
166 // Stop pool requests
167 poolRequests.deactivate()
168 // Flush pool requests
169 poolRequests.forceSend()
170
171 // Get the list of our videos to send to our new friends
172 Videos.listOwned(function (err, videos_list) {
173 if (err) {
174 logger.error('Cannot get the list of videos we own.')
175 return callback(err)
176 }
177
178 var data = {
179 url: http + '://' + host + ':' + port,
180 publicKey: cert,
181 videos: videos_list
182 }
183
184 requests.makeMultipleRetryRequest(
185 { method: 'POST', path: '/api/' + constants.API_VERSION + '/pods/', data: data },
186
187 pods_list,
188
189 function eachRequest (err, response, body, url, pod, callback_each_request) {
190 // We add the pod if it responded correctly with its public certificate
191 if (!err && response.statusCode === 200) {
192 Pods.add({ url: pod.url, publicKey: body.cert, score: constants.FRIEND_BASE_SCORE }, function (err) {
193 if (err) {
194 logger.error('Error with adding %s pod.', pod.url, { error: err })
195 return callback_each_request()
196 }
197
198 Videos.addRemotes(body.videos, function (err) {
199 if (err) {
200 logger.error('Error with adding videos of pod.', pod.url, { error: err })
201 return callback_each_request()
202 }
203
204 logger.debug('Adding remote videos from %s.', pod.url, { videos: body.videos })
205 return callback_each_request()
206 })
207 })
208 } else {
209 logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') })
210 return callback_each_request()
211 }
212 },
213
214 function endRequests (err) {
215 // Now we made new friends, we can re activate the pool of requests
216 poolRequests.activate()
217
218 if (err) {
219 logger.error('There was some errors when we wanted to make friends.')
220 return callback(err)
221 }
222
223 logger.debug('makeRequestsToWinningPods finished.')
224 return callback(null)
225 }
226 )
227 })
228}