Ns2 is an object
oriented network simulator which enables various local and wide area networks. It
creates various network protocols (UDP,TCP),traffic sources (Exponential on/off
, CBR,FTP,web), routing protocols like Dijkstra and variety of queue management
mechanisms like DropTrail , RED ,etc.Ns2 is written in otcl and C++ to separate
data path and control . Ns2 supports a
class hierarchy in C++ and a corresponding hierarchy within Otcl interpreter .
The simulator supports a class hierarchy in C++ and a similar
class hierarchy within the OTcl interpreter. The two hierarchies are closely inter
related . There is a one-to-one
correspondence between a class in the interpreted hierarchy and one in the
compiled hierarchy. The base of this hierarchy is the class Tcl Object. Users
can create new simulator objects with the help of interpreter. These objects
are initiated within the interpreter and these objects are closely mirrored with
the corresponding object in the compiled hierarchy . The interpreted class
hierarchy is created with the help of methods defined in the class TclClass. There
are other hierarchies in the C++ code and OTcl scripts; these other hierarchies
are not mirrored in the manner of TclObject.
With the help of two languages ns has two different things
1.Detailed
simulations of protocols requires a systems programming language which can
effectively calculate packet,bytes,implement algorithms and header that run
over large data sets.
2. A major part of network research involves little varying configurations
or parameters, or quickly exploring a number of scenarios.
NS needs both of the two languages OTcl and C++ . OTcl runs
much slower but can be changed very interactively making it perfect for
simulation configuration and C++ is very fast to run but slower to change
making it suitable for detailed protocol creation . ns with the help of tclcl
gives glue to make objects and variables present on two languages.
Otcl basics
It is important that one should understand how Otcl works
Assigning values to
variables
In tcl, values can be stored to variables and these values
can be further used in commands:
set a 5
set b [expr
$a/5]
In the first line the value 5 is assigned to the variable a .In
the second line, the result of the command [expr $a/5], which equals 1, is then
used as an argument to another command, which in turn assigns a value to the
variable b. The “$” sign is used to obtain a value contained in a variable and
square brackets are an indication of a command substitution.
Class Tcl
The class Tcl encapsulates the original presence of the OTcl
interpreter, and provides the methods to access and communicate with that
interpreter. The methods described in this section are relevant to the ns
programmer who is writing C++ code. The class provides methods for the
following operations:
• obtain a reference to the Tcl instance;
• invoke OTcl procedures through the
interpreter;
• retrieve, or pass back results to the interpreter;
• report error situations and exit in an
uniform manner; and
• store and lookup “TclObjects”.
• acquire direct access to the interpreter.
Procedures
One can define new procedures with the proc command. The
first argument to proc is the name of the procedure and the second argument
contains the list of the argument names to that procedure. For instance a
procedure that calculates the sum of two numbers can be defined as follows:
proc sum {a b} {
expr $a
+ $b
}
One can give an empty string as an argument list.In
the given example the variables used by the procedure is declared as global .
For instance:
proc sum {} {
global
a b
expr $a
+ $b
}
Calling subprocesses
The command exec creates a subprocess and waits for it to
complete. For example, to remove a file:
exec rm $testfile
The exec command is particularly useful when one wants to
call a tcl-script from within another tcl-script. For instance, in order to run
the tcl-script one.tcl multiple times with the value of the parameter “test”
ranging from 1 to 10, one can type the following lines to another tcl-script:
for {set ind 1} {$ind <= 10} {incr ind} {
set
test $ind
exec ns
one.tcl test
}
Nodes
With the help of the following
command new node objects can be created
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]
the Simulator class has a member function , called “node”
creates four nodes and assigns them to the handles n0, n1, n2 and n3. These
handles can later which can be used when referring to the nodes. If the node is
not a router but an end system, traffic agents (TCP, UDP etc.) and traffic
sources (FTP, CBR etc.) must be set up, i.e, sources need to be attached to the
agents and the agents to the nodes, respectively.
Controlling the simulation
Once the simulation topology is created , agents are
configured etc., the start and stop of the simulation and other events have to
be scheduled.
The simulation can be started and stopped with the commands
$ns at $simtime “finish”
$ns run
The first command schedules the procedure finish at the end
of the simulation, and the second command actually starts the simulation. The
finish procedure has to be defined to flush the trace buffer, close the trace
files and terminate the program with the exit routine. It can optionally start
NAM (a graphical network animator), post process information and plot this
information.
The finish procedure has to contain at least the following
elements
proc finish {} {
global
ns trace_all
$ns
flush-trace
close
$trace_all
exit 0
}