XNIO API 3.0.4.GA

org.xnio.channels
Class FramedMessageChannel

java.lang.Object
  extended by org.xnio.channels.TranslatingSuspendableChannel<ConnectedMessageChannel,ConnectedStreamChannel>
      extended by org.xnio.channels.FramedMessageChannel
All Implemented Interfaces:
Closeable, Channel, InterruptibleChannel, BoundChannel, CloseableChannel, Configurable, ConnectedChannel, ConnectedMessageChannel, MessageChannel, ReadableMessageChannel, SuspendableChannel, SuspendableReadChannel, SuspendableWriteChannel, WrappedChannel<ConnectedStreamChannel>, WritableMessageChannel

public class FramedMessageChannel
extends TranslatingSuspendableChannel<ConnectedMessageChannel,ConnectedStreamChannel>
implements ConnectedMessageChannel

A connected message channel providing a SASL-style framing layer over a stream channel where each message is prepended by a four-byte length field.

Author:
David M. Lloyd

Field Summary
 
Fields inherited from class org.xnio.channels.TranslatingSuspendableChannel
channel
 
Constructor Summary
FramedMessageChannel(ConnectedStreamChannel channel, ByteBuffer receiveBuffer, ByteBuffer transmitBuffer)
          Construct a new instance.
FramedMessageChannel(ConnectedStreamChannel channel, Pooled<ByteBuffer> receiveBuffer, Pooled<ByteBuffer> transmitBuffer)
          Construct a new instance.
 
Method Summary
protected  void closeAction(boolean readShutDown, boolean writeShutDown)
          The action to perform when the channel is closed via the TranslatingSuspendableChannel.close() method.
protected  boolean flushAction(boolean shutDown)
          The action to perform when the channel is flushed.
 ConnectedStreamChannel getChannel()
          Get the underlying channel.
 SocketAddress getLocalAddress()
          Get the local address that this channel is bound to.
<A extends SocketAddress>
A
getLocalAddress(Class<A> type)
          Get the local address of a given type, or null if the address is not of that type.
 SocketAddress getPeerAddress()
          Get the peer address of this channel.
<A extends SocketAddress>
A
getPeerAddress(Class<A> type)
          Get the peer address of a given type, or null if the address is not of that type.
 int receive(ByteBuffer buffer)
          Receive a message.
 long receive(ByteBuffer[] buffers)
          Receive a message.
 long receive(ByteBuffer[] buffers, int offs, int len)
          Receive a message.
 boolean send(ByteBuffer buffer)
          Send a complete message.
 boolean send(ByteBuffer[] buffers)
          Send a complete message.
 boolean send(ByteBuffer[] buffers, int offs, int len)
          Send a complete message.
protected  void shutdownReadsAction(boolean writeComplete)
          The action to perform when reads are shut down.
protected  void shutdownWritesComplete(boolean readShutDown)
          Notification that the channel has successfully flushed after having shut down writes.
 
Methods inherited from class org.xnio.channels.TranslatingSuspendableChannel
awaitReadable, awaitReadable, awaitWritable, awaitWritable, clearReadReady, clearReadRequiresWrite, clearWriteReady, clearWriteRequiresRead, close, flush, getCloseSetter, getOption, getReadSetter, getReadThread, getWorker, getWriteSetter, getWriteThread, handleClosed, handleReadable, handleWritable, isOpen, isReadResumed, isReadShutDown, isWriteComplete, isWriteResumed, isWriteShutDown, readRequiresWrite, removeReadRequiresExternal, removeWriteRequiresExternal, resumeReads, resumeWrites, setClosed, setOption, setReadReady, setReadRequiresWrite, setReadShutDown, setWriteReady, setWriteRequiresRead, setWriteShutDown, shutdownReads, shutdownWrites, shutdownWritesAction, supportsOption, suspendReads, suspendWrites, thisTyped, toString, tryAddReadRequiresExternal, tryAddWriteRequiresExternal, wakeupReads, wakeupWrites, writeRequiresRead
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface org.xnio.channels.ConnectedMessageChannel
getCloseSetter, getReadSetter, getWriteSetter
 
Methods inherited from interface org.xnio.channels.SuspendableReadChannel
awaitReadable, awaitReadable, getReadThread, isReadResumed, resumeReads, shutdownReads, suspendReads, wakeupReads
 
Methods inherited from interface org.xnio.channels.SuspendableWriteChannel
awaitWritable, awaitWritable, close, flush, getWriteThread, isWriteResumed, resumeWrites, shutdownWrites, suspendWrites, wakeupWrites
 
Methods inherited from interface org.xnio.channels.CloseableChannel
getWorker
 
Methods inherited from interface java.nio.channels.Channel
isOpen
 
Methods inherited from interface org.xnio.channels.Configurable
getOption, setOption, supportsOption
 

Constructor Detail

FramedMessageChannel

public FramedMessageChannel(ConnectedStreamChannel channel,
                            ByteBuffer receiveBuffer,
                            ByteBuffer transmitBuffer)
Construct a new instance.

Parameters:
channel - the channel to wrap
receiveBuffer - the receive buffer (should be direct)
transmitBuffer - the send buffer (should be direct)

FramedMessageChannel

public FramedMessageChannel(ConnectedStreamChannel channel,
                            Pooled<ByteBuffer> receiveBuffer,
                            Pooled<ByteBuffer> transmitBuffer)
Construct a new instance.

Parameters:
channel - the channel to wrap
receiveBuffer - the receive buffer (should be direct)
transmitBuffer - the send buffer (should be direct)
Method Detail

receive

public int receive(ByteBuffer buffer)
            throws IOException
Receive a message.

Specified by:
receive in interface ReadableMessageChannel
Parameters:
buffer - the buffer that will hold the message
Returns:
the size of the received message, 0 if no message is available, and -1 if the message channel has reached an end-of-file condition
Throws:
IOException - if an I/O error occurs

receive

public long receive(ByteBuffer[] buffers)
             throws IOException
Receive a message.

Specified by:
receive in interface ReadableMessageChannel
Parameters:
buffers - the buffers that will hold the message
Returns:
the size of the received message, 0 if no message is available, and -1 if the message channel has reached an end-of-file condition
Throws:
IOException - if an I/O error occurs

receive

public long receive(ByteBuffer[] buffers,
                    int offs,
                    int len)
             throws IOException
Receive a message.

Specified by:
receive in interface ReadableMessageChannel
Parameters:
buffers - the buffers that will hold the message
offs - the offset into the array of buffers of the first buffer to read into
len - the number of buffers to fill
Returns:
the size of the received message, 0 if no message is available, and -1 if the message channel has reached an end-of-file condition
Throws:
IOException - if an I/O error occurs

shutdownReadsAction

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

Overrides:
shutdownReadsAction in class TranslatingSuspendableChannel<ConnectedMessageChannel,ConnectedStreamChannel>
Throws:
IOException - if an error occurs

send

public boolean send(ByteBuffer buffer)
             throws IOException
Send a complete message.

Specified by:
send in interface WritableMessageChannel
Parameters:
buffer - the message to send
Returns:
the result of the send operation; true if the message was sent, or false if it would block
Throws:
IOException - if an I/O error occurs

send

public boolean send(ByteBuffer[] buffers)
             throws IOException
Send a complete message.

Specified by:
send in interface WritableMessageChannel
Parameters:
buffers - the buffers holding the message to send
Returns:
the result of the send operation; true if the message was sent, or false if it would block
Throws:
IOException - if an I/O error occurs

send

public boolean send(ByteBuffer[] buffers,
                    int offs,
                    int len)
             throws IOException
Send a complete message.

Specified by:
send in interface WritableMessageChannel
Parameters:
buffers - the buffers holding the message to send
offs - the offset into the buffer array of the first buffer
len - the number of buffers that contain data to send
Returns:
the result of the send operation; true if the message was sent, or false if it would block
Throws:
IOException - if an I/O error occurs

flushAction

protected boolean flushAction(boolean shutDown)
                       throws IOException
Description copied from class: TranslatingSuspendableChannel
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 TranslatingSuspendableChannel.flush() will flush the underlying channel until it returns true).

Overrides:
flushAction in class TranslatingSuspendableChannel<ConnectedMessageChannel,ConnectedStreamChannel>
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
Description copied from class: TranslatingSuspendableChannel
Notification that the channel has successfully flushed after having shut down writes. The underlying channel may not yet be fully flushed at this time.

Overrides:
shutdownWritesComplete in class TranslatingSuspendableChannel<ConnectedMessageChannel,ConnectedStreamChannel>
Parameters:
readShutDown - true if the read side was already shut down, false otherwise
Throws:
IOException - if an error occurs

closeAction

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

Overrides:
closeAction in class TranslatingSuspendableChannel<ConnectedMessageChannel,ConnectedStreamChannel>
Parameters:
readShutDown - if reads were previously shut down
writeShutDown - if writes were previously shut down
Throws:
IOException - if an error occurs

getPeerAddress

public SocketAddress getPeerAddress()
Get the peer address of this channel.

Specified by:
getPeerAddress in interface ConnectedChannel
Returns:
the peer address

getPeerAddress

public <A extends SocketAddress> A getPeerAddress(Class<A> type)
Get the peer address of a given type, or null if the address is not of that type.

Specified by:
getPeerAddress in interface ConnectedChannel
Parameters:
type - the address type class
Returns:
the peer address, or null if unknown

getLocalAddress

public SocketAddress getLocalAddress()
Get the local address that this channel is bound to.

Specified by:
getLocalAddress in interface BoundChannel
Returns:
the local address

getLocalAddress

public <A extends SocketAddress> A getLocalAddress(Class<A> type)
Get the local address of a given type, or null if the address is not of that type.

Specified by:
getLocalAddress in interface BoundChannel
Type Parameters:
A - the address type
Parameters:
type - the address type class
Returns:
the local address, or null if unknown

getChannel

public ConnectedStreamChannel getChannel()
Get the underlying channel.

Specified by:
getChannel in interface WrappedChannel<ConnectedStreamChannel>
Overrides:
getChannel in class TranslatingSuspendableChannel<ConnectedMessageChannel,ConnectedStreamChannel>
Returns:
the underlying channel

XNIO API 3.0.4.GA

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