Package zephyr.common
Class ZephyrConnectionManager
java.lang.Object
zephyr.common.ZephyrConnectionManager
Manages a collection of prioritized connections to daemon or wallet RPC endpoints.
Example usage:
// create connection manager
ZephyrConnectionManager connectionManager = new ZephyrConnectionManager();
// add managed connections with priorities
connectionManager.addConnection(new ZephyrRpcConnection("http://localhost:38081").setPriority(1)); // use localhost as first priority
connectionManager.addConnection(new ZephyrRpcConnection("http://example.com")); // default priority is prioritized last
// set current connection
connectionManager.setConnection(new ZephyrRpcConnection("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 ZephyrConnectionManagerListener() {
@Override
public void onConnectionChanged(ZephyrRpcConnection connection) {
System.out.println("Connection changed to: " + connection);
}
});
// check connection status every 10 seconds
connectionManager.startCheckingConnection(10000l);
// automatically switch to best available connection if disconnected
connectionManager.setAutoSwitch(true);
// get best available connection in order of priority then response time
ZephyrRpcConnection 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<ZephyrRpcConnection> connections = connectionManager.getConnections();
// clear connection manager
connectionManager.clear();
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionaddConnection(ZephyrRpcConnection connection) Add a connection.addListener(ZephyrConnectionManagerListener 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.booleanGet if auto switch is enabled or disabled.getBestAvailableConnection(ZephyrRpcConnection... 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.longGet the request timeout.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 best available connection if current connection is disconnected after being checked.setConnection(String uri) Set the current connection without changing the credentials.setConnection(ZephyrRpcConnection connection) Set the current connection.setTimeout(long timeoutInMs) Set the maximum request time before a connection is considered offline.Check the connection and start checking the connection periodically.startCheckingConnection(Long periodMs) Check the connection and start checking the connection periodically.Stop checking the connection status periodically.
-
Constructor Details
-
ZephyrConnectionManager
public ZephyrConnectionManager()
-
-
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. 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
-
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
-
getConnection
Get the current connection.- Returns:
- the current connection or null if no connection set
-
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
-
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
-
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
-
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
-
startCheckingConnection
Check the connection and start checking the connection periodically.- Returns:
- this connection manager for chaining (after first checking the connection)
-
startCheckingConnection
Check the connection and start checking the connection periodically.- Parameters:
periodMs- is the time between checks in milliseconds (default 10000 ms or 10 seconds)- Returns:
- this connection manager for chaining (after first checking the connection)
-
stopCheckingConnection
Stop checking the connection status periodically.- Returns:
- this connection manager for chaining
-
setAutoSwitch
Automatically switch to best available connection if current connection is disconnected after being checked.- Parameters:
autoSwitch- specifies if the connection should switch on disconnect- 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:
timeoutInMs- 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
-