COMM3F Network Simulation
(Ian Fletcher & John Tindle).

Ns2 Tutorial
Paul Guy 3rd December 2003

4. Components of ns2

4.1. The structure of ns, including how to add a C++ patch (briefly)

Ns uses two languages C++ to implement protocols and Object Oriented Tcl (OTcl) to write simulation scripts. Ns is an OTcl script interpreter that has a simulation event scheduler and network libraries. So we write an Otcl script, which initiates an event scheduler. In the script we define the topology of the network using objects and functions from the libraries.

Traffic is added to the network and told when to commence and when to end. This is handled by the event scheduler, as is the termination of the simulation. The results from the interpreter can then be analysed or graphically displayed using nam. Without getting to complicated data path implementations are written and compiled by C++ in order to save processing time.

Once compiled these objects become available to the OTcl interpreter via an OTcl linkage. The linkage creates a matching OTcl object and makes available to it functions and variables that are also available to the linked C++ object. A class hierarchy in C++ (also called the compiled hierarchy), and a similar class hierarchy within the OTcl interpreter (also called the interpreted hierarchy) are also maintained between the linkage.

[Fig-5 Linkage [NIL01]]

[ Fig-6 Architecture [NIL01]]

If we look at the structure of Ns we develop and execute in Tcl using object libraries contained in OTcl. The event scheduler and most network components are developed in C++ to give us the Ns simulation. The Tclcl is the Tcl/C++ interface between OTcl and C++.

In order to enhance the functionality of Ns extensions are added. These extensions are modifications to the C++ or OTcl source code. Alternatively these may be completely new agents or traffic sources etc. As an example the event scheduler and network component object classes are located in the Ns-2 directory. In the Ns-2 directory are UDP.h and, which are the C++ files used to implement the UDP agent (this location may vary with different versions of Ns).

Similarly the tcl/lib directory contains the OTcl source code for node, links etc. To patch Ns to work with a new or modified agent means the creation of C++ code, which will involve the creation of a class and possibly a header file. A linkage has to be set up within the C++ class to ensure the OTcl can use instances of the C++ class. Dependent on what is being written, a header file may have to be registered in the packet.h and ns-packet.tcl.

Methods may have to be added to existing classes. Once these modifications are made the makefile will have to be modified to add yourclass.o to the object file list. Then run make clean and make depend before recompiling using make. This will patch the system to use your new or modified class.

Sometimes a patch may be released to modify or update the software this normally comes with the extension. patch. To add this simply ensure you are in the correct working directory and type:

patch p0 < patchname.patch

4.2. An overview object modelling using C++

The object-oriented paradigm covers three main areas encapsulation, Inheritance and polymorphism. C++ extends C by allowing new data types to be defined in a word it encapsulates C. A class is a logical method that organises data and functions to be contained in the same structure. A class contains members, which can be allocated permissions that are either public private or protected.

The public part is accessible from anywhere the class can be seen. The private part can only be used by member functions that makeup the same class or from a friend class. Protected is similar to private but with the exception of derived classes can also access the class. Each object is an instance of a class, which has its own data and functions. Constructors and deconstructors are used with classes.

Inheritance allows us to build new classes from existing classes and hence reuse code. Inheritance again comes in three forms public, private and protected. Member functions from the base class are made available depending on the form to all derived classes.
Polymorphism meaning many forms allows a collection of different object types to be manipulated uniformly through a generic interface.

Polymorphism comes in three types virtual functions, function name overloading and operator overloading. The inheritance hierarchy of related objects can be manipulated in the same way using virtual functions. Function name overloading is when a function is declared more than once in a program.

When a set of functions that perform a similar operation, overloading is used to collect them under the same name. The compiler will then determine which function to call. Operators are defined similar to functions but can be members or non-members of a class. Operators can take two arguments and are either unary or binary.

We can see how C++ through Tclcl can allow us to create similar hierarchies in both C++ and OTcl using the linkage, which allows members functions to be shared.

4.3. The role of the Tk/Tcl and OTcl command language

Tcl is a command line tool Tk is a graphical interface for Tcl. Tcl is used because of the fast iteration time it takes to modify a simulation model and re run the script file. Tcl/Tk uses a simple interface so users do not have to learn any complex languages such as C++.

OTcl as already discussed is built upon or encapsulates features of Tcl and as OTcl is object oriented it is interfaced with C++ through the use of Tclcl. OTcl is the front-end to the simulator as it is interpreted rather than compiled, the benefits for Ns is development speed rather than it’s slower execution speed. So we could say OTcl is used for control as it allows us to design our simulations by sharing member functions. OTcl is used for triggered actions. It also manipulates the existing objects in C++.

4.4. A typical simple Tcl example program

This is a simple for loop example within a procedure that shows setting variables and outputting the results to screen.

#create a variable and insert a value
set name "Paul Guy example tcl"

#Print the variable to screen
puts $name

#Define a procedure, no need for one really just an example
proc tt {} {

#Create a variable count with a value of 12
set count 12

#Print to screen
puts "My first $count Twelve Times Table:"

#Create a for loop and use an expression to do a calculation
for { set i 1 } { $i <= $count } { incr i 1 } {
puts "$i:\t[expr $i*$count]"


#call the procedure

Run as you would Ns by typing:

ns tclexample2.tcl