public abstract class Algorithm
extends java.lang.Thread
algorithm()
method must be provided that returns the results of the
algorithm as a Java Object. This method should contain a main loop that runs
until doMainLoop()
returns false. For example:
public Object algorithm() {
//Setup anything needed for the algorithm
while(doMainLoop) {
//Do some calls to send and receive here.
}
//Running anything before the algorthim terminates
return someResultHere
}
Implementing classes should only reference or use classes or methods from the
dans.algorithm and dans.utils packages. Use of any other class from the dans
library can break the distributed algorithm model and/or the simulator.
No methods in this class should be called in the constructor of implementing
child classes. Doing so will result in a null value or equivalent being
returned and possible exceptions. This may also break the simulation.Modifier and Type | Field and Description |
---|---|
static int |
FAIL_BYZAN
A Byzantine failure has occurred.
|
static int |
FAIL_CLEAN
A clean failure has occurred.
|
static int |
FAIL_NONE
No failure has occurred.
|
Constructor and Description |
---|
Algorithm() |
Modifier and Type | Method and Description |
---|---|
abstract java.lang.Object |
algorithm()
This method should be overridden by any implementing child class.
|
void |
display(java.lang.String text)
Displays some text above the processor in the GUI front-end.
|
void |
display(java.lang.String text,
int pos)
Displays some text above or below the processor in the GUI front-end.
|
boolean |
doMainLoop()
This method should be checked once per cycle of the main loop in the
algorithm()
method. |
void |
fail()
Causes the algorithm to experience a clean failure.
|
void |
fail(boolean clean)
Causes the algorithm to experience a failure.
|
int |
getDefaultPort()
Gets the default port number for the simulation.
|
int |
getFailType()
Gets a value indicating how this algorithm failed.
|
java.lang.String |
getID()
Gets the ID of the processor the algorithm is running on.
|
double |
getLinkBandwidth(java.lang.String linkID)
Gets the bandwidth of a given link.
|
int |
getLinkDelay(java.lang.String linkID)
Gets the delay of the given link.
|
java.lang.String[] |
getLinks()
Gets the set of directional links leaving this processor.
|
java.util.concurrent.ConcurrentHashMap<java.lang.String,java.lang.Object> |
getMemory()
Gets a reference to a ConcurrentHashMap that represents the shared memory
of the processor the algorithm is running on.
|
java.lang.String |
getNeighborDownLink(java.lang.String linkID)
Finds the neighbor that a given link points to.
|
java.lang.String[] |
getNeighbors()
Gets the set of neighbors for this processor.
|
java.lang.String |
getNeighborsLink(java.lang.String neighborID)
Finds a link pointing to the given neighbor.
|
int |
getPort()
Gets the port the algorithm is listening on.
|
java.lang.String[] |
getSourceLinks()
Gets the set of links that point to this processor.
|
java.lang.String[] |
getSources()
Gets the set of processors that have a directional link to this processor.
|
java.lang.String |
getStatus()
Gets the current status of the algorithm.
|
int |
getTick()
Gets the current tick the simulation is on.
|
boolean |
hasLink(java.lang.String linkID)
Checks if the processor has a link with the given ID.
|
boolean |
hasNeighbor(java.lang.String neighborID)
Checks if the processor has a neighbor with the given ID.
|
boolean |
isRoot()
Deprecated.
this method does not currently work correctly with the front-end.
|
void |
pause()
This method causes the algorithm to be paused.
|
void |
print(java.lang.String msg)
Writes some text to the log.
|
Message |
receive()
Returns the next message in the message buffer (the message input queue).
|
Message |
receive(boolean asynchronous)
Returns the next message in the message buffer (the message input queue).
|
void |
run()
This is an internal method that should not be called directly.
|
boolean |
send(Message msg)
Sends the given message.
|
boolean |
send(java.lang.String id,
java.lang.String msg)
Sends the given message to a processor with the given ID.
|
boolean |
send(java.lang.String id,
java.lang.String msg,
int port)
Sends the given message to a processor with the given ID on the given port.
|
boolean |
sendLink(java.lang.String linkID,
java.lang.String msg)
Sends the given message down the link with the given ID.
|
boolean |
sendLink(java.lang.String linkID,
java.lang.String msg,
int port)
Sends the given message down the link with the given ID on the given port.
|
void |
terminate()
Causes this algorithm to terminate cleanly and for
doMainLoop() to
return false the next time it is called. |
void |
unpause()
This method causes the algorithm to be unpaused.
|
activeCount, checkAccess, clone, countStackFrames, currentThread, destroy, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, stop, suspend, toString, yield
public static final int FAIL_NONE
public static final int FAIL_CLEAN
public static final int FAIL_BYZAN
public java.util.concurrent.ConcurrentHashMap<java.lang.String,java.lang.Object> getMemory()
public int getFailType()
FAIL_NONE
, FAIL_CLEAN
, or FAIL_BYZAN
.public final int getTick()
public final int getDefaultPort()
getPort()
to find the
port the algorithm is listening on.getPort()
public final int getPort()
public final double getLinkBandwidth(java.lang.String linkID)
linkID
- a link ID.public final int getLinkDelay(java.lang.String linkID)
linkID
- a link ID.public final boolean hasNeighbor(java.lang.String neighborID)
neighborID
- the processor ID to check.hasLink(java.lang.String)
public final boolean hasLink(java.lang.String linkID)
linkID
- the link ID to check.hasNeighbor(java.lang.String)
public final java.lang.String getNeighborDownLink(java.lang.String linkID)
linkID
- the ID of the link to find a processor down.getNeighborsLink(java.lang.String)
public final java.lang.String getNeighborsLink(java.lang.String neighborID)
neighborID
- the ID of the neighbor to find a link to.getNeighborDownLink(java.lang.String)
public final java.lang.String[] getNeighbors()
getLinks()
public final java.lang.String[] getLinks()
getNeighbors()
public final java.lang.String[] getSources()
getNeighbors()
as it only includes processors that have a link TO this processor and not
necessarily FROM this processor. If the network is setup in a directional
tree such that all child processor point to their parent, this set would
contain the children of the processor.getNeighbors()
,
getSourceLinks()
public final java.lang.String[] getSourceLinks()
getSources()
,
getLinks()
public final java.lang.String getID()
public final boolean isRoot()
getID()
should be used to check if the processor
has this ID.getID()
public abstract java.lang.Object algorithm()
doMainLoop()
returns false. If the algorithm is finished it
can break or return out of this loop immediately with out checking
doMainLoop()
. All calls to send(dans.algorithm.Message)
and receive()
should be contained in such a loop. A value maybe
returned and will be displayed next to the node in the front-end when the
algorithm terminates. Otherwise null can be returned. See the description
of the Algorithm
class for more details.
This method should not be called directly, just overridden by a child class.Algorithm
public final void run()
run
in interface java.lang.Runnable
run
in class java.lang.Thread
public final java.lang.String getStatus()
public final void print(java.lang.String msg)
msg
- the text to be written to the log.display(java.lang.String)
public final void display(java.lang.String text)
text
- the text to display.print(java.lang.String)
,
display(java.lang.String, int)
public final void display(java.lang.String text, int pos)
text
- the text to display.pos
- the position to display the text in. 0 for above, 1 for below, any other value is equivalent to calling print(java.lang.String)
.print(java.lang.String)
,
display(java.lang.String)
public final boolean send(Message msg)
Message
object. The Message
object must contain either a processor ID or
link ID to send the message to.
This method does NOT block or wait for the message to be delivered. Depending
on the failure settings delivery is not necessarily guaranteed. This method
returning true does not necessarily guarantee the message was delivered.msg
- a new Message
object that has not been sent.Message
,
send(java.lang.String, java.lang.String)
,
send(java.lang.String, java.lang.String, int)
,
sendLink(java.lang.String, java.lang.String)
,
sendLink(java.lang.String, java.lang.String, int)
public final boolean send(java.lang.String id, java.lang.String msg)
id
- the ID of the processor to send the message to.msg
- the message to send to the processor.send(dans.algorithm.Message)
,
send(java.lang.String, java.lang.String, int)
,
sendLink(java.lang.String, java.lang.String)
,
sendLink(java.lang.String, java.lang.String, int)
public final boolean send(java.lang.String id, java.lang.String msg, int port)
id
- the ID of the processor to send the message to.msg
- the message to send to the processor.port
- the port to send the message to on the receiving processor.send(dans.algorithm.Message)
,
send(java.lang.String, java.lang.String)
,
sendLink(java.lang.String, java.lang.String)
,
sendLink(java.lang.String, java.lang.String, int)
public final boolean sendLink(java.lang.String linkID, java.lang.String msg)
linkID
- the ID of the link to send the message down.msg
- the message to send down the link.send(dans.algorithm.Message)
,
send(java.lang.String, java.lang.String)
,
send(java.lang.String, java.lang.String, int)
,
sendLink(java.lang.String, java.lang.String, int)
public final boolean sendLink(java.lang.String linkID, java.lang.String msg, int port)
linkID
- the ID of the link to send the message down.msg
- the message to send down the link.port
- the port to send the message to on the receiving processor.send(dans.algorithm.Message)
,
send(java.lang.String, java.lang.String)
,
send(java.lang.String, java.lang.String, int)
,
sendLink(java.lang.String, java.lang.String)
public final Message receive()
Message
,
receive(boolean)
public final Message receive(boolean asynchronous)
asynchronous
- if true the method will return immediately and function like receive()
. If false the method will block/wait until a message is received.Message
,
receive()
public final boolean doMainLoop()
algorithm()
method. The main loop should be terminated if this function returns false.
This method both ensures that the algorithm is running at the speed dictated
by the tick system and updates dictated about the algorithm. It also allows
the simulation to be paused/restarted and handles failures. See Algorithm
for more details and an example of its use.Algorithm
public final void terminate()
doMainLoop()
to
return false the next time it is called. Commands after calling terminate
will still be executed.public final void unpause()
public final void pause()
public final void fail()
terminate()
method should
be used for that purpose. This method should only be used to simulate a
clean failure.terminate()
public final void fail(boolean clean)
clean
- true if it is a clean failure. Otherwise false.