if you can do what you want by manipulating existing C++ objects
and use C++:
if you are doing anything that requires processing each packet of a flow
if you have to change the behavior of an existing C++ class in ways that weren’t anticipated
For example, links are OTcl objects that assemble delay, queueing, and possibly loss modules. If your experiment can be
done with those pieces, great. If instead you want do something fancier (a special queueing dicipline or model of loss), then
you’ll need a new C++ object.
There are certainly grey areas in this spectrum: most routing is done in OTcl (although the core Dijkstra algorithm is in C++).
We’ve had HTTP simulations where each flow was started in OTcl and per-packet processing was all in C++. This approache
worked OK until we had 100s of flows starting per second of simulated time. In general, if you’re ever having to invoke Tcl
many times per second, you problably should move that code to C++.
3.2 Code Overview
In this document, we use the term “interpreter” to be synonymous with the OTcl interpreter. The code to interface with the
interpreter resides in a separate directory, tclcl. The rest of the simulator code resides in the directory, ns-2. We will use
the notation ~tclcl/
file
to refer to a particular
file
in the Tcl directory. Similarly, we will use the notation, ~ns/
file
to
refer to a particular
file
in the ns-2 directory.
There are a number of classes defined in ~tclcl/. We only focus on the six that are used in ns: The Class Tcl (Section 3.3)
contains the methods that C++ code will use to access the interpreter. The class TclObject (Section 3.4) is the base class for
all simulator objects that are also mirrored in the compiled hierarchy. The class TclClass (Section 3.5) defines the interpreted
class hierarchy, and the methods to permit the user to instantiate TclObjects. The class TclCommand (Section 3.6) is used to
define simple global interpreter commands. The class EmbeddedTcl (Section 3.7) contains the methods to load higher level
builtin commands that make configuring simulations easier. Finally, the class InstVar (Section 3.8) contains methods to access
C++ member variables as OTcl instance variables.
The procedures and functions described in this chapter can be found in ~tclcl/Tcl.{cc, h}, ~tclcl/Tcl2.cc, ~tclcl/tcl-object.tcl,
and, ~tclcl/tracedvar.{cc, h}. The file ~tclcl/tcl2c++.c is used in building ns, and is mentioned briefly in this chapter.
3.3 Class Tcl
The class Tcl encapsulates the actual instance of the OTcl interpreter, and provides the methods to access and communi-
cate 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
18