simtools.util
Class PacketBuffer

java.lang.Object
  extended by simtools.util.PacketBuffer
All Implemented Interfaces:
java.io.Serializable

public class PacketBuffer
extends java.lang.Object
implements java.io.Serializable

A packet buffer collects byte buffers identified by a common key. The byte buffers are stored in a circular array. The array size is defined by a constructor value. Optionaly a timing criterion can be used to increase this initial size until an elapsed time between the first packet and the last is reached Caution : if the packet time value is allways set to zero then this criterion is never reached (if elapsed time greater than zero) and thus it will increase the packet size for ever.

Author:
cazenave
See Also:
Serialized Form

Nested Class Summary
static interface PacketBuffer.Listener
          The packet listener interface
 class PacketBuffer.NoSuchIndex
          The exception returned in case of buffer invalid access
 
Field Summary
protected  java.nio.ByteBuffer[] buffers
           
protected  java.lang.Object key
           
protected  long lastIndex
           
protected  java.util.HashSet listeners
           
protected  double minDuration
           
protected  int minSize
           
protected  int offset
           
protected  int size
           
protected  long startIndex
           
protected  double[] time
           
 
Constructor Summary
protected PacketBuffer(java.lang.Object key, int minSize, double minDuration)
          Create a new packet buffer
 
Method Summary
 int add(java.nio.ByteBuffer bb)
          Add a new packet
 void addListener(PacketBuffer.Listener l)
          Add a listener to be notified on packet reception
 java.nio.ByteBuffer getBuffer(long index)
          Get the packet buffer
 java.lang.Object getKey()
          Get the key common to all these packets
 long getLastIndex()
          Last packet valid index
 int getOffset()
           
 long getStartIndex()
          Oldest packet valid index
 double getTime(long index)
          Get the packet time
 void removeListener(PacketBuffer.Listener l)
          Remove a listener
protected  void reset()
          Reset the buffer indexes The packet ring buffer is empty after this call
protected  void setTime(int index, java.nio.ByteBuffer byteBuffer)
          Method setTime.
protected  void validation(int index)
          To be overiden to provide a validation of the packet content
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

key

protected final java.lang.Object key

minSize

protected int minSize

minDuration

protected double minDuration

size

protected int size

startIndex

protected long startIndex

lastIndex

protected long lastIndex

buffers

protected transient java.nio.ByteBuffer[] buffers

time

protected transient double[] time

listeners

protected transient java.util.HashSet listeners

offset

protected int offset
Constructor Detail

PacketBuffer

protected PacketBuffer(java.lang.Object key,
                       int minSize,
                       double minDuration)
Create a new packet buffer

Parameters:
key - the packet key used by the factory
minSize - the minimum size for the buffer
minDuration - the minimum duration (to be lower than zero if not used)
Method Detail

getKey

public java.lang.Object getKey()
Get the key common to all these packets

Returns:
the key

getOffset

public int getOffset()
Returns:
the offset to be used to read the buffer

add

public int add(java.nio.ByteBuffer bb)
        throws PacketBufferValidationError
Add a new packet

Parameters:
bb - the packet content
Returns:
packet index
Throws:
PacketBufferValidationError

setTime

protected void setTime(int index,
                       java.nio.ByteBuffer byteBuffer)
Method setTime. To be overiden to provide a time value according to the packet content

Parameters:
index - The index of the PacketBuffer to set the time.
byteBuffer - The byteBuffer, could be usefull to determine the time.

validation

protected void validation(int index)
                   throws PacketBufferValidationError
To be overiden to provide a validation of the packet content

Parameters:
index -
Throws:
PacketBufferValidationError

getStartIndex

public long getStartIndex()
Oldest packet valid index

Returns:
the index

getLastIndex

public long getLastIndex()
Last packet valid index

Returns:
the index

getTime

public double getTime(long index)
               throws PacketBuffer.NoSuchIndex
Get the packet time

Parameters:
index - of the packet
Returns:
the packet time
Throws:
PacketBuffer.NoSuchIndex

getBuffer

public java.nio.ByteBuffer getBuffer(long index)
                              throws PacketBuffer.NoSuchIndex
Get the packet buffer

Parameters:
index -
Returns:
the buffer
Throws:
PacketBuffer.NoSuchIndex

reset

protected void reset()
Reset the buffer indexes The packet ring buffer is empty after this call


addListener

public void addListener(PacketBuffer.Listener l)
Add a listener to be notified on packet reception

Parameters:
l - the listener to add

removeListener

public void removeListener(PacketBuffer.Listener l)
Remove a listener

Parameters:
l - the lsitener to remove