]>
git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - src/pods.js
4 var async
= require('async')
5 var config
= require('config')
7 var request
= require('request')
9 var logger
= require('./logger')
10 var PodsDB
= require('./database').PodsDB
11 var poolRequests
= require('./poolRequests')
12 var utils
= require('./utils')
16 var http
= config
.get('webserver.https') ? 'https' : 'http'
17 var host
= config
.get('webserver.host')
18 var port
= config
.get('webserver.port')
20 // ----------- Private functions -----------
22 function getForeignPodsList (url
, callback
) {
23 var path
= '/api/' + global
.API_VERSION
+ '/pods'
25 request
.get(url
+ path
, function (err
, response
, body
) {
27 callback(JSON
.parse(body
))
31 // ----------- Public functions -----------
33 pods
.list = function (callback
) {
34 PodsDB
.find(function (err
, pods_list
) {
36 logger
.error('Cannot get the list of the pods.', { error: err
})
40 return callback(null, pods_list
)
45 pods
.add = function (data
, callback
) {
46 logger
.info('Adding pod: %s', data
.url
)
50 publicKey: data
.publicKey
,
51 score: global
.FRIEND_BASE_SCORE
54 PodsDB
.create(params
, function (err
, pod
) {
56 logger
.error('Cannot insert the pod.', { error: err
})
60 fs
.readFile(utils
.certDir
+ 'peertube.pub', 'utf8', function (err
, cert
) {
62 logger
.error('Cannot read cert file.', { error: err
})
66 return callback(null, { cert: cert
})
71 pods
.addVideoToFriends = function (video
) {
72 // To avoid duplicates
73 var id
= video
.name
+ video
.magnetUri
74 poolRequests
.addToPoolRequests(id
, 'add', video
)
77 pods
.removeVideoToFriends = function (video
) {
78 // To avoid duplicates
79 var id
= video
.name
+ video
.magnetUri
80 poolRequests
.addToPoolRequests(id
, 'remove', video
)
83 pods
.makeFriends = function (callback
) {
86 logger
.info('Make friends!')
87 fs
.readFile(utils
.certDir
+ 'peertube.pub', 'utf8', function (err
, cert
) {
89 logger
.error('Cannot read public cert.', { error: err
})
93 var urls
= config
.get('network.friends')
95 async
.each(urls
, computeForeignPodsList
, function () {
96 logger
.debug('Pods scores computed.', { pods_score: pods_score
})
97 var pods_list
= computeWinningPods(urls
, pods_score
)
98 logger
.debug('Pods that we keep computed.', { pods_to_keep: pods_list
})
100 logger
.debug('Make requests...')
101 makeRequestsToWinningPods(cert
, pods_list
)
105 // -----------------------------------------------------------------------
107 function computeForeignPodsList (url
, callback
) {
108 // Let's give 1 point to the pod we ask the friends list
111 getForeignPodsList(url
, function (foreign_pods_list
) {
112 if (foreign_pods_list
.length
=== 0) return callback()
114 async
.each(foreign_pods_list
, function (foreign_pod
, callback_each
) {
115 var foreign_url
= foreign_pod
.url
117 if (pods_score
[foreign_url
]) pods_score
[foreign_url
]++
118 else pods_score
[foreign_url
] = 1
127 function computeWinningPods (urls
, pods_score
) {
128 // Build the list of pods to add
129 // Only add a pod if it exists in more than a half base pods
131 var base_score
= urls
.length
/ 2
132 Object
.keys(pods_score
).forEach(function (pod
) {
133 if (pods_score
[pod
] > base_score
) pods_list
.push({ url: pod
})
139 function makeRequestsToWinningPods (cert
, pods_list
) {
141 url: http
+ '://' + host
+ ':' + port
,
145 utils
.makeMultipleRetryRequest(
146 { method: 'POST', path: '/api/' + global
.API_VERSION
+ '/pods/', data: data
},
150 function eachRequest (err
, response
, body
, url
, pod
, callback_each_request
) {
151 // We add the pod if it responded correctly with its public certificate
152 if (!err
&& response
.statusCode
=== 200) {
153 pods
.add({ url: pod
.url
, publicKey: body
.cert
, score: global
.FRIEND_BASE_SCORE
}, function (err
) {
155 logger
.error('Error with adding %s pod.', pod
.url
, { error: err
})
158 return callback_each_request()
161 logger
.error('Error with adding %s pod.', pod
.url
, { error: err
|| new Error('Status not 200') })
162 return callback_each_request()
166 function endRequests (err
) {
168 logger
.error('There was some errors when we wanted to make friends.', { error: err
})
172 logger
.debug('makeRequestsToWinningPods finished.')
173 return callback(null)
179 module
.exports
= pods