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 ofAbstractNioContext.
- All Implemented Interfaces:
Runnable
- Direct Known Subclasses:
AcceptorNioServer
- Author:
- Laurent Cohen
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceAn action ot execute on a context.static interfaceInterface for context filtering.protected static interfaceA fuctional interface wrapping an operation on a set of selected channels.Nested classes/interfaces inherited from class java.lang.Thread
Thread.State, Thread.UncaughtExceptionHandler -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected StatelessNioServer.KeysetHandler<C>protected NioMessageReader<C>The nio message reader.protected NioMessageWriter<C>The nio message writer.protected StatelessNioServer.KeysetHandler<C>protected StatelessNioServer.KeysetHandler<C>Fields inherited from class org.jppf.nio.NioServer
attachment, configuration, identifier, requestShutdown, selecting, selector, selectTimeout, sslContext, sslHelper, stopped, sync, wakeUpActionFields inherited from class java.lang.Thread
MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY -
Constructor Summary
ConstructorsModifierConstructorDescriptionStatelessNioServer(int identifier, boolean useSSL, TypedProperties configuration) protectedStatelessNioServer(String name, int identifier, boolean useSSL, TypedProperties configuration) Initialize this server with a specified port number and name. -
Method Summary
Modifier and TypeMethodDescriptionprotected Future<?>doOperation(Set<SelectionKey> selectedKeys, StatelessNioServer.KeysetHandler<C> handler) Perform an operation on all eligible channels.Get all connections accepted by this server.protected voidgo(Set<SelectionKey> selectedKeys) Process the keys selected by the selector for IO operations.protected voidhandleRead(SelectionKey key) Called when a selection key is selected andreadable.protected abstract voidCalled when a selection key iswritable.protected voidhandleWrite(SelectionKey key) Called when a selection key is selected andwritable.protected voidinit()Initialize the underlying server sockets.protected voidprotected abstract voidInitialize the message reader and writer.static booleanisKeyValid(SelectionKey key) performContextAction(StatelessNioServer.ContextFilter<C> filter, StatelessNioServer.ContextAction<C> action) Perform the specified action on the contexts that pass the specified filter.voidregisterChannel(NioChannelHandler channelHandler, SocketChannel channel) Register the specified channel with this server's selectior.voidrun()toString()static StringtoString(SelectionKey key) voidupdateInterestOps(SelectionKey key, int update, boolean add) Set the interest ops of a specified selection key, ensuring no blocking occurs while doing so.static voidupdateInterestOpsNoWakeup(SelectionKey key, int update, boolean add) Set the interest ops of a specified selection key.Methods inherited from class org.jppf.nio.NioServer
accept, addServer, configurePeerSSL, configureSSLEngine, createNioContext, createSSLContext, doAccept, end, externalStopCondition, getConfiguration, getIdentifier, getSelector, isStopped, removeAllConnections, removeServer, setConfiguration, shutdown, wakeUpSelectorIfNeededMethods 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, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, yield
-
Field Details
-
messageReader
The nio message reader. -
messageWriter
The nio message writer. -
acceptHandler
-
readHandler
-
writeHandler
-
-
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
Description copied from class:NioServerInitialize the underlying server sockets. -
initNioHandlers
protected void initNioHandlers() -
initReaderAndWriter
protected abstract void initReaderAndWriter()Initialize the message reader and writer. -
run
public void run() -
go
Process the keys selected by the selector for IO operations.- Parameters:
selectedKeys- the set of keys that were selected by the latestselect()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
Called when a selection key is selected andreadable.- Parameters:
key- the key to handle.- Throws:
Exception- if any error occurs.
-
handleWrite
Called when a selection key is selected andwritable.- Parameters:
key- the key to handle.- Throws:
Exception- if any error occurs.
-
handleSelectionException
Called when a selection key iswritable.- Parameters:
key- the key to handle.e- the exception to handle.
-
updateInterestOps
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
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
- Parameters:
key- the key to check.- Returns:
- whether the key is valid or not.
-
getAllContexts
Get all connections accepted by this server. This method is a shorthand forperformContextAction(null, null).- Returns:
- a list of
StatelessNioServerinstances that passed the filter, possibly empty but nevernull.
-
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 benull, in which case all contexts are accepted.action- the action to execute, may benull, in which case no action is performed.- Returns:
- a list of
StatelessNioServerinstances that passed the filter, possibly empty but nevernull.
-
toString
- Parameters:
key- the selection key to describe.- Returns:
- a string description of the selection key.
-
toString
-