Class ConnectionManager


  • public class ConnectionManager
    extends LifecycleParticipant
    Manges connections to dbserver ports on the players, offering sessions that can be used to perform transactions, and allowing the connections to close when there are no active sessions.
    Author:
    James Elliott
    • Field Detail

      • DEFAULT_SOCKET_TIMEOUT

        public static final int DEFAULT_SOCKET_TIMEOUT
        The default value we will use for timeouts on opening and reading from sockets.
        See Also:
        Constant Field Values
    • Method Detail

      • setIdleLimit

        public void setIdleLimit​(int seconds)
        Determine how long an idle connection will be kept open for reuse. Once this time has elapsed, the connection will be closed. Setting this to zero will close connections immediately after use, which might be somewhat inefficient when multiple queries need to happen in a row, since each will require the establishment of a new connection. The default value is 1.
        Parameters:
        seconds - how many seconds a connection will be kept open while not being used
        Throws:
        IllegalArgumentException - if a negative value is supplied
      • getIdleLimit

        public int getIdleLimit()
        Check how long an idle connection will be kept open for reuse. Once this time has elapsed, the connection will be closed. Setting this to zero will close connections immediately after use, which might be somewhat inefficient when multiple queries need to happen in a row, since each will require the establishment of a new connection. The default value is 1.
        Returns:
        how many seconds a connection will be kept open while not being used
      • invokeWithClientSession

        public <T> T invokeWithClientSession​(int targetPlayer,
                                             ConnectionManager.ClientTask<T> task,
                                             String description)
                                      throws Exception
        Obtain a dbserver client session that can be used to perform some task, call that task with the client, then release the client.
        Type Parameters:
        T - the type that will be returned by the task to be performed
        Parameters:
        targetPlayer - the player number whose dbserver we wish to communicate with
        task - the activity that will be performed with exclusive access to a dbserver connection
        description - a short description of the task being performed for error reporting if it fails, should be a verb phrase like "requesting track metadata"
        Returns:
        the value returned by the completed task
        Throws:
        IOException - if there is a problem communicating
        Exception - from the underlying task, if any
      • getPlayerDBServerPort

        public int getPlayerDBServerPort​(int player)
        Look up the database server port reported by a given player. You should not use this port directly; instead ask this class for a session to use while you communicate with the database.
        Parameters:
        player - the player number of interest
        Returns:
        the port number on which its database server is running, or -1 if unknown
        Throws:
        IllegalStateException - if not running
      • setSocketTimeout

        public void setSocketTimeout​(int timeout)
        Set how long we will wait for a socket to connect or for a read operation to complete. Adjust this if your players or network require it.
        Parameters:
        timeout - after how many milliseconds will an attempt to open or read from a socket fail
      • getSocketTimeout

        public int getSocketTimeout()
        Check how long we will wait for a socket to connect or for a read operation to complete. Adjust this if your players or network require it.
        Returns:
        the number of milliseconds after which an attempt to open or read from a socket will fail
      • isRunning

        public boolean isRunning()
        Check whether we are currently running.
        Specified by:
        isRunning in class LifecycleParticipant
        Returns:
        true if we are offering shared dbserver sessions
      • start

        public void start()
                   throws SocketException
        Start offering shared dbserver sessions.
        Throws:
        SocketException - if there is a problem opening connections
      • stop

        public void stop()
        Stop offering shared dbserver sessions.
      • getInstance

        public static ConnectionManager getInstance()
        Get the singleton instance of this class.
        Returns:
        the only instance of this class which exists.