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 supportedSocket
type.V
- The supportedServerSocket
type.
- 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
ConstructorDescriptionSocketServer
(A listenAddress) Creates a server using the givenSocketAddress
.SocketServer
(V serverSocket) Creates a server using the given, boundServerSocket
. -
Method Summary
Modifier and TypeMethodDescriptionprotected abstract void
doServeSocket
(S socket) Called when a socket is ready to be served.protected void
doSocketClose
(S socket) Called upon closing a socket after serving the connection.protected @NonNull A
Returns the address the server listens to.int
Returns the maximum number of concurrent connections.int
Returns the server-busy timeout (in milliseconds).int
Returns the server timeout (in milliseconds).int
Returns the socket timeout (in milliseconds).boolean
isReady()
Checks if the server is running and accepting new connections.boolean
Checks if the server is running.protected abstract V
Returns a new server socket.protected void
onAfterServingSocket
(S socket) Called after the socket has been served.protected void
onBeforeServingSocket
(S socket) Called before serving the socket.protected void
Deprecated.protected void
Called when an exception was thrown while listening on the server socket.protected void
onServerBound
(A address) Called when the server has been bound to a socket.protected void
onServerBusy
(long busyStartTime) Called when the server is busy / not ready to accept a new connection.protected void
onServerReady
(int activeCount) Called when the server is ready to accept a new connection.protected void
Called when the server is shutting down.protected void
Called when the server is starting up.protected void
onServerStopped
(V socket) Called when the server has been stopped.protected void
onServingException
(S socket, Exception e) Deprecated.protected void
onServingException
(S socket, Throwable t) Called when a throwable was thrown while serving a socket.protected void
onSocketExceptionAfterAccept
(S socket, SocketException e) Called when aSocketException
was thrown during "accept".protected void
Called when aSocketException
was thrown during "accept".protected void
onSubmitted
(S socket, Future<?> submission) Called when a socket gets submitted into the process queue.void
setMaxConcurrentConnections
(int maxConcurrentConnections) Sets the maximum number of concurrent connections.void
setServerBusyTimeout
(int timeout) Sets the server-busy timeout (in milliseconds).void
setServerTimeout
(int timeout) Sets the server timeout (in milliseconds).void
setSocketTimeout
(int timeout) Sets the socket timeout (in milliseconds).void
start()
Starts the server, and returns immediately.void
Starts the server and waits until it is ready or had to stop due to an error.boolean
startAndWaitToBecomeReady
(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.void
stop()
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:
true
if the server is alive.
-
isReady
public boolean isReady()Checks if the server is running and accepting new connections.- Returns:
true
if 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:
true
if 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
AFServerSocket
if 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
IOException
s. 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,
null
is 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 aSocketException
was thrown during "accept".- Parameters:
e
- The exception.
-
onSocketExceptionAfterAccept
Called when aSocketException
was 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)