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

3.6 Class TclCommandThis class (class TclCommand) provides just the mechanism for ns to export simple commands to the interpreter, that canthen be executed within a global context by the interpreter. There are two functions defined in ~ns/misc.cc: ns-random andns-version. These two functions are initialized by the function init_misc(void), defined in ~ns/misc.cc; init_miscis invoked by Tcl_AppInit(void) during startup.• class VersionCommand defines the command ns-version. It takes no argument, and returns the current nsversion string.% ns-version ;# get the current version2.0a12• class RandomCommand defines the command ns-random. With no argument, ns-random returns an integer,uniformly distributed in the interval [0, 2 31 − 1].When specified an argument, it takes that argument as the seed. If this seed value is 0, the command uses a heuristicseed value; otherwise, it sets the seed for the random number generator to the specified value.% ns-random ;# return a random number2078917053% ns-random 0 ;#set the seed heuristically858190129% ns-random 23786 ;#set seed to specified value23786Note that, it is generally not advisable to construct top-level commands that are available to the user. We now describe howto define a new command using the example class say_hello. The example defines the command hi, to print the string“hello world”, followed by any command line arguments specified by the user. For example,% hi this is ns [ns-version]hello world, this is ns 2.0a121. The command must be defined within a class derived from the class TclCommand. The class definition is:class say_hello : public TclCommand {public:say_hello();int command(int argc, const char*const* argv);};2. The constructor for the class must invoke the TclCommand constructor with the command as argument; i.e.,say_hello() : TclCommand("hi") {}The TclCommand constructor sets up "hi" as a global procedure that invokes TclCommand::dispatch_cmd().3. The method command() must perform the desired action.The method is passed two arguments. The first argument, argc, contains the number of actual arguments passed bythe user.33

The actual arguments passed by the user are passed as an argument vector (argv) and contains the following:— argv[0] contains the name of the command (hi).— argv[1...(argc - 1)] contains additional arguments specified on the command line by the user.command() is invoked by dispatch_cmd().#include /* because we are using stream I/O */int say_hello::command(int argc, const char*const* argv) {cout

<strong>The</strong> actual arguments passed by the user are passed as an argument vector (argv) <strong>and</strong> contai<strong>ns</strong> the following:— argv[0] contai<strong>ns</strong> the name of the comm<strong>and</strong> (hi).— argv[1...(argc - 1)] contai<strong>ns</strong> additional arguments specified on the comm<strong>and</strong> line by the user.comm<strong>and</strong>() is invoked by disp<strong>at</strong>ch_cmd().#include /* because we are using stream I/O */int say_hello::comm<strong>and</strong>(int argc, co<strong>ns</strong>t char*co<strong>ns</strong>t* argv) {cout

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

Saved successfully!

Ooh no, something went wrong!