Package monero.common
Class MoneroConnectionManager
java.lang.Object
monero.common.MoneroConnectionManager
Manages a collection of prioritized connections to daemon or wallet RPC endpoints.
Example usage:
// create connection manager
MoneroConnectionManager 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
connectionManager.checkConnection();
System.out.println("Connection manager is connected: " + connectionManager.isConnected());
System.out.println("Connection is online: " + connectionManager.getConnection().isOnline());
System.out.println("Connection is authenticated: " + connectionManager.getConnection().isAuthenticated());
// receive notifications of any changes to current connection
connectionManager.addListener(new MoneroConnectionManagerListener() {
@Override
public void onConnectionChanged(MoneroRpcConnection connection) {
System.out.println("Connection changed to: " + connection);
}
});
// start polling for best connection every 10 seconds and automatically switch
connectionManager.startPolling(10000l);
// get best available connection in order of priority then response time
MoneroRpcConnection bestConnection = connectionManager.getBestAvailableConnection();
// check status of all connections
connectionManager.checkConnections();
// get connections in order of current connection, online status from last check, priority, and name
List<MoneroRpcConnection> connections = connectionManager.getConnections();
// clear connection manager
connectionManager.clear();
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
Specify behavior when polling. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionaddConnection
(String uri) Add a connection URI.addConnection
(MoneroRpcConnection connection) Add a connection.addListener
(MoneroConnectionManagerListener listener) Add a listener to receive notifications when the connection changes.Check the current connection.Check all managed connections.clear()
Remove all connections.Disconnect from the current connection.boolean
Get if auto switch is enabled or disabled.getBestAvailableConnection
(MoneroRpcConnection... excludedConnections) Get the best available connection in order of priority then response time.Get the current connection.getConnectionByUri
(String uri) Get a connection by URI.Get all connections in order of current connection (if applicable), online status, priority, and name.Get all listeners.Collect connectable peers of the managed connections.long
Get the request timeout.boolean
hasConnection
(String uri) Indicates if this manager has a connection with the given URI.Indicates if the connection manager is connected to a node.removeConnection
(String uri) Remove a connection.Remove a listener.Remove all listeners.reset()
Reset to default state.setAutoSwitch
(boolean autoSwitch) Automatically switch to the best available connection as connections are polled, based on priority, response time, and consistency.setConnection
(String uri) Set the current connection without changing the credentials.setConnection
(MoneroRpcConnection connection) Set the current connection.setTimeout
(long timeoutMs) Set the maximum request time before a connection is considered offline.void
Start polling connections.void
startPolling
(Long periodMs) Start polling connections.startPolling
(Long periodMs, Boolean autoSwitch, Long timeoutMs, MoneroConnectionManager.PollType pollType, Collection<MoneroRpcConnection> excludedConnections) Start polling connections.Stop polling connections.
-
Constructor Details
-
MoneroConnectionManager
public MoneroConnectionManager()
-
-
Method Details
-
addListener
Add a listener to receive notifications when the connection changes.- Parameters:
listener
- - the listener to add- Returns:
- this connection manager for chaining
-
removeListener
Remove a listener.- Parameters:
listener
- - the listener to remove- Returns:
- this connection manager for chaining
-
removeListeners
Remove all listeners.- Returns:
- this connection manager for chaining
-
getListeners
Get all listeners.- Returns:
- all listeners
-
addConnection
Add a connection URI.- Parameters:
uri
- - uri of the connection to add- Returns:
- this connection manager for chaining
-
addConnection
Add a connection. The connection may have an elevated priority for this manager to use.- Parameters:
connection
- - the connection to add- Returns:
- this connection manager for chaining
-
removeConnection
Remove a connection.- Parameters:
uri
- - uri of the connection to remove- Returns:
- this connection manager for chaining
-
setConnection
Set the current connection without changing the credentials. Add new connection if URI not previously added. Notify if current connection changes. Does not check the connection.- Parameters:
uri
- identifies the connection to make current- Returns:
- this connection manager for chaining
-
setConnection
Set the current connection. Replace connection if its URI was previously added. Otherwise add new connection. Notify if current connection changes. Does not check the connection.- Parameters:
connection
- is the connection to make current- Returns:
- this connection manager for chaining
-
getConnection
Get the current connection.- Returns:
- the current connection or null if no connection set
-
hasConnection
Indicates if this manager has a connection with the given URI.- Parameters:
uri
- - URI of the connection to check- Returns:
- true if this manager has a connection with the given URI, false otherwise
-
getConnectionByUri
Get a connection by URI.- Parameters:
uri
- - URI of the connection to get- Returns:
- the connection with the URI or null if no connection with the URI exists
-
getConnections
Get all connections in order of current connection (if applicable), online status, priority, and name.- Returns:
- the list of sorted connections
-
isConnected
Indicates if the connection manager is connected to a node.- Returns:
- true if the current connection is set, online, and not unauthenticated, null if unknown, false otherwise
-
startPolling
public void startPolling()Start polling connections. Automatically connects to the best available connection based on priority, response time, and consistency. -
startPolling
Start polling connections. Automatically connects to the best available connection based on priority, response time, and consistency.- Parameters:
periodMs
- poll period in milliseconds (default 20s)
-
startPolling
public MoneroConnectionManager startPolling(Long periodMs, Boolean autoSwitch, Long timeoutMs, MoneroConnectionManager.PollType pollType, Collection<MoneroRpcConnection> excludedConnections) Start polling connections.- Parameters:
periodMs
- poll period in milliseconds (default 20s)autoSwitch
- specifies to automatically switch to the best connection (default true unless changed)timeoutMs
- specifies the timeout to poll a single connection (default 5s unless changed)pollType
- one of PRIORITIZED (poll connections in order of priority until connected; default), CURRENT (poll current connection), or ALL (poll all connections)excludedConnections
- connections excluded from being polled- Returns:
- this connection manager for chaining
-
stopPolling
Stop polling connections.- Returns:
- this connection manager for chaining
-
checkConnection
Check the current connection. If disconnected and auto switch enabled, switches to best available connection.- Returns:
- this connection manager for chaining
-
checkConnections
Check all managed connections.- Returns:
- this connection manager for chaining
-
getBestAvailableConnection
Get the best available connection in order of priority then response time.- Parameters:
excludedConnections
- - connections to be excluded from consideration (optional)- Returns:
- the best available connection in order of priority then response time, null if no connections available
-
setAutoSwitch
Automatically switch to the best available connection as connections are polled, based on priority, response time, and consistency.- Parameters:
autoSwitch
- specifies if the connection should auto switch to a better connection- Returns:
- this connection manager for chaining
-
getAutoSwitch
public boolean getAutoSwitch()Get if auto switch is enabled or disabled.- Returns:
- true if auto switch enabled, false otherwise
-
setTimeout
Set the maximum request time before a connection is considered offline.- Parameters:
timeoutMs
- is the timeout before a connection is considered offline- Returns:
- this connection manager for chaining
-
getTimeout
public long getTimeout()Get the request timeout.- Returns:
- the request timeout before a connection is considered offline
-
getPeerConnections
Collect connectable peers of the managed connections.- Returns:
- connectable peers
-
disconnect
Disconnect from the current connection.- Returns:
- this connection manager for chaining
-
clear
Remove all connections.- Returns:
- this connection manager for chaining
-
reset
Reset to default state.- Returns:
- this connection manager for chaining
-