|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.sun.grizzly.TCPSelectorHandler
public class TCPSelectorHandler
A SelectorHandler handles all java.nio.channels.Selector operations. One or more instance of a Selector are handled by SelectorHandler. The logic for processing of SelectionKey interest (OP_ACCEPT,OP_READ, etc.) is usually defined using an instance of SelectorHandler. This class represents a TCP implementation of a SelectorHandler. This class first bind a ServerSocketChannel to a TCP port and then start waiting for NIO events.
| Field Summary | |
|---|---|
protected AsyncQueueReader |
asyncQueueReader
AsyncQueueWriter |
protected AsyncQueueWriter |
asyncQueueWriter
AsyncQueueWriter |
protected Map<String,Object> |
attributes
Attributes, associated with the SelectorHandler instance |
protected ConnectorInstanceHandler |
connectorInstanceHandler
The ConnectorInstanceHandler used to return a new or pooled ConnectorHandler |
protected InetAddress |
inet
The inet address to use when binding. |
protected ProtocolChainInstanceHandler |
instanceHandler
The ProtocolChainInstanceHandler used by this instance. |
protected AtomicBoolean |
isShutDown
Flag, which shows whether shutdown was called for this SelectorHandler |
protected int |
linger
The socket linger. |
protected Logger |
logger
|
protected LinkedTransferQueue<SelectionKeyOP> |
opToRegister
The list of SelectionKeyOP to register next time the
Selector.select is invoked. |
protected int |
port
The default TCP port. |
protected int |
receiveBufferSize
The size to which to set the receive buffer If this value is not greater than 0, it is not used. |
protected boolean |
reuseAddress
The socket reuseAddress |
protected Role |
role
Is this used for client only or client/server operation. |
protected SelectionKeyHandler |
selectionKeyHandler
The SelectionKeyHandler associated with this SelectorHandler. |
protected Selector |
selector
The single Selector. |
protected long |
selectTimeout
The Selector time out. |
protected int |
sendBufferSize
The size to which to set the send buffer If this value is not greater than 0, it is not used. |
protected ServerSocket |
serverSocket
The ServerSocket instance. |
protected ServerSocketChannel |
serverSocketChannel
The ServerSocketChannel. |
protected int |
serverTimeout
The server socket time out |
protected int |
socketTimeout
The socket time out |
protected int |
ssBackLog
Server socket backlog. |
protected StateHolder<State> |
stateHolder
This SelectorHandler StateHolder, which is shared among
SelectorHandler and its clones |
protected boolean |
tcpNoDelay
The socket tcpDelay. |
protected ExecutorService |
threadPool
The ExecutorService used by this instance. |
| Constructor Summary | |
|---|---|
TCPSelectorHandler()
|
|
TCPSelectorHandler(boolean isClient)
Create a TCPSelectorHandler only used with ConnectorHandler. |
|
TCPSelectorHandler(Role role)
Create a TCPSelectorHandler only used with ConnectorHandler. |
|
| Method Summary | |
|---|---|
SelectableChannel |
acceptWithoutRegistration(SelectionKey key)
Accepts connection, without registering it for reading or writing |
ConnectorHandler |
acquireConnectorHandler()
Return an instance of the default ConnectorHandler,
which is the TCPConnectorHandler |
void |
addPendingIO(Runnable runnable)
|
void |
addPendingKeyCancel(SelectionKey key)
|
protected static Role |
boolean2Role(boolean isClient)
Returns the Role, depending on isClient value |
void |
closeChannel(SelectableChannel channel)
Closes SelectableChannel |
void |
configureChannel(SelectableChannel channel)
Configure the channel operations. |
protected void |
connect(SocketAddress remoteAddress,
SocketAddress localAddress,
CallbackHandler callbackHandler)
Register a CallBackHandler to this Selector. |
void |
copyTo(Copyable copy)
|
AsyncQueueReader |
getAsyncQueueReader()
Returns AsyncQueueReader associated with this
SelectorHandler. |
AsyncQueueWriter |
getAsyncQueueWriter()
Returns AsyncQueueWriter associated with this
SelectorHandler. |
Object |
getAttribute(String key)
Return an object based on a key. |
Map<String,Object> |
getAttributes()
Return a Map of attribute name/value pairs. |
protected Callable<ConnectorHandler> |
getConnectorInstanceHandlerDelegate()
Return ConnectorInstanceHandler corresponding to the protocol |
InetAddress |
getInet()
|
int |
getLinger()
|
Logger |
getLogger()
|
int |
getPendingIOlimitPerThread()
Max number of pendingIO tasks that will be executed per worker thread. |
int |
getPort()
|
int |
getPortLowLevel()
Deprecated. Use getPort |
Class<? extends SelectionKeyHandler> |
getPreferredSelectionKeyHandler()
Get the preffered SelectionKeyHandler implementation for this SelectorHandler. |
ProtocolChainInstanceHandler |
getProtocolChainInstanceHandler()
Return the ProtocolChainInstanceHandler |
Role |
getRole()
Gets this SelectorHandler current role. |
protected SelectableChannel |
getSelectableChannel(SocketAddress remoteAddress,
SocketAddress localAddress)
|
SelectionKeyHandler |
getSelectionKeyHandler()
Get the SelectionKeyHandler associated with this SelectorHandler. |
Selector |
getSelector()
Gets the underlying selector. |
long |
getSelectTimeout()
|
int |
getServerTimeout()
|
int |
getSocketTimeout()
|
int |
getSpinRate()
Returns the Selector spinrate |
int |
getSsBackLog()
|
StateHolder<State> |
getStateHolder()
|
ExecutorService |
getThreadPool()
Return the ExecutorService used to execute this
SelectorHandler's SelectionKey ops |
protected void |
invokeAsyncQueueReader(NIOContext context)
Invoke a AsyncQueueReader |
protected void |
invokeAsyncQueueWriter(NIOContext context)
Invoke a AsyncQueueWriter |
protected void |
invokeCallbackHandler(CallbackHandler callbackHandler,
NIOContext context)
Invoke a CallbackHandler via a Context instance. |
boolean |
isFinishIOUsingCurrentThread()
Return true by default, meaning the WorkerThread used to
execute the I/O operation will also complete the I/O operations. |
boolean |
isOpen()
Is the Selector open. |
boolean |
isReuseAddress()
|
boolean |
isTcpNoDelay()
Return the tcpNoDelay value used by the underlying accepted Sockets. |
SelectionKey |
keyFor(SelectableChannel channel)
Returns SelectionKey, which represents binding between
the SelectableChannel and this SelectorHandler |
Set<SelectionKey> |
keys()
Return the set of SelectionKey registered on this Selector. |
boolean |
onAcceptInterest(SelectionKey key,
Context ctx)
Handle OP_ACCEPT. |
boolean |
onConnectInterest(SelectionKey key,
Context ctx)
Handle OP_CONNECT. |
protected void |
onConnectOp(Context ctx,
SelectionKeyOP.ConnectSelectionKeyOP selectionKeyOp)
Handle new OP_CONNECT ops. |
boolean |
onReadInterest(SelectionKey key,
Context ctx)
Handle OP_READ. |
boolean |
onWriteInterest(SelectionKey key,
Context ctx)
Handle OP_WRITE. |
void |
pause()
Pause this SelectorHandler |
protected NIOContext |
pollContext(Context serverContext,
SelectionKey key,
Context.OpType opType)
Polls Context from pool and initializes it. |
void |
postSelect(Context ctx)
Invoked after Selector.select(). |
void |
preSelect(Context ctx)
Before invoking Selector.select(long), make sure the ServerSocketChannel
has been created. |
protected void |
processPendingOperations(Context ctx)
|
Controller.Protocol |
protocol()
A token decribing the protocol supported by an implementation of this interface |
void |
register(SelectableChannel channel,
int ops)
Register the SelectableChannel on the Selector. |
void |
register(SelectionKey key,
int ops)
Register a SelectionKey to this Selector. Storing each interest type in different queues removes the need of wrapper (SelectionKeyOP) while lowering thread contention due to the load is spread out on different queues. |
void |
releaseConnectorHandler(ConnectorHandler connectorHandler)
Release a ConnectorHandler. |
Object |
removeAttribute(String key)
Remove a key/value object. |
void |
resetSpinCounter()
Resets the spincounter |
void |
resume()
Resume this SelectorHandler |
Set<SelectionKey> |
select(Context ctx)
Execute the Selector.select(...) operations. |
void |
setAttribute(String key,
Object value)
Set a key/value object. |
void |
setAttributes(Map<String,Object> attributes)
Set a Map of attribute name/value pairs. |
void |
setFinishIOUsingCurrentThread(boolean finishIOUsingCurrentThread)
Set to false to use a the kernel thread pool to finish pending I/O operations, like closing connection. |
void |
setInet(InetAddress inet)
|
void |
setLinger(int linger)
|
void |
setLogger(Logger logger)
|
void |
setMaxAcceptRetries(int maxAcceptRetries)
Max number of accept() failures before abording. |
void |
setPendingIOlimitPerThread(int pendingIOlimitPerThread)
Max number of pendingIO tasks that will be executed per worker thread. |
void |
setPort(int port)
|
void |
setProtocolChainInstanceHandler(ProtocolChainInstanceHandler instanceHandler)
Set the ProtocolChainInstanceHandler to use for
creating instance of ProtocolChain. |
void |
setReceiveBufferSize(int size)
Sets the receiveBufferSize to the specified value |
void |
setReuseAddress(boolean reuseAddress)
|
void |
setRole(Role role)
Sets this SelectorHandler current role. |
void |
setSelectionKeyHandler(SelectionKeyHandler selectionKeyHandler)
Set SelectionKeyHandler associated with this SelectorHandler. |
void |
setSelector(Selector selector)
Sets the underlying Selector |
void |
setSelectTimeout(long selectTimeout)
|
void |
setSendBufferSize(int size)
Sets the sendBufferSize to the specified value |
void |
setServerTimeout(int serverTimeout)
|
void |
setSocketTimeout(int socketTimeout)
|
void |
setSsBackLog(int ssBackLog)
|
void |
setTcpNoDelay(boolean tcpNoDelay)
Enable (true) or disable (false) the underlying Socket's tcpNoDelay. |
void |
setThreadPool(ExecutorService threadPool)
Set the ExecutorService used to execute this
SelectorHandler's SelectionKey ops |
void |
shutdown()
Shuntdown this instance by closing its Selector and associated channels. |
void |
workaroundSelectorSpin()
Execute workaround for selector spin issue |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected ConnectorInstanceHandler connectorInstanceHandler
protected final LinkedTransferQueue<SelectionKeyOP> opToRegister
SelectionKeyOP to register next time the
Selector.select is invoked.
can be combined read+write interest or Connect
protected boolean tcpNoDelay
protected boolean reuseAddress
protected int linger
protected int socketTimeout
protected Logger logger
protected int serverTimeout
protected InetAddress inet
protected int port
protected ServerSocket serverSocket
protected ServerSocketChannel serverSocketChannel
protected Selector selector
protected long selectTimeout
protected int ssBackLog
protected Role role
protected SelectionKeyHandler selectionKeyHandler
protected ProtocolChainInstanceHandler instanceHandler
protected ExecutorService threadPool
ExecutorService used by this instance. If null -
Controller's ExecutorService will be used
protected AsyncQueueWriter asyncQueueWriter
AsyncQueueWriter
protected AsyncQueueReader asyncQueueReader
AsyncQueueWriter
protected Map<String,Object> attributes
SelectorHandler instance
protected StateHolder<State> stateHolder
SelectorHandler StateHolder, which is shared among
SelectorHandler and its clones
protected final AtomicBoolean isShutDown
SelectorHandler
protected int sendBufferSize
protected int receiveBufferSize
| Constructor Detail |
|---|
public TCPSelectorHandler()
public TCPSelectorHandler(boolean isClient)
isClient - true if this SelectorHandler is only used
to handle ConnectorHandler.public TCPSelectorHandler(Role role)
role - the TCPSelectorHandler Role| Method Detail |
|---|
public void copyTo(Copyable copy)
copyTo in interface Copyablepublic Set<SelectionKey> keys()
keys in interface SelectorHandlerpublic boolean isOpen()
isOpen in interface SelectorHandler
public void preSelect(Context ctx)
throws IOException
Selector.select(long), make sure the ServerSocketChannel
has been created. If true, then register all SelectionKey to the Selector.
preSelect in interface SelectorHandlerctx - Context
IOException
protected void processPendingOperations(Context ctx)
throws IOException
ctx -
IOException
protected void onConnectOp(Context ctx,
SelectionKeyOP.ConnectSelectionKeyOP selectionKeyOp)
throws IOException
IOException
public Set<SelectionKey> select(Context ctx)
throws IOException
select in interface SelectorHandlerctx - Context
Set of SelectionKey
IOExceptionpublic void postSelect(Context ctx)
postSelect in interface SelectorHandlerctx - Contextpublic void addPendingIO(Runnable runnable)
addPendingIO in interface PendingIOhandlerpublic void addPendingKeyCancel(SelectionKey key)
addPendingKeyCancel in interface PendingIOhandler
public void register(SelectionKey key,
int ops)
register in interface SelectorHandlerkey - ops -
public void register(SelectableChannel channel,
int ops)
SelectorHandlerSelectableChannel on the Selector.
register in interface SelectorHandlerops - interested operations
protected void connect(SocketAddress remoteAddress,
SocketAddress localAddress,
CallbackHandler callbackHandler)
throws IOException
remoteAddress - remote address to connectlocalAddress - local address to bincallbackHandler - CallbackHandler
IOException
protected SelectableChannel getSelectableChannel(SocketAddress remoteAddress,
SocketAddress localAddress)
throws IOException
IOExceptionpublic void pause()
SelectorHandler
pause in interface SelectorHandlerpublic void resume()
SelectorHandler
resume in interface SelectorHandlerpublic StateHolder<State> getStateHolder()
getStateHolder in interface SupportStateHolder<State>public void shutdown()
shutdown in interface SelectorHandler
public SelectableChannel acceptWithoutRegistration(SelectionKey key)
throws IOException
acceptWithoutRegistration in interface SelectorHandlerSelectableChannel
IOException
public boolean onAcceptInterest(SelectionKey key,
Context ctx)
throws IOException
onAcceptInterest in interface SelectorHandlerctx - Contextkey - SelectionKey
IOException
public boolean onReadInterest(SelectionKey key,
Context ctx)
throws IOException
onReadInterest in interface SelectorHandlerctx - Contextkey - SelectionKey
CallbackHandler, otherwise true
IOException
public boolean onWriteInterest(SelectionKey key,
Context ctx)
throws IOException
onWriteInterest in interface SelectorHandlerkey - SelectionKeyctx - Context
IOException
public boolean onConnectInterest(SelectionKey key,
Context ctx)
throws IOException
onConnectInterest in interface SelectorHandlerkey - SelectionKeyctx - Context
IOException
protected void invokeCallbackHandler(CallbackHandler callbackHandler,
NIOContext context)
throws IOException
context - Context
IOException
protected void invokeAsyncQueueReader(NIOContext context)
throws IOException
AsyncQueueReader
context - Context
IOException
protected void invokeAsyncQueueWriter(NIOContext context)
throws IOException
AsyncQueueWriter
context - Context
IOExceptionpublic ConnectorHandler acquireConnectorHandler()
ConnectorHandler,
which is the TCPConnectorHandler
acquireConnectorHandler in interface SelectorHandlerConnectorHandlerpublic void releaseConnectorHandler(ConnectorHandler connectorHandler)
releaseConnectorHandler in interface SelectorHandlerconnectorHandler - ConnectorHandlerpublic Controller.Protocol protocol()
protocol in interface SelectorHandler
public void configureChannel(SelectableChannel channel)
throws IOException
configureChannel in interface SelectorHandlerchannel - SelectableChannel to configure
IOException - on possible configuration related errorpublic int getPendingIOlimitPerThread()
public void setPendingIOlimitPerThread(int pendingIOlimitPerThread)
pendingIOlimitPerThread - public final Selector getSelector()
SelectorHandler
getSelector in interface SelectorHandlerSelectorpublic final void setSelector(Selector selector)
SelectorHandlerSelector
setSelector in interface SelectorHandlerselector - underlying Selectorpublic AsyncQueueReader getAsyncQueueReader()
AsyncQueueReader associated with this
SelectorHandler. Method will return null, if this
SelectorHandler is not running.
getAsyncQueueReader in interface SelectorHandlerAsyncQueueReaderpublic AsyncQueueWriter getAsyncQueueWriter()
AsyncQueueWriter associated with this
SelectorHandler. Method will return null, if this
SelectorHandler is not running.
getAsyncQueueWriter in interface SelectorHandlerAsyncQueueWriterpublic long getSelectTimeout()
public void setSelectTimeout(long selectTimeout)
public int getServerTimeout()
public void setServerTimeout(int serverTimeout)
public InetAddress getInet()
public void setInet(InetAddress inet)
public Role getRole()
SelectorHandler current role.
TCPSelectorHandler could act as client, which corresponds to
Role.CLIENT or client-server, which corresponds
to the Role.CLIENT_SERVER
Rolepublic void setRole(Role role)
SelectorHandler current role.
TCPSelectorHandler could act as client, which corresponds to
Role.CLIENT or client-server, which corresponds
to the Role.CLIENT_SERVER
role - the Rolepublic int getPortLowLevel()
getPort
SelectorHandler is listening on
Similar to getPort(), but getting port number directly from
connection (ServerSocket, DatagramSocket).
So if default port number 0 was set during initialization, then getPort()
will return 0, but getPortLowLevel() will
return port number assigned by OS.
SelectorHandler was not initialized for accepting connections.public int getPort()
public void setPort(int port)
public int getSsBackLog()
public void setSsBackLog(int ssBackLog)
public boolean isTcpNoDelay()
public void setTcpNoDelay(boolean tcpNoDelay)
public int getLinger()
public void setLinger(int linger)
public int getSocketTimeout()
public void setSocketTimeout(int socketTimeout)
public Logger getLogger()
public void setLogger(Logger logger)
public boolean isReuseAddress()
public void setReuseAddress(boolean reuseAddress)
public ExecutorService getThreadPool()
ExecutorService used to execute this
SelectorHandler's SelectionKey ops
getThreadPool in interface SelectorHandlerController's
ExecutorService should be used.public void setThreadPool(ExecutorService threadPool)
ExecutorService used to execute this
SelectorHandler's SelectionKey ops
setThreadPool in interface SelectorHandlerpublic Class<? extends SelectionKeyHandler> getPreferredSelectionKeyHandler()
getPreferredSelectionKeyHandler in interface SelectorHandlerpublic SelectionKeyHandler getSelectionKeyHandler()
getSelectionKeyHandler in interface SelectorHandlerpublic void setSelectionKeyHandler(SelectionKeyHandler selectionKeyHandler)
setSelectionKeyHandler in interface SelectorHandlerpublic void setProtocolChainInstanceHandler(ProtocolChainInstanceHandler instanceHandler)
ProtocolChainInstanceHandler to use for
creating instance of ProtocolChain.
setProtocolChainInstanceHandler in interface SelectorHandlerpublic ProtocolChainInstanceHandler getProtocolChainInstanceHandler()
ProtocolChainInstanceHandler
getProtocolChainInstanceHandler in interface SelectorHandlerpublic void closeChannel(SelectableChannel channel)
SelectableChannel
closeChannel in interface SelectorHandler
protected NIOContext pollContext(Context serverContext,
SelectionKey key,
Context.OpType opType)
Context from pool and initializes it.
serverContext - Controller contextkey - SelectionKey
Contextprotected Callable<ConnectorHandler> getConnectorInstanceHandlerDelegate()
ConnectorInstanceHandler corresponding to the protocol
public Object removeAttribute(String key)
removeAttribute in interface AttributeHolderkey - - name of an attribute
public void setAttribute(String key,
Object value)
setAttribute in interface AttributeHolderkey - - name of an attributevalue - - value of named attributepublic Object getAttribute(String key)
getAttribute in interface AttributeHolderkey - - name of an attribute
public void setAttributes(Map<String,Object> attributes)
Map of attribute name/value pairs.
Old AttributeHolder values will not be available.
Later changes of this Map will lead to changes to the current
AttributeHolder.
setAttributes in interface AttributeHolderattributes - - map of name/value pairspublic Map<String,Object> getAttributes()
Map of attribute name/value pairs.
Updates, performed on the returned Map will be reflected in
this AttributeHolder
getAttributes in interface AttributeHolderMap of attribute name/value pairsprotected static Role boolean2Role(boolean isClient)
Role, depending on isClient value
isClient - true>tt>, if this SelectorHandler works in
the client mode, or false otherwise.
Rolepublic void resetSpinCounter()
resetSpinCounter in interface LinuxSpinningWorkaroundpublic int getSpinRate()
Selector spinrate
getSpinRate in interface LinuxSpinningWorkaround
public void workaroundSelectorSpin()
throws IOException
workaroundSelectorSpin in interface LinuxSpinningWorkaroundIOExceptionpublic SelectionKey keyFor(SelectableChannel channel)
SelectionKey, which represents binding between
the SelectableChannel and this SelectorHandler
keyFor in interface SelectorHandlerchannel - SelectableChannel
SelectionKey, which represents binding between
the SelectableChannel and this SelectorHandlerpublic void setSendBufferSize(int size)
sendBufferSize to the specified value
size - the size to which to set the send buffer. This value should be greater than 0.public void setReceiveBufferSize(int size)
receiveBufferSize to the specified value
size - the size to which to set the receive buffer. This value should be greater than 0.public boolean isFinishIOUsingCurrentThread()
WorkerThread used to
execute the I/O operation will also complete the I/O operations. Setting to false
delegate the task to the kernel thread pool.
public void setFinishIOUsingCurrentThread(boolean finishIOUsingCurrentThread)
finishIOUsingCurrentThread - the finishIOUsingCurrentThread to setpublic void setMaxAcceptRetries(int maxAcceptRetries)
maxAcceptRetries -
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||