|
||||||||||
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.
Nested Class Summary | |
---|---|
protected class |
TCPSelectorHandler.ConnectChannelOperation
|
protected class |
TCPSelectorHandler.RegisterChannelOperation
|
protected class |
TCPSelectorHandler.RegisterKeyOperation
|
protected class |
TCPSelectorHandler.RunnableOperation
|
protected class |
TCPSelectorHandler.SelectionKeyCancelOperation
|
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 boolean |
isKeepAlive
The socket keepAlive mode. |
protected AtomicBoolean |
isShutDown
Flag, which shows whether shutdown was called for this SelectorHandler |
protected int |
linger
The socket linger. |
protected Logger |
logger
|
protected PortRange |
portRange
The default port range. |
protected Queue<SelectorHandlerTask> |
postponedTasks
|
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 Queue<SelectorHandlerTask> |
selectorHandlerTasks
The list of SelectionKeyOP to register next time the
Selector.select is invoked. |
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)
Enqueues runnable to be executed on Selector thread. |
void |
addPendingKeyCancel(SelectionKey key)
Enqueues SelectionKey for later cancel and close on a Selector thread. |
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)
Copies current object content to copy object |
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 |
getPort()
|
int |
getPortLowLevel()
Deprecated. Use getPort |
PortRange |
getPortRange()
|
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()
Max number of pendingIO tasks that will be executed per worker thread. |
long |
getSelectTimeout()
|
int |
getServerTimeout()
|
int |
getSocketTimeout()
|
int |
getSpinRate()
Returns the Selector spinrate |
int |
getSsBackLog()
|
StateHolder<State> |
getStateHolder()
Gets StateHolder for this object |
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 |
isExecutePendingIOUsingSelectorThread()
Return true, if selector thread has to be applied to execute I/O operation, or false (by default), meaning that I/O operation could be executed in the current thread. |
boolean |
isKeepAlive()
|
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,
TCPSelectorHandler.ConnectChannelOperation 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(SelectableChannel channel,
int ops,
Object attachment)
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 |
setExecutePendingIOUsingSelectorThread(boolean executePendingIOUsingSelectorThread)
Set true, if selector thread has to be applied to execute I/O operation, or false (by default), meaning that I/O operation could be executed in the current thread. |
void |
setInet(InetAddress inet)
|
void |
setKeepAlive(boolean isKeepAlive)
|
void |
setLinger(int linger)
|
void |
setLogger(Logger logger)
|
void |
setMaxAcceptRetries(int maxAcceptRetries)
Max number of accept() failures before abording. |
void |
setPort(int port)
|
void |
setPortRange(PortRange portRange)
|
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()
Shutdown 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 Queue<SelectorHandlerTask> selectorHandlerTasks
SelectionKeyOP
to register next time the
Selector.select is invoked.
can be combined read+write interest or Connect
protected final Queue<SelectorHandlerTask> postponedTasks
protected boolean tcpNoDelay
protected boolean reuseAddress
protected boolean isKeepAlive
protected int linger
protected int socketTimeout
protected Logger logger
protected int serverTimeout
protected InetAddress inet
protected PortRange portRange
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)
Copyable
copyTo
in interface Copyable
copy
- represents target object, where current object's content will be copiedpublic Set<SelectionKey> keys()
keys
in interface SelectorHandler
public 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 SelectorHandler
ctx
- Context
IOException
protected void processPendingOperations(Context ctx) throws IOException
ctx
-
IOException
protected void onConnectOp(Context ctx, TCPSelectorHandler.ConnectChannelOperation selectionKeyOp) throws IOException
IOException
public Set<SelectionKey> select(Context ctx) throws IOException
select
in interface SelectorHandler
ctx
- Context
Set
of SelectionKey
IOException
public void postSelect(Context ctx)
postSelect
in interface SelectorHandler
ctx
- Context
public void addPendingIO(Runnable runnable)
addPendingIO
in interface PendingIOhandler
public void addPendingKeyCancel(SelectionKey key)
addPendingKeyCancel
in interface PendingIOhandler
public void register(SelectionKey key, int ops)
register
in interface SelectorHandler
key
- ops
- public void register(SelectableChannel channel, int ops)
SelectorHandler
SelectableChannel
on the Selector
.
register
in interface SelectorHandler
ops
- interested operationspublic void register(SelectableChannel channel, int ops, Object attachment)
SelectorHandler
SelectableChannel
on the Selector
.
register
in interface SelectorHandler
ops
- interested operationsprotected 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
IOException
public void pause()
SelectorHandler
pause
in interface SelectorHandler
public void resume()
SelectorHandler
resume
in interface SelectorHandler
public StateHolder<State> getStateHolder()
StateHolder
for this object
getStateHolder
in interface SupportStateHolder<State>
StateHolder
public void shutdown()
shutdown
in interface SelectorHandler
public SelectableChannel acceptWithoutRegistration(SelectionKey key) throws IOException
acceptWithoutRegistration
in interface SelectorHandler
SelectableChannel
IOException
public boolean onAcceptInterest(SelectionKey key, Context ctx) throws IOException
onAcceptInterest
in interface SelectorHandler
ctx
- Context
key
- SelectionKey
IOException
public boolean onReadInterest(SelectionKey key, Context ctx) throws IOException
onReadInterest
in interface SelectorHandler
ctx
- Context
key
- SelectionKey
CallbackHandler
, otherwise true
IOException
public boolean onWriteInterest(SelectionKey key, Context ctx) throws IOException
onWriteInterest
in interface SelectorHandler
key
- SelectionKey
ctx
- Context
IOException
public boolean onConnectInterest(SelectionKey key, Context ctx) throws IOException
onConnectInterest
in interface SelectorHandler
key
- SelectionKey
ctx
- 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
IOException
public ConnectorHandler acquireConnectorHandler()
ConnectorHandler
,
which is the TCPConnectorHandler
acquireConnectorHandler
in interface SelectorHandler
ConnectorHandler
public void releaseConnectorHandler(ConnectorHandler connectorHandler)
releaseConnectorHandler
in interface SelectorHandler
connectorHandler
- ConnectorHandler
public Controller.Protocol protocol()
protocol
in interface SelectorHandler
public void configureChannel(SelectableChannel channel) throws IOException
configureChannel
in interface SelectorHandler
channel
- SelectableChannel
to configure
IOException
- on possible configuration related errorpublic final Selector getSelector()
getSelector
in interface SelectorHandler
public final void setSelector(Selector selector)
SelectorHandler
Selector
setSelector
in interface SelectorHandler
selector
- underlying Selector
public AsyncQueueReader getAsyncQueueReader()
AsyncQueueReader
associated with this
SelectorHandler
. Method will return null, if this
SelectorHandler
is not running.
getAsyncQueueReader
in interface SelectorHandler
AsyncQueueReader
public AsyncQueueWriter getAsyncQueueWriter()
AsyncQueueWriter
associated with this
SelectorHandler
. Method will return null, if this
SelectorHandler
is not running.
getAsyncQueueWriter
in interface SelectorHandler
AsyncQueueWriter
public 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
Role
public 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 Role
public 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 PortRange getPortRange()
public void setPortRange(PortRange portRange)
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 boolean isKeepAlive()
public void setKeepAlive(boolean isKeepAlive)
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 SelectorHandler
Controller
's
ExecutorService
should be used.public void setThreadPool(ExecutorService threadPool)
ExecutorService
used to execute this
SelectorHandler
's SelectionKey
ops
setThreadPool
in interface SelectorHandler
public Class<? extends SelectionKeyHandler> getPreferredSelectionKeyHandler()
getPreferredSelectionKeyHandler
in interface SelectorHandler
public SelectionKeyHandler getSelectionKeyHandler()
getSelectionKeyHandler
in interface SelectorHandler
public void setSelectionKeyHandler(SelectionKeyHandler selectionKeyHandler)
setSelectionKeyHandler
in interface SelectorHandler
public void setProtocolChainInstanceHandler(ProtocolChainInstanceHandler instanceHandler)
ProtocolChainInstanceHandler
to use for
creating instance of ProtocolChain
.
setProtocolChainInstanceHandler
in interface SelectorHandler
public ProtocolChainInstanceHandler getProtocolChainInstanceHandler()
ProtocolChainInstanceHandler
getProtocolChainInstanceHandler
in interface SelectorHandler
public 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
Context
protected Callable<ConnectorHandler> getConnectorInstanceHandlerDelegate()
ConnectorInstanceHandler
corresponding to the protocol
public Object removeAttribute(String key)
removeAttribute
in interface AttributeHolder
key
- - name of an attribute
public void setAttribute(String key, Object value)
setAttribute
in interface AttributeHolder
key
- - name of an attributevalue
- - value of named attributepublic Object getAttribute(String key)
getAttribute
in interface AttributeHolder
key
- - 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 AttributeHolder
attributes
- - 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 AttributeHolder
Map
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.
Role
public void resetSpinCounter()
resetSpinCounter
in interface LinuxSpinningWorkaround
public int getSpinRate()
Selector
spinrate
getSpinRate
in interface LinuxSpinningWorkaround
public void workaroundSelectorSpin() throws IOException
workaroundSelectorSpin
in interface LinuxSpinningWorkaround
IOException
public SelectionKey keyFor(SelectableChannel channel)
SelectionKey
, which represents binding between
the SelectableChannel
and this SelectorHandler
keyFor
in interface SelectorHandler
channel
- 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 isExecutePendingIOUsingSelectorThread()
public void setExecutePendingIOUsingSelectorThread(boolean executePendingIOUsingSelectorThread)
executePendingIOUsingSelectorThread
- the executePendingIOUsingSelectorThread to setpublic void setMaxAcceptRetries(int maxAcceptRetries)
maxAcceptRetries
-
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |