hypercast
Class MessageBuffer

java.lang.Object
  |
  +--hypercast.MessageBuffer
All Implemented Interfaces:
I_Stats

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
static int SOCKET_TIMEOUT_INFINITY
           
 
Constructor Summary
MessageBuffer(hypercast.HyperCastConfig config, int queueSize)
           
MessageBuffer(hypercast.HyperCastConfig config, int queueSize, int socketTimeout)
          Constructs the MessageBuffer with a config file and a size.
MessageBuffer(hypercast.HyperCastConfig config, int queueSize, java.lang.String name)
          Constructs the MessageBuffer with a config file and a size.
 
Method Summary
 boolean available()
          Checks if there is any messages available in the queue.
 void blockingWrite(hypercast.I_Message msg)
          Writes a message to the BoundedBuffer
 org.w3c.dom.Element[] getReadSchema(org.w3c.dom.Document doc, org.apache.xpath.XPath xpath)
          Return the schema element which represents the root of the sub-tree, specified by the given xpath, in read schema tree.
 org.w3c.dom.Element[] getStats(org.w3c.dom.Document doc, org.apache.xpath.XPath xpath)
          Gets statistics information from an object which implements this interface.
 java.lang.String getStatsName()
          Gets the element name of this I_Stats instance.
 org.w3c.dom.Element[] getWriteSchema(org.w3c.dom.Document doc, org.apache.xpath.XPath xpath)
          Return the schema element which represents the root of the sub-tree, specified by the given xpath, in write schema tree.
 void nonblockingWrite(hypercast.I_Message msg)
          Writes a message to the DropTailMessageBuffer
 int numInQueue()
          Returns the number of messages in the queue.
 hypercast.I_Message peek()
          Peeks the message at the head of the queue
 hypercast.I_Message read()
          Reads a message from the BoundedBuffer.
 org.w3c.dom.Element[] setStats(org.w3c.dom.Document doc, org.apache.xpath.XPath xpath, org.w3c.dom.Element newValue)
          Set the statistics specified by the given xpath.
 void setStatsName(java.lang.String name)
          Assigns a name to this I_Stats instance.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SOCKET_TIMEOUT_INFINITY

public static final int SOCKET_TIMEOUT_INFINITY
See Also:
Constant Field Values
Constructor Detail

MessageBuffer

public MessageBuffer(hypercast.HyperCastConfig config,
                     int queueSize,
                     java.lang.String name)
Constructs the MessageBuffer with a config file and a size.

Parameters:
config - The configuration object.
queueSize - The size of the buffer (number of messages)

MessageBuffer

public MessageBuffer(hypercast.HyperCastConfig config,
                     int queueSize)

MessageBuffer

public MessageBuffer(hypercast.HyperCastConfig config,
                     int queueSize,
                     int socketTimeout)
Constructs the MessageBuffer with a config file and a size.

Parameters:
config - The configuration object.
queueSize - The size of the buffer (number of messages)
socketTimeout - The amount of time (in milliseconds) to wait before giving up on a read operation.
Method Detail

read

public hypercast.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
java.lang.InterruptedException

nonblockingWrite

public void nonblockingWrite(hypercast.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(hypercast.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
java.lang.InterruptedException

peek

public hypercast.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

getStatsName

public java.lang.String getStatsName()
Description copied from interface: I_Stats
Gets the element name of this I_Stats instance. The element name is originally assigned from the attribute StstsName in the configuration file, and can be changed by this method.

Specified by:
getStatsName in interface I_Stats
See Also:
I_Stats.getStatsName()

setStatsName

public void setStatsName(java.lang.String name)
Description copied from interface: I_Stats
Assigns a name to this I_Stats instance. The assigned name becomes the tag of the element associated to this I_Stats instance on the statistics tree.

Specified by:
setStatsName in interface I_Stats
Parameters:
name - the name assigned to the I_Stats instance.
See Also:
I_Stats.setStatsName(String)

getStats

public org.w3c.dom.Element[] getStats(org.w3c.dom.Document doc,
                                      org.apache.xpath.XPath xpath)
                               throws HyperCastStatsException
Description copied from interface: I_Stats
Gets statistics information from an object which implements this interface. Statistics are arranged in a tree structure. This method has the ability to query an arbitrary subtree.

Specified by:
getStats in interface I_Stats
Parameters:
xpath - Specifies the statistic subtree to query.
doc - The document is used as a factory to create XML objects such as Nodes and Elements as needed. TODO: describe how the XML document in the doc is used.
Throws:
HyperCastStatsException - If the xpath does not specify a valid subtree.
See Also:
I_Stats.getStats(Document, XPath)

setStats

public org.w3c.dom.Element[] setStats(org.w3c.dom.Document doc,
                                      org.apache.xpath.XPath xpath,
                                      org.w3c.dom.Element newValue)
                               throws HyperCastStatsException
Set the statistics specified by the given xpath. The value actually set is returned.

Specified by:
setStats in interface I_Stats
Parameters:
doc - Document used for create new elements or nodes.
xpath - XPath instance represents the statistics to be queried.
newValue - Element representing the value or sub-tree to be set.
Throws:
HyperCastStatsException - If the xpath does not specify a valid subtree or if any part of the the subtree is read-only, or if the newValue does not conform with the expected format.
See Also:
I_Stats.setStats(org.w3c.dom.Document, org.apache.xpath.XPath, org.w3c.dom.Element)

getReadSchema

public org.w3c.dom.Element[] getReadSchema(org.w3c.dom.Document doc,
                                           org.apache.xpath.XPath xpath)
                                    throws HyperCastStatsException
Return the schema element which represents the root of the sub-tree, specified by the given xpath, in read schema tree.

Specified by:
getReadSchema in interface I_Stats
Parameters:
doc - Document used for create new elements or nodes.
xpath - XPath instance representing the statistics which is the root of the sub-tree to be queried.
Throws:
HyperCastStatsException - If some part of the specified subtree does not correctly implement this interface method.
See Also:
I_Stats.getReadSchema(Document, XPath)

getWriteSchema

public org.w3c.dom.Element[] getWriteSchema(org.w3c.dom.Document doc,
                                            org.apache.xpath.XPath xpath)
                                     throws HyperCastStatsException
Return the schema element which represents the root of the sub-tree, specified by the given xpath, in write schema tree.

Specified by:
getWriteSchema in interface I_Stats
Parameters:
doc - Document used for create new elements or nodes.
xpath - XPath instance representing the statistics which is the root of the sub-tree to be queried.
Throws:
HyperCastStatsException - If some part of the specified subtree does not correctly implement this interface method.
See Also:
I_Stats.getWriteSchema(Document, XPath)