IV. The first Tcl script

In this section, you are going to develop a Tcl script for ns which
simulates a simple topology. You are going to learn how to set up
nodes and links, how to send data from one node to another, how to
monitor a queue and how to start nam from your simulation script to
visualize your simulation.

Now we open a file for writing that is going to be used for the
nam trace data.

set nf [open out.nam w]
$ns namtrace-all $nf

The first line opens the file 'out.nam' for writing and gives it
the file handle 'nf'. In the second line we tell the simulator object
that we created above to write all simulation data that is going to be
relevant for nam into this file.

The next step is to add a 'finish' procedure that closes the trace
file and starts nam.

You don't really have to understand all of the above code yet. It will
get clearer to you once you see what the code does.

The next line tells the simulator object to execute the 'finish'
procedure after 5.0 seconds of simulation time.

$ns at 5.0 "finish"

You probably understand what this line does just by looking at it.
ns provides you with a very simple way to schedule events with the
'at' command.

The last line finally starts the simulation.

$ns run

You can actually save the file now and try to run it with
'ns example1.tcl'. You are going to get an error message like
'nam: empty trace file out.nam' though, because until now we haven't
defined any objects (nodes, links, etc.) or events. We are going to
define the objects in section 2 and the events
in section 3.

You will have to use the code from this section as starting point
in the other sections. You can download it here.

These lines create a UDP agent and attach it to the node n0, then attach a CBR
traffic generatot to the UDP agent. CBR stands for
'constant bit rate'. Line 7 and 8 should be self-explaining.
The packetSize is being set to 500 bytes and a packet will be sent every
0.005 seconds (i.e. 200 packets per second). You can find the relevant
parameters for each agent type in the
ns manual page

The next lines create a Null agent which acts as traffic sink and attach it to
node n1.

set null0 [new Agent/Null]
$ns attach-agent $n1 $null0

Now the two agents have to be connected with each other.

$ns connect $udp0 $null0

And now we have to tell the CBR agent when to send data and when to stop sending.
Note: It's probably best to put the following lines just before the line
'$ns at 5.0 "finish"'.

$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"

This code should be self-explaining again.

Now you can save the file and start the simulation again. When you click on
the 'play' button in the nam window, you will see that after 0.5 simulation
seconds, node 0 starts sending data packets to node 1. You might want to slow
nam down then with the 'Step' slider.

I suggest that now you start some experiments with nam and the Tcl script.
You can click on any packet in the nam window to monitor it, and you can also
click directly on the link to get some graphs with statistics. I also suggest
that you try to change the 'packetsize_' and 'interval_' parameters in the
Tcl script to see what happens. You can download the full example
here.

Most of the information that I needed to be able to write this Tcl script
was taken directly from the example files in the 'tcl/ex/' directory, while
I learned which CBR agent arguments (packetSize_, interval_) I had to set from
the ns manual page.