public class DefaultMessageEngine extends Object implements MessageEngine
The DefaultMessageEngine
is JBotSim's default MessageEngine
implementation.
It is responsible for the regular transmission of the available Messages
from any sender
Node
of the Topology
to their destination.
Messages
sent during round n are delivered at the beginning of round
n+1; provided that the corresponding arc (a Link
going, at least, from the
Message
's source Node
to its destination) actually exists at the beginning of round n+1.
If a Message
can't be delivered during one round, and Message.isRetryModeEnabled()
returns
true
(i.e. it has been sent with Node.sendRetry(Node, Message)
), the
DefaultMessageEngine
will keep re-queuing it until it is actually delivered.
Modifier and Type | Field and Description |
---|---|
protected boolean |
debug |
protected Topology |
topology |
Constructor and Description |
---|
DefaultMessageEngine(Topology topology)
Creates a
DefaultMessageEngine . |
Modifier and Type | Method and Description |
---|---|
protected void |
clearMailboxes()
Clears the mailboxes of all
Nodes present in the topology. |
protected void |
clearMailboxes(Collection<Node> nodes)
Clears the mailboxes of the specified
Collection of Nodes . |
protected void |
clearSendQueues()
Clears the send queue of all
Nodes present in the topology. |
protected void |
clearSendQueues(Collection<Node> nodes)
Clears the send queue of all
Nodes present in the topology. |
protected List<Message> |
collectMessages()
|
protected void |
collectMessages(Collection<Message> newMessages,
Node node)
|
protected List<Message> |
collectMessages(Collection<Node> nodes)
|
protected void |
deliverMessage(Message message)
Delivers the
Message to its destination. |
protected void |
deliverMessages(Collection<Message> messagesToSend)
Delivers the provided
Messages to send. |
protected boolean |
isMessageStillRelevant(Message message)
Tests whether the provided
Message is still relevant. |
void |
onClock()
Method responsible for the delivery of the
Messages . |
protected void |
removeIrrelevantMessages(ListIterator<Message> from,
Collection<Node> existingNodes)
Removes any irrelevant messages from the
ListIterator according to the Collection of existing
Nodes . |
protected void |
removeNextMessageIfIrrelevant(ListIterator<Message> from,
Collection<Node> existingNodes)
Removes the next messages from the
ListIterator if it is irrelevant according to the Collection
of existing Nodes . |
protected void |
requeueIfNeeded(Message message,
Collection<Node> existingNodes)
|
protected boolean |
requeueMessage(Message message)
Re-queues the specified
Message in its sender's send queue. |
void |
reset()
Resets the
DefaultMessageEngine . |
void |
setDebug(boolean debug) |
void |
setTopology(Topology topology)
Sets the
Topology to which the MessageEngine refers. |
protected Topology topology
protected boolean debug
public DefaultMessageEngine(Topology topology)
Creates a DefaultMessageEngine
.
topology
- the Topology
to use.public void setTopology(Topology topology)
MessageEngine
Sets the Topology
to which the MessageEngine
refers.
setTopology
in interface MessageEngine
topology
- a Topology
.public void onClock()
MessageEngine
onClock
in interface MessageEngine
protected void removeIrrelevantMessages(ListIterator<Message> from, Collection<Node> existingNodes)
Removes any irrelevant messages from the ListIterator
according to the Collection
of existing
Nodes
.
from
- the ListIterator
from which irrelevant messages should be removed.existingNodes
- the Collection
of existing Nodes
.protected void removeNextMessageIfIrrelevant(ListIterator<Message> from, Collection<Node> existingNodes)
Removes the next messages from the ListIterator
if it is irrelevant according to the Collection
of existing Nodes
.
from
- the ListIterator
from which the next message should be inspected.existingNodes
- the Collection
of existing Nodes
.protected void clearMailboxes()
Clears the mailboxes of all Nodes
present in the topology.
clearMailboxes(Collection)
protected void clearMailboxes(Collection<Node> nodes)
Clears the mailboxes of the specified Collection
of Nodes
.
nodes
- a Collection
of Node
which mailboxes should be cleared.Node.getMailbox()
protected List<Message> collectMessages()
List
of Messages
that have been collected.collectMessages(Collection)
protected List<Message> collectMessages(Collection<Node> nodes)
Collects outgoing Messages
from the specified Collection
of Nodes
.
nodes
- a Collection
of Node
whose outgoing messages should be collected.List
of Messages
that have been collected.protected void collectMessages(Collection<Message> newMessages, Node node)
Collects outgoing Messages
from the specified Node
.
A message with a null
destination will be duplicated for each neighbor of the send.
newMessages
- a Collection
of Messages
in which outgoing messages should be added.node
- the Node
whose outgoing messages should be collected.Node.getOutNeighbors()
protected boolean isMessageStillRelevant(Message message)
Tests whether the provided Message
is still relevant.
To be relevant, an arc must exist between the sender and the destination of the message.
message
- the Message
to be tested.true
if an arc exists between the send and the destination of the message.Node.getOutNeighbors()
,
Message.getSender()
,
Message.getDestination()
protected void requeueIfNeeded(Message message, Collection<Node> existingNodes)
Re-queues the specified Message
if necessary
For a Message
to be re-queueable, the following criteria must be met:
message
- the Message
which should be re-queued.existingNodes
- the Collection
of existing Nodes
.Message.isRetryModeEnabled()
,
Message.getSender()
,
Message.getDestination()
protected boolean requeueMessage(Message message)
Re-queues the specified Message
in its sender's send queue.
message
- the Message
which should be re-queued.true
if the re-queue has been successful.protected void deliverMessages(Collection<Message> messagesToSend)
Delivers the provided Messages
to send.
messagesToSend
- a Collection
of Messages
to deliver.deliverMessage(Message)
protected void deliverMessage(Message message)
Delivers the Message
to its destination.
message
- the Message
to be delivered.public void setDebug(boolean debug)
public void reset()
Resets the DefaultMessageEngine
.
Message
(ready to be sent or ready to be received) handled by the
DefaultMessageEngine
is discarded.reset
in interface MessageEngine
protected void clearSendQueues()
Clears the send queue of all Nodes
present in the topology.
clearSendQueues(Collection)
protected void clearSendQueues(Collection<Node> nodes)
Clears the send queue of all Nodes
present in the topology.
nodes
- a Collection
of Node
whose send queues should be cleared.