com.sun.grizzly
Class TCPConnectorHandler

java.lang.Object
  extended by com.sun.grizzly.AbstractConnectorHandler<TCPSelectorHandler,CallbackHandler>
      extended by com.sun.grizzly.TCPConnectorHandler
All Implemented Interfaces:
AsyncQueueReadable, AsyncQueueWritable, ConnectorHandler<TCPSelectorHandler,CallbackHandler>, Handler, Closeable

public class TCPConnectorHandler
extends AbstractConnectorHandler<TCPSelectorHandler,CallbackHandler>

Non blocking TCP Connector Handler. The recommended way to use this class is by creating an external Controller and share the same SelectorHandler instance.

Recommended -----------


 Controller controller = new Controller();
 // new TCPSelectorHandler(true) means the Selector will be used only
 // for client operation (OP_READ, OP_WRITE, OP_CONNECT).
 TCPSelectorHandler tcpSelectorHandler = new TCPSelectorHandler(true);
 controller.setSelectorHandler(tcpSelectorHandler);
 TCPConnectorHandler tcpConnectorHandler = new TCPConnectorHandler();
 tcpConnectorHandler.connect(localhost,port, new CallbackHandler(){...},
                             tcpSelectorHandler);
 TCPConnectorHandler tcpConnectorHandler2 = new TCPConnectorHandler();
 tcpConnectorHandler2.connect(localhost,port, new CallbackHandler(){...},
                             tcpSelectorHandler);
 

Not recommended (but still works) ---------------------------------


 TCPConnectorHandler tcpConnectorHandler = new TCPConnectorHandler();
 tcpConnectorHandler.connect(localhost,port);
 

Internally, a new Controller will be created everytime connect(localhost,port) is invoked, which has an impact on performance.

Author:
Jeanfrancois Arcand

Field Summary
protected  int connectionTimeout
          Connection timeout is milliseconds
protected  int linger
          The socket linger.
protected  boolean reuseAddress
          The socket reuseAddress
protected  boolean tcpNoDelay
          The socket tcpDelay.
 
Fields inherited from class com.sun.grizzly.AbstractConnectorHandler
callbackHandler, controller, inputStream, isConnected, protocol, selectorHandler, underlyingChannel
 
Constructor Summary
TCPConnectorHandler()
           
 
Method Summary
 void close()
          Close the underlying connection.
 void configureChannel(SelectableChannel channel)
          
 void connect(SocketAddress remoteAddress, SocketAddress localAddress)
          Connect to hostname:port.
 void connect(SocketAddress remoteAddress, SocketAddress localAddress, CallbackHandler callbackHandler, TCPSelectorHandler selectorHandler)
          Connect to hostname:port.
 void finishConnect(SelectionKey key)
          Finish handling the OP_CONNECT interest ops.
 int getConnectionTimeout()
          Get TCP channel connection timeout in milliseconds
 int getLinger()
           
 boolean isReuseAddress()
           
 boolean isTcpNoDelay()
          Return the tcpNoDelay value used by the underlying accepted Sockets.
 Controller.Protocol protocol()
          A token decribing the protocol supported by an implementation of this interface
 void setConnectionTimeout(int connectionTimeout)
          Set TCP channel connection timeout in milliseconds
 void setLinger(int linger)
           
 void setReuseAddress(boolean reuseAddress)
           
 void setTcpNoDelay(boolean tcpNoDelay)
          Enable (true) or disable (false) the underlying Socket's tcpNoDelay.
 
Methods inherited from class com.sun.grizzly.AbstractConnectorHandler
connect, connect, connect, connect, getCallbackHandler, getController, getSelectorHandler, getUnderlyingChannel, isConnected, protocol, read, readFromAsyncQueue, readFromAsyncQueue, readFromAsyncQueue, setCallbackHandler, setController, setSelectorHandler, setUnderlyingChannel, write, writeToAsyncQueue, writeToAsyncQueue, writeToAsyncQueue, writeToAsyncQueue, writeToAsyncQueue, writeToAsyncQueue, writeToAsyncQueue, writeToAsyncQueue
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

tcpNoDelay

protected boolean tcpNoDelay
The socket tcpDelay. Default value for tcpNoDelay.


reuseAddress

protected boolean reuseAddress
The socket reuseAddress


linger

protected int linger
The socket linger.


connectionTimeout

protected int connectionTimeout
Connection timeout is milliseconds

Constructor Detail

TCPConnectorHandler

public TCPConnectorHandler()
Method Detail

connect

public void connect(SocketAddress remoteAddress,
                    SocketAddress localAddress,
                    CallbackHandler callbackHandler,
                    TCPSelectorHandler selectorHandler)
             throws IOException
Connect to hostname:port. When an aysnchronous event happens (e.g OP_READ or OP_WRITE), the Controller will invoke the CallBackHandler.

Parameters:
remoteAddress - remote address to connect
localAddress - local address to bin
callbackHandler - the handler invoked by its associated SelectorHandler when a non blocking operation is ready to be handled. When null, all read and write operation will be delegated to the default ProtocolChain and its list of ProtocolFilter . When null, this ConnectorHandler will create an instance of DefaultCallbackHandler.
selectorHandler - an instance of SelectorHandler.
Throws:
IOException

connect

public void connect(SocketAddress remoteAddress,
                    SocketAddress localAddress)
             throws IOException
Connect to hostname:port. Internally an instance of Controller and its default SelectorHandler will be created everytime this method is called. This method should be used only and only if no external Controller has been initialized.

Parameters:
remoteAddress - remote address to connect
localAddress - local address to bin
Throws:
IOException

close

public void close()
           throws IOException
Close the underlying connection.

Throws:
IOException

finishConnect

public void finishConnect(SelectionKey key)
                   throws IOException
Finish handling the OP_CONNECT interest ops.

Parameters:
key - - a SelectionKey
Throws:
IOException

configureChannel

public void configureChannel(SelectableChannel channel)
                      throws IOException

Throws:
IOException

protocol

public final Controller.Protocol protocol()
A token decribing the protocol supported by an implementation of this interface

Specified by:
protocol in interface ConnectorHandler<TCPSelectorHandler,CallbackHandler>
Overrides:
protocol in class AbstractConnectorHandler<TCPSelectorHandler,CallbackHandler>
Returns:
this ConnectorHandler's protocol

isTcpNoDelay

public boolean isTcpNoDelay()
Return the tcpNoDelay value used by the underlying accepted Sockets. Also see setTcpNoDelay(boolean tcpNoDelay)


setTcpNoDelay

public void setTcpNoDelay(boolean tcpNoDelay)
Enable (true) or disable (false) the underlying Socket's tcpNoDelay. Default value for tcpNoDelay is disabled (set to false). Disabled by default since enabling tcpNoDelay for most applications can cause packets to appear to arrive in a fragmented fashion where it takes multiple OP_READ events (i.e. multiple calls to read small messages). The common behaviour seen when this occurs is that often times a small number of bytes, as small as 1 byte at a time is read per OP_READ event dispatch. This results in a large number of system calls to read(), system calls to enable and disable interest ops and potentially a large number of thread context switches between a thread doing the Select(ing) and a worker thread doing the read. The Connector side should also set tcpNoDelay the same as it is set here whenever possible.


getLinger

public int getLinger()
Returns:
the linger value.
See Also:
java.net.Socket#setLinger()

setLinger

public void setLinger(int linger)
See Also:
java.net.Socket#setLinger()

getConnectionTimeout

public int getConnectionTimeout()
Get TCP channel connection timeout in milliseconds

Returns:
TCP channel connection timeout in milliseconds

setConnectionTimeout

public void setConnectionTimeout(int connectionTimeout)
Set TCP channel connection timeout in milliseconds

Parameters:
connectionTimeout - TCP channel connection timeout in milliseconds

isReuseAddress

public boolean isReuseAddress()
See Also:
java.net.Socket#setReuseAddress()

setReuseAddress

public void setReuseAddress(boolean reuseAddress)
See Also:
java.net.Socket#setReuseAddress()


Copyright © 2012 Oracle Corporation. All Rights Reserved.