Class ConnectionManager
- java.lang.Object
-
- org.deepsymmetry.beatlink.LifecycleParticipant
-
- org.deepsymmetry.beatlink.dbserver.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
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interface
ConnectionManager.ClientTask<T>
An interface for all the kinds of activities that need a connection to the dbserver, so we can keep track of how many sessions are in effect, clean up after them, and know when the client is idle and can be closed.
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_SOCKET_TIMEOUT
The default value we will use for timeouts on opening and reading from sockets.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
getIdleLimit()
Check how long an idle connection will be kept open for reuse.static ConnectionManager
getInstance()
Get the singleton instance of this class.int
getPlayerDBServerPort(int player)
Look up the database server port reported by a given player.int
getSocketTimeout()
Check how long we will wait for a socket to connect or for a read operation to complete.<T> T
invokeWithClientSession(int targetPlayer, ConnectionManager.ClientTask<T> task, String description)
Obtain a dbserver client session that can be used to perform some task, call that task with the client, then release the client.boolean
isRunning()
Check whether we are currently running.void
setIdleLimit(int seconds)
Determine how long an idle connection will be kept open for reuse.void
setSocketTimeout(int timeout)
Set how long we will wait for a socket to connect or for a read operation to complete.void
start()
Start offering shared dbserver sessions.void
stop()
Stop offering shared dbserver sessions.String
toString()
-
Methods inherited from class org.deepsymmetry.beatlink.LifecycleParticipant
addLifecycleListener, deliverLifecycleAnnouncement, ensureRunning, getLifecycleListeners, removeLifecycleListener
-
-
-
-
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 withtask
- the activity that will be performed with exclusive access to a dbserver connectiondescription
- 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 communicatingException
- from the underlyingtask
, 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 classLifecycleParticipant
- 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.
-
-