Connected nodes and user calls on nodes
A simple grid
Last chapter you learned how to write a program and how to start the simulation. Let’s now build a structure with more than one node. We’ll create a 5 times 5 grid. Therefor every node needs to know its neighbors. NetSimLan offers a generator for differed structures including grids. It calls the entry(node) method to tell a node about its initial neighbors. Every node gets an unique identifier, starting with one and incrementing with the created nodes. It can be requested by the build in function id(n) for the node n. We can use that to distinguish in which direction (top, left, bottom, right) a node belongs. Therefor we calculate a x and y coordinate from the id for the node itself and for the nodes handed over by framework. Combining this thoughts results in code like that:
gridNode{
/* square root of total numbers of nodes */
int size = 5;
node top;
node left;
node bottom;
node right;
int x_this;
int y_this;
init(){
x_this=(id(this)-1)/size;
y_this=(id(this)-1)%size;
}
entry(node n){
int x_n=(id(n)-1)/size;
int y_n=(id(n)-1)%size;
if (x_n == x_this+1 & y_n == y_this) right=n;
if (x_n == x_this & y_n == y_this+1) top=n;
if (x_n == x_this-1 & y_n == y_this) left=n;
if (x_n == x_this & y_n == y_this-1) bottom=n;
}
}
Start the software as described last chapter. This time select the build structure called grid (nxn) and enter 5 in the field nodes per row. Now press Create and the visualization window will show a grid with 25 nodes. If it isn’t arranged in a pretty way, you can move a node with the right mouse key. You can also zoom in and out using the mouse wheel.
Routing messages in the grid
Let’s extend that grid with message routing, such that every node can send a message to any other node. We’ll use a X-Y-Routing.
All we need is a method having the text and the id of the destination as parameter.
Using the deistination id the node calculates the x and the y coordinate, compare it to its own.
As long as the message didn’t arrive the destination, the current node shall hand it over to the neighbor the routing protocol tells us.
The syntax to call a method f on another node n is n->f();
.
This call isn’t proceeded directly but stored in a input queue and proceeded later. By default that’s up to 100ms. We’ll have a more detailed look at that in a later chapter.
Whenever n is empty this message is simply dropped. This makes code smaller for it’s not nessessary to check for n not beeing empty.
Adding this method leads to code like that. You can just add it to the other methods in the example above.
message (string text, int to_id){
int x_n=(to_id-1)/size; int y_n=(to_id-1)%size;
if (x_n == x_this & y_n == y_this) {
print("Message " + text + " received.");
} else if (x_this != x_n) {
if (x_n < x_this)
left -> message(text, to_id);
else
right -> message(text, to_id);
} else {
if (y_n < y_this)
bottom -> message(text, to_id);
if (y_n > y_this)
top -> message(text, to_id);
}
}
After restarting the simulation and recreating the graph have a look an the upper right area of the main window. This area shows in four tabs the status, the data and some statistics about a specific node. You can also call a method there. To select the node to work on, you can use the drop down menu above or left click a node on the visualization window. Select an arbitrary node, select the tab Call method and select the new method message. Enter a text like “hello” and the id of any node in the simulation, meaning an number between 1 and 25. After pressing Send call you can see the message call on the first node and a little later the print of the other node in the output area.
Coloring edges
Currently all edges look the same. Let’s color the edges. NetSimLan offers the colors blue, red, green, cyan, magenta, yellow and hidden. Hidden means the edge isn’t displayed in the visualization. To color an edge just select a color and write it after the variable name in the declaration.
By default the visualization arranges the nodes based on the edges to other nodes. To exclude single edges from this you may enter the keyword ignored after the variable name but before the color. Combining both may looks like follows:
node top ignore blue;
Marking the way
To see the way the message actually takes you can mark every node the message parses adding mark(true);
to the message method and rerun the simulation.
Here you find the complete source code for this chapter:
gridNode{
int size = 5; int x_this; int y_this;
node top blue; node left yellow;
node bottom red; node right green;
init(){
x_this=(id(this)-1)/size; y_this=(id(this)-1)%size;
}
entry(node n){
int x_n=(id(n)-1)/size; int y_n=(id(n)-1)%size;
if (x_n == x_this+1 & y_n == y_this) right=n;
if (x_n == x_this & y_n == y_this+1) top=n;
if (x_n == x_this-1 & y_n == y_this) left=n;
if (x_n == x_this & y_n == y_this-1) bottom=n;
}
message (string text, int to_id){
int x_n=(to_id-1)/size; int y_n=(to_id-1)%size;
if (x_n == x_this & y_n == y_this) {
print("Message " + text + " received.");
} else if (x_this != x_n) {
if (x_n < x_this)
left -> message(text, to_id);
else
right -> message(text, to_id);
} else {
if (y_n < y_this)
bottom -> message(text, to_id);
if (y_n > y_this)
top -> message(text, to_id);
}
mark(true);
}
}
Previous: First program Next: Timer