The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ...

The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ... The ns Manual (formerly ns Notes and Documentation)1 - NM Lab at ...

nmlab.korea.ac.kr
from nmlab.korea.ac.kr More from this publisher
12.07.2015 Views

script after making their own changes. Finally, after adding the scripts to ~ns/tcl/lib/ns-lib.tcl, and every time thereafter thatthey change their script, the user must recompile ns for their changes to take effect. Of course, in most cases 4 , the user cansource their script to override the embedded code.The rest of this subsection illustrate how to integrate individual scripts directly into ns. The first step is convert the script intoan EmbeddedTcl object. The lines below expand ns-lib.tcl and create the EmbeddedTcl object instance called et_ns_lib:tclsh bin/tcl-expand.tcl tcl/lib/ns-lib.tcl | \../Tcl/tcl2c++ et_ns_lib > gen/ns_tcl.ccThe script, ~ns/bin/tcl-expand.tcl expands ns-lib.tcl by replacing all source lines with the corresponding source files.The program, ~tclcl/tcl2cc.c, converts the OTcl code into an equivalent EmbeddedTcl object, et_ns_lib.During initialization, invoking the method EmbeddedTcl::load explicitly evaluates the array.— ~tclcl/tcl-object.tcl is evaluated by the method Tcl::init(void); Tcl_AppInit() invokes Tcl::Init(). Theexact command syntax for the load is:et_tclobject.load();— Similarly, ~ns/tcl/lib/ns-lib.tcl is evaluated directly by Tcl_AppInit in ~ns/ns_tclsh.cc.et_ns_lib.load();3.8 Class InstVarThis section describes the internals of the class InstVar. This class defines the methods and mechanisms to bind a C++member variable in the compiled shadow object to a specified OTcl instance variable in the equivalent interpreted object. Thebinding is set up such that the value of the variable can be set or accessed either from within the interpreter, or from withinthe compiled code at all times.There are five instance variable classes: class InstVarReal, class InstVarTime, class InstVarBandwidth,class InstVarInt, and class InstVarBool, corresponding to bindings for real, time, bandwidth, integer, andboolean valued variables respectively.We now describe the mechanism by which instance variables are set up. We use the class InstVarReal to illustrate theconcept. However, this mechanism is applicable to all five types of instance variables.When setting up an interpreted variable to access a member variable, the member functions of the class InstVar assume thatthey are executing in the appropriate method execution context; therefore, they do not query the interpreter to determine thecontext in which this variable must exist.In order to guarantee the correct method execution context, a variable must only be bound if its class is already establishedwithin the interpreter, and the interpreter is currently operating on an object in that class. Note that the former requires thatwhen a method in a given class is going to make its variables accessible via the interpreter, there must be an associated4 The few places where this might not work are when certain variables might have to be defined or undefined, or otherwise the script contains code otherthan procedure and variable definitions and executes actions directly that might not be reversible.35

class TclClass (Section 3.5) defined that identifies the appropriate class hierarchy to the interpreter. The appropriate methodexecution context can therefore be created in one of two ways.An implicit solution occurs whenever a new TclObject is created within the interpreter. This sets up the method executioncontext within the interpreter. When the compiled shadow object of the interpreted TclObject is created, the constructor forthat compiled object can bind its member variables of that object to interpreted instance variables in the context of the newlycreated interpreted object.An explicit solution is to define a bind-variables operation within a command function, that can then be invokedvia the cmd method. The correct method execution context is established in order to execute the cmd method. Likewise,the compiled code is now operating on the appropriate shadow object, and can therefore safely bind the required membervariables.An instance variable is created by specifying the name of the interpreted variable, and the address of the member variable inthe compiled object. The constructor for the base class InstVar creates an instance of the variable in the interpreter, and thensets up a trap routine to catch all accesses to the variable through the interpreter.Whenever the variable is read through the interpreter, the trap routine is invoked just prior to the occurrence of the read. Theroutine invokes the appropriate get function that returns the current value of the variable. This value is then used to set thevalue of the interpreted variable that is then read by the interpreter.Likewise, whenever the variable is set through the interpreter, the trap routine is invoked just after to the write is completed.The routine gets the current value set by the interpreter, and invokes the appropriate set function that sets the value of thecompiled member to the current value set within the interpreter.36

class TclClass (Section 3.5) defined th<strong>at</strong> identifies the appropri<strong>at</strong>e class hierarchy to the interpreter. <strong>The</strong> appropri<strong>at</strong>e methodexecution context can therefore be cre<strong>at</strong>ed in one of two ways.An implicit solution occurs whenever a new TclObject is cre<strong>at</strong>ed within the interpreter. This sets up the method executioncontext within the interpreter. When the compiled shadow object of the interpreted TclObject is cre<strong>at</strong>ed, the co<strong>ns</strong>tructor forth<strong>at</strong> compiled object can bind its member variables of th<strong>at</strong> object to interpreted i<strong>ns</strong>tance variables in the context of the newlycre<strong>at</strong>ed interpreted object.An explicit solution is to define a bind-variables oper<strong>at</strong>ion within a comm<strong>and</strong> function, th<strong>at</strong> can then be invokedvia the cmd method. <strong>The</strong> correct method execution context is established in order to execute the cmd method. Likewise,the compiled code is now oper<strong>at</strong>ing on the appropri<strong>at</strong>e shadow object, <strong>and</strong> can therefore safely bind the required membervariables.An i<strong>ns</strong>tance variable is cre<strong>at</strong>ed by specifying the name of the interpreted variable, <strong>and</strong> the address of the member variable inthe compiled object. <strong>The</strong> co<strong>ns</strong>tructor for the base class I<strong>ns</strong>tVar cre<strong>at</strong>es an i<strong>ns</strong>tance of the variable in the interpreter, <strong>and</strong> the<strong>ns</strong>ets up a trap routine to c<strong>at</strong>ch all accesses to the variable through the interpreter.Whenever the variable is read through the interpreter, the trap routine is invoked just prior to the occurrence of the read. <strong>The</strong>routine invokes the appropri<strong>at</strong>e get function th<strong>at</strong> retur<strong>ns</strong> the current value of the variable. This value is then used to set thevalue of the interpreted variable th<strong>at</strong> is then read by the interpreter.Likewise, whenever the variable is set through the interpreter, the trap routine is invoked just after to the write is completed.<strong>The</strong> routine gets the current value set by the interpreter, <strong>and</strong> invokes the appropri<strong>at</strong>e set function th<strong>at</strong> sets the value of thecompiled member to the current value set within the interpreter.36

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!