2016-11-22 21:19:08 +01:00
|
|
|
import lbry from './lbry.js';
|
|
|
|
|
|
|
|
var lighthouse = {
|
2016-12-29 10:57:07 +01:00
|
|
|
_query_timeout: 5000,
|
|
|
|
_max_query_tries: 5,
|
2016-11-11 14:17:43 +01:00
|
|
|
|
2016-08-18 09:14:38 +02:00
|
|
|
servers: [
|
2016-12-29 02:59:30 +01:00
|
|
|
'http://lighthouse4.lbry.io:50005',
|
|
|
|
'http://lighthouse5.lbry.io:50005',
|
|
|
|
'http://lighthouse6.lbry.io:50005',
|
2016-08-18 09:14:38 +02:00
|
|
|
],
|
|
|
|
path: '/',
|
|
|
|
|
2016-11-11 14:17:43 +01:00
|
|
|
call: function(method, params, callback, errorCallback, connectFailedCallback, timeout) {
|
2016-12-29 10:57:07 +01:00
|
|
|
const handleConnectFailed = function(tryNum=0) {
|
|
|
|
if (tryNum > lighthouse._max_query_tries) {
|
2016-12-08 00:00:31 +01:00
|
|
|
if (connectFailedCallback) {
|
|
|
|
connectFailedCallback();
|
|
|
|
} else {
|
|
|
|
throw new Error(`Could not connect to Lighthouse server. Last server attempted: ${lighthouse.server}`);
|
|
|
|
}
|
2016-11-11 14:17:43 +01:00
|
|
|
} else {
|
2016-12-29 10:57:07 +01:00
|
|
|
lbry.call(method, params, callback, errorCallback, () => { handleConnectFailed(tryNum + 1) }, timeout);
|
2016-11-11 14:17:43 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-29 10:57:07 +01:00
|
|
|
// Set the Lighthouse server if it hasn't been set yet, or if the current server is not in
|
|
|
|
// the current set of servers (most likely because of a settings change).
|
|
|
|
if (typeof lighthouse.server === 'undefined' || lighthouse.getServers().indexOf(lighthouse.server) == -1) {
|
|
|
|
lighthouse.chooseNewServer();
|
|
|
|
}
|
|
|
|
|
|
|
|
lbry.jsonrpc_call(this.server + this.path, method, params, callback, errorCallback,
|
|
|
|
() => { handleConnectFailed() }, timeout || lighthouse.query_timeout);
|
|
|
|
},
|
|
|
|
|
|
|
|
getServers: function() {
|
|
|
|
return lbry.getClientSetting('useCustomLighthouseServers')
|
|
|
|
? lbry.getClientSetting('customLighthouseServers')
|
|
|
|
: lighthouse.servers;
|
|
|
|
},
|
|
|
|
|
|
|
|
chooseNewServer: function() {
|
|
|
|
// Randomly choose a new Lighthouse server and switch to it. If a server is already set, this
|
|
|
|
// will choose a different one (used for switching servers after a failed query).
|
|
|
|
const servers = lighthouse.getServers();
|
|
|
|
let newServerChoices;
|
|
|
|
if (!lighthouse.server) {
|
|
|
|
newServerChoices = servers;
|
|
|
|
} else {
|
|
|
|
newServerChoices = lighthouse.getServers().slice();
|
|
|
|
newServerChoices.splice(newServerChoices.indexOf(lighthouse.server), 1);
|
|
|
|
}
|
|
|
|
lighthouse.server = newServerChoices[Math.round(Math.random() * (newServerChoices.length - 1))];
|
|
|
|
},
|
|
|
|
|
|
|
|
search: function(query, callback, errorCallback, connectFailedCallback, timeout) {
|
|
|
|
lighthouse.call('search', [query], callback, errorCallback, connectFailedCallback, timeout);
|
2016-12-09 10:57:13 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
getSizeForName: function(name, callback, errorCallback, connectFailedCallback, timeout) {
|
2016-12-29 10:57:07 +01:00
|
|
|
lighthouse.call('get_size_for_name', [name], callback, errorCallback, connectFailedCallback, timeout);
|
2016-11-11 14:17:43 +01:00
|
|
|
}
|
2016-08-18 09:14:38 +02:00
|
|
|
};
|
|
|
|
|
2016-11-22 21:19:08 +01:00
|
|
|
|
|
|
|
export default lighthouse;
|