Node models, Link types, and icons

This example explains how to:

Node models: In JBotSim, one can specify new models of nodes that extend the Node class. In this example, we declare three models of nodes corresponding to the three classes defined below: HighwayCar, HighwayTower, and HighwayServer. Each is associated with a model name.

public static void main(String[] args) {
    Topology tp = new Topology(400,300);
    tp.setNodeModel("car", HighwayCar.class);
    tp.setNodeModel("tower", HighwayTower.class);
    tp.setNodeModel("server", HighwayServer.class);
    new JViewer(tp);
}    

Once the models are registered, left clicks on the surface will prompt the user with a selection menu (as shown in the video).

Let us look at the three classes in more details, starting with HighwayCar. This class defines a type of node that moves from left to right at constant random speed. Most instructions are familiar to you if you've followed the MovingNode example. One novelty is that it redefines the icon to be used.

Node icons: One can set an icon by providing the fully qualified path to a picture in the classpath (here, in the root directory). To be nice and consistent, your icon must have a square format (e.g., 100x100), have a transparent background, and be oriented upward by default. JBotSim will automatically rotate the icon as per the node's direction. The setSize method sets the size of the underlying node, which is then used by JBotSim to scale the image. You can download the three icons used in this example here: car.png, server.png, and tower.png

import jbotsim.Node;

public class HighwayCar extends Node{
    double speed = 3 + Math.random(); // Random speed between 3 and 5

    public HighwayCar(){
        setDirection(0); // Eastward
        setIcon("/car.png");
        setSize(10);
    }
    public void onClock() {
        move(speed);
        wrapLocation();
    }    
}    

In addition to vehicles, our scenario involves global servers and roadside towers. We consider typically a single server (perhaps two) that are connected to all roadside towers in a wired fashion. Towers, on the other hand, must have wireless capabilities to communicate with vehicles. Since default nodes in JBotSim do have wireless capabilities (namely, a communication range of 100), nothing is required on that side in the constructors of HighwayCar and HighwayTower.

import jbotsim.Node;

public class HighwayTower extends Node{ 
    public HighwayTower(){
        setIcon("/tower.png");
        setSize(14);
    }
}    

Objects of type HighwayServer, on the other hand, need to disable wireless capabilities at construction time using disableWireless(). The main feature of HighwayServer in our scenario is to connect automatically to new towers. This is realized by means of the onNodeAdded() method of the TopologyListener interface. Every time a node is added, the server checks whether its type is HighwayTower, and if so, creates a wired link to that node (explained shortly below). In order to deal with the case that servers are created after the towers, we also add a loop that iterates over all nodes in the search for towers.

import jbotsim.Link;
import jbotsim.Node;
import jbotsim.event.TopologyListener;

public class HighwayServer extends Node implements TopologyListener{   
    public HighwayServer(){
        setIcon("/server.png");
        setSize(10);
        disableWireless();
    }
    public void onStart(){
        for (Node node : getTopology().getNodes())
            if (node.getClass()==HighwayTower.class)
                getTopology().addLink(new Link(this, node));
        getTopology().addTopologyListener(this);
    }
    public void onNodeAdded(Node node) {
        if (node.getClass()==HighwayTower.class)
            getTopology().addLink(new Link(this, node));
    }
    public void onNodeRemoved(Node node) {
    }
}    

Wired links: JBotSim can deal with several types of links. In particular, a link can be directed or undirected (default is undirected) and it can also be wired or wireless. As you already know, wireless links are updated automatically by JBotSim. Wired links, on the other hand, are intended to last regardless of nodes movements and distances. In the present case, one wants to create a wired undirected link between servers and towers. Both the Type (directed/undirected) and the Mode (wired/wireless) are nested types in class Link. Their value can be specified upon construction. In this scenario, we want to create the link using new Link(n1, n2, Link.Type.UNDIRECTED, Link.Mode.WIRED) before adding it to the topology.