Module org.newsclub.net.unix.server
Package org.newsclub.net.unix.server
Class SocketServer<A extends SocketAddress,S extends Socket,V extends ServerSocket>
java.lang.Object
org.newsclub.net.unix.server.SocketServer<A,S,V>
- Type Parameters:
A- The supported address type.S- The supportedSockettype.V- The supportedServerSockettype.
- Direct Known Subclasses:
AFSocketServer,AFUNIXSocketServer
public abstract class SocketServer<A extends SocketAddress,S extends Socket,V extends ServerSocket>
extends Object
A base implementation for a simple, multi-threaded socket server.
- Author:
- Christian Kohlschütter
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionSocketServer(A listenAddress) Creates a server using the givenSocketAddress.SocketServer(V serverSocket) Creates a server using the given, boundServerSocket. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract voiddoServeSocket(S socket) Called when a socket is ready to be served.protected voiddoSocketClose(S socket) Called upon closing a socket after serving the connection.protected @NonNull AReturns the address the server listens to.intReturns the maximum number of concurrent connections.intReturns the server-busy timeout (in milliseconds).intReturns the server timeout (in milliseconds).intReturns the socket timeout (in milliseconds).booleanisReady()Checks if the server is running and accepting new connections.booleanChecks if the server is running.protected abstract VReturns a new server socket.protected voidonAfterServingSocket(S socket) Called after the socket has been served.protected voidonBeforeServingSocket(S socket) Called before serving the socket.protected voidDeprecated.protected voidCalled when an exception was thrown while listening on the server socket.protected voidonServerBound(A address) Called when the server has been bound to a socket.protected voidonServerBusy(long busyStartTime) Called when the server is busy / not ready to accept a new connection.protected voidonServerReady(int activeCount) Called when the server is ready to accept a new connection.protected voidCalled when the server is shutting down.protected voidCalled when the server is starting up.protected voidonServerStopped(V socket) Called when the server has been stopped.protected voidonServingException(S socket, Exception e) Deprecated.protected voidonServingException(S socket, Throwable t) Called when a throwable was thrown while serving a socket.protected voidonSocketExceptionAfterAccept(S socket, SocketException e) Called when aSocketExceptionwas thrown during "accept".protected voidCalled when aSocketExceptionwas thrown during "accept".protected voidonSubmitted(S socket, Future<?> submission) Called when a socket gets submitted into the process queue.voidsetMaxConcurrentConnections(int maxConcurrentConnections) Sets the maximum number of concurrent connections.voidsetServerBusyTimeout(int timeout) Sets the server-busy timeout (in milliseconds).voidsetServerTimeout(int timeout) Sets the server timeout (in milliseconds).voidsetSocketTimeout(int timeout) Sets the socket timeout (in milliseconds).voidstart()Starts the server, and returns immediately.voidStarts the server and waits until it is ready or had to stop due to an error.booleanstartAndWaitToBecomeReady(long duration, TimeUnit unit) Starts the server and waits until it is ready or had to stop due to an error.startThenStopAfter(long delay, TimeUnit unit) Requests that the server will be stopped after the given time delay.voidstop()Stops the server.
-
Constructor Details
-
SocketServer
Creates a server using the given, boundServerSocket.- Parameters:
serverSocket- The server socket to use (must be bound).
-
SocketServer
Creates a server using the givenSocketAddress.- Parameters:
listenAddress- The address to bind the socket on.
-
-
Method Details
-
getMaxConcurrentConnections
public int getMaxConcurrentConnections()Returns the maximum number of concurrent connections.- Returns:
- The maximum number of concurrent connections.
-
setMaxConcurrentConnections
public void setMaxConcurrentConnections(int maxConcurrentConnections) Sets the maximum number of concurrent connections.- Parameters:
maxConcurrentConnections- The new maximum.
-
getServerTimeout
public int getServerTimeout()Returns the server timeout (in milliseconds).- Returns:
- The server timeout in milliseconds (0 = no timeout).
-
setServerTimeout
public void setServerTimeout(int timeout) Sets the server timeout (in milliseconds).- Parameters:
timeout- The new timeout in milliseconds (0 = no timeout).
-
getSocketTimeout
public int getSocketTimeout()Returns the socket timeout (in milliseconds).- Returns:
- The socket timeout in milliseconds (0 = no timeout).
-
setSocketTimeout
public void setSocketTimeout(int timeout) Sets the socket timeout (in milliseconds).- Parameters:
timeout- The new timeout in milliseconds (0 = no timeout).
-
getServerBusyTimeout
public int getServerBusyTimeout()Returns the server-busy timeout (in milliseconds).- Returns:
- The server-busy timeout in milliseconds (0 = no timeout).
-
setServerBusyTimeout
public void setServerBusyTimeout(int timeout) Sets the server-busy timeout (in milliseconds).- Parameters:
timeout- The new timeout in milliseconds (0 = no timeout).
-
isRunning
public boolean isRunning()Checks if the server is running.- Returns:
trueif the server is alive.
-
isReady
public boolean isReady()Checks if the server is running and accepting new connections.- Returns:
trueif the server is alive and ready to accept new connections.
-
start
public void start()Starts the server, and returns immediately.- See Also:
-
startAndWaitToBecomeReady
Starts the server and waits until it is ready or had to stop due to an error.- Throws:
InterruptedException- If the wait was interrupted.
-
startAndWaitToBecomeReady
Starts the server and waits until it is ready or had to stop due to an error.- Parameters:
duration- The duration wait.unit- The duration's time unit.- Returns:
trueif the server is ready to serve requests.- Throws:
InterruptedException- If the wait was interrupted.
-
newServerSocket
Returns a new server socket.- Returns:
- The new socket (an
AFServerSocketif the listen address is anAFSocketAddress). - Throws:
IOException- on error.
-
stop
Stops the server.- Throws:
IOException- If there was an error.
-
doSocketClose
Called upon closing a socket after serving the connection.The default implementation closes the socket directly, ignoring any
IOExceptions. You may override this method to close the socket in a separate thread, for example.- Parameters:
socket- The socket to close.
-
startThenStopAfter
Requests that the server will be stopped after the given time delay. If the server is not started yet (andstop()was not called yet, it will be started first.- Parameters:
delay- The delay.unit- The time unit for the delay.- Returns:
- A scheduled future that can be used to monitor progress / cancel the request. If there
was a problem with stopping, an IOException is returned as the value (not thrown). If
stop was already requested,
nullis returned.
-
doServeSocket
Called when a socket is ready to be served.- Parameters:
socket- The socket to serve.- Throws:
IOException- If there was an error.
-
onServerStarting
protected void onServerStarting()Called when the server is starting up. -
onServerBound
Called when the server has been bound to a socket. This is not called when you instantiated the server with a pre-bound socket.- Parameters:
address- The bound address.
-
onServerReady
protected void onServerReady(int activeCount) Called when the server is ready to accept a new connection.- Parameters:
activeCount- The current number of active tasks (= serving sockets).
-
onServerBusy
protected void onServerBusy(long busyStartTime) Called when the server is busy / not ready to accept a new connection. The frequency on how often this method is called when the server is busy is determined bygetServerBusyTimeout().- Parameters:
busyStartTime- The time stamp since the server became busy.
-
onServerStopped
Called when the server has been stopped.- Parameters:
socket- The server's socket that stopped, ornull.
-
onSubmitted
-
onServerShuttingDown
protected void onServerShuttingDown()Called when the server is shutting down. -
onSocketExceptionDuringAccept
Called when aSocketExceptionwas thrown during "accept".- Parameters:
e- The exception.
-
onSocketExceptionAfterAccept
Called when aSocketExceptionwas thrown during "accept".- Parameters:
socket- The socket.e- The exception.
-
onBeforeServingSocket
Called before serving the socket.- Parameters:
socket- The socket.
-
onServingException
Deprecated.Called when an exception was thrown while serving a socket.- Parameters:
socket- The socket.e- The exception.- See Also:
-
onServingException
-
onAfterServingSocket
Called after the socket has been served.- Parameters:
socket- The socket.
-
onListenException
Deprecated.Called when an exception was thrown while listening on the server socket.- Parameters:
e- The exception.- See Also:
-
onListenException
Called when an exception was thrown while listening on the server socket.- Parameters:
t- The throwable.
-
getListenAddress
Returns the address the server listens to.- Returns:
- The listen address.
-
onListenException(Throwable)