Package com.sun.akuma

Class NetworkServer

  • Direct Known Subclasses:
    EchoServer

    public abstract class NetworkServer
    extends Daemon
    Multi-process network server that accepts connections on the same TCP port.

    This class lets you write a Unix-like multi-process network daemon. The first process acts as the frontend. This creates a new socket, then fork several worker processes, which inherits this socket.

    Worker threads will all accept connections on this port, so even when one of the worker processes die off, your clients won't notice that there's a problem.

    The user of this class needs to override this class and implement abstract methods. Several protected methods can be also overridden to customize the behaviors. See EchoServer source code as an example.

    This class also inherits from Daemon to support the daemonization.

    From your main method, call into run() method. Depending on whether the current process is started as a front end or a worker process, the run method behave accordingly.

    Author:
    Kohsuke Kawaguchi
    • Field Detail

      • arguments

        protected final java.util.List<java.lang.String> arguments
        Java arguments.
    • Constructor Detail

      • NetworkServer

        protected NetworkServer​(java.lang.String[] args)
    • Method Detail

      • run

        public void run()
                 throws java.lang.Exception
        Entry point. Should be called from your main method.
        Throws:
        java.lang.Exception
      • shouldBeDaemonized

        protected boolean shouldBeDaemonized()
        Determine if we should daemonize ourselves.
      • frontend

        protected void frontend()
                         throws java.lang.Exception
        Front-end.
        Throws:
        java.lang.Exception
      • forkWorkers

        protected abstract void forkWorkers​(JavaVMArguments args)
                                     throws java.lang.Exception
        Forks the worker thread with the given JVM args. The implementation is expected to modify the arguments to suit their need, then call into forkWorkerThreads(JavaVMArguments, int).
        Throws:
        java.lang.Exception
      • forkWorkerThreads

        protected void forkWorkerThreads​(JavaVMArguments arguments,
                                         int n)
                                  throws java.lang.Exception
        Called by the front-end code to fork a number of worker processes into the background. This method never returns.
        Throws:
        java.lang.Exception
      • createServerSocket

        protected abstract java.net.ServerSocket createServerSocket()
                                                             throws java.lang.Exception
        Creates a bound ServerSocket that will be shared by all worker processes. This method is called in the frontend process.
        Throws:
        java.lang.Exception
      • worker

        protected void worker()
                       throws java.lang.Exception
        Throws:
        java.lang.Exception
      • worker

        protected abstract void worker​(java.net.ServerSocket ss)
                                throws java.lang.Exception
        Worker thread main code.
        Parameters:
        ss - The server socket that the frontend process created.
        Throws:
        java.lang.Exception