Package org.jppf.nio
Class NioServer
java.lang.Object
java.lang.Thread
org.jppf.nio.NioServer
- All Implemented Interfaces:
Runnable
- Direct Known Subclasses:
StatelessNioServer
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.
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)
-
Nested Class Summary
Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected final ObjectAn arbitrary object attached to this server.protected TypedPropertiesThe configuration to use.protected final intThe channel identifier for channels handled by this server.protected final AtomicBooleanShutdown requested for this serverprotected final SynchronizedBooleanWhether the selector is currently selecting.protected final Selectorthe selector of all socket channels open with providers or nodes.protected longTimeout for the select() operations.protected SSLContextThe SSL context associated with this server.protected SSLHelperprotected final AtomicBooleanFlag indicating that this socket server is closed.protected final SelectorSynchronizerUsed to synchronize on th selector for blocking operations.protected final RunnableFields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedNioServer(int identifier, boolean useSSL, Object attachment, TypedProperties configuration) Initialize this server with a specified identifier and name.protectedNioServer(String name, int identifier, boolean useSSL, Object attachment, TypedProperties configuration) Initialize this server with a specified port number and name. -
Method Summary
Modifier and TypeMethodDescriptionabstract voidaccept(ServerSocketChannel serverSocketChannel, SocketChannel channel, SSLHandler sslHandler, boolean ssl, boolean peer, Object... params) Register an incoming connection with this server's selector.booleanInitialize the underlying server sockets for the spcified array of ports.voidconfigurePeerSSL(NioContext context) Configure the SSL options for the specified channel.protected voidconfigureSSLEngine(SSLEngine engine) Configure all SSL settings for the specified SSL engine.abstract NioContextcreateNioContext(Object... params) Define a context for a newly created channel.protected voidConfigure all SSL settings.protected voiddoAccept(SelectionKey key) accept the incoming connection.voidend()Close the underlying server socket and stop this socket server.protected booleanDetermine whether a stop condition external to this server has been reached.intGet the channel identifier for channels handled by this server.Get the selector for this server.protected abstract voidinit()Initialize the underlying server sockets.protected booleanGet the stopped state of this server.voidClose and remove all connections accepted by this server.voidremoveServer(int port) Remove the server identified by the local port it is listneing to.voidsetConfiguration(TypedProperties configuration) Set the configuration to use.voidshutdown()Initiates shutdown of this server.static StringtoString(SelectionKey key) booleanMethods inherited from class java.lang.Thread
activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, run, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
-
Field Details
-
selector
the selector of all socket channels open with providers or nodes. -
stopped
Flag indicating that this socket server is closed. -
selectTimeout
protected long selectTimeoutTimeout for the select() operations. A value of 0 means no timeout, i.e. theSelector.select()method will be invoked without parameters. -
requestShutdown
Shutdown requested for this server -
sslContext
The SSL context associated with this server. -
sslHelper
-
identifier
protected final int identifierThe channel identifier for channels handled by this server. -
selecting
Whether the selector is currently selecting. -
wakeUpAction
-
sync
Used to synchronize on th selector for blocking operations. -
attachment
An arbitrary object attached to this server. -
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
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 ExceptionInitialize the underlying server sockets for the spcified array of ports. This implementation throws aJPPFUnsupportedOperationException, it should be overriden by subclasses for a different outcome.- Parameters:
portToInit- the array of ports to initiialize.ssl-trueif the server sockets should be initialized with SSL enabled,falseotherwise.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:
trueifd a new server was created,falseotherwxise.- Throws:
Exception- if any error occurs while initializing the server sockets.
-
removeServer
Remove the server identified by the local port it is listneing to. This implementation throws aJPPFUnsupportedOperationException, 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
Configure all SSL settings. This method is for interested subclasses classes to override.- Throws:
Exception- if any error occurs during the SSL configuration.
-
configureSSLEngine
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
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 correspondingSelectionKey'sinterestOps()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 anSSLHandlershould 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
Define a context for a newly created channel.- Parameters:
params- optional parameters.- Returns:
- an
NioContextinstance.
-
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
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
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
- Returns:
- the configuration to use.
-
setConfiguration
Set the configuration to use.- Parameters:
configuration- the configuration to use.
-
toString
-