Package zephyr.common

Class ZephyrConnectionManager

java.lang.Object
zephyr.common.ZephyrConnectionManager

public class ZephyrConnectionManager extends Object

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 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

      public ZephyrConnectionManager removeListener(ZephyrConnectionManagerListener listener)
      Remove a listener.
      Parameters:
      listener - - the listener to remove
      Returns:
      this connection manager for chaining
    • removeListeners

      public ZephyrConnectionManager removeListeners()
      Remove all listeners.
      Returns:
      this connection manager for chaining
    • getListeners

      public List<ZephyrConnectionManagerListener> getListeners()
      Get all listeners.
      Returns:
      all listeners
    • addConnection

      public ZephyrConnectionManager addConnection(ZephyrRpcConnection connection)
      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

      public ZephyrConnectionManager removeConnection(String uri)
      Remove a connection.
      Parameters:
      uri - - uri of the connection to remove
      Returns:
      this connection manager for chaining
    • isConnected

      public Boolean 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

      public ZephyrRpcConnection getConnection()
      Get the current connection.
      Returns:
      the current connection or null if no connection set
    • getConnectionByUri

      public ZephyrRpcConnection getConnectionByUri(String uri)
      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

      public List<ZephyrRpcConnection> getConnections()
      Get all connections in order of current connection (if applicable), online status, priority, and name.
      Returns:
      the list of sorted connections
    • getBestAvailableConnection

      public ZephyrRpcConnection getBestAvailableConnection(ZephyrRpcConnection... excludedConnections)
      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

      public ZephyrConnectionManager setConnection(String uri)
      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

      public ZephyrConnectionManager setConnection(ZephyrRpcConnection connection)
      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

      public ZephyrConnectionManager checkConnection()
      Check the current connection. If disconnected and auto switch enabled, switches to best available connection.
      Returns:
      this connection manager for chaining
    • checkConnections

      public ZephyrConnectionManager checkConnections()
      Check all managed connections.
      Returns:
      this connection manager for chaining
    • startCheckingConnection

      public ZephyrConnectionManager startCheckingConnection()
      Check the connection and start checking the connection periodically.
      Returns:
      this connection manager for chaining (after first checking the connection)
    • startCheckingConnection

      public ZephyrConnectionManager startCheckingConnection(Long periodMs)
      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

      public ZephyrConnectionManager stopCheckingConnection()
      Stop checking the connection status periodically.
      Returns:
      this connection manager for chaining
    • setAutoSwitch

      public ZephyrConnectionManager setAutoSwitch(boolean autoSwitch)
      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

      public ZephyrConnectionManager setTimeout(long timeoutInMs)
      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

      public List<ZephyrRpcConnection> getPeerConnections()
      Collect connectable peers of the managed connections.
      Returns:
      connectable peers
    • disconnect

      public ZephyrConnectionManager disconnect()
      Disconnect from the current connection.
      Returns:
      this connection manager for chaining
    • clear

      public ZephyrConnectionManager clear()
      Remove all connections.
      Returns:
      this connection manager for chaining
    • reset

      public ZephyrConnectionManager reset()
      Reset to default state.
      Returns:
      this connection manager for chaining