Package monero.common

Class MoneroConnectionManager

java.lang.Object
monero.common.MoneroConnectionManager

public class MoneroConnectionManager extends Object

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();
  • 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

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

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

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

      public MoneroConnectionManager addConnection(String uri)
      Add a connection URI.
      Parameters:
      uri - - uri of the connection to add
      Returns:
      this connection manager for chaining
    • addConnection

      public MoneroConnectionManager addConnection(MoneroRpcConnection 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 MoneroConnectionManager removeConnection(String uri)
      Remove a connection.
      Parameters:
      uri - - uri of the connection to remove
      Returns:
      this connection manager for chaining
    • setConnection

      public MoneroConnectionManager 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 MoneroConnectionManager setConnection(MoneroRpcConnection 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
    • getConnection

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

      public boolean hasConnection(String uri)
      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

      public MoneroRpcConnection 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<MoneroRpcConnection> getConnections()
      Get all connections in order of current connection (if applicable), online status, priority, and name.
      Returns:
      the list of sorted connections
    • 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
    • startPolling

      public void startPolling()
      Start polling connections. Automatically connects to the best available connection based on priority, response time, and consistency.
    • startPolling

      public void startPolling(Long periodMs)
      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

      public MoneroConnectionManager stopPolling()
      Stop polling connections.
      Returns:
      this connection manager for chaining
    • checkConnection

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

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

      public MoneroRpcConnection getBestAvailableConnection(MoneroRpcConnection... 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
    • setAutoSwitch

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

      public MoneroConnectionManager setTimeout(long timeoutMs)
      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

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

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

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

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