scorej
Class SignalWaitQueue

java.lang.Object
  |
  +--scorej.SignalWait
        |
        +--scorej.SignalWaitQueue

public class SignalWaitQueue
extends SignalWait

SignalWaitQueue is an subclass of SignalWait that encapsulates a FIFO Queue of signals. It behaves just as a SignalWait object, except that it is never removed from the signalWait list of the SignalProcessor. So, it is always active and ready to match signals. When a signal matches, that matching signal is pushed onto a queue. Processes can pop signals off the queue by calling S.waitForSignalQueue or pop.

SignalWaitQueues are not necessary if you are using a SimulationClock, since a simple waitForSignal inside of an infinite loop would work just as well. Though, you can use these SignalWaitQueues if you so choose. SignalWaitQueues are important for NonSimulationClocks, since the canonical loop-forever/waitForSignal could miss signals that arrive while one is being processed.

Use S.createSignalQueue to make a queue and register it. Use S.waitForSignalQueue to grab a signal off a queue (or wait for one)

See Also:
SignalWait, NonSimulationClock, S

Field Summary
protected  java.util.Vector signalQueue
           
protected  java.util.Vector waitingThreads
           
 
Fields inherited from class scorej.SignalWait
_pattern, _receiver, _sender, _signalEvaluator, _subject, _thread
 
Method Summary
 void clear()
          Removes all signals from the queue.
 Signal elementAt(int index)
          Returns the signal at the specified index.
 Signal findMatch(ID receiver, ID sender, java.lang.String[] subject, java.lang.Object[] pattern, SignalEvaluator func)
          Tries to find a signal in the Queue with same values of receiver, sender, subject, pattern and evaluator specified in the argument list.
 int getWaitingThreadCount()
          Returns how many threads are waiting for this queue
 boolean isEmpty()
          Tests if the queue contains no signals.
 boolean matchSignal(Signal signal)
          Notify the thread waiting using this SignalWait that the signal arrived.
 Signal peek()
          Peeks at the next signal in the queue without removing it from the queue.
 Signal pop()
          Pops a signal off the queue.
 boolean remove(Signal sig)
          Removes the first signal from the queue that matches the specified signal If the queue does not contain the specified signal, the queue is unchanged.
 void setMatchedSignal(Signal signal)
          Adds a signal to the queue.
 int size()
          Returns the number of signals in the queue.
 java.lang.String toString()
          Returns a string representation of the SignalWaitQueue object.
 Signal waitForSignal()
          Waits for a Signal to arrive in the queue.
 Signal waitForSignal(ID receiver, ID sender, java.lang.String[] subject, java.lang.Object[] pattern, SignalEvaluator func)
          Waits for a Signal to arrive in the queue that matches against the receiver, sender, subject, pattern and evaluator contained in the argument list.
 
Methods inherited from class scorej.SignalWait
getMatchedSignal, getPattern, getReceiver, getSender, getSubject, matches
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

signalQueue

protected java.util.Vector signalQueue

waitingThreads

protected java.util.Vector waitingThreads
Method Detail

setMatchedSignal

public void setMatchedSignal(Signal signal)
Adds a signal to the queue. Called by SignalProcessor. Makes the Signal immutable.

Overrides:
setMatchedSignal in class SignalWait

findMatch

public Signal findMatch(ID receiver,
                        ID sender,
                        java.lang.String[] subject,
                        java.lang.Object[] pattern,
                        SignalEvaluator func)
Tries to find a signal in the Queue with same values of receiver, sender, subject, pattern and evaluator specified in the argument list.

Returns:
the first matching signal, otherwise null.

clear

public void clear()
Removes all signals from the queue. The queue will be empty after this call returns.


pop

public Signal pop()
Pops a signal off the queue.

Returns:
the first signal in the queue. This call is non-blocking, so it returns null if there are no signals to be gotten. Called by S.waitForSignalQueue.

remove

public boolean remove(Signal sig)
Removes the first signal from the queue that matches the specified signal If the queue does not contain the specified signal, the queue is unchanged.

Parameters:
sig - Signal to be removed, if present.
Returns:
true of the queue contained the signal.

peek

public Signal peek()
Peeks at the next signal in the queue without removing it from the queue.

Returns:
The first signal in the queue. peek is non-blocking, so it returns null if there are no signals in the queue.

elementAt

public Signal elementAt(int index)
Returns the signal at the specified index.


size

public int size()
Returns the number of signals in the queue.


isEmpty

public boolean isEmpty()
Tests if the queue contains no signals.

Returns:
true if there are no signals in the queue, that is its size is zero; false otherwise.

getWaitingThreadCount

public int getWaitingThreadCount()
Returns how many threads are waiting for this queue


toString

public java.lang.String toString()
Returns a string representation of the SignalWaitQueue object.

Overrides:
toString in class SignalWait

waitForSignal

public Signal waitForSignal()
Waits for a Signal to arrive in the queue. If the queue already contained one or more signals, at the time this call was made, this call removes the first signal in the queue and returns it. If the queue was empty when the call was made, this call blocks until a signal arrives in the queue.


waitForSignal

public Signal waitForSignal(ID receiver,
                            ID sender,
                            java.lang.String[] subject,
                            java.lang.Object[] pattern,
                            SignalEvaluator func)
Waits for a Signal to arrive in the queue that matches against the receiver, sender, subject, pattern and evaluator contained in the argument list. If the queue already contained one or more of such matching signals, at the time this call was made, this call removes the first matching signal in the queue and returns it. If the queue was empty when the call was made, this call blocks until a matching signal arrives in the queue.


matchSignal

public boolean matchSignal(Signal signal)
Notify the thread waiting using this SignalWait that the signal arrived. This is called by SignalProcessor.java

Overrides:
matchSignal in class SignalWait