se.lth.cs.realtime.event
Class RTEventBuffer

java.lang.Object
  |
  +--se.lth.cs.realtime.event.RTEventBuffer

public class RTEventBuffer
extends java.lang.Object

A basic queue/buffer for RTEvent objects. Essentially a protected bounded buffer with both blocking and non-blocking methods for posting and fetching messages in terms of references to objects.

For the functionality provided by this basic class, the buffer could have been typed to contain EventObjects (the base class of RTEvent), but to permit subclasses of RTEventBuffer to utilize the properties of the RTEvents that class is used here. For instance, one might need a buffer that keeps track of the total delay of data by referring to the time-stamps of the newest and oldest RTEvent.

The methods checking or awaiting buffer full/empty are useful for operation supervision, capturing events until a buffer is filled, mode changes, etc.


Field Summary
protected  int currSize
          Current queue size.
protected  RTEvent[] events
          The actual buffer.
protected  int fetchAt
          Position of first element to fetch, if any.
protected  int maxSizeR
          Maximal queue size for Reading events.
protected  int maxSizeW
          Maximal queue size, which is also the size for Writing/posting.
protected  int postAt
           
protected  boolean resizing
          Marks that setMaxSize is being evaluated.
 
Constructor Summary
RTEventBuffer(int maxSize)
          Initalizes the queue with a given maximal size.
 
Method Summary
 void awaitEmpty()
          Waits for buffer to become empty.
 void awaitFull()
          Waits for buffer to become full.
 int currentSize()
          The current size of the buffer.
 RTEvent doFetch()
          Returns the next RTEvent in the queue, blocks if none available.
 void doPost(RTEvent e)
          Adds an RTEvent to the queue, blocks caller if the queue is full.
protected  void error(java.lang.String message)
          Takes care of InterruptedException.
 void flush()
          Removes all buffered events.
 int getMaxSize()
          Gets the maximum size of the buffer.
 boolean isEmpty()
          Checks if buffer is empty.
 boolean isFull()
          Checks if buffer is full.
 void setMaxSize(int newSize)
          Changes size of buffer.
 RTEvent tryFetch()
          Returns the next available RTEvent in the queue, or null if the queue is empty.
 RTEvent tryPost(RTEvent e)
          Adds an EventObject to the queue, without blocking if the queue is full.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

events

protected RTEvent[] events
The actual buffer.

maxSizeR

protected int maxSizeR
Maximal queue size for Reading events. Temporarily, during decrease of full buffer with setMaxSize, this attribute can differ from the ordinary buffer size.
See Also:
maxSizeW

maxSizeW

protected int maxSizeW
Maximal queue size, which is also the size for Writing/posting.

resizing

protected boolean resizing
Marks that setMaxSize is being evaluated. More than one call whould screw-up that operation because it need to be blocked until all messages fit into the new (possibly smaller) size.

currSize

protected int currSize
Current queue size. That is, the number of events currently buffered.

postAt

protected int postAt

fetchAt

protected int fetchAt
Position of first element to fetch, if any.
Constructor Detail

RTEventBuffer

public RTEventBuffer(int maxSize)
Initalizes the queue with a given maximal size.
Parameters:
maxSize - maximum queue size
Method Detail

tryFetch

public RTEvent tryFetch()
Returns the next available RTEvent in the queue, or null if the queue is empty. In other words, this method always returns immediately, even if the queue is empty (non-blocking).
Returns:
an RTEvent if any available, null otherwise

doFetch

public RTEvent doFetch()
Returns the next RTEvent in the queue, blocks if none available.
Returns:
a non-null event.

tryPost

public RTEvent tryPost(RTEvent e)
Adds an EventObject to the queue, without blocking if the queue is full.
Parameters:
e - the event to enqueue
Returns:
null when event posted to non-full buffer, the event otherwise

doPost

public void doPost(RTEvent e)
Adds an RTEvent to the queue, blocks caller if the queue is full.
Parameters:
e - the event to enqueue

isEmpty

public boolean isEmpty()
Checks if buffer is empty.
Returns:
true if the queue is empty, false if it is non-empty

awaitEmpty

public void awaitEmpty()
Waits for buffer to become empty.

isFull

public boolean isFull()
Checks if buffer is full.
Returns:
true if the queue is full, false if it is non-full

awaitFull

public void awaitFull()
Waits for buffer to become full.

currentSize

public int currentSize()
The current size of the buffer.
Returns:
the number of objects currently enqueued
See Also:
maxSizeR

flush

public void flush()
Removes all buffered events.

getMaxSize

public int getMaxSize()
Gets the maximum size of the buffer.
Returns:
the maximum number of objects that can be enqueued

setMaxSize

public void setMaxSize(int newSize)
Changes size of buffer.
Parameters:
the - new maximum number of objects that can be enqueued

error

protected void error(java.lang.String message)
Takes care of InterruptedException.