org.glassfish.grizzly.nio
Class NIOConnection

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

public abstract class NIOConnection
extends Object
implements Connection<SocketAddress>

Common Connection implementation for Java NIO Connections.

Author:
Alexey Stashok

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.glassfish.grizzly.Connection
Connection.CloseListener, Connection.CloseType
 
Field Summary
protected  TaskQueue<AsyncReadQueueRecord> asyncReadQueue
           
protected  TaskQueue<AsyncWriteQueueRecord> asyncWriteQueue
           
protected  AttributeHolder attributes
           
protected  SelectableChannel channel
           
protected  AtomicReference<Connection.CloseType> closeTypeFlag
           
protected  AtomicReference<Object> connectCloseSemaphor
           
protected  boolean isBlocking
           
protected  boolean isStandalone
           
protected  int maxAsyncWriteQueueSize
           
protected  MonitoringConfigImpl<ConnectionProbe> monitoringConfig
          Connection probes
protected static Object NOTIFICATION_CLOSED_COMPLETE
           
protected static Object NOTIFICATION_INITIALIZED
           
protected  Processor processor
           
protected  ProcessorSelector processorSelector
           
protected  int readBufferSize
           
protected  long readTimeoutMillis
           
protected  SelectionKey selectionKey
           
protected  SelectorRunner selectorRunner
           
protected  NIOTransport transport
           
protected  int writeBufferSize
           
protected  long writeTimeoutMillis
           
protected  short zeroByteReadCount
           
 
Constructor Summary
NIOConnection(NIOTransport transport)
           
 
Method Summary
 void addCloseListener(Connection.CloseListener closeListener)
          Add the Connection.CloseListener, which will be notified once Connection will be closed.
 void attachToSelectorRunner(SelectorRunner selectorRunner)
           
protected  void checkEmptyRead(int size)
           
 GrizzlyFuture<Connection> close()
          Close the Connection
 void close(CompletionHandler<Connection> completionHandler)
          Close the Connection
protected  void close0(CompletionHandler<Connection> completionHandler, boolean isClosedLocally)
           
 void closeSilently()
          Close the Connection silently, no notification required on completion or failure.
 void configureBlocking(boolean isBlocking)
          Sets the Connection mode.
 void configureStandalone(boolean isStandalone)
           
 void detachSelectorRunner()
           
 void disableIOEvent(IOEvent ioEvent)
           
 void enableIOEvent(IOEvent ioEvent)
           
 TaskQueue<AsyncReadQueueRecord> getAsyncReadQueue()
           
 TaskQueue<AsyncWriteQueueRecord> getAsyncWriteQueue()
           
 AttributeHolder getAttributes()
          Get associated AttributeHolder.
 SelectableChannel getChannel()
           
 int getMaxAsyncWriteQueueSize()
          Get the max size (in bytes) of asynchronous write queue associated with connection.
 MonitoringConfig<ConnectionProbe> getMonitoringConfig()
          Return the object associated MonitoringConfig.
 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.
 long getReadTimeout(TimeUnit timeUnit)
           
 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.
 long getWriteTimeout(TimeUnit timeUnit)
           
 boolean isBlocking()
           
 boolean isOpen()
          Is Connection open and ready.
 boolean isStandalone()
           
 void notifyConnectionError(Throwable error)
          Method gets invoked, when error occur during the Connection lifecycle.
protected static void notifyIOEventDisabled(NIOConnection connection, IOEvent ioEvent)
          Notify registered ConnectionProbes about the IO Event disabled event.
protected static void notifyIOEventEnabled(NIOConnection connection, IOEvent ioEvent)
          Notify registered ConnectionProbes about the IO Event enabled event.
protected static void notifyIOEventReady(NIOConnection connection, IOEvent ioEvent)
          Notify registered ConnectionProbes about the IO Event ready event.
protected static void notifyProbesAccept(NIOConnection serverConnection, NIOConnection clientConnection)
          Notify registered ConnectionProbes about the accept event.
protected static void notifyProbesBind(NIOConnection connection)
          Notify registered ConnectionProbes about the bind event.
protected static void notifyProbesClose(NIOConnection connection)
          Notify registered ConnectionProbes about the close event.
protected static void notifyProbesConnect(NIOConnection connection)
          Notify registered ConnectionProbes about the connect event.
protected static void notifyProbesError(NIOConnection connection, Throwable error)
          Notify registered ConnectionProbes about the error.
protected static void notifyProbesRead(NIOConnection connection, Buffer data, int size)
          Notify registered ConnectionProbes about the read event.
protected static void notifyProbesWrite(NIOConnection connection, Buffer data, long size)
          Notify registered ConnectionProbes about the write event.
 Processor obtainProcessor(IOEvent ioEvent)
          Gets the Processor, which will process Connection I/O event.
<E> E
obtainProcessorState(Processor processor, NullaryFunction<E> factory)
          Returns the Processor state associated with this Connection.
protected  void preClose()
           
<M> GrizzlyFuture<ReadResult<M,SocketAddress>>
read()
          Method reads data.
<M> void
read(CompletionHandler<ReadResult<M,SocketAddress>> completionHandler)
           
 boolean removeCloseListener(Connection.CloseListener closeListener)
          Remove the Connection.CloseListener.
protected  void setChannel(SelectableChannel channel)
           
 void setMaxAsyncWriteQueueSize(int maxAsyncWriteQueueSize)
          Set the max size (in bytes) of asynchronous write queue associated with connection.
 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.
 void setReadTimeout(long timeout, TimeUnit timeUnit)
           
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.
 void setWriteTimeout(long timeout, TimeUnit timeUnit)
           
 void simulateIOEvent(IOEvent ioEvent)
           
<M> GrizzlyFuture<WriteResult<M,SocketAddress>>
write(M message)
          Method writes the buffer.
<M> void
write(M message, CompletionHandler<WriteResult<M,SocketAddress>> completionHandler)
          Method writes the buffer.
<M> void
write(M message, CompletionHandler<WriteResult<M,SocketAddress>> completionHandler, PushBackHandler pushbackHandler)
          Method writes the buffer.
<M> void
write(SocketAddress dstAddress, M message, CompletionHandler<WriteResult<M,SocketAddress>> completionHandler, PushBackHandler pushbackHandler)
          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
 

Field Detail

NOTIFICATION_INITIALIZED

protected static final Object NOTIFICATION_INITIALIZED

NOTIFICATION_CLOSED_COMPLETE

protected static final Object NOTIFICATION_CLOSED_COMPLETE

transport

protected final NIOTransport transport

readBufferSize

protected volatile int readBufferSize

writeBufferSize

protected volatile int writeBufferSize

maxAsyncWriteQueueSize

protected volatile int maxAsyncWriteQueueSize

readTimeoutMillis

protected volatile long readTimeoutMillis

writeTimeoutMillis

protected volatile long writeTimeoutMillis

channel

protected volatile SelectableChannel channel

selectionKey

protected volatile SelectionKey selectionKey

selectorRunner

protected volatile SelectorRunner selectorRunner

processor

protected volatile Processor processor

processorSelector

protected volatile ProcessorSelector processorSelector

attributes

protected final AttributeHolder attributes

asyncReadQueue

protected final TaskQueue<AsyncReadQueueRecord> asyncReadQueue

asyncWriteQueue

protected final TaskQueue<AsyncWriteQueueRecord> asyncWriteQueue

connectCloseSemaphor

protected final AtomicReference<Object> connectCloseSemaphor

closeTypeFlag

protected final AtomicReference<Connection.CloseType> closeTypeFlag

isBlocking

protected volatile boolean isBlocking

isStandalone

protected volatile boolean isStandalone

zeroByteReadCount

protected short zeroByteReadCount

monitoringConfig

protected final MonitoringConfigImpl<ConnectionProbe> monitoringConfig
Connection probes

Constructor Detail

NIOConnection

public NIOConnection(NIOTransport transport)
Method Detail

configureBlocking

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

Specified by:
configureBlocking in interface Connection<SocketAddress>
Parameters:
isBlocking - the Connection mode. true, if Connection should operate in blocking mode, or false otherwise.

isBlocking

public boolean isBlocking()
Specified by:
isBlocking in interface Connection<SocketAddress>
Returns:
the Connection mode. true, if Connection is operating in blocking mode, or false otherwise.

configureStandalone

public void configureStandalone(boolean isStandalone)
Specified by:
configureStandalone in interface Connection<SocketAddress>

isStandalone

public boolean isStandalone()
Specified by:
isStandalone 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.

getMaxAsyncWriteQueueSize

public int getMaxAsyncWriteQueueSize()
Get the max size (in bytes) of asynchronous write queue associated with connection.

Specified by:
getMaxAsyncWriteQueueSize in interface Connection<SocketAddress>
Returns:
the max size (in bytes) of asynchronous write queue associated with connection.

setMaxAsyncWriteQueueSize

public void setMaxAsyncWriteQueueSize(int maxAsyncWriteQueueSize)
Set the max size (in bytes) of asynchronous write queue associated with connection.

Specified by:
setMaxAsyncWriteQueueSize in interface Connection<SocketAddress>
Parameters:
maxAsyncWriteQueueSize - the max size (in bytes) of asynchronous write queue associated with connection.

getReadTimeout

public long getReadTimeout(TimeUnit timeUnit)
Specified by:
getReadTimeout in interface Connection<SocketAddress>

setReadTimeout

public void setReadTimeout(long timeout,
                           TimeUnit timeUnit)
Specified by:
setReadTimeout in interface Connection<SocketAddress>

getWriteTimeout

public long getWriteTimeout(TimeUnit timeUnit)
Specified by:
getWriteTimeout in interface Connection<SocketAddress>

setWriteTimeout

public void setWriteTimeout(long timeout,
                            TimeUnit timeUnit)
Specified by:
setWriteTimeout in interface Connection<SocketAddress>

getSelectorRunner

public SelectorRunner getSelectorRunner()

setSelectorRunner

protected void setSelectorRunner(SelectorRunner selectorRunner)

attachToSelectorRunner

public void attachToSelectorRunner(SelectorRunner selectorRunner)
                            throws IOException
Throws:
IOException

detachSelectorRunner

public void detachSelectorRunner()
                          throws IOException
Throws:
IOException

getChannel

public SelectableChannel getChannel()

setChannel

protected void setChannel(SelectableChannel channel)

getSelectionKey

public SelectionKey getSelectionKey()

setSelectionKey

protected void setSelectionKey(SelectionKey selectionKey)

obtainProcessor

public Processor obtainProcessor(IOEvent ioEvent)
Description copied from interface: Connection
Gets the Processor, which will process Connection I/O event. 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 - will ask Transport for a Processor.

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

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.

obtainProcessorState

public <E> E obtainProcessorState(Processor processor,
                                  NullaryFunction<E> factory)
Description copied from interface: Connection
Returns the Processor state associated with this Connection.

Specified by:
obtainProcessorState in interface Connection<SocketAddress>
Parameters:
processor - Processor
Returns:
the Processor state associated with this Connection.

getAsyncReadQueue

public TaskQueue<AsyncReadQueueRecord> getAsyncReadQueue()

getAsyncWriteQueue

public TaskQueue<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.

read

public <M> GrizzlyFuture<ReadResult<M,SocketAddress>> read()
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

read

public <M> void read(CompletionHandler<ReadResult<M,SocketAddress>> completionHandler)
Specified by:
read in interface Readable<SocketAddress>

write

public <M> GrizzlyFuture<WriteResult<M,SocketAddress>> write(M message)
Description copied from interface: Writable
Method writes the buffer.

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

write

public <M> void write(M message,
                      CompletionHandler<WriteResult<M,SocketAddress>> completionHandler)
Description copied from interface: Writable
Method writes the buffer.

Specified by:
write in interface Writable<SocketAddress>
Parameters:
message - the buffer, from which the data will be written
completionHandler - CompletionHandler, which will get notified, when write will be completed

write

public <M> void write(M message,
                      CompletionHandler<WriteResult<M,SocketAddress>> completionHandler,
                      PushBackHandler pushbackHandler)
Description copied from interface: Writable
Method writes the buffer.

Specified by:
write in interface Writable<SocketAddress>
Parameters:
message - the buffer, from which the data will be written
completionHandler - CompletionHandler, which will get notified, when write will be completed
pushbackHandler - PushBackHandler, which will be notified if message was accepted by transport write queue or refused

write

public <M> void write(SocketAddress dstAddress,
                      M message,
                      CompletionHandler<WriteResult<M,SocketAddress>> completionHandler,
                      PushBackHandler pushbackHandler)
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
message - the buffer, from which the data will be written
completionHandler - CompletionHandler, which will get notified, when write will be completed
pushbackHandler - PushBackHandler, which will be notified if message was accepted by transport write queue or refused

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 GrizzlyFuture<Connection> close()
Description copied from interface: Connection
Close the Connection

Specified by:
close in interface Closeable<Connection>
Specified by:
close in interface Connection<SocketAddress>
Returns:
Future, which could be checked in case, if close operation will be run asynchronously

close

public void close(CompletionHandler<Connection> completionHandler)
Description copied from interface: Connection
Close the Connection

Specified by:
close in interface Closeable<Connection>
Specified by:
close in interface Connection<SocketAddress>
Parameters:
completionHandler - CompletionHandler to be called, when the connection is closed.

closeSilently

public final void closeSilently()
Description copied from interface: Connection
Close the Connection silently, no notification required on completion or failure.

Specified by:
closeSilently in interface Connection<SocketAddress>

close0

protected void close0(CompletionHandler<Connection> completionHandler,
                      boolean isClosedLocally)

addCloseListener

public void addCloseListener(Connection.CloseListener closeListener)
Add the Connection.CloseListener, which will be notified once Connection will be closed.

Specified by:
addCloseListener in interface Connection<SocketAddress>
Parameters:
closeListener - Connection.CloseListener.

removeCloseListener

public boolean removeCloseListener(Connection.CloseListener closeListener)
Remove the Connection.CloseListener.

Specified by:
removeCloseListener in interface Connection<SocketAddress>
Parameters:
closeListener - Connection.CloseListener.

notifyConnectionError

public void notifyConnectionError(Throwable error)
Method gets invoked, when error occur during the Connection lifecycle.

Specified by:
notifyConnectionError in interface Connection<SocketAddress>
Parameters:
error - Throwable.

getMonitoringConfig

public final MonitoringConfig<ConnectionProbe> getMonitoringConfig()
Return the object associated MonitoringConfig.

Specified by:
getMonitoringConfig in interface Connection<SocketAddress>
Specified by:
getMonitoringConfig in interface MonitoringAware<ConnectionProbe>
Returns:
the Connection monitoring configuration MonitoringConfig.

notifyProbesBind

protected static void notifyProbesBind(NIOConnection connection)
Notify registered ConnectionProbes about the bind event.

Parameters:
connection - the Connection event occurred on.

notifyProbesAccept

protected static void notifyProbesAccept(NIOConnection serverConnection,
                                         NIOConnection clientConnection)
Notify registered ConnectionProbes about the accept event.

Parameters:
serverConnection - the server Connection, which accepted the client connection.
clientConnection - the client Connection.

notifyProbesConnect

protected static void notifyProbesConnect(NIOConnection connection)
Notify registered ConnectionProbes about the connect event.

Parameters:
connection - the Connection event occurred on.

notifyProbesRead

protected static void notifyProbesRead(NIOConnection connection,
                                       Buffer data,
                                       int size)
Notify registered ConnectionProbes about the read event.


notifyProbesWrite

protected static void notifyProbesWrite(NIOConnection connection,
                                        Buffer data,
                                        long size)
Notify registered ConnectionProbes about the write event.


notifyIOEventReady

protected static void notifyIOEventReady(NIOConnection connection,
                                         IOEvent ioEvent)
Notify registered ConnectionProbes about the IO Event ready event.

Parameters:
connection - the Connection event occurred on.
ioEvent - the IOEvent.

notifyIOEventEnabled

protected static void notifyIOEventEnabled(NIOConnection connection,
                                           IOEvent ioEvent)
Notify registered ConnectionProbes about the IO Event enabled event.

Parameters:
connection - the Connection event occurred on.
ioEvent - the IOEvent.

notifyIOEventDisabled

protected static void notifyIOEventDisabled(NIOConnection connection,
                                            IOEvent ioEvent)
Notify registered ConnectionProbes about the IO Event disabled event.

Parameters:
connection - the Connection event occurred on.
ioEvent - the IOEvent.

notifyProbesClose

protected static void notifyProbesClose(NIOConnection connection)
Notify registered ConnectionProbes about the close event.

Parameters:
connection - the Connection event occurred on.

notifyProbesError

protected static void notifyProbesError(NIOConnection connection,
                                        Throwable error)
Notify registered ConnectionProbes about the error.

Parameters:
connection - the Connection event occurred on.

preClose

protected void preClose()

simulateIOEvent

public void simulateIOEvent(IOEvent ioEvent)
                     throws IOException
Specified by:
simulateIOEvent in interface Connection<SocketAddress>
Throws:
IOException

enableIOEvent

public final void enableIOEvent(IOEvent ioEvent)
                         throws IOException
Specified by:
enableIOEvent in interface Connection<SocketAddress>
Throws:
IOException

disableIOEvent

public final void disableIOEvent(IOEvent ioEvent)
                          throws IOException
Specified by:
disableIOEvent in interface Connection<SocketAddress>
Throws:
IOException

checkEmptyRead

protected final void checkEmptyRead(int size)


Copyright © 2012 Oracle Corporation. All Rights Reserved.