]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - src/pods.js
Logging refractoring
[github/Chocobozzz/PeerTube.git] / src / pods.js
CommitLineData
8c308c2b
C
1;(function () {
2 'use strict'
3
8c308c2b 4 var async = require('async')
a1860380
C
5 var config = require('config')
6 var fs = require('fs')
8c308c2b
C
7 var request = require('request')
8
9 var logger = require('./logger')
8c308c2b 10 var PodsDB = require('./database').PodsDB
0b697522 11 var poolRequests = require('./poolRequests')
a1860380 12 var utils = require('./utils')
8c308c2b
C
13
14 var pods = {}
a1860380 15
8c308c2b
C
16 var http = config.get('webserver.https') ? 'https' : 'http'
17 var host = config.get('webserver.host')
18 var port = config.get('webserver.port')
19
20 // ----------- Private functions -----------
21
22 function getForeignPodsList (url, callback) {
f5a60a51 23 var path = '/api/' + global.API_VERSION + '/pods'
8c308c2b
C
24
25 request.get(url + path, function (err, response, body) {
26 if (err) throw err
27 callback(JSON.parse(body))
28 })
29 }
30
31 // ----------- Public functions -----------
a1860380 32
8c308c2b
C
33 pods.list = function (callback) {
34 PodsDB.find(function (err, pods_list) {
35 if (err) {
36 logger.error('Cannot get the list of the pods.', { error: err })
37 return callback(err)
38 }
39
40 return callback(null, pods_list)
41 })
42 }
43
44 // { url }
45 pods.add = function (data, callback) {
46 logger.info('Adding pod: %s', data.url)
47
48 var params = {
49 url: data.url,
3bcb78b3
C
50 publicKey: data.publicKey,
51 score: global.FRIEND_BASE_SCORE
8c308c2b
C
52 }
53
54 PodsDB.create(params, function (err, pod) {
55 if (err) {
56 logger.error('Cannot insert the pod.', { error: err })
57 return callback(err)
58 }
59
d148f3b9 60 fs.readFile(utils.certDir + 'peertube.pub', 'utf8', function (err, cert) {
8c308c2b
C
61 if (err) {
62 logger.error('Cannot read cert file.', { error: err })
63 return callback(err)
64 }
65
66 return callback(null, { cert: cert })
67 })
68 })
69 }
70
0b697522
C
71 pods.addVideoToFriends = function (video) {
72 // To avoid duplicates
73 var id = video.name + video.magnetUri
74 poolRequests.addToPoolRequests(id, 'add', video)
75 }
3bcb78b3 76
0b697522
C
77 pods.removeVideoToFriends = function (video) {
78 // To avoid duplicates
79 var id = video.name + video.magnetUri
80 poolRequests.addToPoolRequests(id, 'remove', video)
8c308c2b
C
81 }
82
83 pods.makeFriends = function (callback) {
a1860380
C
84 var pods_score = {}
85
86 logger.info('Make friends!')
d148f3b9 87 fs.readFile(utils.certDir + 'peertube.pub', 'utf8', function (err, cert) {
8c308c2b
C
88 if (err) {
89 logger.error('Cannot read public cert.', { error: err })
90 return callback(err)
91 }
92
93 var urls = config.get('network.friends')
8c308c2b 94
a1860380
C
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 })
99
a1860380
C
100 makeRequestsToWinningPods(cert, pods_list)
101 })
102 })
103
104 // -----------------------------------------------------------------------
8c308c2b 105
a1860380 106 function computeForeignPodsList (url, callback) {
3bcb78b3
C
107 // Let's give 1 point to the pod we ask the friends list
108 pods_score[url] = 1
8c308c2b 109
a1860380
C
110 getForeignPodsList(url, function (foreign_pods_list) {
111 if (foreign_pods_list.length === 0) return callback()
112
113 async.each(foreign_pods_list, function (foreign_pod, callback_each) {
114 var foreign_url = foreign_pod.url
115
116 if (pods_score[foreign_url]) pods_score[foreign_url]++
117 else pods_score[foreign_url] = 1
118
119 callback_each()
120 }, function () {
121 callback()
8c308c2b 122 })
a1860380
C
123 })
124 }
8c308c2b 125
a1860380
C
126 function computeWinningPods (urls, pods_score) {
127 // Build the list of pods to add
128 // Only add a pod if it exists in more than a half base pods
129 var pods_list = []
130 var base_score = urls.length / 2
131 Object.keys(pods_score).forEach(function (pod) {
132 if (pods_score[pod] > base_score) pods_list.push({ url: pod })
133 })
8c308c2b 134
a1860380
C
135 return pods_list
136 }
8c308c2b 137
a1860380
C
138 function makeRequestsToWinningPods (cert, pods_list) {
139 var data = {
140 url: http + '://' + host + ':' + port,
141 publicKey: cert
142 }
8c308c2b 143
a1860380
C
144 utils.makeMultipleRetryRequest(
145 { method: 'POST', path: '/api/' + global.API_VERSION + '/pods/', data: data },
146
147 pods_list,
148
0b697522 149 function eachRequest (err, response, body, url, pod, callback_each_request) {
a1860380
C
150 // We add the pod if it responded correctly with its public certificate
151 if (!err && response.statusCode === 200) {
3bcb78b3 152 pods.add({ url: pod.url, publicKey: body.cert, score: global.FRIEND_BASE_SCORE }, function (err) {
a1860380 153 if (err) {
3bcb78b3 154 logger.error('Error with adding %s pod.', pod.url, { error: err })
a1860380 155 }
3bcb78b3
C
156
157 return callback_each_request()
a1860380
C
158 })
159 } else {
3bcb78b3
C
160 logger.error('Error with adding %s pod.', pod.url, { error: err || new Error('Status not 200') })
161 return callback_each_request()
8c308c2b 162 }
a1860380
C
163 },
164
165 function endRequests (err) {
166 if (err) {
167 logger.error('There was some errors when we wanted to make friends.', { error: err })
168 return callback(err)
169 }
170
171 logger.debug('makeRequestsToWinningPods finished.')
172 return callback(null)
173 }
174 )
175 }
8c308c2b
C
176 }
177
178 module.exports = pods
179})()