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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

}$self i<strong>ns</strong>tvar distanceCache_if ![info exists distanceCache_($addr)] {set distanceCache_($addr) [$self cmd distance? $addr]}set distanceCache_($addr)We now illustr<strong>at</strong>e how the comm<strong>and</strong>() method using ASRMAgent::comm<strong>and</strong>() as an example.int ASRMAgent::comm<strong>and</strong>(int argc, co<strong>ns</strong>t char*co<strong>ns</strong>t*argv) {Tcl& tcl = Tcl::i<strong>ns</strong>tance();if (argc == 3) {if (strcmp(argv[1], "distance?") == 0) {int sender = <strong>at</strong>oi(argv[2]);SRMinfo* sp = get_st<strong>at</strong>e(sender);tcl.tesultf("%f", sp->distance_);return TCL_OK;}}return (SRMAgent::comm<strong>and</strong>(argc, argv));}We can make the following observ<strong>at</strong>io<strong>ns</strong> from this piece of code:• <strong>The</strong> function is called with two arguments:<strong>The</strong> first argument (argc) indic<strong>at</strong>es the number of arguments specified in the comm<strong>and</strong> line to the interpreter.<strong>The</strong> comm<strong>and</strong> line arguments vector (argv) co<strong>ns</strong>ists of— argv[0] contai<strong>ns</strong> the name of the method, “cmd”.— argv[1] specifies the desired oper<strong>at</strong>ion.— If the user specified any arguments, then they are placed in argv[2...(argc - 1)].<strong>The</strong> arguments are passed as strings; they must be converted to the appropri<strong>at</strong>e d<strong>at</strong>a type.• If the oper<strong>at</strong>ion is successfully m<strong>at</strong>ched, the m<strong>at</strong>ch should return the result of the oper<strong>at</strong>ion using methods describedearlier (Section 3.3.3).• comm<strong>and</strong>() itself must return either TCL_OK or TCL_ERROR to indic<strong>at</strong>e success or failure as its return code.• If the oper<strong>at</strong>ion is not m<strong>at</strong>ched in this method, it must invoke its parent’s comm<strong>and</strong> method, <strong>and</strong> return the correspondingresult.This permits the user to concieve of oper<strong>at</strong>io<strong>ns</strong> as having the same inheritance properties as i<strong>ns</strong>tance procedures orcompiled methods.In the event th<strong>at</strong> this comm<strong>and</strong> method is defined for a class with multiple inheritance, the programmer has the libertyto choose one of two implement<strong>at</strong>io<strong>ns</strong>:1) Either they can invoke one of the parent’s comm<strong>and</strong> method, <strong>and</strong> return the result of th<strong>at</strong> invoc<strong>at</strong>ion, or2) <strong>The</strong>y can each of the parent’s comm<strong>and</strong> methods in some sequence, <strong>and</strong> return the result of the first invoc<strong>at</strong>ion th<strong>at</strong>is successful. If none of them are successful, then they should return an error.In our document, we call oper<strong>at</strong>io<strong>ns</strong> executed through the comm<strong>and</strong>() i<strong>ns</strong>tproc-likes. This reflects the usage of these oper<strong>at</strong>io<strong>ns</strong>as if they were OTcl i<strong>ns</strong>tance procedures of an object, but can be very subtly different in their realis<strong>at</strong>ion <strong>and</strong> usage.29

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

Saved successfully!

Ooh no, something went wrong!