export = MoneroConnectionManager; /** *
Manages a collection of prioritized connections to daemon or wallet RPC endpoints.
* *Example usage:
* *
* // imports
* const monerojs = require("monero-javascript");
* const MoneroRpcConnection = monerojs.MoneroRpcConnection;
* const MoneroConnectionManager = monerojs.MoneroConnectionManager;
* const MoneroConnectionManagerListener = monerojs.MoneroConnectionManagerListener;
*
* // create connection manager
* let connectionManager = new MoneroConnectionManager();
*
* // add managed connections with priorities
* connectionManager.addConnection(new MoneroRpcConnection("http://localhost:38081").setPriority(1)); // use localhost as first priority
* connectionManager.addConnection(new MoneroRpcConnection("http://example.com")); // default priority is prioritized last
*
* // set current connection
* connectionManager.setConnection(new MoneroRpcConnection("http://foo.bar", "admin", "password")); // connection is added if new
*
* // check connection status
* await connectionManager.checkConnection();
* console.log("Connection manager is connected: " + connectionManager.isConnected());
* console.log("Connection is online: " + connectionManager.getConnection().isOnline());
* console.log("Connection is authenticated: " + connectionManager.getConnection().isAuthenticated());
*
* // receive notifications of any changes to current connection
* connectionManager.addListener(new class extends MoneroConnectionManagerListener {
* onConnectionChanged(connection) {
* console.log("Connection changed to: " + connection);
* }
* });
*
* // check connection status every 10 seconds
* await connectionManager.startCheckingConnection(10000);
*
* // automatically switch to best available connection if disconnected
* connectionManager.setAutoSwitch(true);
*
* // get best available connection in order of priority then response time
* let bestConnection = await connectionManager.getBestAvailableConnection();
*
* // check status of all connections
* await connectionManager.checkConnections();
*
* // get connections in order of current connection, online status from last check, priority, and name
* let connections = connectionManager.getConnections();
*
* // clear connection manager
* connectionManager.clear();
*
*/
declare class MoneroConnectionManager {
/**
* Construct a connection manager.
*
* @param {boolean} proxyToWorker - configure all connections to proxy to worker (default true)
*/
constructor(proxyToWorker: boolean);
_proxyToWorker: boolean;
_timeoutInMs: number;
_connections: any[];
_listeners: any[];
/**
* Add a listener to receive notifications when the connection changes.
*
* @param {MoneroConnectionManagerListener} listener - the listener to add
* @return {MoneroConnectionManager} this connection manager for chaining
*/
addListener(listener: MoneroConnectionManagerListener): MoneroConnectionManager;
/**
* Remove a listener.
*
* @param {MoneroConnectionManagerListener} listener - the listener to remove
* @return {MoneroConnectionManager} this connection manager for chaining
*/
removeListener(listener: MoneroConnectionManagerListener): MoneroConnectionManager;
/**
* Remove all listeners.
*
* @return {MoneroConnectionManager} this connection manager for chaining
*/
removeListeners(): MoneroConnectionManager;
/**
* Add a connection. The connection may have an elevated priority for this manager to use.
*
* @param {MoneroRpcConnection} connection - the connection to add
* @return {Promise} this connection manager for chaining
*/
addConnection(connection: MoneroRpcConnection): Promise;
/**
* Remove a connection.
*
* @param {string} uri - of the the connection to remove
* @return {Promise} this connection manager for chaining
*/
removeConnection(uri: string): Promise;
_currentConnection: any;
/**
* Indicates if the connection manager is connected to a node.
*
* @return {boolean} true if the current connection is set, online, and not unauthenticated. false otherwise
*/
isConnected(): boolean;
/**
* Get the current connection.
*
* @return {MoneroRpcConnection} the current connection or undefined if no connection set
*/
getConnection(): MoneroRpcConnection;
/**
* Get a connection by URI.
*
* @param {string} uri is the URI of the connection to get
* @return {MoneroRpcConnection} the connection with the URI or undefined if no connection with the URI exists
*/
getConnectionByUri(uri: string): MoneroRpcConnection;
/**
* Get all connections in order of current connection (if applicable), online status, priority, and name.
*
* @return {MoneroRpcConnection[]} the list of sorted connections
*/
getConnections(): MoneroRpcConnection[];
/**
* Get the best available connection in order of priority then response time.
*
* @param {MoneroRpcConnection[]} excludedConnections - connections to be excluded from consideration (optional)
* @return {Promise} the best available connection in order of priority then response time, undefined if no connections available
*/
getBestAvailableConnection(excludedConnections: MoneroRpcConnection[]): Promise;
/**
* Set the current connection.
* Provide a URI to select an existing connection without updating its credentials.
* Provide a MoneroRpcConnection to add new connection or update credentials of existing connection with same URI.
* Notify if current connection changes.
* Does not check the connection.
*
* @param {string|MoneroRpcConnection} uriOrConnection - is the uri of the connection or the connection to make current (default undefined for no current connection)
* @return {MoneroConnectionManager} this connection manager for chaining
*/
setConnection(uriOrConnection: string | MoneroRpcConnection): MoneroConnectionManager;
/**
* Check the current connection. If disconnected and auto switch enabled, switches to best available connection.
*
* @return {Promise} this connection manager for chaining
*/
checkConnection(): Promise;
/**
* Check all managed connections.
*
* @return {Promise} this connection manager for chaining
*/
checkConnections(): Promise;
/**
* Check all managed connections, returning a promise for each connection check.
* Does not auto switch if disconnected.
*
* @return {Promise[]} a promise for each connection in the order of getConnections().
*/
checkConnectionPromises(): Promise[];
/**
* Check the connection and start checking the connection periodically.
*
* @param {number} periodMs is the time between checks in milliseconds (default 10000 or 10 seconds)
* @return {Promise} this connection manager for chaining (after first checking the connection)
*/
startCheckingConnection(periodMs: number): Promise;
_checkLooper: TaskLooper;
/**
* Stop checking the connection status periodically.
*
* @return {MoneroConnectionManager} this connection manager for chaining
*/
stopCheckingConnection(): MoneroConnectionManager;
/**
* Automatically switch to best available connection if current connection is disconnected after being checked.
*
* @param {boolean} autoSwitch specifies if the connection should switch on disconnect
* @return {MoneroConnectionManager} this connection manager for chaining
*/
setAutoSwitch(autoSwitch: boolean): MoneroConnectionManager;
_autoSwitch: boolean;
/**
* Get if auto switch is enabled or disabled.
*
* @return {boolean} true if auto switch enabled, false otherwise
*/
getAutoSwitch(): boolean;
/**
* Set the maximum request time before its connection is considered offline.
*
* @param {int} timeoutInMs - the timeout before the connection is considered offline
* @return {MoneroConnectionManager} this connection manager for chaining
*/
setTimeout(timeoutInMs: int): MoneroConnectionManager;
/**
* Get the request timeout.
*
* @return {int} the request timeout before a connection is considered offline
*/
getTimeout(): int;
/**
* Collect connectable peers of the managed connections.
*
* @return {MoneroRpcConnection[]} connectable peers
*/
getPeerConnections(): MoneroRpcConnection[];
/**
* Disconnect from the current connection.
*
* @return {MoneroConnectionManager} this connection manager for chaining
*/
disconnect(): MoneroConnectionManager;
/**
* Remove all connections.
*
* @return {MoneroConnectonManager} this connection manager for chaining
*/
clear(): MoneroConnectonManager;
/**
* Reset to default state.
*
* @return {MoneroConnectonManager} this connection manager for chaining
*/
reset(): MoneroConnectonManager;
_timeoutMs: number;
_onConnectionChanged(connection: any): Promise;
_getConnectionsInAscendingPriority(): any[];
_compareConnections(c1: any, c2: any): any;
}
declare namespace MoneroConnectionManager {
const DEFAULT_TIMEOUT: number;
const DEFAULT_CHECK_CONNECTION_PERIOD: number;
}
import MoneroRpcConnection = require("./MoneroRpcConnection");
import TaskLooper = require("./TaskLooper");