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.

• store <strong>and</strong> lookup “TclObjects”.• acquire direct access to the interpreter.We describe each of the methods in the following subsectio<strong>ns</strong>.3.3.1 Obtain a Reference to the class Tcl i<strong>ns</strong>tanceA single i<strong>ns</strong>tance of the class is declared in ~tclcl/Tcl.cc as a st<strong>at</strong>ic member variable; the programmer must obtain a referenceto this i<strong>ns</strong>tance to access other methods described in this section. <strong>The</strong> st<strong>at</strong>ement required to access this i<strong>ns</strong>tance is:Tcl& tcl = Tcl::i<strong>ns</strong>tance();3.3.2 Invoking OTcl Procedures<strong>The</strong>re are four different methods to invoke an OTcl comm<strong>and</strong> through the i<strong>ns</strong>tance, tcl. <strong>The</strong>y differ essentially in theircalling arguments. Each function passes a string to the interpreter, th<strong>at</strong> then evalu<strong>at</strong>es the string in a global context. <strong>The</strong>semethods will return to the caller if the interpreter retur<strong>ns</strong> TCL_OK. On the other h<strong>and</strong>, if the interpreter retur<strong>ns</strong> TCL_ERROR,the methods will call tkerror{}. <strong>The</strong> user can overload this procedure to selectively disregard certain types of errors. Suchintricacies of OTcl programming are outside the scope of this document. <strong>The</strong> next section (Section 3.3.3) describes methodsto access the result returned by the interpreter.• tcl.eval(char* s) invokes Tcl_GlobalEval() to execute s through the interpreter.• tcl.evalc(co<strong>ns</strong>t char* s) preserves the argument string s. It copies the string s into its internal buffer; it then invokesthe previous eval(char* s) on the internal buffer.• tcl.eval() assumes th<strong>at</strong> the comm<strong>and</strong> is already stored in the class’ internal bp_; it directly invokes tcl.eval(char*bp_). A h<strong>and</strong>le to the buffer itself is available through the method tcl.buffer(void).• tcl.evalf(co<strong>ns</strong>t char* s, . . . ) is a Printf(3) like equivalent. It uses vsprintf(3) internally to cre<strong>at</strong>e the inputstring.As an example, here are some of the ways of using the above methods:Tcl& tcl = Tcl::i<strong>ns</strong>tance();char wrk[128];strcpy(wrk, "Simul<strong>at</strong>or set NumberInterfaces_ 1");tcl.eval(wrk);sprintf(tcl.buffer(), "Agent/SRM set requestFunction_ %s", "Fixed");tcl.eval();tcl.evalc("puts stdout hello world");tcl.evalf("%s request %d %d", name_, sender, msgid);3.3.3 Passing Results to/from the InterpreterWhen the interpreter invokes a C++ method, it expects the result back in the priv<strong>at</strong>e member variable, tcl_->result. Twomethods are available to set this variable.21

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

Saved successfully!

Ooh no, something went wrong!