import MoneroConnectionManagerListener from "./MoneroConnectionManagerListener"; import MoneroRpcConnection from "./MoneroRpcConnection"; /** *

Manages a collection of prioritized connections to daemon or wallet RPC endpoints.

* *

Example usage:

* * * // imports
* import { MoneroRpcConnection, MoneroConnectionManager, MoneroConnectionManagerListener } from "monero-ts";
*
* // create connection manager
* let connectionManager = new MoneroConnectionManager();
*
* // add managed connections with priorities
* await connectionManager.addConnection({uri: "http://localhost:38081", priority: 1}); // use localhost as first priority
* await connectionManager.addConnection({uri: "http://example.com"}); // default priority is prioritized last
*
* // set current connection
* await connectionManager.setConnection({uri: "http://foo.bar", username: "admin", password: "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().getIsOnline());
* console.log("Connection is authenticated: " + connectionManager.getConnection().getIsAuthenticated());
*
* // receive notifications of any changes to current connection
* connectionManager.addListener(new class extends MoneroConnectionManagerListener {
*    async onConnectionChanged(connection) {
*      console.log("Connection changed to: " + connection);
*    }
* });
*
* // start polling for best connection every 10 seconds and automatically switch
* connectionManager.startPolling(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(); *
*/ export default class MoneroConnectionManager { static DEFAULT_TIMEOUT: number; static DEFAULT_POLL_PERIOD: number; static DEFAULT_AUTO_SWITCH: boolean; static MIN_BETTER_RESPONSES: number; protected proxyToWorker: any; protected timeoutMs: any; protected autoSwitch: any; protected connections: any; protected responseTimes: any; protected listeners: any; protected currentConnection: any; protected poller: any; /** * Specify behavior when polling. * * One of PRIORITIZED (poll connections in order of priority until connected; default), CURRENT (poll current connection), or ALL (poll all connections). */ static PollType: { PRIORITIZED: number; CURRENT: number; ALL: number; }; /** * Construct a connection manager. * * @param {boolean} [proxyToWorker] - configure all connections to proxy to worker (default true) */ constructor(proxyToWorker?: boolean); /** * 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; /** * Get all listeners. * * @return {MoneroConnectionManagerListener[]} all listeners */ getListeners(): MoneroConnectionManagerListener[]; /** * Add a connection. The connection may have an elevated priority for this manager to use. * * @param {string|Partial} uriOrConnection - uri or connection to add * @return {Promise} this connection manager for chaining */ addConnection(uriOrConnection: string | Partial): Promise; /** * Remove a connection. * * @param {string} uri - of the the connection to remove * @return {Promise} this connection manager for chaining */ removeConnection(uri: string): 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 replace existing connection with the same URI. * Notify if current connection changes. * Does not check the connection. * * @param {string|Partial} [uriOrConnection] - is the uri of the connection or the connection to make current (default undefined for no current connection) * @return {Promise} this connection manager for chaining */ setConnection(uriOrConnection?: string | Partial): Promise; /** * Get the current connection. * * @return {MoneroRpcConnection} the current connection or undefined if no connection set */ getConnection(): MoneroRpcConnection; /** * Indicates if this manager has a connection with the given URI. * * @param {string} uri URI of the connection to check * @return {boolean} true if this manager has a connection with the given URI, false otherwise */ hasConnection(uri: string): boolean; /** * 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[]; /** * Indicates if the connection manager is connected to a node. * * @return {boolean|undefined} true if the current connection is set, online, and not unauthenticated, undefined if unknown, false otherwise */ isConnected(): boolean | undefined; /** * Start polling connections. * * @param {number} [periodMs] poll period in milliseconds (default 20s) * @param {boolean} [autoSwitch] specifies to automatically switch to the best connection (default true unless changed) * @param {number} [timeoutMs] specifies the timeout to poll a single connection (default 5s unless changed) * @param {number} [pollType] one of PRIORITIZED (poll connections in order of priority until connected; default), CURRENT (poll current connection), or ALL (poll all connections) * @param {MoneroRpcConnection[]} [excludedConnections] connections excluded from being polled * @return {MoneroConnectionManager} this connection manager for chaining */ startPolling(periodMs?: number, autoSwitch?: boolean, timeoutMs?: number, pollType?: number, excludedConnections?: MoneroRpcConnection[]): MoneroConnectionManager; /** * Stop polling connections. * * @return {MoneroConnectionManager} this connection manager for chaining */ stopPolling(): 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[]; /** * 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; /** * Automatically switch to the best available connection as connections are polled, based on priority, response time, and consistency. * * @param {boolean} autoSwitch specifies if the connection should auto switch to a better connection * @return {MoneroConnectionManager} this connection manager for chaining */ setAutoSwitch(autoSwitch: boolean): MoneroConnectionManager; /** * 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 {number} timeoutMs - the timeout before the connection is considered offline * @return {MoneroConnectionManager} this connection manager for chaining */ setTimeout(timeoutMs: number): MoneroConnectionManager; /** * Get the request timeout. * * @return {number} the request timeout before a connection is considered offline */ getTimeout(): number; /** * Collect connectable peers of the managed connections. * * @return {Promise} connectable peers */ getPeerConnections(): Promise; /** * Disconnect from the current connection. * * @return {Promise} this connection manager for chaining */ disconnect(): Promise; /** * Remove all connections. * * @return {Promise} this connection manager for chaining */ clear(): Promise; /** * Reset to default state. * * @return {MoneroConnectionManager} this connection manager for chaining */ reset(): MoneroConnectionManager; protected onConnectionChanged(connection: any): Promise; protected getConnectionsInAscendingPriority(): any[]; protected compareConnections(c1: any, c2: any): any; protected comparePriorities(p1: any, p2: any): number; protected startPollingConnection(periodMs: any): this; protected startPollingConnections(periodMs: any): this; protected startPollingPrioritizedConnections(periodMs: any, excludedConnections: any): this; checkPrioritizedConnections(excludedConnections: any): Promise; protected checkConnectionsAux(connections: any, excludedConnections?: any): Promise; protected processResponses(responses: any): Promise; protected updateBestConnectionInPriority(): Promise; /** * Get the best connection from the given responses. * * @param {MoneroRpcConnection[]} responses connection responses to update from * @return {MoneroRpcConnection} the best response among the given responses or undefined if none are best */ protected getBestConnectionFromPrioritizedResponses(responses: any): Promise; }