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

3.6 Class TclComm<strong>and</strong>This class (class TclComm<strong>and</strong>) provides just the mechanism for <strong>ns</strong> to export simple comm<strong>and</strong>s to the interpreter, th<strong>at</strong> canthen be executed within a global context by the interpreter. <strong>The</strong>re are two functio<strong>ns</strong> defined in ~<strong>ns</strong>/misc.cc: <strong>ns</strong>-r<strong>and</strong>om <strong>and</strong><strong>ns</strong>-version. <strong>The</strong>se two functio<strong>ns</strong> are initialized by the function init_misc(void), defined in ~<strong>ns</strong>/misc.cc; init_miscis invoked by Tcl_AppInit(void) during startup.• class VersionComm<strong>and</strong> defines the comm<strong>and</strong> <strong>ns</strong>-version. It takes no argument, <strong>and</strong> retur<strong>ns</strong> the current <strong>ns</strong>version string.% <strong>ns</strong>-version ;# get the current version2.0a12• class R<strong>and</strong>omComm<strong>and</strong> defines the comm<strong>and</strong> <strong>ns</strong>-r<strong>and</strong>om. With no argument, <strong>ns</strong>-r<strong>and</strong>om retur<strong>ns</strong> an integer,uniformly distributed in the interval [0, 2 31 − 1].When specified an argument, it takes th<strong>at</strong> argument as the seed. If this seed value is 0, the comm<strong>and</strong> uses a heuristicseed value; otherwise, it sets the seed for the r<strong>and</strong>om number gener<strong>at</strong>or to the specified value.% <strong>ns</strong>-r<strong>and</strong>om ;# return a r<strong>and</strong>om number2078917053% <strong>ns</strong>-r<strong>and</strong>om 0 ;#set the seed heuristically858190129% <strong>ns</strong>-r<strong>and</strong>om 23786 ;#set seed to specified value23786Note th<strong>at</strong>, it is generally not advisable to co<strong>ns</strong>truct top-level comm<strong>and</strong>s th<strong>at</strong> are available to the user. We now describe howto define a new comm<strong>and</strong> using the example class say_hello. <strong>The</strong> example defines the comm<strong>and</strong> hi, to print the string“hello world”, followed by any comm<strong>and</strong> line arguments specified by the user. For example,% hi this is <strong>ns</strong> [<strong>ns</strong>-version]hello world, this is <strong>ns</strong> 2.0a121. <strong>The</strong> comm<strong>and</strong> must be defined within a class derived from the class TclComm<strong>and</strong>. <strong>The</strong> class definition is:class say_hello : public TclComm<strong>and</strong> {public:say_hello();int comm<strong>and</strong>(int argc, co<strong>ns</strong>t char*co<strong>ns</strong>t* argv);};2. <strong>The</strong> co<strong>ns</strong>tructor for the class must invoke the TclComm<strong>and</strong> co<strong>ns</strong>tructor with the comm<strong>and</strong> as argument; i.e.,say_hello() : TclComm<strong>and</strong>("hi") {}<strong>The</strong> TclComm<strong>and</strong> co<strong>ns</strong>tructor sets up "hi" as a global procedure th<strong>at</strong> invokes TclComm<strong>and</strong>::disp<strong>at</strong>ch_cmd().3. <strong>The</strong> method comm<strong>and</strong>() must perform the desired action.<strong>The</strong> method is passed two arguments. <strong>The</strong> first argument, argc, contai<strong>ns</strong> the number of actual arguments passed bythe user.33

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

Saved successfully!

Ooh no, something went wrong!