Class BaseServer

  • Direct Known Subclasses:
    VinciServer

    public class BaseServer
    extends java.lang.Object
    Class for running VinciServables in the Vinci framework. This implementation supports multiple concurrent clients (in a Thread per client manner). Creating a service typically requires defining implementing a VinciServable which is passed to this class to service requests. This class can be used independently of VNS. For VNS-enhanced serving, use the VinciServer. This class is designed to be extensible. For example you can extend to provide new Runnable objects that are used to handle requests in the appropriate fashion. You can also override configure socket to install SSL-supporting server sockets, and so on...
    • Constructor Summary

      Constructors 
      Constructor Description
      BaseServer​(VinciServable my_servable)
      Create a new BaseServer that will delegate requests to the provided servable.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void cleanExit()
      Cleanly shut down this server.
      protected java.net.ServerSocket createServerSocket​(int port)
      Create the server socket used to listen for requests.
      Transportable eval​(Transportable in, KeyValuePair header)
      This function is a callback for the (Base/Vinci)ServerRunnable.
      protected java.lang.Runnable getRunnable​(java.net.Socket client)
      Get a runnable object to run within a pooled thread that will handle the request.
      VinciServable getServable()
      Get the servable object being used by this server.
      protected java.net.ServerSocket getServerSocket()
      Get the server socket that this server uses to listen for requests.
      protected void handleRequest​(java.net.Socket client)
      Initialize a new socket connection.
      protected void handleRequests()
      Enter the server socket accept() loop.
      Transportable makeTransportable()
      This is another callback for the (Base/Vinci)ServerRunnable that creates the document to be populated by the service with the request response.
      void serve​(int port)
      Serve requests, blocking until a clean shutdown is triggered.
      void setSocketTimeout​(int millis)
      This method is used to override the default timeout value of one minute.
      void setThreadPoolSize​(int initial, int max)
      Set the intitial and maximum size of the threadpool used by this server.
      void shutdownServing()
      Cause the serve() method to terminate.
      void startServing​(int port)
      Asynchronously start serving requests.
      • Methods inherited from class java.lang.Object

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

      • BaseServer

        public BaseServer​(VinciServable my_servable)
        Create a new BaseServer that will delegate requests to the provided servable.
        Parameters:
        my_servable - The servable object implementing the service.
    • Method Detail

      • getServable

        public VinciServable getServable()
        Get the servable object being used by this server.
        Returns:
        -
      • setSocketTimeout

        public void setSocketTimeout​(int millis)
                              throws java.io.IOException
        This method is used to override the default timeout value of one minute. You can provide "0" for "never timeout" but this is not recommended, as in some cases threads will end up forever blocking for input, eventually causing the threadpool to max out and block all pending requests.
        Parameters:
        millis - The socket timeout value in milliseconds.
        Throws:
        java.io.IOException - -
      • setThreadPoolSize

        public void setThreadPoolSize​(int initial,
                                      int max)
        Set the intitial and maximum size of the threadpool used by this server. This should be called before serving starts otherwise it has no effect.
        Parameters:
        initial - -
        max - -
      • startServing

        public void startServing​(int port)
                          throws java.io.IOException
        Asynchronously start serving requests. If this method returns without throwing an exception, then another thread has been successfully launched to begin serving requests.
        Parameters:
        port - The port on which to listen for requests.
        Throws:
        java.io.IOException - Thrown if there was some problem with the server socket.
        Since:
        2.0.15
      • serve

        public void serve​(int port)
                   throws java.io.IOException
        Serve requests, blocking until a clean shutdown is triggered.
        Parameters:
        port - The port on which to listen for requests.
        Throws:
        java.io.IOException - Thrown if there was some problem with the server socket.
      • getServerSocket

        protected java.net.ServerSocket getServerSocket()
        Get the server socket that this server uses to listen for requests.
        Returns:
        The server's server socket
      • createServerSocket

        protected java.net.ServerSocket createServerSocket​(int port)
                                                    throws java.io.IOException
        Create the server socket used to listen for requests. This method can be overridden to provide non-standard server sockets, such as those supporting SSL, those requiring special configuration/initialization, etc.
        Parameters:
        port - The port which is to be listened to by the created socket.
        Returns:
        The server socket to be used by this server for accepting requests.
        Throws:
        java.io.IOException - -
      • handleRequests

        protected void handleRequests()
        Enter the server socket accept() loop. Loop terminates when shutdown shutdownServing() method is called.
      • shutdownServing

        public void shutdownServing()
        Cause the serve() method to terminate.
      • getRunnable

        protected java.lang.Runnable getRunnable​(java.net.Socket client)
        Get a runnable object to run within a pooled thread that will handle the request.
        Parameters:
        client - -
        Returns:
        -
      • handleRequest

        protected void handleRequest​(java.net.Socket client)
        Initialize a new socket connection.
        Parameters:
        client - -
      • cleanExit

        protected void cleanExit()
        Cleanly shut down this server. Called when handleRequests completes the accept loop.
      • eval

        public Transportable eval​(Transportable in,
                                  KeyValuePair header)
        This function is a callback for the (Base/Vinci)ServerRunnable. It invokes the eval method of the appropriate VinciServable.
        Parameters:
        in - -
        header - -
        Returns:
        -
      • makeTransportable

        public Transportable makeTransportable()
        This is another callback for the (Base/Vinci)ServerRunnable that creates the document to be populated by the service with the request response.
        Returns:
        The document the service will populate to create the request response.