Chapter 3
OTcl Linkage
ns is an object oriented simulator, written in C++, with an OTcl interpreter as a frontend. The simulator supports a class
hierarchy in C++ (also called the compiled hierarchy in this document), and a similar class hierarchy within the OTcl inter-
preter (also called the interpreted hierarchy in this document). The two hierarchies are closely related to each other; from the
user’s perspective, there is a one-to-one correspondence between a class in the interpreted hierarchy and one in the compiled
hierarchy. The root of this hierarchy is the class TclObject. Users create new simulator objects through the interpreter; these
objects are instantiated within the interpreter, and are closely mirrored by a corresponding object in the compiled hierarchy.
The interpreted class hierarchy is automatically established through methods defined in the class TclClass. user instantiated
objects are mirrored through methods defined in the class TclObject. There are other hierarchies in the C++ code and OTcl
scripts; these other hierarchies are not mirrored in the manner of TclObject.
3.1 Concept Overview
Why two languages? ns uses two languages because simulator has two different kinds of things it needs to do. On one hand,
detailed simulations of protocols requires a systems programming language which can efficiently manipulate bytes, packet
headers, and implement algorithms that run over large data sets. For these tasks run-time speed is important and turn-around
time (run simulation, find bug, fix bug, recompile, re-run) is less important.
On the other hand, a large part of network research involves slightly varying parameters or configurations, or quickly exploring
a number of scenarios. In these cases, iteration time (change the model and re-run) is more important. Since configuration
runs once (at the beginning of the simulation), run-time of this part of the task is less important.
ns meets both of these needs with two languages, C++ and OTcl. C++ is fast to run but slower to change, making it suitable
for detailed protocol implementation. OTcl runs much slower but can be changed very quickly (and interactively), making it
ideal for simulation configuration. ns (via tclcl) provides glue to make objects and variables appear on both langauges.
For more information about the idea of scripting languages and split-language programming, see Ousterhout’s article in IEEE
Computer [26]. For more information about split level programming for network simulation, see the ns paper [2].
Which language for what? Having two languages raises the question of which language should be used for what purpose.
Our basic advice is to use OTcl:
• for configuration, setup, and “one-time” stuff
19