- java.lang.Object
-
- org.newsclub.net.unix.server.AFUNIXSocketServer
-
public abstract class AFUNIXSocketServer extends java.lang.ObjectA base implementation for a simple, multi-threaded socket server. This class supports both unix and "regular" sockets.- Author:
- Christian Kohlschütter
-
-
Constructor Summary
Constructors Constructor Description AFUNIXSocketServer(java.net.ServerSocket serverSocket)Creates a server using the given, boundServerSocket.AFUNIXSocketServer(java.net.SocketAddress listenAddress)Creates a server using the givenSocketAddress.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description protected abstract voiddoServeSocket(java.net.Socket socket)Called when a socket is ready to be served.intgetMaxConcurrentConnections()intgetServerBusyTimeout()intgetServerTimeout()intgetSocketTimeout()booleanisReady()Checks if the server is running and accepting new connections.booleanisRunning()Checks if the server is running.protected java.net.ServerSocketnewServerSocket()protected voidonAfterServingSocket(java.net.Socket socket)Called after the socket has been served.protected voidonBeforeServingSocket(java.net.Socket socket)Called before serving the socket.protected voidonListenException(java.lang.Exception e)Called when an exception was thrown while listening on the server socket.protected voidonServerBound(java.net.SocketAddress 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 voidonServerShuttingDown()Called when the server is shutting down.protected voidonServerStarting()Called when the server is starting up.protected voidonServerStopped(java.net.ServerSocket socket)Called when the server has been stopped.protected voidonServingException(java.net.Socket socket, java.lang.Exception e)Called when an exception was thrown while serving a socket.protected voidonSocketExceptionAfterAccept(java.net.Socket socket, java.net.SocketException e)Called when aSocketExceptionwas thrown during "accept".protected voidonSocketExceptionDuringAccept(java.net.SocketException e)Called when aSocketExceptionwas thrown during "accept".protected voidonSubmitted(java.net.Socket socket, java.util.concurrent.Future<?> submission)Called when a socket gets submitted into the process queue.voidsetMaxConcurrentConnections(int maxConcurrentConnections)voidsetServerBusyTimeout(int serverFullTimeout)voidsetServerTimeout(int serverTimeout)voidsetSocketTimeout(int socketTimeout)voidstart()Starts the server, and returns immediately.booleanstartAndWait(long duration, java.util.concurrent.TimeUnit unit)Starts the server and waits until it is ready or had to shop due to an error.voidstop()Stops the server.java.util.concurrent.ScheduledFuture<java.io.IOException>stopAfter(long delay, java.util.concurrent.TimeUnit unit)Requests that the server will be stopped after the given time delay.
-
-
-
Constructor Detail
-
AFUNIXSocketServer
public AFUNIXSocketServer(java.net.SocketAddress listenAddress)
Creates a server using the givenSocketAddress.- Parameters:
listenAddress- The address to bind the socket on.
-
AFUNIXSocketServer
public AFUNIXSocketServer(java.net.ServerSocket serverSocket)
Creates a server using the given, boundServerSocket.- Parameters:
serverSocket- The server socket to use (must be bound).
-
-
Method Detail
-
getMaxConcurrentConnections
public int getMaxConcurrentConnections()
-
setMaxConcurrentConnections
public void setMaxConcurrentConnections(int maxConcurrentConnections)
-
getServerTimeout
public int getServerTimeout()
-
setServerTimeout
public void setServerTimeout(int serverTimeout)
-
getSocketTimeout
public int getSocketTimeout()
-
setSocketTimeout
public void setSocketTimeout(int socketTimeout)
-
getServerBusyTimeout
public int getServerBusyTimeout()
-
setServerBusyTimeout
public void setServerBusyTimeout(int serverFullTimeout)
-
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.
-
startAndWait
public boolean startAndWait(long duration, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedExceptionStarts the server and waits until it is ready or had to shop 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:
java.lang.InterruptedException- If the wait was interrupted.
-
newServerSocket
protected java.net.ServerSocket newServerSocket() throws java.io.IOException- Throws:
java.io.IOException
-
stop
public void stop() throws java.io.IOExceptionStops the server.- Throws:
java.io.IOException- If there was an error.
-
stopAfter
public java.util.concurrent.ScheduledFuture<java.io.IOException> stopAfter(long delay, java.util.concurrent.TimeUnit unit)Requests that the server will be stopped after the given time delay.- 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).
-
doServeSocket
protected abstract void doServeSocket(java.net.Socket socket) throws java.io.IOExceptionCalled when a socket is ready to be served.- Parameters:
socket- The socket to serve.- Throws:
java.io.IOException- If there was an error.
-
onServerStarting
protected void onServerStarting()
Called when the server is starting up.
-
onServerBound
protected void onServerBound(java.net.SocketAddress address)
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
protected void onServerStopped(java.net.ServerSocket socket)
Called when the server has been stopped.- Parameters:
socket- The server's socket that stopped.
-
onSubmitted
protected void onSubmitted(java.net.Socket socket, java.util.concurrent.Future<?> submission)Called when a socket gets submitted into the process queue.- Parameters:
socket- The socket.submission- TheFuturereferencing the submission; it's "done" after the socket has been served.
-
onServerShuttingDown
protected void onServerShuttingDown()
Called when the server is shutting down.
-
onSocketExceptionDuringAccept
protected void onSocketExceptionDuringAccept(java.net.SocketException e)
Called when aSocketExceptionwas thrown during "accept".- Parameters:
e- The exception.
-
onSocketExceptionAfterAccept
protected void onSocketExceptionAfterAccept(java.net.Socket socket, java.net.SocketException e)Called when aSocketExceptionwas thrown during "accept".- Parameters:
socket- The socket.e- The exception.
-
onBeforeServingSocket
protected void onBeforeServingSocket(java.net.Socket socket)
Called before serving the socket.- Parameters:
socket- The socket.
-
onServingException
protected void onServingException(java.net.Socket socket, java.lang.Exception e)Called when an exception was thrown while serving a socket.- Parameters:
socket- The socket.e- The exception.
-
onAfterServingSocket
protected void onAfterServingSocket(java.net.Socket socket)
Called after the socket has been served.- Parameters:
socket- The socket.
-
onListenException
protected void onListenException(java.lang.Exception e)
Called when an exception was thrown while listening on the server socket.- Parameters:
e- The exception.
-
-