org.glassfish.grizzly.nio
Class AbstractNIOConnection

java.lang.Object
  extended by org.glassfish.grizzly.nio.AbstractNIOConnection
All Implemented Interfaces:
Closeable, AttributeStorage, Connection<SocketAddress>, NIOConnection, Readable<SocketAddress>, Writable<SocketAddress>
Direct Known Subclasses:
TCPNIOConnection, UDPNIOConnection

public abstract class AbstractNIOConnection
extends Object
implements NIOConnection

Common Connection implementation for Java NIO Connections.

Author:
Alexey Stashok

Field Summary
protected  AsyncQueue<AsyncReadQueueRecord> asyncReadQueue
           
protected  AsyncQueue<AsyncWriteQueueRecord> asyncWriteQueue
           
protected  AttributeHolder attributes
           
protected  SelectableChannel channel
           
protected  boolean isBlocking
           
protected  AtomicBoolean isClosed
           
protected  Processor processor
           
protected  ProcessorSelector processorSelector
           
protected  int readBufferSize
           
protected  SelectionKey selectionKey
           
protected  SelectorRunner selectorRunner
           
protected  NIOTransport transport
           
protected  int writeBufferSize
           
 
Constructor Summary
AbstractNIOConnection(NIOTransport transport)
           
 
Method Summary
 void close()
          Close the Connection
 void configureBlocking(boolean isBlocking)
          Returns the Connection mode.
 void disableIOEvent(IOEvent ioEvent)
           
 void enableIOEvent(IOEvent ioEvent)
           
 AsyncQueue<AsyncReadQueueRecord> getAsyncReadQueue()
           
 AsyncQueue<AsyncWriteQueueRecord> getAsyncWriteQueue()
           
 AttributeHolder getAttributes()
          Get associated AttributeHolder.
 SelectableChannel getChannel()
           
 Processor getProcessor()
          Gets the default Processor, which will process Connection I/O events.
 ProcessorSelector getProcessorSelector()
          Gets the default ProcessorSelector, which will be used to get Processor to process Connection I/O events, in case if this Connection's Processor is null.
 int getReadBufferSize()
          Get the default size of Buffers, which will be allocated for reading data from Connection.
 SelectionKey getSelectionKey()
           
 SelectorRunner getSelectorRunner()
           
 Transport getTransport()
          Get the Transport, to which this Connection belongs to.
 int getWriteBufferSize()
          Get the default size of Buffers, which will be allocated for writing data to Connection.
 boolean isBlocking()
          Sets the Connection mode.
 boolean isOpen()
          Is Connection open and ready.
 AttributeHolder obtainAttributes()
          Get associated AttributeHolder.
protected abstract  void preClose()
           
 Future<ReadResult<Buffer,SocketAddress>> read()
          Method reads data.
 Future<ReadResult<Buffer,SocketAddress>> read(Buffer buffer)
          Method reads data to the buffer.
 Future<ReadResult<Buffer,SocketAddress>> read(Buffer buffer, CompletionHandler<ReadResult<Buffer,SocketAddress>> completionHandler)
          Method reads data to the buffer.
protected  void setChannel(SelectableChannel channel)
           
 void setProcessor(Processor preferableProcessor)
          Sets the default Processor, which will process Connection I/O events.
 void setProcessorSelector(ProcessorSelector preferableProcessorSelector)
          Sets the default ProcessorSelector, which will be used to get Processor to process Connection I/O events, in case if this Connection's Processor is null.
 void setReadBufferSize(int readBufferSize)
          Set the default size of Buffers, which will be allocated for reading data from Connection.
protected  void setSelectionKey(SelectionKey selectionKey)
           
protected  void setSelectorRunner(SelectorRunner selectorRunner)
           
 void setWriteBufferSize(int writeBufferSize)
          Set the default size of Buffers, which will be allocated for writing data to Connection.
 Future<WriteResult<Buffer,SocketAddress>> write(Buffer buffer)
          Method writes the buffer.
 Future<WriteResult<Buffer,SocketAddress>> write(Buffer buffer, CompletionHandler<WriteResult<Buffer,SocketAddress>> completionHandler)
          Method writes the buffer.
 Future<WriteResult<Buffer,SocketAddress>> write(SocketAddress dstAddress, Buffer buffer)
          Method writes the buffer to the specific address.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.glassfish.grizzly.Connection
getLocalAddress, getPeerAddress, getStreamReader, getStreamWriter
 
Methods inherited from interface org.glassfish.grizzly.Readable
read
 
Methods inherited from interface org.glassfish.grizzly.Writable
write
 

Field Detail

transport

protected final NIOTransport transport

readBufferSize

protected int readBufferSize

writeBufferSize

protected int writeBufferSize

selectorRunner

protected SelectorRunner selectorRunner

channel

protected SelectableChannel channel

selectionKey

protected SelectionKey selectionKey

processor

protected Processor processor

processorSelector

protected ProcessorSelector processorSelector

attributes

protected final AttributeHolder attributes

asyncReadQueue

protected final AsyncQueue<AsyncReadQueueRecord> asyncReadQueue

asyncWriteQueue

protected final AsyncQueue<AsyncWriteQueueRecord> asyncWriteQueue

isClosed

protected AtomicBoolean isClosed

isBlocking

protected boolean isBlocking
Constructor Detail

AbstractNIOConnection

public AbstractNIOConnection(NIOTransport transport)
Method Detail

configureBlocking

public void configureBlocking(boolean isBlocking)
Description copied from interface: Connection
Returns the Connection mode. true, if Connection is operating in blocking mode, or false otherwise.

Specified by:
configureBlocking in interface Connection<SocketAddress>

isBlocking

public boolean isBlocking()
Description copied from interface: Connection
Sets the Connection mode.

Specified by:
isBlocking in interface Connection<SocketAddress>

getTransport

public Transport getTransport()
Description copied from interface: Connection
Get the Transport, to which this Connection belongs to.

Specified by:
getTransport in interface Connection<SocketAddress>
Returns:
the Transport, to which this Connection belongs to.

getReadBufferSize

public int getReadBufferSize()
Description copied from interface: Connection
Get the default size of Buffers, which will be allocated for reading data from Connection.

Specified by:
getReadBufferSize in interface Connection<SocketAddress>
Returns:
the default size of Buffers, which will be allocated for reading data from Connection.

setReadBufferSize

public void setReadBufferSize(int readBufferSize)
Description copied from interface: Connection
Set the default size of Buffers, which will be allocated for reading data from Connection.

Specified by:
setReadBufferSize in interface Connection<SocketAddress>
Parameters:
readBufferSize - the default size of Buffers, which will be allocated for reading data from Connection.

getWriteBufferSize

public int getWriteBufferSize()
Description copied from interface: Connection
Get the default size of Buffers, which will be allocated for writing data to Connection.

Specified by:
getWriteBufferSize in interface Connection<SocketAddress>
Returns:
the default size of Buffers, which will be allocated for writing data to Connection.

setWriteBufferSize

public void setWriteBufferSize(int writeBufferSize)
Description copied from interface: Connection
Set the default size of Buffers, which will be allocated for writing data to Connection.

Specified by:
setWriteBufferSize in interface Connection<SocketAddress>
Parameters:
writeBufferSize - the default size of Buffers, which will be allocated for writing data to Connection.

getSelectorRunner

public SelectorRunner getSelectorRunner()
Specified by:
getSelectorRunner in interface NIOConnection

setSelectorRunner

protected void setSelectorRunner(SelectorRunner selectorRunner)

getChannel

public SelectableChannel getChannel()
Specified by:
getChannel in interface NIOConnection

setChannel

protected void setChannel(SelectableChannel channel)

getSelectionKey

public SelectionKey getSelectionKey()
Specified by:
getSelectionKey in interface NIOConnection

setSelectionKey

protected void setSelectionKey(SelectionKey selectionKey)

getProcessor

public Processor getProcessor()
Description copied from interface: Connection
Gets the default Processor, which will process Connection I/O events. If Processor is null, - then Transport will try to get Processor using Connection's ProcessorSelector.select(IOEvent, Connection). If ProcessorSelector, associated withthe Connection is also null - Transport will try to get Processor using own settings.

Specified by:
getProcessor in interface Connection<SocketAddress>
Returns:
the default Processor, which will process Connection I/O events.

setProcessor

public void setProcessor(Processor preferableProcessor)
Description copied from interface: Connection
Sets the default Processor, which will process Connection I/O events. If Processor is null, - then Transport will try to get Processor using Connection's ProcessorSelector.select(IOEvent, Connection). If ProcessorSelector, associated withthe Connection is also null - Transport will try to get Processor using own settings.

Specified by:
setProcessor in interface Connection<SocketAddress>
Parameters:
preferableProcessor - the default Processor, which will process Connection I/O events.

getProcessorSelector

public ProcessorSelector getProcessorSelector()
Description copied from interface: Connection
Gets the default ProcessorSelector, which will be used to get Processor to process Connection I/O events, in case if this Connection's Processor is null.

Specified by:
getProcessorSelector in interface Connection<SocketAddress>
Returns:
the default ProcessorSelector, which will be used to get Processor to process Connection I/O events, in case if this Connection's Processor is null.

setProcessorSelector

public void setProcessorSelector(ProcessorSelector preferableProcessorSelector)
Description copied from interface: Connection
Sets the default ProcessorSelector, which will be used to get Processor to process Connection I/O events, in case if this Connection's Processor is null.

Specified by:
setProcessorSelector in interface Connection<SocketAddress>
Parameters:
preferableProcessorSelector - the default ProcessorSelector, which will be used to get Processor to process Connection I/O events, in case if this Connection's Processor is null.

getAsyncReadQueue

public AsyncQueue<AsyncReadQueueRecord> getAsyncReadQueue()

getAsyncWriteQueue

public AsyncQueue<AsyncWriteQueueRecord> getAsyncWriteQueue()

getAttributes

public AttributeHolder getAttributes()
Description copied from interface: AttributeStorage
Get associated AttributeHolder. Implementation may return null if AttributeHolder wasn't initialized yet.

Specified by:
getAttributes in interface AttributeStorage
Returns:
associated AttributeHolder. Implementation may return null if AttributeHolder wasn't initialized yet.

obtainAttributes

public AttributeHolder obtainAttributes()
Description copied from interface: AttributeStorage
Get associated AttributeHolder. Unlike AttributeStorage.getAttributes() - method never returns null.

Specified by:
obtainAttributes in interface AttributeStorage
Returns:
associated AttributeHolder. Unlike AttributeStorage.getAttributes() - method never returns null.

read

public Future<ReadResult<Buffer,SocketAddress>> read()
                                              throws IOException
Description copied from interface: Readable
Method reads data.

Specified by:
read in interface Readable<SocketAddress>
Returns:
Future, using which it's possible to check the result
Throws:
IOException

read

public Future<ReadResult<Buffer,SocketAddress>> read(Buffer buffer)
                                              throws IOException
Description copied from interface: Readable
Method reads data to the buffer.

Specified by:
read in interface Readable<SocketAddress>
Parameters:
buffer - the buffer, where data will be read
Returns:
Future, using which it's possible to check the result
Throws:
IOException

read

public Future<ReadResult<Buffer,SocketAddress>> read(Buffer buffer,
                                                     CompletionHandler<ReadResult<Buffer,SocketAddress>> completionHandler)
                                              throws IOException
Description copied from interface: Readable
Method reads data to the buffer.

Specified by:
read in interface Readable<SocketAddress>
Parameters:
buffer - the buffer, where data will be read
completionHandler - CompletionHandler, which will get notified, when read will be completed
Returns:
Future, using which it's possible to check the result
Throws:
IOException

write

public Future<WriteResult<Buffer,SocketAddress>> write(Buffer buffer)
                                                throws IOException
Description copied from interface: Writable
Method writes the buffer.

Specified by:
write in interface Writable<SocketAddress>
Parameters:
buffer - the buffer, from which the data will be written
Returns:
Future, using which it's possible to check the result
Throws:
IOException

write

public Future<WriteResult<Buffer,SocketAddress>> write(Buffer buffer,
                                                       CompletionHandler<WriteResult<Buffer,SocketAddress>> completionHandler)
                                                throws IOException
Description copied from interface: Writable
Method writes the buffer.

Specified by:
write in interface Writable<SocketAddress>
Parameters:
buffer - the buffer, from which the data will be written
completionHandler - CompletionHandler, which will get notified, when write will be completed
Returns:
Future, using which it's possible to check the result
Throws:
IOException

write

public Future<WriteResult<Buffer,SocketAddress>> write(SocketAddress dstAddress,
                                                       Buffer buffer)
                                                throws IOException
Description copied from interface: Writable
Method writes the buffer to the specific address.

Specified by:
write in interface Writable<SocketAddress>
Parameters:
dstAddress - the destination address the buffer will be sent to
buffer - the buffer, from which the data will be written
Returns:
Future, using which it's possible to check the result
Throws:
IOException

isOpen

public boolean isOpen()
Description copied from interface: Connection
Is Connection open and ready. Returns true, if connection is open and ready, or false otherwise.

Specified by:
isOpen in interface Connection<SocketAddress>
Returns:
true, if connection is open and ready, or false otherwise.

close

public void close()
           throws IOException
Description copied from interface: Connection
Close the Connection

Specified by:
close in interface Closeable
Specified by:
close in interface Connection<SocketAddress>
Throws:
IOException

preClose

protected abstract void preClose()

enableIOEvent

public void enableIOEvent(IOEvent ioEvent)
                   throws IOException
Specified by:
enableIOEvent in interface NIOConnection
Throws:
IOException

disableIOEvent

public void disableIOEvent(IOEvent ioEvent)
                    throws IOException
Specified by:
disableIOEvent in interface NIOConnection
Throws:
IOException


Copyright © 2009 SUN Microsystems. All Rights Reserved.