http://inesc-0.tagus.ist.utl.pt/~pmsrve/ns2/ns2_tips.html
NEW: How to modify existing procedures from the standard TCL library (/tcl/lib/) without having to recompile NS2 at each step. If you are modifying the standard procedures that are present in /tcl/lib/ns-lib.tcl, or in any of its sourced files, then you are supposed to recompile NS2 to re-generate the standard NS2 tcl library and make your changes permanent. However, if you are testing with a single script, its much easier to notice that TCL enables to replace all procedures in run-time; thus, to avoid modifying the ns2 core files, you should clone the procedure that you are modifying to the top of your script, and its sufficient to use your own modified, private copy of it. When its perfect, only then modify the original core file in /tcl/lib to make your changes permanent. More info: http://inesc-0.tagus.ist.utl.pt/~pmsrve/ns2/ns2_tips.html#_Toc121320478 Also check and step 4 of http://inesc-0.tagus.ist.utl.pt/~pmsrve/ns2/ns2_debugging3.html NEW: How to know values at run-time which are "easy" to store at configuration time One common FAQ is to know specific configuration details at run-time in C++ modules like classifiers and agents of the current node handle and addresses. Many variations of this FAQ exist, like this example: http://mailman.isi.edu/pipermail/ns-users/2006-January/053450.html For this, at configuration time, normally is fairly easy to store a reference to the corresponding node immediately after the components creation. At the time of creation, add to it an instance variable called mynode, eg $agent set mynode_handle $node $agent set mynode_id [$node id] then, at run time just check the variable: $agent set mynode_handle $agent set mynode_id In C++, its simply a case of correctly using "tcl binding", where otcl instance variables have always the same values of C++ variables, eg: contructor() { ... bind("mynode_id", mynode_id); // but don't forget to add "Class set mynode_id 0" to /tcl/lib/ns-default.tcl !!! ... } or just calling the same tcl code from C++, which is useful for more situations (see below) tcl.eval("%s set mynode_id", name()); // equivelent to "$self set mynode_id" assert(*tcl.result()); int mynode_id = atoi(tcl.result()); Another way, not discussed here, but recommended for more serious programming in NS2, is to modify C++ code to add references to fairly used variables directly in the C++ objects. This is essential to enable performance enouncements to components that deal with all the data packets, like classifiers, queues, etc. NEW: How to trigger procedures in other modules at run-time. Picture this: you have an module, say a classifier (C), which resides on each node (N) and listens to all packets; on certain run-time conditions, you want to trigger something at another component residing on that the node (N), namely an agent (A). However, in the place (C) where you are aware of that special condition (eg, where the triggering "if" is) you don't hold any variable to either (N) or (A) that could be used to trigger what you want! The first part of this FAQ is done by simply storing a reference on C to both N and A at configuration time (see above) The second part of this FAQ is done by calling otcl methods from C++ code (see below) Then, simply call on C the appropriate otcl method of A using this mechanism: tcl.eval("%s set myagent_handle", name()); assert(*tcl.result()); int myagent_handle = tcl.result(); tcl.eval("%s call_special_instproc %d, myagent_handle, some_int_parameter); Again, this is the easiest way to do such things, and is only recommended for immediate results; for more serious programming in NS2, one should add appropriate pointers to objects in C++ and respect the Object-Oriented rules.
