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 supported Socket type.
V - The supported ServerSocket 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 Details

    • SocketServer

      public SocketServer(V serverSocket)
      Creates a server using the given, bound ServerSocket.
      Parameters:
      serverSocket - The server socket to use (must be bound).
    • SocketServer

      public SocketServer(A listenAddress)
      Creates a server using the given SocketAddress.
      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

      public boolean startAndWaitToBecomeReady(long duration, TimeUnit unit) throws InterruptedException
      Starts 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:
      true if the server is ready to serve requests.
      Throws:
      InterruptedException - If the wait was interrupted.
    • newServerSocket

      protected abstract V newServerSocket() throws IOException
      Returns a new server socket.
      Returns:
      The new socket (an AFServerSocket if the listen address is an AFSocketAddress).
      Throws:
      IOException - on error.
    • stop

      public void stop() throws IOException
      Stops the server.
      Throws:
      IOException - If there was an error.
    • startThenStopAfter

      public ScheduledFuture<IOException> startThenStopAfter(long delay, TimeUnit unit)
      Requests that the server will be stopped after the given time delay. If the server is not started yet (and stop() 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

      protected abstract void doServeSocket(S socket) throws IOException
      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

      protected void onServerBound(A 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 by getServerBusyTimeout().
      Parameters:
      busyStartTime - The time stamp since the server became busy.
    • onServerStopped

      protected void onServerStopped(V socket)
      Called when the server has been stopped.
      Parameters:
      socket - The server's socket that stopped.
    • onSubmitted

      protected void onSubmitted(S socket, Future<?> submission)
      Called when a socket gets submitted into the process queue.
      Parameters:
      socket - The socket.
      submission - The Future referencing 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(SocketException e)
      Called when a SocketException was thrown during "accept".
      Parameters:
      e - The exception.
    • onSocketExceptionAfterAccept

      protected void onSocketExceptionAfterAccept(S socket, SocketException e)
      Called when a SocketException was thrown during "accept".
      Parameters:
      socket - The socket.
      e - The exception.
    • onBeforeServingSocket

      protected void onBeforeServingSocket(S socket)
      Called before serving the socket.
      Parameters:
      socket - The socket.
    • onServingException

      protected void onServingException(S socket, Exception e)
      Called when an exception was thrown while serving a socket.
      Parameters:
      socket - The socket.
      e - The exception.
    • onAfterServingSocket

      protected void onAfterServingSocket(S socket)
      Called after the socket has been served.
      Parameters:
      socket - The socket.
    • onListenException

      protected void onListenException(Exception e)
      Called when an exception was thrown while listening on the server socket.
      Parameters:
      e - The exception.
    • getListenAddress

      protected @NonNull A getListenAddress()
      Returns the address the server listens to.
      Returns:
      The listen address.