-----------------------------------------------------------------------
HOW TO CREATE YOUR OWN .cc FILES AND ASSOCIATED .tcl SCRIPTS
in ns2.29 (Checked on ns-allinone-ns-2.29)
-----------------------------------------------------------------------
Files you need to create :
1) test.cc
2) own-test.tcl
(*Code present at the end of this document, Just pastethe code and create
the files)
-----------
START
-----------
step 1:
create own .cc file : "test.cc"
say save at : ~/ns-2.29/test.cc
step 2:
In ~/ns-2.29/Makefile.in
Add at :
OBJ_CC
test.o \
step 3:
./configure
step 4:
make
step 5:
create your own .tcl script
say save at: ~/ns-2.29/own-test.tcl
step 6:
Now run this .tcl file :
./ns own-test.tcl
step 7:
You will get this output:
*********************************************************
warning: no class variable Agent/MyVar::x_tcl
see tcl-object.tcl in tclcl for info about this warning.
warning: no class variable Agent/MyVar::y_tcl
From add()
x=5 y = 10 x+y = 15
*********************************************************
step 8:
The reason for the warning is : that x_tcl, y_tcl needs to be
given some default values.
step 9:
To give the default values to the tcl variables used (x_tcl,
y_tcl)
Goto the file : ~/ns-2.29/tcl/lib/ns-default.tcl
Add in this file :
Agent/MyVar set x_tcl 0
Agent/MyVar set y_tcl 0
##Note : The name "Agent/MyVar" should be same, the one used in
MyAgentClass() constructor , i,e your .cc file
Also the name is used in the .tcl scripts to access
these variable (x_tcl, y_tcl) and member function (add())
step 10:
After adding default values in ns-default.tcl,
you need to re-make:
make
step 11:
Now run your .tcl script :
./ns own-test.tcl
step 12:
Correct Output comes as :
*********************************************************
From add()
x=5 y = 10 x+y = 15
*********************************************************
------------
END
------------
/*** CODE LISTING ***/
-1-
/****************************************************************/
test.cc (copy this and save as a file named :test.cc)
/****************************************************************/
#include <stdio.h>
#include <string.h>
#include "agent.h"
/* Contents */
/* Class : MyAgent (Your own Class) */
/* Member Functions : */
/* MyAgent() : bind() */
/* command() */
/* add() (Your function) */
/* static Class : MyAgentClass (Your class defined appened with Class) */
/* e.g if the class name above was : xyz then this static class will */
/* be called xyzClass */
/* Member Function */
/* MyAgentClass() */
/* create() */
class MyAgent : public Agent
{
private:
int x,y;
void add();
protected:
int command(int argc, const char* const* argv);
public:
MyAgent();
};
/* Constructor of Class MyAgent */
/* Used to bind the variables used */
MyAgent::MyAgent() : Agent(PT_UDP)
{
bind("x_tcl", &x);
bind("y_tcl", &y);
}
static class MyAgentClass: public TclClass
{
public:
MyAgentClass():TclClass("Agent/MyVar") {}
TclObject* create (int, const char*const*)
{
return (new MyAgent());
}
}my_obj;
int MyAgent::command(int argc, const char*const* argv)
{ if(argc==2)
{
if(strcmp(argv[1] ,"add") ==0)
{
add();
return (TCL_OK);
}
}
return(Agent::command(argc, argv));
}
/* Your member function */
void MyAgent::add()
{
Tcl& tcl = Tcl::instance();
tcl.eval("puts \" \n From add() \"");
printf("\n x=%d y = %d x+y = %d", x,y,x+y);
}
/****************************************************************/
END OF "test.cc"
/****************************************************************/
-2-
/****************************************************************/
own-test.tcl (your associated .tcl script)
/*****************************************************************/
set ns [new Simulator]
set myobj [new Agent/MyVar]
$myobj set x_tcl 5
$myobj set y_tcl 10
$myobj add
$ns run
/****************************************************************/
END OF "own-test.tcl"
/*****************************************************************/
--
Saurabh Sinha
Department of Computer Science,
University of Delhi.