org.miv.mbox.net
Class Receiver.IncomingBuffer

java.lang.Object
  extended by org.miv.mbox.net.Receiver.IncomingBuffer
Enclosing class:
Receiver

protected class Receiver.IncomingBuffer
extends Object

The connection to a sender. The receiver maintains several incoming connections and demultiplexes them.


Field Summary
protected  boolean active
          When false the socket is closed and this buffer must be removed from the active connections.
protected  int beg
          Index in the buffer of the first byte that forms the currents message.
protected  ByteBuffer buf
          Buffer for reading.
protected static int BUFFER_INITIAL_SIZE
           
protected  int end
          Index in the buffer past the last byte that forms the current message.
protected  int pos
          Position inside beg and end past the last byte read.
 
Constructor Summary
Receiver.IncomingBuffer()
           
 
Method Summary
protected  int bufferize(int at, SocketChannel socket)
          Read more data from the socket and put it in the buffer at at.
protected  int compactBuffer()
          Compact the buffer by removing all read data before beg.
protected  void decodeMessage(int limit)
          Decode one message.
protected  void enlargeBuffer()
          Not used in the current implementation, we assumes that no message will be larger than the size of the buffer.
 void readDataChunk(SelectionKey key)
          Read the available bytes and buffers them.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BUFFER_INITIAL_SIZE

protected static final int BUFFER_INITIAL_SIZE
See Also:
Constant Field Values

buf

protected ByteBuffer buf
Buffer for reading.


end

protected int end
Index in the buffer past the last byte that forms the current message. End can be out of the buffer or out of the data read actually.


beg

protected int beg
Index in the buffer of the first byte that forms the currents message. Beg does not count the 4 bytes that give the size of the message. While the header is being read, beg is the first byte of the header.


pos

protected int pos
Position inside beg and end past the last byte read. All bytes at and after pos have unspecified contents. Pos always verifies pos>=beg and pos<end. While the header is being read, pos is past the last byte of the header that has been read.


active

protected boolean active
When false the socket is closed and this buffer must be removed from the active connections.

Constructor Detail

Receiver.IncomingBuffer

public Receiver.IncomingBuffer()
Method Detail

readDataChunk

public void readDataChunk(SelectionKey key)
                   throws IOException
Read the available bytes and buffers them. If one or more complete serialised objects are available, send them to their respective MBoxes. Here is the junk...

Throws:
IOException

bufferize

protected int bufferize(int at,
                        SocketChannel socket)
                 throws IOException
Read more data from the socket and put it in the buffer at at. If the read returns -1 bytes (meaning the connection ended), the socket is closed and this buffer will be made inactive (and therefore removed from the active connections by the Receiver that called it).

Returns:
the number of bytes read.
Throws:
IOException - if an I/O error occurs, in between the socket is closed and the connection is made inactive, then the exception is thrown.

decodeMessage

protected void decodeMessage(int limit)
                      throws IOException
Decode one message.

Throws:
IOException

compactBuffer

protected int compactBuffer()
Compact the buffer by removing all read data before beg. The beg, end and pos markers are updated accordingly. Compact works only if beg is larger than four (the size of a header).

Returns:
the offset.

enlargeBuffer

protected void enlargeBuffer()
Not used in the current implementation, we assumes that no message will be larger than the size of the buffer.



Copyright © 2011. All Rights Reserved.