12.07.2015 Views

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 ...

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

By convention in <strong>ns</strong>, the class Agent/SRM/Adaptive is a subclass of Agent/SRM, is a subclass of Agent, is a subclass ofTclObject. <strong>The</strong> corresponding compiled class hierarchy is the ASRMAgent, derived from SRMAgent, derived from Agent,derived from TclObject respectively. <strong>The</strong> first line of the above example shows how a TclObject is cre<strong>at</strong>ed (or destroyed)(Section 3.4.1); the next line configures a bound variable (Section 3.4.2); <strong>and</strong> finally, the last line illustr<strong>at</strong>es the interpretedobject invoking a C++ method as if they were an i<strong>ns</strong>tance procedure (Section 3.4.4).3.4.1 Cre<strong>at</strong>ing <strong>and</strong> Destroying TclObjectsWhen the user cre<strong>at</strong>es a new TclObject, using the procedures new{} <strong>and</strong> delete{}; these procedures are defined in~tclcl/tcl-object.tcl. <strong>The</strong>y can be used to cre<strong>at</strong>e <strong>and</strong> destroy objects in all classes, including TclObjects. 2 . In this section,we describe the internal actio<strong>ns</strong> executed when a TclObject is cre<strong>at</strong>ed.Cre<strong>at</strong>ing TclObjects By using new{}, the user cre<strong>at</strong>es an interpreted TclObject. the interpreter will execute the co<strong>ns</strong>tructorfor th<strong>at</strong> object, init{}, passing it any arguments provided by the user. <strong>ns</strong> is respo<strong>ns</strong>ible for autom<strong>at</strong>ically cre<strong>at</strong>ing thecompiled object. <strong>The</strong> shadow object gets cre<strong>at</strong>ed by the base class TclObject’s co<strong>ns</strong>tructor. <strong>The</strong>refore, the co<strong>ns</strong>tructor forthe new TclObject must call the parent class co<strong>ns</strong>tructor first. new{} retur<strong>ns</strong> a h<strong>and</strong>le to the object, th<strong>at</strong> can then be used forfurther oper<strong>at</strong>io<strong>ns</strong> upon th<strong>at</strong> object.<strong>The</strong> following example illustr<strong>at</strong>es the Agent/SRM/Adaptive co<strong>ns</strong>tructor:Agent/SRM/Adaptive i<strong>ns</strong>tproc init args {eval $self next $args$self array set closest_ "requestor 0 repairor 0"$self set eps_ [$class set eps_]}<strong>The</strong> following sequence of actio<strong>ns</strong> are performed by the interpreter as part of i<strong>ns</strong>tanti<strong>at</strong>ing a new TclObject. For ease ofexposition, we describe the steps th<strong>at</strong> are executed to cre<strong>at</strong>e an Agent/SRM/Adaptive object. <strong>The</strong> steps are:1. Obtain an unique h<strong>and</strong>le for the new object from the TclObject name space. <strong>The</strong> h<strong>and</strong>le is returned to the user. Mosth<strong>and</strong>les in <strong>ns</strong> have the form _o〈NNN〉, where 〈NNN〉 is an integer. This h<strong>and</strong>le is cre<strong>at</strong>ed by getid{}. It can beretrieved from C++ with the name(){} method.2. Execute the co<strong>ns</strong>tructor for the new object. Any user-specified arguments are passed as arguments to the co<strong>ns</strong>tructor.This co<strong>ns</strong>tructor must invoke the co<strong>ns</strong>tructor associ<strong>at</strong>ed with its parent class.In our example above, the Agent/SRM/Adaptive calls its parent class in the very first line.Note th<strong>at</strong> each co<strong>ns</strong>tructor, in turn invokes its parent class’ co<strong>ns</strong>tructor ad nauseum. <strong>The</strong> last co<strong>ns</strong>tructor in <strong>ns</strong> is theTclObject co<strong>ns</strong>tructor. This co<strong>ns</strong>tructor is respo<strong>ns</strong>ible for setting up the shadow object, <strong>and</strong> performing other initializ<strong>at</strong>io<strong>ns</strong><strong>and</strong> bindings, as we explain below. It is preferable to call the parent co<strong>ns</strong>tructors first before performing theinitializ<strong>at</strong>io<strong>ns</strong> required in this class. This allows the shadow objects to be set up, <strong>and</strong> the variable bindings established.3. <strong>The</strong> TclObject co<strong>ns</strong>tructor invokes the i<strong>ns</strong>tance procedure cre<strong>at</strong>e-shadow{} for the class Agent/SRM/Adaptive.4. When the shadow object is cre<strong>at</strong>ed, <strong>ns</strong> calls all of the co<strong>ns</strong>tructors for the compiled object, each of which may establishvariable bindings for objects in th<strong>at</strong> class, <strong>and</strong> perform other necessary initializ<strong>at</strong>io<strong>ns</strong>. Hence our earlier injunction th<strong>at</strong>it is preferable to invoke the parent co<strong>ns</strong>tructors prior to performing the class initializ<strong>at</strong>io<strong>ns</strong>.5. After the shadow object is successfully cre<strong>at</strong>ed, cre<strong>at</strong>e_shadow(void)2 As an example, the classes Simul<strong>at</strong>or, Node, Link, or rtObject, are classes th<strong>at</strong> are not derived from the class TclObject. Objects in these classes are not,therefore, TclObjects. However, a Simul<strong>at</strong>or, Node, Link, or route Object is also i<strong>ns</strong>tanti<strong>at</strong>ed using the new procedure in <strong>ns</strong>.24

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

Saved successfully!

Ooh no, something went wrong!