org.jppf.server.nio
Class NioServer<S extends Enum<S>,T extends Enum<T>>

java.lang.Object
  extended by java.lang.Thread
      extended by org.jppf.server.nio.NioServer<S,T>
Type Parameters:
S - the type of the states to use.
T - the type of the transitions to use.
All Implemented Interfaces:
Runnable
Direct Known Subclasses:
ClassNioServer, MultiplexerNioServer, MultiplexerNioServer, NodeNioServer

public abstract class NioServer<S extends Enum<S>,T extends Enum<T>>
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

Nested Class Summary
 
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler
 
Field Summary
static String CLASS_SERVER
          Name of the class server.
protected  NioServerFactory<S,T> factory
          The factory for this server.
protected  ReentrantLock lock
          Lock used to synchronize selector operations.
static String NODE_SERVER
          Name of the class server.
protected  int[] ports
          The ports this server is listening to.
protected  ResourceProvider resourceProvider
          Reads resource files from the classpath.
protected  Selector selector
          the selector of all socket channels open with providers or nodes.
protected  long selectTimeout
          Timeout for the select() operations.
protected  StateTransitionManager<S,T> transitionManager
          Performs all operations that relate to channel states.
 
Fields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
 
Constructor Summary
  NioServer(int[] ports, String name, boolean sequential)
          Initialize this server with a specified list of port numbers and name.
  NioServer(int port, String name)
          Initialize this server with a specified port number and name.
protected NioServer(String name)
          Initialize this server with a specified port number and name.
protected NioServer(String name, boolean sequential)
          Initialize this server with a specified port number and name.
 
Method Summary
protected abstract  NioServerFactory<S,T> createFactory()
          Create the factory holding all the states and transition mappings.
abstract  NioContext<?> createNioContext()
          Define a context for a newly created channel.
 void end()
          Close the underlying server socket and stop this socket server.
protected  boolean externalStopCondition()
          Determine whether a stop condition external to this server has been reached.
 NioServerFactory<S,T> getFactory()
          Get the factory for this server.
abstract  int getInitialInterest()
          Get the IO operations a connection is initially interested in.
 ReentrantLock getLock()
          Get the lock used to synchronize selector operations.
 int[] getPorts()
          Get the ports this server is listening to.
 Selector getSelector()
          Get the selector for this server.
 StateTransitionManager<S,T> getTransitionManager()
          Get the manager that performs all operations that relate to channel states.
 void go(Set<SelectionKey> selectedKeys)
          Process the keys selected by the selector for IO operations.
protected  void init(int[] ports)
          Initialize the underlying server socket with a specified port.
protected  boolean isStopped()
          Get the stopped state of this server.
abstract  void postAccept(ChannelWrapper<?> key)
          Process a channel that was accepted by the server socket channel.
 void postAccept(ChannelWrapper<?> key, ServerSocketChannel serverChannel)
          Process a channel that was accepted by the server socket channel.
 void postSelect()
          This method is invoked after all selected keys have been processed.
 void removeAllConnections()
          Close and remove all connections accepted by this server.
 void run()
          Start the underlying server socket by making it accept incoming connections.
protected  void setStopped(boolean stopped)
          Set this server in the specified stopped state.
 
Methods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

CLASS_SERVER

public static final String CLASS_SERVER
Name of the class server.

See Also:
Constant Field Values

NODE_SERVER

public static final String NODE_SERVER
Name of the class server.

See Also:
Constant Field Values

selector

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


resourceProvider

protected ResourceProvider resourceProvider
Reads resource files from the classpath.


ports

protected int[] ports
The ports this server is listening to.


selectTimeout

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


factory

protected NioServerFactory<S extends Enum<S>,T extends Enum<T>> factory
The factory for this server.


lock

protected ReentrantLock lock
Lock used to synchronize selector operations.


transitionManager

protected StateTransitionManager<S extends Enum<S>,T extends Enum<T>> transitionManager
Performs all operations that relate to channel states.

Constructor Detail

NioServer

protected NioServer(String name)
             throws JPPFException
Initialize this server with a specified port number and name.

Parameters:
name - the name given to this thread.
Throws:
JPPFException - if the underlying server socket can't be opened.

NioServer

protected NioServer(String name,
                    boolean sequential)
             throws JPPFException
Initialize this server with a specified port number and name.

Parameters:
name - the name given to this thread.
sequential - determines whether the submission of state transitions should be performed sequentially or through the executor thread pool.
Throws:
JPPFException - if the underlying server socket can't be opened.

NioServer

public NioServer(int port,
                 String name)
          throws JPPFException
Initialize this server with a specified port number and name.

Parameters:
port - the port this socket server is listening to.
name - the name given to this thread.
Throws:
JPPFException - if the underlying server socket can't be opened.

NioServer

public NioServer(int[] ports,
                 String name,
                 boolean sequential)
          throws JPPFException
Initialize this server with a specified list of port numbers and name.

Parameters:
ports - the list of port this server accepts connections from.
name - the name given to this thread.
sequential - Determines whether the submission of state transitions should be performed sequentially or through the executor thread pool.
Throws:
JPPFException - if the underlying server socket can't be opened.
Method Detail

createFactory

protected abstract NioServerFactory<S,T> createFactory()
Create the factory holding all the states and transition mappings.

Returns:
an NioServerFactory instance.

init

protected final void init(int[] ports)
                   throws JPPFException
Initialize the underlying server socket with a specified port.

Parameters:
ports - the port the underlying server listens to.
Throws:
JPPFException - if the server socket can't be opened on the specified port.

run

public void run()
Start the underlying server socket by making it accept incoming connections.

Specified by:
run in interface Runnable
Overrides:
run in class Thread
See Also:
Runnable.run()

externalStopCondition

protected boolean externalStopCondition()
Determine whether a stop condition external to this server has been reached. The default implementation always returns false.
Subclasses may override this behavior.

Returns:
true if this server should be stopped, false otherwise.

go

public void go(Set<SelectionKey> selectedKeys)
        throws Exception
Process the keys selected by the selector for IO operations.

Parameters:
selectedKeys - the set of keys thast were selected by the latest select() invocation.
Throws:
Exception - if an error is raised while processing the keys.

postSelect

public void postSelect()
This method is invoked after all selected keys have been processed. This implementation does nothing. Subclasses should override this method as needed.


postAccept

public void postAccept(ChannelWrapper<?> key,
                       ServerSocketChannel serverChannel)
Process a channel that was accepted by the server socket channel.

Parameters:
key - the selection key for the socket channel to process.
serverChannel - the ServerSocketChannel that accepted the channel.

postAccept

public abstract void postAccept(ChannelWrapper<?> key)
Process a channel that was accepted by the server socket channel.

Parameters:
key - the selection key for the socket channel to process.

createNioContext

public abstract NioContext<?> createNioContext()
Define a context for a newly created channel.

Returns:
an NioContext instance.

getInitialInterest

public abstract int getInitialInterest()
Get the IO operations a connection is initially interested in.

Returns:
a bit-wise combination of the interests, taken from SelectionKey constants definitions.

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.

getFactory

public NioServerFactory<S,T> getFactory()
Get the factory for this server.

Returns:
an NioServerFactory instance.

getLock

public ReentrantLock getLock()
Get the lock used to synchronize selector operations.

Returns:
a ReentrantLock instance.

setStopped

protected void setStopped(boolean stopped)
Set this server in the specified stopped state.

Parameters:
stopped - true if this server is stopped, false otherwise.

isStopped

protected boolean isStopped()
Get the stopped state of this server.

Returns:
true if this server is stopped, false otherwise.

getTransitionManager

public StateTransitionManager<S,T> getTransitionManager()
Get the manager that performs all operations that relate to channel states.

Returns:
a StateTransitionManager instance.

getPorts

public int[] getPorts()
Get the ports this server is listening to.

Returns:
an array of int values.


Copyright © 2005-2010 JPPF Team.