Package org.jppf.nio

Class NioServer

java.lang.Object
java.lang.Thread
org.jppf.nio.NioServer
All Implemented Interfaces:
Runnable
Direct Known Subclasses:
StatelessNioServer

public abstract class NioServer extends Thread
Generic server for non-blocking asynchronous socket channel based communications.
Instances of this class rely on a number of possible states for each socket channel, along with the possible transitions between thoses states.
The design of this class enforces the use of typesafe enumerations for the states and transitions, so the developers must think ahead of how to implement their server as a state machine.
Author:
Laurent Cohen, Lane Schwartz (dynamically allocated server port)
  • Field Details

    • selector

      protected final Selector selector
      the selector of all socket channels open with providers or nodes.
    • stopped

      protected final AtomicBoolean stopped
      Flag indicating that this socket server is closed.
    • selectTimeout

      protected long selectTimeout
      Timeout for the select() operations. A value of 0 means no timeout, i.e. the Selector.select() method will be invoked without parameters.
    • requestShutdown

      protected final AtomicBoolean requestShutdown
      Shutdown requested for this server
    • sslContext

      protected SSLContext sslContext
      The SSL context associated with this server.
    • sslHelper

      protected SSLHelper sslHelper
    • identifier

      protected final int identifier
      The channel identifier for channels handled by this server.
    • selecting

      protected final SynchronizedBoolean selecting
      Whether the selector is currently selecting.
    • wakeUpAction

      protected final Runnable wakeUpAction
    • sync

      protected final SelectorSynchronizer sync
      Used to synchronize on th selector for blocking operations.
    • attachment

      protected final Object attachment
      An arbitrary object attached to this server.
    • configuration

      protected TypedProperties configuration
      The configuration to use.
  • Constructor Details

    • NioServer

      protected NioServer(int identifier, boolean useSSL, Object attachment, TypedProperties configuration) throws Exception
      Initialize this server with a specified identifier and name.
      Parameters:
      identifier - the channel identifier for channels handled by this server.
      useSSL - determines whether an SSLContext should be created for this server.
      attachment - an arbitrary object attached to this server.
      configuration - the JPPF configuration to use.
      Throws:
      Exception - if the underlying server socket can't be opened.
    • NioServer

      protected NioServer(String name, int identifier, boolean useSSL, Object attachment, TypedProperties configuration) throws Exception
      Initialize this server with a specified port number and name.
      Parameters:
      name - the name of this thread.
      identifier - the channel identifier for channels handled by this server.
      useSSL - determines whether an SSLContext should be created for this server.
      attachment - an arbitrary object attached to this server.
      configuration - the JPPF configuration to use.
      Throws:
      Exception - if the underlying server socket can't be opened.
  • Method Details

    • init

      protected abstract void init() throws Exception
      Initialize the underlying server sockets.
      Throws:
      Exception - if any error occurs while initializing the server sockets.
    • addServer

      public boolean addServer(int portToInit, boolean ssl, Map<String,?> env, boolean retryOnException) throws Exception
      Initialize the underlying server sockets for the spcified array of ports. This implementation throws a JPPFUnsupportedOperationException, it should be overriden by subclasses for a different outcome.
      Parameters:
      portToInit - the array of ports to initiialize.
      ssl - true if the server sockets should be initialized with SSL enabled, false otherwise.
      env - optional map of parameters to associate with the server socket channel.
      retryOnException - whether to retry if an exception occurs when binding the server.
      Returns:
      true ifd a new server was created, false otherwxise.
      Throws:
      Exception - if any error occurs while initializing the server sockets.
    • removeServer

      public void removeServer(int port) throws Exception
      Remove the server identified by the local port it is listneing to. This implementation throws a JPPFUnsupportedOperationException, it should be overriden by subclasses for a different outcome.
      Parameters:
      port - the port the sever is listening to.
      Throws:
      Exception - if any error occurs closing the specified server socket channel.
    • createSSLContext

      protected void createSSLContext() throws Exception
      Configure all SSL settings. This method is for interested subclasses classes to override.
      Throws:
      Exception - if any error occurs during the SSL configuration.
    • configureSSLEngine

      protected void configureSSLEngine(SSLEngine engine) throws Exception
      Configure all SSL settings for the specified SSL engine. This method is for interested subclasses classes to override.
      Parameters:
      engine - the SSL engine to configure.
      Throws:
      Exception - if any error occurs during the SSL configuration.
    • externalStopCondition

      protected boolean externalStopCondition()
      Determine whether a stop condition external to this server has been reached. The default implementation always returns whether shutdown was requested.
      Subclasses may override this behavior.
      Returns:
      true if this server should be stopped, false otherwise.
    • shutdown

      public void shutdown()
      Initiates shutdown of this server.
    • doAccept

      protected void doAccept(SelectionKey key)
      accept the incoming connection. It accept and put it in a state to define what type of peer is.
      Parameters:
      key - the selection key that represents the channel's registration with the selector.
    • accept

      public abstract void accept(ServerSocketChannel serverSocketChannel, SocketChannel channel, SSLHandler sslHandler, boolean ssl, boolean peer, Object... params) throws Exception
      Register an incoming connection with this server's selector. The channel is registered with an empty set of initial interest operations, which means a call to the corresponding SelectionKey's interestOps() method will return 0.
      Parameters:
      serverSocketChannel - the server socket channel accepting the connection.
      channel - the socket channel representing the connection.
      sslHandler - an sslEngine eventually passed on from a different server.
      ssl - specifies whether an SSLHandler should be initialized for the channel.
      peer - specifiies whether the channel is for a peer driver.
      params - optional parameters.
      Throws:
      Exception - if any error occurs.
    • createNioContext

      public abstract NioContext createNioContext(Object... params)
      Define a context for a newly created channel.
      Parameters:
      params - optional parameters.
      Returns:
      an NioContext instance.
    • end

      public void end()
      Close the underlying server socket and stop this socket server.
    • removeAllConnections

      public void removeAllConnections()
      Close and remove all connections accepted by this server.
    • getSelector

      public Selector getSelector()
      Get the selector for this server.
      Returns:
      a Selector instance.
    • isStopped

      protected boolean isStopped()
      Get the stopped state of this server.
      Returns:
      true if this server is stopped, false otherwise.
    • configurePeerSSL

      public void configurePeerSSL(NioContext context) throws Exception
      Configure the SSL options for the specified channel.
      Parameters:
      context - the channel context for which to configure SSL.
      Throws:
      Exception - if any error occurs.
    • getIdentifier

      public int getIdentifier()
      Get the channel identifier for channels handled by this server.
      Returns:
      an int whose value is one of the constants defined in JPPFIdentifiers.
    • wakeUpSelectorIfNeeded

      public boolean wakeUpSelectorIfNeeded()
      Returns:
      whether the selector had to be awaken.
    • getConfiguration

      public TypedProperties getConfiguration()
      Returns:
      the configuration to use.
    • setConfiguration

      public void setConfiguration(TypedProperties configuration)
      Set the configuration to use.
      Parameters:
      configuration - the configuration to use.
    • toString

      public static String toString(SelectionKey key)