20100905 ns2: passing values between c++ and otcl
DESCRIPTION
TRANSCRIPT
NS2: Passing Values NS2: Passing Values between C++ and OTcl between C++ and OTcl domainsdomains
by Teerawat Issariyakul
http://www.ns2ultimate.com
September 2010
http://www.ns2ultimate.com 1
OutlineOutlineIntroductionMotivationExecuting OTcl statements
◦ C++ OTcl result(<constant string>)
resultf(<string with printf format>)
◦ OTcl C++: result()
http://www.ns2ultimate.com 2
IntroductionIntroductionThis is a series on how NS2 binds C++ and OTcl
together. This is the second topic of the series:
1. Why Two Languages?
2. Binding C++ and OTcl classes
3. Variable binding
4. OTcl command: Invoking C++ statements from the
OTcl domain
5. Eval and result: Invoking OTcl statements from the
C++ domain
6. Object binding and object construction process.
http://www.ns2ultimate.com 3
MotivationMotivation C++ is the place where users modify NS2 internal
mechanism.
Sometimes, we need to send/receive values between
C++ and OTcl domain
http://www.ns2ultimate.com 4
MyObject
C++
Constructor
MyOTclObject
OTcl
OTcl statement
Invoking OTcl function
binding class name
Pre-requisite
◦ Class binding [see here and here]
◦ OTcl command [see here, here, and here]
Given the bound environment
Objective:
1. C++ variable delay_ Value in the OTcl
domain
2. A Value in the OTcl domain the C++ variable
delay_
Pre-requisitePre-requisite
http://www.ns2ultimate.com 5
C++:oclass = MyObject
ovariable = delay_
OTcl:oclass =
MyOTclObject
ovariable = none
1. Tcl::result(<constant string>);
Pass <constant string> to the OTcl domain
2. Tcl::resultf(<string with printf format>);
Use the <string with printf format> to create a string, and pass it to the OTcl domain.
3. Tcl::result()
Receive a value from the OTcl domain. The return types is char*
Executing OTcl StatementExecuting OTcl Statement
http://www.ns2ultimate.com 6
Environment SetupEnvironment Setup
http://www.ns2ultimate.com 7
Constructor
OTcl command “get-delay”
MyObject::MyObject(){ delay_ = 9.9;};
int MyObject::command(int argc, const char*const* argv) {
if (argc==2) {if (strcmp(argv[1], “get-delay") == 0) {
[Passing delay_ to the OTcl]return (TCL_OK);
}}return TclObject::command(argc, argv);
};
Environment SetupEnvironment Setup
http://www.ns2ultimate.com 8
OTcl file “result.tcl”
set ns [new Simulator]set obj [new MyOTclObject]set d [$obj get-delay]puts "The variable d contains” puts "$d"
1. result(…): Details1. result(…): Details
http://www.ns2ultimate.com 9
Syntax
Tcl::result(“<const string>”);
where < const string > is a string you’d like to send to the OTcl domain.
Note: In C++, we invoke function through an object
◦ Our example Tcl& tcl = Tcl::instance();tcl.result(“A string from the C++ domain");
Create a Tcl object tcl
Invoke function eval(…) throught tcl
1. result(…): Usage1. result(…): Usage
http://www.ns2ultimate.com 10
Modify the OTcl command “get-delay”int MyObject::command(int argc, const char*const* argv) { Tcl& tcl = Tcl::instance(); if (argc==2) { if (strcmp(argv[1], “get-delay") == 0) { tcl.result("A string from the C++ domain"); return (TCL_OK); } } return TclObject::command(argc, argv);};
set ns [new Simulator]set obj [new MyOTclObject]set d [$obj get-delay]puts "The variable d contains” puts "$d"
Run the Tcl simulation
script “result.tcl” Output
The string from the C++ domain
is stored in the variable $d
The value is displayed on this line
2. resultf(…): Details2. resultf(…): Details
http://www.ns2ultimate.com 11
Syntax (similar to evalf(…))
Tcl::resultf(“<printf format>”,vars);
where - < printf format > is a formatting string.- vars is the list of variables
Note: In C++, we invoke function through an object
◦ Our example Tcl& tcl = Tcl::instance();tcl.resultf("%2.2f\n", delay_);
formatting string
variable to be passed to the OTcl domain
2. resultf(…): Usage2. resultf(…): Usage
http://www.ns2ultimate.com 12
Modify the OTcl command “get-delay”int MyObject::command(int argc, const char*const* argv) { Tcl& tcl = Tcl::instance(); if (argc==2) { if (strcmp(argv[1], “get-delay") == 0) { tcl.resultf("%2.2f\n", delay_); return (TCL_OK); } } return TclObject::command(argc, argv);};
set ns [new Simulator]set obj [new MyOTclObject]set d [$obj get-delay]puts "The variable d contains” puts "$d"
Run the Tcl simulation
script “result.tcl” Output
The C++ variable delay_ was set to 9.9
in the constructor;It is now stored in
the variable $d
The value is displayed on this line
1. Tcl::result(<constant string>);
Pass <constant string> to the OTcl domain
2. Tcl::resultf(<string with printf format>);
Use the <string with printf format> to create a string, and pass it to the OTcl domain.
3. Tcl::result()
Receive a value from the OTcl domain. The return types is char*
Executing OTcl StatementExecuting OTcl Statement
http://www.ns2ultimate.com 13
Environment SetupEnvironment Setup
http://www.ns2ultimate.com 14
Suppose we would like to initiate the C++ variable delay_ with an OTcl local variable $d
The Tcl Simulation script “result.tcl” would be
In C++, the constructor of class MyObject
needs to
◦ Read the value of $d from the OTcl domain
◦ Stored the value in the variable delay_
set ns [new Simulator]set d 50 set obj [new MyOTclObject]puts "The variable d contains” puts "$d"
3. result(): Usage3. result(): Usage
http://www.ns2ultimate.com 15
Objective: Get a value returned from OTcl statement execution
Syntax ()
Tcl::result();
Suppose we would like to initiate the C++ variable delay_ with an OTcl local variable $d
◦ Add the following code into the constructor Tcl& tcl = Tcl::instance(); tcl.eval("set d"); delay_ = atoi(tcl.result());
Execute “set d” in the OTcl domain;
Get the value returned from the previous OTcl statement
Note: set d returns the value stored in $d
3. result(): Usage3. result(): Usage
http://www.ns2ultimate.com 16
Modify the ConstructorMyObject::MyObject(){ Tcl& tcl = Tcl::instance(); tcl.eval("set d"); delay_ = atoi(tcl.result());}
set ns [new Simulator]set d 50 set obj [new MyOTclObject]puts "The variable d contains” puts "$d"
Run the Tcl simulation
script “result.tcl” Output
When creating a MyOTclObject
object,The C++ constructor
of class MyObject is invoked.
The value is displayed on this line
Now, the value stored in delay_ is 50.
For more information For more information about NS 2about NS 2
Please see this book from Springer
T. Issaraiyakul and E. Hossain, “Introduction to Network Simulator NS2”, Springer 2009
http://www.ns2ultimate.com 17