|
||||||||||
| 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 boolean |
isClient
Is this used for client only or client/server operation. |
protected AtomicBoolean |
isShutDown
Flag, which shows whether shutdown was called for this SelectorHandler |
protected int |
linger
The socket linger. |
protected Logger |
logger
|
protected ConcurrentLinkedQueue<SelectionKeyOP> |
opToRegister
The list of SelectionKeyOP to register next time the
Selector.select is invoked. |
protected Pipeline |
pipeline
The Pipeline used by this instance. |
protected int |
port
The default TCP port. |
protected boolean |
reuseAddress
The socket reuseAddress |
protected SelectionKeyHandler |
selectionKeyHandler
The SelectionKeyHandler associated with this SelectorHandler. |
protected Selector |
selector
The single Selector. |
protected long |
selectTimeout
The Selector time out. |
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. |
| Constructor Summary | |
|---|---|
TCPSelectorHandler()
|
|
TCPSelectorHandler(boolean isClient)
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 |
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 |
InetAddress |
getInet()
|
int |
getLinger()
|
Logger |
getLogger()
|
int |
getPort()
|
int |
getPortLowLevel()
Returns port number SelectorHandler is listening on
Similar to getPort(), but getting port number directly from
connection (ServerSocket, DatagramSocket). |
Class<? extends SelectionKeyHandler> |
getPreferredSelectionKeyHandler()
Get the preffered SelectionKeyHandler implementation for this SelectorHandler. |
ProtocolChainInstanceHandler |
getProtocolChainInstanceHandler()
Return the ProtocolChainInstanceHandler |
SelectionKeyHandler |
getSelectionKeyHandler()
Get the SelectionKeyHandler associated with this SelectorHandler. |
Selector |
getSelector()
Gets the underlying selector. |
long |
getSelectTimeout()
|
int |
getServerTimeout()
|
int |
getSocketTimeout()
|
int |
getSsBackLog()
|
StateHolder<State> |
getStateHolder()
Gets StateHolder for this object |
protected void |
initOpRegistriesIfRequired()
Initializes SelectionKey operation registries |
protected void |
invokeAsyncQueueReader(Context context)
Invoke a AsyncQueueReader |
protected void |
invokeAsyncQueueWriter(Context context)
Invoke a AsyncQueueWriter |
protected void |
invokeCallbackHandler(CallbackHandler callbackHandler,
Context context)
Invoke a CallbackHandler via a Context instance. |
boolean |
isOpen()
Is the Selector open. |
boolean |
isReuseAddress()
|
boolean |
isTcpNoDelay()
Return the tcpNoDelay value used by the underlying accepted Sockets. |
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. |
protected void |
onReadOp(SelectionKeyOP selectionKeyOp)
Handle new OP_READ ops. |
boolean |
onWriteInterest(SelectionKey key,
Context ctx)
Handle OP_WRITE. |
protected void |
onWriteOp(SelectionKeyOP selectionKeyOp)
Handle new OP_WRITE ops. |
void |
pause()
Pause this SelectorHandler |
Pipeline |
pipeline()
Return the Pipeline used to execute this
SelectorHandler's SelectionKey ops |
protected Context |
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(), make sure the ServerScoketChannel 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. |
void |
releaseConnectorHandler(ConnectorHandler connectorHandler)
Release a ConnectorHandler. |
Object |
removeAttribute(String key)
Remove a key/value object. |
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 |
setInet(InetAddress inet)
|
void |
setLinger(int linger)
|
void |
setLogger(Logger logger)
|
void |
setPipeline(Pipeline pipeline)
Set the Pipeline used to execute this
SelectorHandler's SelectionKey ops |
void |
setPort(int port)
|
void |
setProtocolChainInstanceHandler(ProtocolChainInstanceHandler instanceHandler)
Set the ProtocolChainInstanceHandler to use for
creating instance of ProtocolChain. |
void |
setReuseAddress(boolean reuseAddress)
|
void |
setSelectionKeyHandler(SelectionKeyHandler selectionKeyHandler)
Set SelectionKeyHandler associated with this SelectorHandler. |
void |
setSelector(Selector selector)
Sets the underlying Selector |
void |
setSelectTimeout(long selectTimeout)
|
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 |
shutdown()
Shuntdown this instance by closing its Selector and associated channels. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
protected ConnectorInstanceHandler connectorInstanceHandler
protected ConcurrentLinkedQueue<SelectionKeyOP> opToRegister
SelectionKeyOP to register next time the
Selector.select is invoked.
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 boolean isClient
protected SelectionKeyHandler selectionKeyHandler
protected ProtocolChainInstanceHandler instanceHandler
protected Pipeline pipeline
Pipeline used by this instance. If null -
Controller's Pipeline 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 AtomicBoolean isShutDown
SelectorHandler
| Constructor Detail |
|---|
public TCPSelectorHandler()
public TCPSelectorHandler(boolean isClient)
isClient - true if this SelectorHandler is only used
to handle ConnectorHandler.| Method Detail |
|---|
public void copyTo(Copyable copy)
Copyable
copyTo in interface Copyablecopy - represents target object, where current object's content will be copiedpublic Set<SelectionKey> keys()
keys in interface SelectorHandlerSet of SelectionKeypublic boolean isOpen()
isOpen in interface SelectorHandler
public void preSelect(Context ctx)
throws IOException
preSelect in interface SelectorHandlerctx - Context
IOException
protected void processPendingOperations(Context ctx)
throws IOException
IOException
protected void onReadOp(SelectionKeyOP selectionKeyOp)
throws ClosedChannelException
ClosedChannelException
protected void onWriteOp(SelectionKeyOP selectionKeyOp)
throws ClosedChannelException
ClosedChannelException
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 Context
IOExceptionpublic void postSelect(Context ctx)
postSelect in interface SelectorHandlerctx - Context
public void register(SelectionKey key,
int ops)
register in interface SelectorHandlerops - interested operations
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
IOExceptionpublic void pause()
SelectorHandler
pause in interface SelectorHandlerpublic void resume()
SelectorHandler
resume in interface SelectorHandlerpublic StateHolder<State> getStateHolder()
StateHolder for this object
getStateHolder in interface SupportStateHolder<State>StateHolderpublic 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,
Context context)
throws IOException
context - Context
IOException
protected void invokeAsyncQueueReader(Context context)
throws IOException
AsyncQueueReader
context - Context
IOException
protected void invokeAsyncQueueWriter(Context 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 SelectorHandlerprotected void initOpRegistriesIfRequired()
SelectionKey operation registries
public void configureChannel(SelectableChannel channel)
throws IOException
configureChannel in interface SelectorHandlerchannel - SelectableChannel to configure
IOException - on possible configuration related errorpublic 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 SelectorHandlerAsyncQuqueWriterpublic long getSelectTimeout()
public void setSelectTimeout(long selectTimeout)
public int getServerTimeout()
public void setServerTimeout(int serverTimeout)
public InetAddress getInet()
public void setInet(InetAddress inet)
public int getPortLowLevel()
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 Pipeline pipeline()
Pipeline used to execute this
SelectorHandler's SelectionKey ops
pipeline in interface SelectorHandlerController's
Pipeline should be used.public void setPipeline(Pipeline pipeline)
Pipeline used to execute this
SelectorHandler's SelectionKey ops
setPipeline 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 Context 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
- Returns:
factory
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 pairs
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||