📄 node26.html
字号:
<html><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"><!--Converted with jLaTeX2HTML 2002 (1.62) JA patch-1.4patched version by: Kenshi Muto, Debian Project.LaTeX2HTML 2002 (1.62),original version by: Nikos Drakos, CBLU, University of Leeds* revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan* with significant contributions from: Jens Lippmann, Marek Rouchal, Martin Wilck and others --><HTML><HEAD><TITLE>3.6 Class TclCommand</TITLE><META NAME="description" CONTENT="3.6 Class TclCommand"><META NAME="keywords" CONTENT="everything"><META NAME="resource-type" CONTENT="document"><META NAME="distribution" CONTENT="global"><META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"><META NAME="Generator" CONTENT="jLaTeX2HTML v2002 JA patch-1.4"><META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css"><LINK REL="STYLESHEET" HREF="everything.css" tppabs="http://www.isi.edu/nsnam/ns/doc/everything.css"><LINK REL="next" HREF="node27.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node27.html"><LINK REL="previous" HREF="node24.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node24.html"><LINK REL="up" HREF="node5.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node5.html"><LINK REL="next" HREF="node27.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node27.html"></HEAD><BODY ><!--Navigation Panel--><A NAME="tex2html1521" HREF="node27.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node27.html"><IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="file:/usr/share/latex2html/icons/next.png"></A> <A NAME="tex2html1515" HREF="node5.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node5.html"><IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="file:/usr/share/latex2html/icons/up.png"></A> <A NAME="tex2html1509" HREF="node25.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node25.html"><IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="file:/usr/share/latex2html/icons/prev.png"></A> <A NAME="tex2html1517" HREF="node1.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node1.html"><IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="file:/usr/share/latex2html/icons/contents.png"></A> <A NAME="tex2html1519" HREF="node590.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node590.html"><IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="file:/usr/share/latex2html/icons/index.png"></A> <BR><B> Next:</B> <A NAME="tex2html1522" HREF="node27.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node27.html">3.7 Class EmbeddedTcl</A><B> Up:</B> <A NAME="tex2html1516" HREF="node5.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node5.html">3. OTcl Linkage</A><B> Previous:</B> <A NAME="tex2html1510" HREF="node25.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node25.html">3.5.1 How to Bind</A>   <B> <A NAME="tex2html1518" HREF="node1.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node1.html">Contents</A></B>   <B> <A NAME="tex2html1520" HREF="node590.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node590.html">Index</A></B> <BR><BR><!--End of Navigation Panel--><H1><A NAME="SECTION02160000000000000000"></A><A NAME="sec:TclCommand"></A><BR>3.6 Class TclCommand</H1><P>This class (TclCommand../Tcl/Tcl.h)provides just the mechanism for to exportsimple commands to the interpreter, that can then be executed within a global context by the interpreter.There are two functions defined in misc.cc:<TT>ns-random</TT> and <TT>ns-version</TT>.These two functions are initialized by the functioninit_misc../ns-2/misc.cc::init_misc,defined in misc.cc;<TT>init_misc</TT> is invoked byTcl_AppInit../ns-2/ns_tclsh.cc::Tcl_AppInitduring startup.<UL><LI>VersionCommand../ns-2/misc.cc defines the command <TT>ns-version</TT>. It takes no argument, and returns the current version string.<PRE> % ns-version # get the current version; 2.0a12</PRE><P></LI><LI>RandomCommand../ns-2/misc.cc defines the command <TT>ns-random</TT>. With no argument, <TT>ns-random</TT> returns an integer, uniformly distributed in the interval .<P>When specified an argument, it takes that argument as the seed. If this seed value is 0, the command uses a heuristic seed value; otherwise, it sets the seed for the random number generator to the specified value.<PRE> % ns-random # return a random number; 2078917053 % ns-random 0 #set the seed heuristically; 858190129 % ns-random 23786 #set seed to specified value; 23786</PRE></LI></UL><P><I>Note that, it is generally not advisable to construct top-level commands that are available to the user.</I>We now describe how to define a new commandusing the example <TT>class say_hello</TT>.The example defines the command <TT>hi</TT>,to print the string ``hello world'',followed by any command line arguments specified by the user.For example,<PRE> % hi this is ns [ns-version] hello world, this is ns 2.0a12</PRE><OL><LI>The command must be defined within a class derived from the TclCommand../Tcl/Tcl.h. The class definition is: <PRE> class say\_hello : public TclCommand { public: say\_hello(); int command(int argc, const char*const* argv); };</PRE></LI><LI>The constructor for the class must invoke the TclCommand constructor../Tcl/Tcl.ccTclCommand::TclCommand with the command as argument; , <PRE> say\_hello() : TclCommand("hi") {}</PRE> The <TT>TclCommand</TT> constructor sets up "hi" as a global procedure that invokes []TclCommand::dispatch_cmd../ns-2/Tcl.ccTclCommand::dispatch_cmd.</LI><LI>The method []command must perform the desired action.<P>The method is passed two arguments. The first argument, <TT>argc</TT>, contains the number of actual arguments passed by the user.<P>The actual arguments passed by the user are passed as an argument vector (<TT>argv</TT>) and contains the following:<P>-- <TT>argv[0]</TT> contains the name of the command (<TT>hi</TT>).<P>-- <TT>argv[1...(argc - 1)]</TT> contains additional arguments specified on the command line by the user.<P>[]command is invoked by []dispatch_cmd.<PRE> #include \<streams.h\> /* because we are using stream I/O / int say_hello::command(int argc, const char*const* argv) { cout \<\< "hello world:"; for (int i = 1; i \< argc; i++) cout \<\< ' ' \<\< argv[i]; cout \<\< '\bs n'; return TCL_OK; }</PRE></LI><LI>Finally, we require an instance of this class. <TT>TclCommand</TT> instances are created in the routine init_misc../ns-2/misc.cc::init_misc. <PRE> new say\_hello;</PRE></LI></OL>Note that there used to be more functions such as <TT>ns-at</TT> and<TT>ns-now</TT> that were accessible in this manner.Most of these functions have been subsumed into existing classes.In particular, <TT>ns-at</TT> and <TT>ns-now</TT> are accessiblethrough thescheduler TclObject../ns-2/scheduler.ccScheduler::command.These functions are defined in tcl/lib/ns-lib.tcl.<PRE> % set ns [new Simulator] # get new instance of simulator; _o1 % $ns now # query simulator for current time; 0 % $ns at \ldots # specify at operations for simulator; \ldots</PRE><P><HR><!--Navigation Panel--><A NAME="tex2html1521" HREF="node27.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node27.html"><IMG WIDTH="37" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="next" SRC="file:/usr/share/latex2html/icons/next.png"></A> <A NAME="tex2html1515" HREF="node5.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node5.html"><IMG WIDTH="26" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="up" SRC="file:/usr/share/latex2html/icons/up.png"></A> <A NAME="tex2html1509" HREF="node25.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node25.html"><IMG WIDTH="63" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="previous" SRC="file:/usr/share/latex2html/icons/prev.png"></A> <A NAME="tex2html1517" HREF="node1.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node1.html"><IMG WIDTH="65" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="contents" SRC="file:/usr/share/latex2html/icons/contents.png"></A> <A NAME="tex2html1519" HREF="node590.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node590.html"><IMG WIDTH="43" HEIGHT="24" ALIGN="BOTTOM" BORDER="0" ALT="index" SRC="file:/usr/share/latex2html/icons/index.png"></A> <BR><B> Next:</B> <A NAME="tex2html1522" HREF="node27.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node27.html">3.7 Class EmbeddedTcl</A><B> Up:</B> <A NAME="tex2html1516" HREF="node5.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node5.html">3. OTcl Linkage</A><B> Previous:</B> <A NAME="tex2html1510" HREF="node25.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node25.html">3.5.1 How to Bind</A>   <B> <A NAME="tex2html1518" HREF="node1.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node1.html">Contents</A></B>   <B> <A NAME="tex2html1520" HREF="node590.html" tppabs="http://www.isi.edu/nsnam/ns/doc/node590.html">Index</A></B> <!--End of Navigation Panel--><ADDRESS>2003-09-23</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -