com.sun.grizzly
Class AbstractConnectorHandler<E extends SelectorHandler,K extends CallbackHandler>

java.lang.Object
  extended by com.sun.grizzly.AbstractConnectorHandler<E,K>
All Implemented Interfaces:
AsyncQueueReadable, AsyncQueueWritable, ConnectorHandler<E,K>, Handler, Closeable
Direct Known Subclasses:
CacheableConnectorHandler, SSLConnectorHandler, TCPConnectorHandler, UDPConnectorHandler

public abstract class AbstractConnectorHandler<E extends SelectorHandler,K extends CallbackHandler>
extends Object
implements ConnectorHandler<E,K>

Abstract ConnectorHandler definition, which preimplements common getter/setter methods.

Author:
Alexey Stashok

Field Summary
protected  K callbackHandler
          A CallbackHandler handler invoked by the SelectorHandler when a non blocking operation is ready to be processed.
protected  Controller controller
          The internal Controller used (in case not specified).
protected  InputReader inputStream
          A blocking InputStream that use a pool of Selector to execute a blocking read operation.
protected  boolean isConnected
          Is the connection established.
protected  Controller.Protocol protocol
          The ConnectorHandler Controller.Protocol.
protected  E selectorHandler
          The underlying SelectorHandler used to mange SelectionKeys.
protected  SelectableChannel underlyingChannel
          The connection's SelectableChannel.
 
Constructor Summary
AbstractConnectorHandler()
           
 
Method Summary
 void connect(SocketAddress remoteAddress)
          Connect to hostname:port.
 void connect(SocketAddress remoteAddress, K callbackHandler)
          Connect to hostname:port.
 void connect(SocketAddress remoteAddress, K callbackHandler, E selectorHandler)
          Connect to hostname:port.
 void connect(SocketAddress remoteAddress, SocketAddress localAddress, K callbackHandler)
          Connect to hostname:port.
 K getCallbackHandler()
          Return the CallbackHandler.
 Controller getController()
          Return the Controller
 E getSelectorHandler()
          Return the associated SelectorHandler.
 SelectableChannel getUnderlyingChannel()
          Return the current SelectableChannel used.
 boolean isConnected()
          Is the underlying channel connected.
 Controller.Protocol protocol()
          Get the ConnectorHandler Controller.Protocol.
 void protocol(Controller.Protocol protocol)
          Set the ConnectorHandler Controller.Protocol.
 long read(ByteBuffer byteBuffer, boolean blocking)
          Read bytes.
 Future<AsyncQueueReadUnit> readFromAsyncQueue(ByteBuffer buffer, AsyncReadCallbackHandler callbackHandler)
          Method reads data to the ByteBuffer using async read queue.
 Future<AsyncQueueReadUnit> readFromAsyncQueue(ByteBuffer buffer, AsyncReadCallbackHandler callbackHandler, AsyncReadCondition condition)
          Method reads data to the ByteBuffer using async read queue.
 Future<AsyncQueueReadUnit> readFromAsyncQueue(ByteBuffer buffer, AsyncReadCallbackHandler callbackHandler, AsyncReadCondition condition, AsyncQueueDataProcessor readPostProcessor)
          Method reads data to the ByteBuffer using async read queue.
 void setCallbackHandler(K callbackHandler)
          Set the CallbackHandler.
 void setController(Controller controller)
          Set the Controller to use with this instance.
 void setSelectorHandler(E selectorHandler)
          Set the associated SelectorHandler
 void setUnderlyingChannel(SelectableChannel underlyingChannel)
          Set the SelectableChannel.
 long write(ByteBuffer byteBuffer, boolean blocking)
          Writes bytes.
 Future<AsyncQueueWriteUnit> writeToAsyncQueue(ByteBuffer buffer)
          Method writes ByteBuffer using async write queue.
 Future<AsyncQueueWriteUnit> writeToAsyncQueue(ByteBuffer buffer, AsyncWriteCallbackHandler callbackHandler)
          Method writes ByteBuffer using async write queue.
 Future<AsyncQueueWriteUnit> writeToAsyncQueue(ByteBuffer buffer, AsyncWriteCallbackHandler callbackHandler, AsyncQueueDataProcessor writePreProcessor)
          Method writes ByteBuffer using async write queue.
 Future<AsyncQueueWriteUnit> writeToAsyncQueue(ByteBuffer buffer, AsyncWriteCallbackHandler callbackHandler, AsyncQueueDataProcessor writePreProcessor, ByteBufferCloner cloner)
          Method writes ByteBuffer using async write queue.
 Future<AsyncQueueWriteUnit> writeToAsyncQueue(SocketAddress dstAddress, ByteBuffer buffer)
          Method sends ByteBuffer using async write queue.
 Future<AsyncQueueWriteUnit> writeToAsyncQueue(SocketAddress dstAddress, ByteBuffer buffer, AsyncWriteCallbackHandler callbackHandler)
          Method sends ByteBuffer using async write queue.
 Future<AsyncQueueWriteUnit> writeToAsyncQueue(SocketAddress dstAddress, ByteBuffer buffer, AsyncWriteCallbackHandler callbackHandler, AsyncQueueDataProcessor writePreProcessor)
          Method sends ByteBuffer using async write queue.
 Future<AsyncQueueWriteUnit> writeToAsyncQueue(SocketAddress dstAddress, ByteBuffer buffer, AsyncWriteCallbackHandler callbackHandler, AsyncQueueDataProcessor writePreProcessor, ByteBufferCloner cloner)
          Method sends ByteBuffer using async write queue.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.sun.grizzly.ConnectorHandler
close, connect, connect, finishConnect
 

Field Detail

protocol

protected Controller.Protocol protocol
The ConnectorHandler Controller.Protocol.


controller

protected Controller controller
The internal Controller used (in case not specified).


selectorHandler

protected E extends SelectorHandler selectorHandler
The underlying SelectorHandler used to mange SelectionKeys.


callbackHandler

protected K extends CallbackHandler callbackHandler
A CallbackHandler handler invoked by the SelectorHandler when a non blocking operation is ready to be processed.


underlyingChannel

protected SelectableChannel underlyingChannel
The connection's SelectableChannel.


isConnected

protected volatile boolean isConnected
Is the connection established.


inputStream

protected InputReader inputStream
A blocking InputStream that use a pool of Selector to execute a blocking read operation.

Constructor Detail

AbstractConnectorHandler

public AbstractConnectorHandler()
Method Detail

protocol

public Controller.Protocol protocol()
Get the ConnectorHandler Controller.Protocol.

Specified by:
protocol in interface ConnectorHandler<E extends SelectorHandler,K extends CallbackHandler>
Returns:
the ConnectorHandler Controller.Protocol.

protocol

public void protocol(Controller.Protocol protocol)
Set the ConnectorHandler Controller.Protocol.

Parameters:
protocol - the ConnectorHandler Controller.Protocol.

setController

public void setController(Controller controller)
Set the Controller to use with this instance.

Specified by:
setController in interface ConnectorHandler<E extends SelectorHandler,K extends CallbackHandler>
Parameters:
controller - the Controller to use with this instance.

getController

public Controller getController()
Return the Controller

Specified by:
getController in interface ConnectorHandler<E extends SelectorHandler,K extends CallbackHandler>
Returns:
the Controller

getSelectorHandler

public E getSelectorHandler()
Return the associated SelectorHandler.

Specified by:
getSelectorHandler in interface ConnectorHandler<E extends SelectorHandler,K extends CallbackHandler>
Returns:
the associated SelectorHandler.

connect

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

Specified by:
connect in interface ConnectorHandler<E extends SelectorHandler,K extends CallbackHandler>
Parameters:
remoteAddress - remote address to connect
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.
Throws:
IOException

connect

public void connect(SocketAddress remoteAddress)
             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.

Specified by:
connect in interface ConnectorHandler<E extends SelectorHandler,K extends CallbackHandler>
Parameters:
remoteAddress - remote address to connect
Throws:
IOException

connect

public void connect(SocketAddress remoteAddress,
                    K callbackHandler,
                    E 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.

Specified by:
connect in interface ConnectorHandler<E extends SelectorHandler,K extends CallbackHandler>
Parameters:
remoteAddress - remote address to connect
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,
                    K callbackHandler)
             throws IOException
Connect to hostname:port. When an aysnchronous event happens (e.g OP_READ or OP_WRITE), the Controller will invoke the CallBackHandler.

Specified by:
connect in interface ConnectorHandler<E extends SelectorHandler,K extends CallbackHandler>
Parameters:
remoteAddress - remote address to connect
localAddress - local address to bind
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.
Throws:
IOException

setSelectorHandler

public void setSelectorHandler(E selectorHandler)
Set the associated SelectorHandler

Parameters:
selectorHandler - the associated SelectorHandler.

getUnderlyingChannel

public SelectableChannel getUnderlyingChannel()
Return the current SelectableChannel used.

Specified by:
getUnderlyingChannel in interface ConnectorHandler<E extends SelectorHandler,K extends CallbackHandler>
Returns:
the current SelectableChannel used.

setUnderlyingChannel

public void setUnderlyingChannel(SelectableChannel underlyingChannel)
Set the SelectableChannel.

Parameters:
underlyingChannel - the SelectableChannel to use.

getCallbackHandler

public K getCallbackHandler()
Return the CallbackHandler.

Specified by:
getCallbackHandler in interface ConnectorHandler<E extends SelectorHandler,K extends CallbackHandler>
Returns:
the CallbackHandler.

setCallbackHandler

public void setCallbackHandler(K callbackHandler)
Set the CallbackHandler.

Specified by:
setCallbackHandler in interface ConnectorHandler<E extends SelectorHandler,K extends CallbackHandler>
Parameters:
callbackHandler - the CallbackHandler.

isConnected

public boolean isConnected()
Is the underlying channel connected.

Specified by:
isConnected in interface ConnectorHandler<E extends SelectorHandler,K extends CallbackHandler>
Returns:
true if connected, otherwise false

read

public long read(ByteBuffer byteBuffer,
                 boolean blocking)
          throws IOException
Read bytes. If blocking is set to true, a pool of temporary Selector will be used to read bytes.

Specified by:
read in interface ConnectorHandler<E extends SelectorHandler,K extends CallbackHandler>
Parameters:
byteBuffer - The byteBuffer to store bytes.
blocking - true if a a pool of temporary Selector is required to handle a blocking read.
Returns:
number of bytes read
Throws:
IOException

write

public long write(ByteBuffer byteBuffer,
                  boolean blocking)
           throws IOException
Writes bytes. If blocking is set to true, a pool of temporary Selector will be used to writes bytes.

Specified by:
write in interface ConnectorHandler<E extends SelectorHandler,K extends CallbackHandler>
Parameters:
byteBuffer - The byteBuffer to write.
blocking - true if a a pool of temporary Selector is required to handle a blocking write.
Returns:
number of bytes written
Throws:
IOException

readFromAsyncQueue

public Future<AsyncQueueReadUnit> readFromAsyncQueue(ByteBuffer buffer,
                                                     AsyncReadCallbackHandler callbackHandler)
                                              throws IOException
Method reads data to the ByteBuffer using async read queue. First, if read queue is empty - it tries to read to the ByteBuffer directly (without putting to the queue). If associated read queue is not empty or after direct reading ByteBuffer still has remaining place for next read - ByteBuffer will be added to AsyncQueue. If an exception occurs, during direct reading - it will be propagated to the caller directly, otherwise, if the ByteBuffer is added to a reading queue - exception notification will come via AsyncReadCallbackHandler.onIOException()

Specified by:
readFromAsyncQueue in interface AsyncQueueReadable
Parameters:
buffer - ByteBuffer
callbackHandler - AsyncReadCallbackHandler, which will get notified, when ByteBuffer will get full
Returns:
true, if ByteBuffer was read completely, false if read operation was put to queue
Throws:
IOException

readFromAsyncQueue

public Future<AsyncQueueReadUnit> readFromAsyncQueue(ByteBuffer buffer,
                                                     AsyncReadCallbackHandler callbackHandler,
                                                     AsyncReadCondition condition)
                                              throws IOException
Method reads data to the ByteBuffer using async read queue. First, if read queue is empty - it tries to read to the ByteBuffer directly (without putting to the queue). If associated read queue is not empty or after direct reading ByteBuffer still has remaining place for next read - ByteBuffer will be added to AsyncQueue. If an exception occurs, during direct reading - it will be propagated to the caller directly, otherwise, if the ByteBuffer is added to a reading queue - exception notification will come via AsyncReadCallbackHandler.onIOException()

Specified by:
readFromAsyncQueue in interface AsyncQueueReadable
Parameters:
buffer - ByteBuffer
callbackHandler - AsyncReadCallbackHandler, which will get notified, when ByteBuffer will get full
condition - AsyncReadCondition, which will be called to check if read data is complete, and callbackHandler could be called
Returns:
true, if ByteBuffer was read completely, false if read operation was put to queue
Throws:
IOException

readFromAsyncQueue

public Future<AsyncQueueReadUnit> readFromAsyncQueue(ByteBuffer buffer,
                                                     AsyncReadCallbackHandler callbackHandler,
                                                     AsyncReadCondition condition,
                                                     AsyncQueueDataProcessor readPostProcessor)
                                              throws IOException
Method reads data to the ByteBuffer using async read queue. First, if read queue is empty - it tries to read to the ByteBuffer directly (without putting to the queue). If associated read queue is not empty or after direct reading ByteBuffer still has remaining place for next read - ByteBuffer will be added to AsyncQueue. If an exception occurs, during direct reading - it will be propagated to the caller directly, otherwise, if the ByteBuffer is added to a reading queue - exception notification will come via AsyncReadCallbackHandler.onIOException()

Specified by:
readFromAsyncQueue in interface AsyncQueueReadable
Parameters:
buffer - ByteBuffer
callbackHandler - AsyncReadCallbackHandler, which will get notified, when ByteBuffer will get full
condition - AsyncReadCondition, which will be called to check if read data is complete, and callbackHandler could be called
readPostProcessor - post processor, to be called to process read data
Returns:
true, if ByteBuffer was read completely, false if read operation was put to queue
Throws:
IOException

writeToAsyncQueue

public Future<AsyncQueueWriteUnit> writeToAsyncQueue(ByteBuffer buffer)
                                              throws IOException
Method writes ByteBuffer using async write queue. First, if write queue is empty - it tries to write ByteBuffer directly (without putting to the queue). If associated write queue is not empty or after direct writing ByteBuffer still has ready data to be written - ByteBuffer will be added to AsyncQueue. If an exception occurs, during direct writing - it will be propagated to the caller directly, otherwise it will be just logged by Grizzly framework.

Specified by:
writeToAsyncQueue in interface AsyncQueueWritable
Parameters:
buffer - ByteBuffer
Throws:
IOException

writeToAsyncQueue

public Future<AsyncQueueWriteUnit> writeToAsyncQueue(ByteBuffer buffer,
                                                     AsyncWriteCallbackHandler callbackHandler)
                                              throws IOException
Method writes ByteBuffer using async write queue. First, if write queue is empty - it tries to write ByteBuffer directly (without putting to the queue). If associated write queue is not empty or after direct writing ByteBuffer still has ready data to be written - ByteBuffer will be added to AsyncQueue. If an exception occurs, during direct writing - it will be propagated to the caller directly, otherwise, if the ByteBuffer is added to a writing queue - exception notification will come via AsyncWriteCallbackHandler.onIOException()

Specified by:
writeToAsyncQueue in interface AsyncQueueWritable
Parameters:
buffer - ByteBuffer
callbackHandler - AsyncWriteCallbackHandler, which will get notified, when ByteBuffer will be completely written
Throws:
IOException

writeToAsyncQueue

public Future<AsyncQueueWriteUnit> writeToAsyncQueue(ByteBuffer buffer,
                                                     AsyncWriteCallbackHandler callbackHandler,
                                                     AsyncQueueDataProcessor writePreProcessor)
                                              throws IOException
Method writes ByteBuffer using async write queue. First, if write queue is empty - it tries to write ByteBuffer directly (without putting to the queue). If associated write queue is not empty or after direct writing ByteBuffer still has ready data to be written - ByteBuffer will be added to AsyncQueue. If an exception occurs, during direct writing - it will be propagated to the caller directly, otherwise, if the ByteBuffer is added to a writing queue - exception notification will come via AsyncWriteCallbackHandler.onIOException() Before data will be written on SelectableChannel, first it will be passed for preprocessing to AsyncQueueDataProcessor, and then preprocessor result data (AsyncQueueDataProcessor.getResultByteBuffer()) will be written on the SelectableChannel.

Specified by:
writeToAsyncQueue in interface AsyncQueueWritable
Parameters:
buffer - ByteBuffer
callbackHandler - AsyncWriteCallbackHandler, which will get notified, when ByteBuffer will be completely written
writePreProcessor - AsyncQueueDataProcessor, which will perform data processing, before it will be written on SelectableChannel
Throws:
IOException

writeToAsyncQueue

public Future<AsyncQueueWriteUnit> writeToAsyncQueue(ByteBuffer buffer,
                                                     AsyncWriteCallbackHandler callbackHandler,
                                                     AsyncQueueDataProcessor writePreProcessor,
                                                     ByteBufferCloner cloner)
                                              throws IOException
Method writes ByteBuffer using async write queue. First, if write queue is empty - it tries to write ByteBuffer directly (without putting to the queue). If associated write queue is not empty or after direct writing ByteBuffer still has ready data to be written - ByteBuffer will be added to AsyncQueue. If an exception occurs, during direct writing - it will be propagated to the caller directly, otherwise, if the ByteBuffer is added to a writing queue - exception notification will come via AsyncWriteCallbackHandler.onIOException() Before data will be written on SelectableChannel, first it will be passed for preprocessing to AsyncQueueDataProcessor, and then preprocessor result data (AsyncQueueDataProcessor.getResultByteBuffer()) will be written on the SelectableChannel.

Specified by:
writeToAsyncQueue in interface AsyncQueueWritable
Parameters:
buffer - ByteBuffer
callbackHandler - AsyncWriteCallbackHandler, which will get notified, when ByteBuffer will be completely written
writePreProcessor - AsyncQueueDataProcessor, which will perform data processing, before it will be written on SelectableChannel
Throws:
IOException

writeToAsyncQueue

public Future<AsyncQueueWriteUnit> writeToAsyncQueue(SocketAddress dstAddress,
                                                     ByteBuffer buffer)
                                              throws IOException
Method sends ByteBuffer using async write queue. First, if write queue is empty - it tries to send ByteBuffer to the given SocketAddress directly (without putting to the queue). If associated write queue is not empty or after direct sending ByteBuffer still has ready data to be written - ByteBuffer will be added to AsyncQueue. If an exception occurs, during direct writing - it will be propagated to the caller directly, otherwise it will be just logged by Grizzly framework.

Specified by:
writeToAsyncQueue in interface AsyncQueueWritable
Parameters:
dstAddress - destination SocketAddress data will be sent to
buffer - ByteBuffer
Throws:
IOException

writeToAsyncQueue

public Future<AsyncQueueWriteUnit> writeToAsyncQueue(SocketAddress dstAddress,
                                                     ByteBuffer buffer,
                                                     AsyncWriteCallbackHandler callbackHandler)
                                              throws IOException
Method sends ByteBuffer using async write queue. First, if write queue is empty - it tries to send ByteBuffer to the given SocketAddress directly (without putting to the queue). If associated write queue is not empty or after direct sending ByteBuffer still has ready data to be written - ByteBuffer will be added to AsyncQueue. If an exception occurs, during direct writing - it will be propagated to the caller directly, otherwise, if the ByteBuffer is added to a writing queue - exception notification will come via AsyncWriteCallbackHandler.onIOException()

Specified by:
writeToAsyncQueue in interface AsyncQueueWritable
Parameters:
dstAddress - destination SocketAddress data will be sent to
buffer - ByteBuffer
callbackHandler - AsyncWriteCallbackHandler, which will get notified, when ByteBuffer will be completely written
Throws:
IOException

writeToAsyncQueue

public Future<AsyncQueueWriteUnit> writeToAsyncQueue(SocketAddress dstAddress,
                                                     ByteBuffer buffer,
                                                     AsyncWriteCallbackHandler callbackHandler,
                                                     AsyncQueueDataProcessor writePreProcessor)
                                              throws IOException
Method sends ByteBuffer using async write queue. First, if write queue is empty - it tries to send ByteBuffer to the given SocketAddress directly (without putting to the queue). If associated write queue is not empty or after direct sending ByteBuffer still has ready data to be written - ByteBuffer will be added to AsyncQueue. If an exception occurs, during direct writing - it will be propagated to the caller directly, otherwise, if the ByteBuffer is added to a writing queue - exception notification will come via AsyncWriteCallbackHandler.onIOException() Before data will be written on SelectableChannel, first it will be passed for preprocessing to AsyncQueueDataProcessor, and then preprocessor result data (AsyncQueueDataProcessor.getResultByteBuffer()) will be written on the SelectableChannel.

Specified by:
writeToAsyncQueue in interface AsyncQueueWritable
Parameters:
dstAddress - destination SocketAddress data will be sent to
buffer - ByteBuffer
callbackHandler - AsyncWriteCallbackHandler, which will get notified, when ByteBuffer will be completely written
writePreProcessor - AsyncQueueDataProcessor, which will perform data processing, before it will be written on SelectableChannel
Throws:
IOException

writeToAsyncQueue

public Future<AsyncQueueWriteUnit> writeToAsyncQueue(SocketAddress dstAddress,
                                                     ByteBuffer buffer,
                                                     AsyncWriteCallbackHandler callbackHandler,
                                                     AsyncQueueDataProcessor writePreProcessor,
                                                     ByteBufferCloner cloner)
                                              throws IOException
Method sends ByteBuffer using async write queue. First, if write queue is empty - it tries to send ByteBuffer to the given SocketAddress directly (without putting to the queue). If associated write queue is not empty or after direct sending ByteBuffer still has ready data to be written - ByteBuffer will be added to AsyncQueue. If an exception occurs, during direct writing - it will be propagated to the caller directly, otherwise, if the ByteBuffer is added to a writing queue - exception notification will come via AsyncWriteCallbackHandler.onIOException() Before data will be written on SelectableChannel, first it will be passed for preprocessing to AsyncQueueDataProcessor, and then preprocessor result data (AsyncQueueDataProcessor.getResultByteBuffer()) will be written on the SelectableChannel.

Specified by:
writeToAsyncQueue in interface AsyncQueueWritable
Parameters:
dstAddress - destination SocketAddress data will be sent to
buffer - ByteBuffer
callbackHandler - AsyncWriteCallbackHandler, which will get notified, when ByteBuffer will be completely written
writePreProcessor - AsyncQueueDataProcessor, which will perform data processing, before it will be written on SelectableChannel
Throws:
IOException


Copyright © 2012 Oracle Corporation. All Rights Reserved.