Package org.jppf.nio

Class StatelessNioServer<C extends AbstractNioContext>

java.lang.Object
java.lang.Thread
org.jppf.nio.NioServer
org.jppf.nio.StatelessNioServer<C>
Type Parameters:
C - the type of connection context, a subclass of AbstractNioContext.
All Implemented Interfaces:
Runnable
Direct Known Subclasses:
AcceptorNioServer

public abstract class StatelessNioServer<C extends AbstractNioContext> extends NioServer
Author:
Laurent Cohen
  • Field Details

  • Constructor Details

    • StatelessNioServer

      public StatelessNioServer(int identifier, boolean useSSL, TypedProperties configuration) throws Exception
      Parameters:
      identifier - the channel identifier for channels handled by this server.
      useSSL - determines whether an SSLContext should be created for this server.
      configuration - the JPPF configuration to use.
      Throws:
      Exception - if any error occurs.
    • StatelessNioServer

      protected StatelessNioServer(String name, int identifier, boolean useSSL, 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.
      configuration - the JPPF configuration to use.
      Throws:
      Exception - if the underlying server socket can't be opened.
  • Method Details

    • init

      protected void init() throws Exception
      Description copied from class: NioServer
      Initialize the underlying server sockets.
      Specified by:
      init in class NioServer
      Throws:
      Exception - if any error occurs while initializing the server sockets.
    • initNioHandlers

      protected void initNioHandlers()
    • initReaderAndWriter

      protected abstract void initReaderAndWriter()
      Initialize the message reader and writer.
    • run

      public void run()
      Specified by:
      run in interface Runnable
      Overrides:
      run in class Thread
    • go

      protected void go(Set<SelectionKey> selectedKeys) throws Exception
      Process the keys selected by the selector for IO operations.
      Parameters:
      selectedKeys - the set of keys that were selected by the latest select() invocation.
      Throws:
      Exception - if an error is raised while processing the keys.
    • doOperation

      protected Future<?> doOperation(Set<SelectionKey> selectedKeys, StatelessNioServer.KeysetHandler<C> handler) throws Exception
      Perform an operation on all eligible channels.
      Parameters:
      selectedKeys - set of selected keys from which to extract the eligible ones.
      handler - .
      Returns:
      a future the result of the channels read performed in a separate thread.
      Throws:
      Exception - if any error occurs.
    • handleRead

      protected void handleRead(SelectionKey key) throws Exception
      Called when a selection key is selected and readable.
      Parameters:
      key - the key to handle.
      Throws:
      Exception - if any error occurs.
    • handleWrite

      protected void handleWrite(SelectionKey key) throws Exception
      Called when a selection key is selected and writable.
      Parameters:
      key - the key to handle.
      Throws:
      Exception - if any error occurs.
    • handleSelectionException

      protected abstract void handleSelectionException(SelectionKey key, Exception e)
      Called when a selection key is writable.
      Parameters:
      key - the key to handle.
      e - the exception to handle.
    • updateInterestOps

      public void updateInterestOps(SelectionKey key, int update, boolean add) throws Exception
      Set the interest ops of a specified selection key, ensuring no blocking occurs while doing so. This method is proposed as a convenience, to encapsulate the inner locking mechanism.
      Parameters:
      key - the key on which to set the interest operations.
      update - the operations to update on the key.
      add - whether to add the update (true) or remove it (false).
      Throws:
      Exception - if any error occurs.
    • registerChannel

      public void registerChannel(NioChannelHandler channelHandler, SocketChannel channel) throws Exception
      Register the specified channel with this server's selectior.
      Parameters:
      channelHandler - the context associated with the channel.
      channel - the channel to register.
      Throws:
      Exception - if any error occurs.
    • updateInterestOpsNoWakeup

      public static void updateInterestOpsNoWakeup(SelectionKey key, int update, boolean add)
      Set the interest ops of a specified selection key. This method is proposed as a convenience, to encapsulate the inner locking mechanism.
      Parameters:
      key - the key on which to set the interest operations.
      update - the operations to update on the key.
      add - whether to add the update (true) or remove it (false).
    • isKeyValid

      public static boolean isKeyValid(SelectionKey key)
      Parameters:
      key - the key to check.
      Returns:
      whether the key is valid or not.
    • getAllContexts

      public Map<String,C> getAllContexts()
      Get all connections accepted by this server. This method is a shorthand for performContextAction(null, null).
      Returns:
      a list of StatelessNioServer instances that passed the filter, possibly empty but never null.
    • performContextAction

      public Map<String,C> performContextAction(StatelessNioServer.ContextFilter<C> filter, StatelessNioServer.ContextAction<C> action)
      Perform the specified action on the contexts that pass the specified filter.
      Parameters:
      filter - the context filter, may be null, in which case all contexts are accepted.
      action - the action to execute, may be null, in which case no action is performed.
      Returns:
      a list of StatelessNioServer instances that passed the filter, possibly empty but never null.
    • toString

      public static String toString(SelectionKey key)
      Parameters:
      key - the selection key to describe.
      Returns:
      a string description of the selection key.
    • toString

      public String toString()
      Overrides:
      toString in class Thread