com.sun.sgs.impl.protocol.simple
Class AsynchronousMessageChannel

java.lang.Object
  extended by com.sun.sgs.impl.protocol.simple.AsynchronousMessageChannel
All Implemented Interfaces:
Closeable, Channel

public class AsynchronousMessageChannel
extends Object
implements Channel

A wrapper channel that reads and writes complete messages by framing messages with a 2-byte message length, and masking (and re-issuing) partial I/O operations. Also enforces a fixed buffer size when reading.


Field Summary
(package private)  AsynchronousByteChannel channel
          The underlying channel (possibly another layer of abstraction, e.g.
(package private) static LoggerWrapper logger
          The logger for this class.
static int PREFIX_LENGTH
          The number of bytes used to represent the message length.
(package private)  ByteBuffer readBuffer
          The read buffer.
(package private)  AtomicBoolean readPending
          Whether there is a read underway.
(package private)  AtomicBoolean writePending
          Whether there is a write underway.
 
Constructor Summary
AsynchronousMessageChannel(AsynchronousByteChannel channel, int readBufferSize)
          Creates a new instance of this class with the given channel and read buffer size.
 
Method Summary
 void close()
          
(package private)  int getMessageLength()
          Returns the length of the complete message, including the length prefix, based on the data read into the buffer between position 0 and the current position, or -1 if the length cannot be determined.
 boolean isOpen()
          
 IoFuture<ByteBuffer,Void> read(CompletionHandler<ByteBuffer,Void> handler)
          Initiates reading a complete message from this channel.
 IoFuture<Void,Void> write(ByteBuffer src, CompletionHandler<Void,Void> handler)
          Initiates writing a complete message from the given buffer to the underlying channel, and returns a future for controlling the operation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

PREFIX_LENGTH

public static final int PREFIX_LENGTH
The number of bytes used to represent the message length.

See Also:
Constant Field Values

logger

static final LoggerWrapper logger
The logger for this class.


channel

final AsynchronousByteChannel channel
The underlying channel (possibly another layer of abstraction, e.g. compression, retransmission...)


readPending

final AtomicBoolean readPending
Whether there is a read underway.


writePending

final AtomicBoolean writePending
Whether there is a write underway.


readBuffer

final ByteBuffer readBuffer
The read buffer.

Constructor Detail

AsynchronousMessageChannel

public AsynchronousMessageChannel(AsynchronousByteChannel channel,
                                  int readBufferSize)
Creates a new instance of this class with the given channel and read buffer size.

Parameters:
channel - a channel
readBufferSize - the number of bytes in the read buffer
Throws:
IllegalArgumentException - if readBufferSize is smaller than 2
Method Detail

read

public IoFuture<ByteBuffer,Void> read(CompletionHandler<ByteBuffer,Void> handler)
Initiates reading a complete message from this channel. Returns a future which will contain a read-only view of a buffer containing the complete message. Calls handler when the read operation has completed, if handler is not null. The buffer's position will be set to 0 and it's limit will be set to the length of the complete message. The contents of the buffer will remain valid until the next call to read.

Parameters:
handler - the completion handler object; can be null
Returns:
a future representing the result of the operation
Throws:
BufferOverflowException - if the buffer does not contain enough space to read the next message
ReadPendingException - if a read is in progress

write

public IoFuture<Void,Void> write(ByteBuffer src,
                                 CompletionHandler<Void,Void> handler)
Initiates writing a complete message from the given buffer to the underlying channel, and returns a future for controlling the operation. Writes bytes starting at the buffer's current position and up to its limit.

Parameters:
src - the buffer from which bytes are to be retrieved
handler - the completion handler object; can be null
Returns:
a future representing the result of the operation
Throws:
WritePendingException - if a write is in progress

close

public void close()
           throws IOException

Specified by:
close in interface Closeable
Specified by:
close in interface Channel
Throws:
IOException

isOpen

public boolean isOpen()

Specified by:
isOpen in interface Channel

getMessageLength

int getMessageLength()
Returns the length of the complete message, including the length prefix, based on the data read into the buffer between position 0 and the current position, or -1 if the length cannot be determined.

Returns:
the length, or -1

RedDwarf, Version 0.10.1
2010-03-14 10:56:12

Copyright © 2010 The RedDwarf Authors. All rights reserved
Copyright © 2007-2010 Sun Microsystems, Inc. All rights reserved