XNIO API 3.0.4.GA

org.xnio.channels
Class TranslatingSuspendableChannel<C extends SuspendableChannel,W extends SuspendableChannel>

java.lang.Object
  extended by org.xnio.channels.TranslatingSuspendableChannel<C,W>
Type Parameters:
C - the channel type implemented by this class
W - the channel type being wrapped by this class
All Implemented Interfaces:
Closeable, Channel, InterruptibleChannel, CloseableChannel, Configurable, SuspendableChannel, SuspendableReadChannel, SuspendableWriteChannel, WrappedChannel<W>
Direct Known Subclasses:
FramedMessageChannel

public abstract class TranslatingSuspendableChannel<C extends SuspendableChannel,W extends SuspendableChannel>
extends Object
implements SuspendableChannel, WrappedChannel<W>

An abstract wrapped channel.

Author:
David M. Lloyd

Field Summary
protected  W channel
          The wrapped channel.
 
Constructor Summary
protected TranslatingSuspendableChannel(W channel)
          Construct a new instance.
 
Method Summary
 void awaitReadable()
          Block until this channel becomes readable again.
 void awaitReadable(long time, TimeUnit timeUnit)
          Block until this channel becomes readable again, or until the timeout expires.
 void awaitWritable()
          Block until this channel becomes writable again.
 void awaitWritable(long time, TimeUnit timeUnit)
          Block until this channel becomes writable again, or until the timeout expires.
protected  void clearReadReady()
          Indicate that the channel is no longer definitely immediately readable.
protected  void clearReadRequiresWrite()
          Indicate that the channel no longer requires writability for reads to proceed.
protected  void clearWriteReady()
          Indicate that the channel is no longer definitely immediately writable.
protected  void clearWriteRequiresRead()
          Indicate that the channel no longer requires writability for writes to proceed.
 void close()
          Close this channel.
protected  void closeAction(boolean readShutDown, boolean writeShutDown)
          The action to perform when the channel is closed via the close() method.
 boolean flush()
          Perform channel flush.
protected  boolean flushAction(boolean shutDown)
          The action to perform when the channel is flushed.
 W getChannel()
          Get the channel which is wrapped by this object.
 ChannelListener.Setter<C> getCloseSetter()
          Get the setter which can be used to change the close listener for this channel.
<T> T
getOption(Option<T> option)
          Get the value of a channel option.
 ChannelListener.Setter<C> getReadSetter()
          Get the setter which can be used to change the read listener for this channel.
 XnioExecutor getReadThread()
          Get the read thread for this channel.
 XnioWorker getWorker()
          Get the worker for this channel.
 ChannelListener.Setter<C> getWriteSetter()
          Get the setter which can be used to change the write listener for this channel.
 XnioExecutor getWriteThread()
          Get the write thread for this channel.
protected  void handleClosed()
          Called when the underlying channel is closed.
protected  void handleReadable()
          Called when the underlying channel is readable.
protected  void handleWritable()
          Called when the underlying channel is writable.
 boolean isOpen()
          
 boolean isReadResumed()
          Determine whether reads are resumed.
protected  boolean isReadShutDown()
          Determine whether the channel is shut down for reads.
protected  boolean isWriteComplete()
           
 boolean isWriteResumed()
          Determine whether writes are resumed.
protected  boolean isWriteShutDown()
          Determine whether the channel is shut down for writes.
protected  boolean readRequiresWrite()
          Indicate if the channel is not readable until the write handler is called.
protected  void removeReadRequiresExternal()
          Indicate that one external read task was completed.
protected  void removeWriteRequiresExternal()
          Indicate that one external write task was completed.
 void resumeReads()
          Resume reads on this channel.
 void resumeWrites()
          Resume writes on this channel.
protected  boolean setClosed()
          Set both the channel read and write shut down flags.
<T> T
setOption(Option<T> option, T value)
          Set an option for this channel.
protected  void setReadReady()
          Indicate that the channel is definitely immediately readable, regardless of the underlying channel state.
protected  void setReadRequiresWrite()
          Indicate that the channel will not be readable until the write handler is called.
protected  boolean setReadShutDown()
          Set the channel read shut down flag.
protected  void setWriteReady()
          Indicate that the channel is definitely immediately writable, regardless of the underlying channel state.
protected  void setWriteRequiresRead()
          Indicate that the channel will not be writable until the read handler is called.
protected  boolean setWriteShutDown()
          Set the channel write shut down flag.
 void shutdownReads()
          Perform the read shutdown action if it hasn't been performed already.
protected  void shutdownReadsAction(boolean writeComplete)
          The action to perform when reads are shut down.
 void shutdownWrites()
          Base implementation method which simply delegates the shutdown request to the delegate channel.
protected  void shutdownWritesAction()
          The action to perform when writes are requested to be shut down.
protected  void shutdownWritesComplete(boolean readShutDown)
          Notification that the channel has successfully flushed after having shut down writes.
 boolean supportsOption(Option<?> option)
          Determine whether an option is supported on this channel.
 void suspendReads()
          Suspend further read notifications on this channel.
 void suspendWrites()
          Suspend further write notifications on this channel.
protected  C thisTyped()
          Get this channel, cast to the implemented channel type.
 String toString()
          
protected  boolean tryAddReadRequiresExternal()
          Indicate that read requires an external task to complete.
protected  boolean tryAddWriteRequiresExternal()
          Indicate that write requires an external task to complete.
 void wakeupReads()
          Resume reads on this channel, and force the read listener to be triggered even if the channel isn't actually readable.
 void wakeupWrites()
          Resume writes on this channel, and force the write listener to be triggered even if the channel isn't actually writable.
protected  boolean writeRequiresRead()
          Indicate if the channel is not writable until the read handler is called.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

channel

protected final W extends SuspendableChannel channel
The wrapped channel.

Constructor Detail

TranslatingSuspendableChannel

protected TranslatingSuspendableChannel(W channel)
Construct a new instance.

Parameters:
channel - the channel being wrapped
Method Detail

handleReadable

protected void handleReadable()
Called when the underlying channel is readable.


handleWritable

protected void handleWritable()
Called when the underlying channel is writable.


handleClosed

protected void handleClosed()
Called when the underlying channel is closed.


setReadReady

protected void setReadReady()
Indicate that the channel is definitely immediately readable, regardless of the underlying channel state.


clearReadReady

protected void clearReadReady()
Indicate that the channel is no longer definitely immediately readable.


setReadRequiresWrite

protected void setReadRequiresWrite()
Indicate that the channel will not be readable until the write handler is called.


readRequiresWrite

protected boolean readRequiresWrite()
Indicate if the channel is not readable until the write handler is called.


clearReadRequiresWrite

protected void clearReadRequiresWrite()
Indicate that the channel no longer requires writability for reads to proceed.


tryAddReadRequiresExternal

protected boolean tryAddReadRequiresExternal()
Indicate that read requires an external task to complete.

Returns:
true if the flag was set, false if too many tasks are already outstanding

removeReadRequiresExternal

protected void removeReadRequiresExternal()
Indicate that one external read task was completed. This method should be called once for every time that tryAddReadRequiresExternal() returned true.


setReadShutDown

protected boolean setReadShutDown()
Set the channel read shut down flag.

Returns:
true if the channel has fully closed due to this call, false otherwise

setWriteReady

protected void setWriteReady()
Indicate that the channel is definitely immediately writable, regardless of the underlying channel state.


clearWriteReady

protected void clearWriteReady()
Indicate that the channel is no longer definitely immediately writable.


setWriteRequiresRead

protected void setWriteRequiresRead()
Indicate that the channel will not be writable until the read handler is called.


writeRequiresRead

protected boolean writeRequiresRead()
Indicate if the channel is not writable until the read handler is called.


clearWriteRequiresRead

protected void clearWriteRequiresRead()
Indicate that the channel no longer requires writability for writes to proceed.


tryAddWriteRequiresExternal

protected boolean tryAddWriteRequiresExternal()
Indicate that write requires an external task to complete.

Returns:
true if the flag was set, false if too many tasks are already outstanding

removeWriteRequiresExternal

protected void removeWriteRequiresExternal()
Indicate that one external write task was completed. This method should be called once for every time that tryAddWriteRequiresExternal() returned true.


setWriteShutDown

protected boolean setWriteShutDown()
Set the channel write shut down flag.

Returns:
true if the channel has fully closed due to this call, false otherwise

setClosed

protected boolean setClosed()
Set both the channel read and write shut down flags.

Returns:
true if the channel has fully closed (for the first time) due to this call, false otherwise

thisTyped

protected final C thisTyped()
Get this channel, cast to the implemented channel type.

Returns:
this

getCloseSetter

public ChannelListener.Setter<C> getCloseSetter()
Get the setter which can be used to change the close listener for this channel. If the channel is already closed, then the listener will not be called.

Specified by:
getCloseSetter in interface CloseableChannel
Specified by:
getCloseSetter in interface SuspendableChannel
Specified by:
getCloseSetter in interface SuspendableReadChannel
Specified by:
getCloseSetter in interface SuspendableWriteChannel
Returns:
the setter

getReadSetter

public ChannelListener.Setter<C> getReadSetter()
Get the setter which can be used to change the read listener for this channel. When the listener is called, additional notifications are automatically suspended.

Specified by:
getReadSetter in interface SuspendableChannel
Specified by:
getReadSetter in interface SuspendableReadChannel
Returns:
the setter

getWriteSetter

public ChannelListener.Setter<C> getWriteSetter()
Get the setter which can be used to change the write listener for this channel. When the listener is called, additional notifications are automatically suspended.

Specified by:
getWriteSetter in interface SuspendableChannel
Specified by:
getWriteSetter in interface SuspendableWriteChannel
Returns:
the setter

suspendReads

public void suspendReads()
Suspend further read notifications on this channel.

Specified by:
suspendReads in interface SuspendableReadChannel

resumeReads

public void resumeReads()
Resume reads on this channel. The read listener will be called as soon as there is data available to be read.

Specified by:
resumeReads in interface SuspendableReadChannel

isReadResumed

public boolean isReadResumed()
Description copied from interface: SuspendableReadChannel
Determine whether reads are resumed.

Specified by:
isReadResumed in interface SuspendableReadChannel
Returns:
true if reads are resumed, false if reads are suspended

wakeupReads

public void wakeupReads()
Resume reads on this channel, and force the read listener to be triggered even if the channel isn't actually readable.

Specified by:
wakeupReads in interface SuspendableReadChannel

suspendWrites

public void suspendWrites()
Suspend further write notifications on this channel.

Specified by:
suspendWrites in interface SuspendableWriteChannel

resumeWrites

public void resumeWrites()
Resume writes on this channel. The write listener will be called as soon as the channel becomes writable.

Specified by:
resumeWrites in interface SuspendableWriteChannel

isWriteResumed

public boolean isWriteResumed()
Description copied from interface: SuspendableWriteChannel
Determine whether writes are resumed.

Specified by:
isWriteResumed in interface SuspendableWriteChannel
Returns:
true if writes are resumed, false if writes are suspended

wakeupWrites

public void wakeupWrites()
Resume writes on this channel, and force the write listener to be triggered even if the channel isn't actually writable.

Specified by:
wakeupWrites in interface SuspendableWriteChannel

supportsOption

public boolean supportsOption(Option<?> option)
Determine whether an option is supported on this channel.

Specified by:
supportsOption in interface Configurable
Parameters:
option - the option
Returns:
true if it is supported

getOption

public <T> T getOption(Option<T> option)
            throws IOException
Get the value of a channel option.

Specified by:
getOption in interface Configurable
Type Parameters:
T - the type of the option value
Parameters:
option - the option to get
Returns:
the value of the option, or null if it is not set
Throws:
IOException - if an I/O error occurred when reading the option

setOption

public <T> T setOption(Option<T> option,
                       T value)
            throws IllegalArgumentException,
                   IOException
Set an option for this channel. Unsupported options are ignored.

Specified by:
setOption in interface Configurable
Type Parameters:
T - the type of the option value
Parameters:
option - the option to set
value - the value of the option to set
Returns:
the previous option value, if any
Throws:
IllegalArgumentException - if the value is not acceptable for this option
IOException - if an I/O error occurred when modifying the option

flush

public final boolean flush()
                    throws IOException
Perform channel flush. To change the action taken to flush, subclasses should override flushAction(boolean).

Specified by:
flush in interface SuspendableWriteChannel
Returns:
true if the flush completed, or false if the operation would block
Throws:
IOException - if an error occurs

flushAction

protected boolean flushAction(boolean shutDown)
                       throws IOException
The action to perform when the channel is flushed. By default, this method delegates to the underlying channel. If the shutDown parameter is set, and this method returns true, the underlying channel will be shut down and this method will never be called again (future calls to flush() will flush the underlying channel until it returns true).

Parameters:
shutDown - true if the channel's write side has been shut down, false otherwise
Returns:
true if the flush succeeded, false if it would block
Throws:
IOException - if an error occurs

shutdownWritesComplete

protected void shutdownWritesComplete(boolean readShutDown)
                               throws IOException
Notification that the channel has successfully flushed after having shut down writes. The underlying channel may not yet be fully flushed at this time.

Parameters:
readShutDown - true if the read side was already shut down, false otherwise
Throws:
IOException - if an error occurs

shutdownReads

public void shutdownReads()
                   throws IOException
Perform the read shutdown action if it hasn't been performed already.

Specified by:
shutdownReads in interface SuspendableReadChannel
Throws:
IOException - if an I/O error occurs

shutdownReadsAction

protected void shutdownReadsAction(boolean writeComplete)
                            throws IOException
The action to perform when reads are shut down. By default, this method delegates to the underlying channel.

Parameters:
writeComplete -
Throws:
IOException - if an error occurs

isReadShutDown

protected boolean isReadShutDown()
Determine whether the channel is shut down for reads.

Returns:
whether the channel is shut down for reads

shutdownWrites

public void shutdownWrites()
                    throws IOException
Base implementation method which simply delegates the shutdown request to the delegate channel. Subclasses may override this method and call up to this method as appropriate.

Specified by:
shutdownWrites in interface SuspendableWriteChannel
Throws:
IOException - if an I/O error occurs

shutdownWritesAction

protected void shutdownWritesAction()
                             throws IOException
The action to perform when writes are requested to be shut down. By default, this method delegates to the underlying channel.

Throws:
IOException - if an error occurs

isWriteShutDown

protected boolean isWriteShutDown()
Determine whether the channel is shut down for writes.

Returns:
whether the channel is shut down for writes

isWriteComplete

protected boolean isWriteComplete()

awaitReadable

public void awaitReadable()
                   throws IOException
Block until this channel becomes readable again. This method may return spuriously before the channel becomes readable.

Specified by:
awaitReadable in interface SuspendableReadChannel
Throws:
InterruptedIOException - if the operation is interrupted; the thread's interrupt flag will be set as well
IOException - if an I/O error occurs

awaitReadable

public void awaitReadable(long time,
                          TimeUnit timeUnit)
                   throws IOException
Block until this channel becomes readable again, or until the timeout expires. This method may return spuriously before the channel becomes readable or the timeout expires.

Specified by:
awaitReadable in interface SuspendableReadChannel
Parameters:
time - the time to wait
timeUnit - the time unit
Throws:
InterruptedIOException - if the operation is interrupted; the thread's interrupt flag will be set as well
IOException - if an I/O error occurs

getReadThread

public XnioExecutor getReadThread()
Description copied from interface: SuspendableReadChannel
Get the read thread for this channel.

Specified by:
getReadThread in interface SuspendableReadChannel
Returns:
the thread, or null if none is configured or available

awaitWritable

public void awaitWritable()
                   throws IOException
Block until this channel becomes writable again. This method may return spuriously before the channel becomes writable.

Specified by:
awaitWritable in interface SuspendableWriteChannel
Throws:
InterruptedIOException - if the operation is interrupted; the thread's interrupt flag will be set as well
IOException - if an I/O error occurs

awaitWritable

public void awaitWritable(long time,
                          TimeUnit timeUnit)
                   throws IOException
Block until this channel becomes writable again, or until the timeout expires. This method may return spuriously before the channel becomes writable or the timeout expires.

Specified by:
awaitWritable in interface SuspendableWriteChannel
Parameters:
time - the time to wait
timeUnit - the time unit
Throws:
InterruptedIOException - if the operation is interrupted; the thread's interrupt flag will be set as well
IOException - if an I/O error occurs

getWriteThread

public XnioExecutor getWriteThread()
Description copied from interface: SuspendableWriteChannel
Get the write thread for this channel.

Specified by:
getWriteThread in interface SuspendableWriteChannel
Returns:
the thread, or null if none is configured or available

close

public void close()
           throws IOException
Close this channel. This method is idempotent.

Specified by:
close in interface Closeable
Specified by:
close in interface Channel
Specified by:
close in interface InterruptibleChannel
Specified by:
close in interface CloseableChannel
Specified by:
close in interface SuspendableWriteChannel
Throws:
IOException - if an I/O error occurs

closeAction

protected void closeAction(boolean readShutDown,
                           boolean writeShutDown)
                    throws IOException
The action to perform when the channel is closed via the close() method. By default, the underlying channel is closed.

Parameters:
readShutDown - if reads were previously shut down
writeShutDown - if writes were previously shut down
Throws:
IOException - if an error occurs

isOpen

public boolean isOpen()

Specified by:
isOpen in interface Channel

getChannel

public W getChannel()
Get the channel which is wrapped by this object.

Specified by:
getChannel in interface WrappedChannel<W extends SuspendableChannel>
Returns:
the wrapped channel

getWorker

public XnioWorker getWorker()
Get the worker for this channel.

Specified by:
getWorker in interface CloseableChannel
Returns:
the worker

toString

public String toString()

Overrides:
toString in class Object

XNIO API 3.0.4.GA

Copyright © 2010 JBoss, a division of Red Hat, Inc.