naga
Interface PacketReader

All Known Implementing Classes:
AsciiLinePacketReader, DelimiterPacketReader, RawPacketReader, RegularPacketReader, ZeroDelimitedPacketReader

public interface PacketReader

Interface for packet reader plugins to assist a socket in reading.

To implement a packet reader, the reader has to offer the currently used byte buffer whenever the NIO service calls PacketReader#getBuffer()

PacketReader#getNextPacket() should return a byte-array if it is possible to create one from the data loaded into the buffer(s).

Note that getNextPacket() will be called repeatedly until it returns null.

Author:
Christoffer Lerno

Method Summary
 java.nio.ByteBuffer getBuffer()
          Return the currently used byte buffer.
 byte[] getNextPacket()
          Return the next packet constructed from the data read in the buffers.
 

Method Detail

getBuffer

java.nio.ByteBuffer getBuffer()
                              throws ProtocolViolationException
Return the currently used byte buffer. The NIOSocket will use this byte buffer and perform a SocketChannel.read(ByteBuffer) on it.

The reader is guaranteed not to have this method called more than once before a call to PacketReader#getNextPacket() is made.

Returns:
the byte buffer to use.
Throws:
ProtocolViolationException - if a protocol violation was detected when reading preparing the buffer.

getNextPacket

byte[] getNextPacket()
                     throws ProtocolViolationException
Return the next packet constructed from the data read in the buffers.

This call may or may not have been proceeded by a call to getBuffer().

The calling thread will call this method repeatedly until it returns null.

Returns:
a byte array containing the data of a packet, or null if not packet can be created yet from the data read.
Throws:
ProtocolViolationException - if a protocol violation was detected when parsing the next packet.