Creating a node algorithm

Node algorithms (aka distributed algorithm) are created in three steps:

  1. Create a class that extends Node
  2. Write the algorithm through overriding methods
  3. Tell JBotSim to use your type of nodes

Here is a skeleton of algorithm where four methods are overridden

import io.jbotsim.core.Node;
import io.jbotsim.core.Message;

public class EmptyNode extends Node{
    @Override
    public void onStart() {
        // JBotSim executes this method on each node upon initialization
    }

    @Override
    public void onSelection() {
        // JBotSim executes this method on a selected node
    }

    @Override
    public void onClock() {
        // JBotSim executes this method on each node in each round
    }

    @Override
    public void onMessage(Message message) {
        // JBotSim executes this method on a node every time it receives a message
    }
}

Thus, coding an algorithm boils down to insert code in the above method. Most algorithms can be specified using 2 or 3 such methods. Other methods exist, and you can add your own.

Once your class is ready, you can tell JBotSim to use it as the default model. This is done through calling setDefaultNodeModel() on the topology object, passing it your class of node. Typically, this is made in the main() method, itself being located either in the same class (not very clean) or in a dedicated class, as below.

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(EmptyNode.class);
        new JViewer(tp);
        tp.start();
    }
}

Let us now write a node algorithm that effectively does something, namely a broadcasting algorithm using messages →.