edu.virginia.cs.mng.hypercast
Class MessageBuffer

java.lang.Object
  |
  +--edu.virginia.cs.mng.hypercast.MessageBuffer

public class MessageBuffer
extends java.lang.Object
implements I_Stats

This class provides a buffer which handles all the functionality for the producer/consumer problem. It assumes that there can be multiple producers and consumers. This is a FIFO style Buffer.


Field Summary
private  OverlaySocketConfig config
          OverlaySocketConfig object.
private  int NumberOfElements
          The number of messages in the Queue
private  I_Message[] Queue
          The queue of messages.
private  int readersWaiting
          Number of threads waiting to read.
private  int soTimeout
          Timeout for reads.
private  int Start
          The location of the oldest message in the queue.
private  int writersWaiting
          Number of threads waiting to write.
 
Constructor Summary
(package private) MessageBuffer(OverlaySocketConfig c, int size)
          Constructs the BoundedBuffer with a size and a config file.
 
Method Summary
 boolean available()
          Checks if there is any messages available in the queue.
 void blockingWrite(I_Message msg)
          Writes a message to the BoundedBuffer Algorithm:
1.
 java.lang.String[] getSchema()
          Get the Statistics information description of this AdapterBuffer.
(package private)  int getSoTimeout()
          Get the timeout time for a read operation.
 java.lang.String getStats(java.lang.String name)
          Get the Statistics information of this AdapterBuffer.
 void nonblockingWrite(I_Message msg)
          Writes a message to the DropTailMessageBuffer Algorithm:
1.
 int numInQueue()
          Returns the number of messages in the queue.
 I_Message peek()
          Peeks the message at the head of the queue Algorithm:
If queue is empty return null, else return the packet at the head of the queue
 I_Message read()
          Reads a message from the BoundedBuffer.
(package private)  void setSoTimeout(int i)
          Set the timeout time for a read operation.
 void setStats(java.lang.String name, java.lang.String value)
          Set the Statistic information in this AdapterBuffer.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

config

private OverlaySocketConfig config
OverlaySocketConfig object.

NumberOfElements

private int NumberOfElements
The number of messages in the Queue

Queue

private I_Message[] Queue
The queue of messages.

Start

private int Start
The location of the oldest message in the queue. (The queue is circular, so the start moves.)

soTimeout

private int soTimeout
Timeout for reads.

readersWaiting

private int readersWaiting
Number of threads waiting to read.

writersWaiting

private int writersWaiting
Number of threads waiting to write.
Constructor Detail

MessageBuffer

MessageBuffer(OverlaySocketConfig c,
              int size)
Constructs the BoundedBuffer with a size and a config file. Simply initializes the parameters
Parameters:
Size - size of the buffer (number of messages)
fd - FileWriter of a debug file
Method Detail

getSoTimeout

int getSoTimeout()
Get the timeout time for a read operation.

setSoTimeout

void setSoTimeout(int i)
Set the timeout time for a read operation.

read

public I_Message read()
               throws java.lang.InterruptedException
Reads a message from the BoundedBuffer.

Algorithm:
1. Waits till the queue is non-empty
2. Copies first element in the queue
3. Increments the Start pointer modulo Queue length
4. Decreases size of the Queue
5. Notifies any other process waiting
6. Returns the copied element

Returns:
the first message in the Queue

nonblockingWrite

public void nonblockingWrite(I_Message msg)
Writes a message to the DropTailMessageBuffer

Algorithm:
1. checks if Queue is full
2. Places New packet at the end of the Queue
3. Increments number of elements
4. Notifies all threads waiting

Parameters:
msg - a message to be added into the Queue

blockingWrite

public void blockingWrite(I_Message msg)
                   throws java.lang.InterruptedException
Writes a message to the BoundedBuffer

Algorithm:
1. checks if Queue is full
2. Places New packet at the end of the Queue
3. Increments number of elements
4. Notifies all threads waiting

Parameters:
msg - a message to be added into the Queue

peek

public I_Message peek()
Peeks the message at the head of the queue

Algorithm:
If queue is empty return null, else return the packet at the head of the queue

Returns:
message at the head of the queue, but does not remove it

available

public boolean available()
Checks if there is any messages available in the queue. If queue is empty return false, else return true.
Returns:
false if Queue is non empty, and true otherwise

numInQueue

public int numInQueue()
Returns the number of messages in the queue.
Returns:
the number of elements in the queue

getStats

public java.lang.String getStats(java.lang.String name)
                          throws StatsException
Get the Statistics information of this AdapterBuffer.
Specified by:
getStats in interface I_Stats
Parameters:
name - the String representation of a Statistic name
Returns:
String representation of the Statistic
Throws:
throw - StatisticNotFound exception

setStats

public void setStats(java.lang.String name,
                     java.lang.String value)
              throws StatsException
Set the Statistic information in this AdapterBuffer.
Specified by:
setStats in interface I_Stats
Parameters:
name - the String representation of a Statistic name
value - the String representation of the Statistic value
Throws:
throw - WriteToAReadOnlyValue exception
throw - StatisticNotFound exception

getSchema

public java.lang.String[] getSchema()
                             throws StatsException
Get the Statistics information description of this AdapterBuffer.
Specified by:
getSchema in interface I_Stats
Returns:
the String description of the Schema.