In this tutorial, we create a simple message-passing algorithm, in which a selected node disseminates a piece of information to all the other nodes (through multi-hop). As usual, the algorithm is coded through the three following steps:
Node
onStart()
,
onSelection()
,
onMessage()
Here is the code of our algorithm, followed by some explanations.
import io.jbotsim.core.Message;
import io.jbotsim.core.Color;
import io.jbotsim.core.Node;
public class BroadcastNode extends Node {
boolean informed;
@Override
public void onStart() {
informed = false;
setColor(null);
}
@Override
public void onSelection() {
informed = true;
setColor(Color.RED);
sendAll(new Message("My message"));
}
@Override
public void onMessage(Message message) {
if (! informed) {
informed = true;
setColor(Color.RED);
sendAll(new Message(message.getContent()));
}
}
}
Before executing this algorithm, we tell the topology that this class is the default node model.
We also set the duration of a round to 500ms (for the sake of visualization) via setTimeUnit()
.
import io.jbotsim.core.Topology;
import io.jbotsim.ui.JViewer;
public class Main{
public static void main(String[] args){
Topology tp = new Topology();
tp.setDefaultNodeModel(BroadcastNode.class);
tp.setTimeUnit(500);
new JViewer(tp);
tp.start();
}
}
The program can be executed by right-clicking anywhere around this code and selecting Run 'Main.main()'
(subsequent
runs can be done through the "play" icon in the top bar). When the program starts, you should see an empty window in
which you can add nodes and play with them.
As you can see, the nodes are colored in red. This is due to the execution of onStart()
on each node after it is added to the topology.
You can select the source node by Middle-Clicking (or Ctrl+Clicking) on it, causing onSelection()
to be executed on that node. You should now see the propagation occurring through the network.
Some observations:
sendAll()
method, whose effect is to send the given message to
all the local neighbors. It is also possible to send a message to a single neighbor using send()
. onMessage()
to be called on the receiving node.
A node may possibly receive several messages in the same round, causing as many invocations of
onMessage()
(sequentially).onMessage()
as above, or to
retrieve all of them at once for the current round, by calling getMailbox()
from another
method, e.g. from onClock()
.All these aspects are covered in the Advanced Message Passing tutorial.
At this point, you may have a few other questions about initialization, such as:
→ Essentially, all the answers are yes, as covered in the Topology Setup and Node Initialization tutorial.
In the above example, the nodes are static. We will now see how to make them move in the Basic Movement and Timing tutorial.