2017-06-20 14:08:52 +02:00
|
|
|
import lbry from "./lbry.js";
|
|
|
|
import jsonrpc from "./jsonrpc.js";
|
2016-11-22 21:19:08 +01:00
|
|
|
|
2017-04-13 21:37:41 +02:00
|
|
|
const queryTimeout = 3000;
|
|
|
|
const maxQueryTries = 2;
|
2017-03-08 07:23:07 +01:00
|
|
|
const defaultServers = [
|
2017-06-20 14:08:52 +02:00
|
|
|
"http://lighthouse7.lbry.io:50005",
|
|
|
|
"http://lighthouse8.lbry.io:50005",
|
|
|
|
"http://lighthouse9.lbry.io:50005",
|
2017-03-08 07:23:07 +01:00
|
|
|
];
|
2017-06-20 14:08:52 +02:00
|
|
|
const path = "/";
|
2016-11-11 14:17:43 +01:00
|
|
|
|
2017-03-08 07:23:07 +01:00
|
|
|
let server = null;
|
|
|
|
let connectTryNum = 0;
|
2016-08-18 09:14:38 +02:00
|
|
|
|
2017-03-08 07:23:07 +01:00
|
|
|
function getServers() {
|
2017-09-23 01:23:51 +02:00
|
|
|
return defaultServers;
|
2017-03-08 07:23:07 +01:00
|
|
|
}
|
2016-12-29 10:57:07 +01:00
|
|
|
|
2017-03-08 07:23:07 +01:00
|
|
|
function call(method, params, callback, errorCallback) {
|
2017-06-20 14:08:52 +02:00
|
|
|
if (connectTryNum >= maxQueryTries) {
|
|
|
|
errorCallback(
|
|
|
|
new Error(
|
|
|
|
__(
|
|
|
|
`Could not connect to Lighthouse server. Last server attempted: %s`,
|
|
|
|
server
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
2016-12-09 10:57:13 +01:00
|
|
|
|
2017-06-20 14:08:52 +02:00
|
|
|
/**
|
2017-03-08 07:23:07 +01:00
|
|
|
* Set the Lighthouse server if it hasn't been set yet, if the current server is not in current
|
|
|
|
* set of servers (most likely because of a settings change), or we're re-trying after a failed
|
|
|
|
* query.
|
|
|
|
*/
|
2017-06-20 14:08:52 +02:00
|
|
|
if (!server || !getServers().includes(server) || connectTryNum > 0) {
|
|
|
|
// If there's a current server, filter it out so we get a new one
|
|
|
|
const newServerChoices = server
|
|
|
|
? getServers().filter(s => s != server)
|
|
|
|
: getServers();
|
|
|
|
server =
|
|
|
|
newServerChoices[
|
|
|
|
Math.round(Math.random() * (newServerChoices.length - 1))
|
|
|
|
];
|
|
|
|
}
|
2017-06-06 06:21:55 +02:00
|
|
|
|
2017-06-20 14:08:52 +02:00
|
|
|
jsonrpc.call(
|
|
|
|
server + path,
|
|
|
|
method,
|
|
|
|
params,
|
|
|
|
response => {
|
|
|
|
connectTryNum = 0;
|
|
|
|
callback(response);
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
connectTryNum = 0;
|
|
|
|
errorCallback(error);
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
connectTryNum++;
|
|
|
|
call(method, params, callback, errorCallback);
|
|
|
|
},
|
|
|
|
queryTimeout
|
|
|
|
);
|
2017-03-08 07:23:07 +01:00
|
|
|
}
|
|
|
|
|
2017-06-06 06:21:55 +02:00
|
|
|
const lighthouse = new Proxy(
|
2017-06-20 14:08:52 +02:00
|
|
|
{},
|
|
|
|
{
|
|
|
|
get: function(target, name) {
|
|
|
|
return function(...params) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
call(name, params, resolve, reject);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
},
|
|
|
|
}
|
2017-06-06 06:21:55 +02:00
|
|
|
);
|
2016-11-22 21:19:08 +01:00
|
|
|
|
|
|
|
export default lighthouse;
|