Class AFUNIXSocketServer


  • public abstract class AFUNIXSocketServer
    extends java.lang.Object
    A 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, bound ServerSocket.
      AFUNIXSocketServer​(java.net.SocketAddress listenAddress)
      Creates a server using the given SocketAddress.
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      protected abstract void doServeSocket​(java.net.Socket socket)
      Called when a socket is ready to be served.
      int getMaxConcurrentConnections()  
      int getServerBusyTimeout()  
      int getServerTimeout()  
      int getSocketTimeout()  
      boolean isReady()
      Checks if the server is running and accepting new connections.
      boolean isRunning()
      Checks if the server is running.
      protected java.net.ServerSocket newServerSocket()  
      protected void onAfterServingSocket​(java.net.Socket socket)
      Called after the socket has been served.
      protected void onBeforeServingSocket​(java.net.Socket socket)
      Called before serving the socket.
      protected void onListenException​(java.lang.Exception e)
      Called when an exception was thrown while listening on the server socket.
      protected void onServerBound​(java.net.SocketAddress 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 onServerShuttingDown()
      Called when the server is shutting down.
      protected void onServerStarting()
      Called when the server is starting up.
      protected void onServerStopped​(java.net.ServerSocket socket)
      Called when the server has been stopped.
      protected void onServingException​(java.net.Socket socket, java.lang.Exception e)
      Called when an exception was thrown while serving a socket.
      protected void onSocketExceptionAfterAccept​(java.net.Socket socket, java.net.SocketException e)
      Called when a SocketException was thrown during "accept".
      protected void onSocketExceptionDuringAccept​(java.net.SocketException e)
      Called when a SocketException was thrown during "accept".
      protected void onSubmitted​(java.net.Socket socket, java.util.concurrent.Future<?> submission)
      Called when a socket gets submitted into the process queue.
      void setMaxConcurrentConnections​(int maxConcurrentConnections)  
      void setServerBusyTimeout​(int serverFullTimeout)  
      void setServerTimeout​(int serverTimeout)  
      void setSocketTimeout​(int socketTimeout)  
      void start()
      Starts the server, and returns immediately.
      boolean startAndWait​(long duration, java.util.concurrent.TimeUnit unit)
      Starts the server and waits until it is ready or had to shop due to an error.
      void stop()
      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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • AFUNIXSocketServer

        public AFUNIXSocketServer​(java.net.SocketAddress listenAddress)
        Creates a server using the given SocketAddress.
        Parameters:
        listenAddress - The address to bind the socket on.
      • AFUNIXSocketServer

        public AFUNIXSocketServer​(java.net.ServerSocket serverSocket)
        Creates a server using the given, bound ServerSocket.
        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:
        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.
      • startAndWait

        public boolean startAndWait​(long duration,
                                    java.util.concurrent.TimeUnit unit)
                             throws java.lang.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:
        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.IOException
        Stops 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.IOException
        Called 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 by getServerBusyTimeout().
        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 - 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​(java.net.SocketException e)
        Called when a SocketException was thrown during "accept".
        Parameters:
        e - The exception.
      • onSocketExceptionAfterAccept

        protected void onSocketExceptionAfterAccept​(java.net.Socket socket,
                                                    java.net.SocketException e)
        Called when a SocketException was 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.