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.

For a C++ variable to be traceable, it must belong to a class th<strong>at</strong> derives from TracedVar. <strong>The</strong> virtual base class TracedVarkeeps track of the variable’s name, owner, <strong>and</strong> tracer. Classes th<strong>at</strong> derives from TracedVar must implement the virtual methodvalue, th<strong>at</strong> takes a character buffer as an argument <strong>and</strong> writes the value of the variable into th<strong>at</strong> buffer.class TracedVar {...virtual char* value(char* buf) = 0;protected:TracedVar(co<strong>ns</strong>t char* name);co<strong>ns</strong>t char* name_; // name of the variableTclObject* owner_; // the object th<strong>at</strong> ow<strong>ns</strong> this variableTclObject* tracer_; // callback when the variable is changed...};<strong>The</strong> TclCL library exports two classes of TracedVar: TracedInt <strong>and</strong> TracedDouble. <strong>The</strong>se classes can be used inplace of the basic type int <strong>and</strong> double respectively. Both TracedInt <strong>and</strong> TracedDouble overload all the oper<strong>at</strong>ors th<strong>at</strong> canchange the value of the variable such as assignment, increment, <strong>and</strong> decrement. <strong>The</strong>se overloaded oper<strong>at</strong>ors use the assignmethod to assign the new value to the variable <strong>and</strong> call the tracer if the new value is different from the old one. TracedInt <strong>and</strong>TracedDouble also implement their value methods th<strong>at</strong> output the value of the variable into string. <strong>The</strong> width <strong>and</strong> precisionof the output can be pre-specified.3.4.4 comm<strong>and</strong>Methods: Definition <strong>and</strong> Invoc<strong>at</strong>ionFor every TclObject th<strong>at</strong> is cre<strong>at</strong>ed, <strong>ns</strong> establishes the i<strong>ns</strong>tance procedure, cmd{}, as a hook to executing methods through thecompiled shadow object. <strong>The</strong> procedure cmd{} invokes the method comm<strong>and</strong>() of the shadow object autom<strong>at</strong>ically, passingthe arguments to cmd{} as an argument vector to the comm<strong>and</strong>() method.<strong>The</strong> user can invoke the cmd{} method in one of two ways: by explicitly invoking the procedure, specifying the desiredoper<strong>at</strong>ion as the first argument, or implicitly, as if there were an i<strong>ns</strong>tance procedure of the same name as the desired oper<strong>at</strong>ion.Most simul<strong>at</strong>ion scripts will use the l<strong>at</strong>ter form, hence, we will describe th<strong>at</strong> mode of invoc<strong>at</strong>ion first.Co<strong>ns</strong>ider the th<strong>at</strong> the distance comput<strong>at</strong>ion in SRM is done by the compiled object; however, it is often used by the interpretedobject. It is usually invoked as:$srmObject distance? 〈agentAddress〉If there is no i<strong>ns</strong>tance procedure called distance?, the interpreter will invoke the i<strong>ns</strong>tance procedure unknown{}, definedin the base class TclObject. <strong>The</strong> unknown procedure then invokes$srmObject cmd distance? 〈agentAddress〉to execute the oper<strong>at</strong>ion through the compiled object’s comm<strong>and</strong>() procedure.Ofcourse, the user could explicitly invoke the oper<strong>at</strong>ion directly. One reason for this might be to overload the oper<strong>at</strong>ion byusing an i<strong>ns</strong>tance procedure of the same name. For example,Agent/SRM/Adaptive i<strong>ns</strong>tproc distance? addr {28

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

Saved successfully!

Ooh no, something went wrong!