com.sun.grizzly
Class ReusableTCPSelectorHandler

java.lang.Object
  extended by com.sun.grizzly.TCPSelectorHandler
      extended by com.sun.grizzly.ReusableTCPSelectorHandler
All Implemented Interfaces:
Handler, LinuxSpinningWorkaround, SelectorHandler, PendingIOhandler, AttributeHolder, Copyable, SupportStateHolder<State>

public class ReusableTCPSelectorHandler
extends TCPSelectorHandler

This class extends a TCP implementation of a SelectorHandler and reuses the accepted SocketChannel if a user tries to connect the same remote address. This class is useful for only CLIENT_SERVER Role. When the ServerSocketChannel of TCPSelectorHandler accepts a SocketChannel, it is stored in acceptedSocketChannelMap which is based on the key(remote SocketAddress) and the value(SocketChannel) pair. Because of reusing the SocketChannel, if a user has initialized the Selectorhandler with inet, when a user tries to connect the remote with a local address for binding it, duplicated binding will be prevented. Here is scenario. - accepted a SocketChannel from a remoteAddress - tries to connect the remoteAddress with Controller like this. "connectorHandler = controller.acquireConnectorHandler( Protocol.TCP );" "connectorHandler.connect( remoteAddress, localAddress );" Then, the accepted SocketChannel of the remoteAddress is reused for I/O operations. Note: Actually, connectorHandler.close() doesn't allow the SocketChannel to be closed because the SocketChannel is shared between server-side and client-side. But you should guarantee calling connectorHandler.close() in order to prevent any connection leak, after you have used connectorHandler.connect() once.

Author:
Bongjae Chang

Nested Class Summary
 
Nested classes/interfaces inherited from class com.sun.grizzly.TCPSelectorHandler
TCPSelectorHandler.ConnectChannelOperation, TCPSelectorHandler.RegisterChannelOperation, TCPSelectorHandler.RegisterKeyOperation, TCPSelectorHandler.RunnableOperation, TCPSelectorHandler.SelectionKeyCancelOperation
 
Field Summary
 
Fields inherited from class com.sun.grizzly.TCPSelectorHandler
asyncQueueReader, asyncQueueWriter, attributes, connectorInstanceHandler, inet, instanceHandler, isKeepAlive, isShutDown, linger, logger, portRange, postponedTasks, receiveBufferSize, reuseAddress, role, selectionKeyHandler, selector, selectorHandlerTasks, selectTimeout, sendBufferSize, serverSocket, serverSocketChannel, serverTimeout, socketTimeout, ssBackLog, stateHolder, tcpNoDelay, threadPool
 
Constructor Summary
ReusableTCPSelectorHandler()
           
 
Method Summary
 SelectableChannel acceptWithoutRegistration(SelectionKey key)
          Accepts connection, without registering it for reading or writing
 void closeChannel(SelectableChannel channel)
          Closes SelectableChannel
 void copyTo(Copyable copy)
          Copies current object content to copy object
protected  SelectableChannel getSelectableChannel(SocketAddress remoteAddress, SocketAddress localAddress)
           
 void shutdown()
          Shutdown this instance by closing its Selector and associated channels.
 
Methods inherited from class com.sun.grizzly.TCPSelectorHandler
acquireConnectorHandler, addPendingIO, addPendingKeyCancel, boolean2Role, configureChannel, connect, getAsyncQueueReader, getAsyncQueueWriter, getAttribute, getAttributes, getConnectorInstanceHandlerDelegate, getInet, getLinger, getLogger, getPort, getPortLowLevel, getPortRange, getPreferredSelectionKeyHandler, getProtocolChainInstanceHandler, getRole, getSelectionKeyHandler, getSelector, getSelectTimeout, getServerTimeout, getSocketTimeout, getSpinRate, getSsBackLog, getStateHolder, getThreadPool, invokeAsyncQueueReader, invokeAsyncQueueWriter, invokeCallbackHandler, isExecutePendingIOUsingSelectorThread, isKeepAlive, isOpen, isReuseAddress, isTcpNoDelay, keyFor, keys, onAcceptInterest, onConnectInterest, onConnectOp, onReadInterest, onWriteInterest, pause, pollContext, postSelect, preSelect, processPendingOperations, protocol, register, register, register, releaseConnectorHandler, removeAttribute, resetSpinCounter, resume, select, setAttribute, setAttributes, setExecutePendingIOUsingSelectorThread, setInet, setKeepAlive, setLinger, setLogger, setMaxAcceptRetries, setPort, setPortRange, setProtocolChainInstanceHandler, setReceiveBufferSize, setReuseAddress, setRole, setSelectionKeyHandler, setSelector, setSelectTimeout, setSendBufferSize, setServerTimeout, setSocketTimeout, setSsBackLog, setTcpNoDelay, setThreadPool, workaroundSelectorSpin
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReusableTCPSelectorHandler

public ReusableTCPSelectorHandler()
Method Detail

copyTo

public void copyTo(Copyable copy)
Description copied from interface: Copyable
Copies current object content to copy object

Specified by:
copyTo in interface Copyable
Overrides:
copyTo in class TCPSelectorHandler
Parameters:
copy - represents target object, where current object's content will be copied

getSelectableChannel

protected SelectableChannel getSelectableChannel(SocketAddress remoteAddress,
                                                 SocketAddress localAddress)
                                          throws IOException
Overrides:
getSelectableChannel in class TCPSelectorHandler
Throws:
IOException

shutdown

public void shutdown()
Shutdown this instance by closing its Selector and associated channels.

Specified by:
shutdown in interface SelectorHandler
Overrides:
shutdown in class TCPSelectorHandler

acceptWithoutRegistration

public SelectableChannel acceptWithoutRegistration(SelectionKey key)
                                            throws IOException
Accepts connection, without registering it for reading or writing

Specified by:
acceptWithoutRegistration in interface SelectorHandler
Overrides:
acceptWithoutRegistration in class TCPSelectorHandler
Returns:
accepted SelectableChannel
Throws:
IOException

closeChannel

public void closeChannel(SelectableChannel channel)
Closes SelectableChannel

Specified by:
closeChannel in interface SelectorHandler
Overrides:
closeChannel in class TCPSelectorHandler


Copyright © 2012 Oracle Corporation. All Rights Reserved.