3. IntroductionIntroduction
This 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
5. What we would like to doWhat we would like to do
http://www.ns2ultimate.com 5
C++:
oclass = MyObject
ovariable = count_
OTcl:
oclass = MyOTclObject
ovariable = ocount_
Bind C++ and OTcl
ocount_count_
MyObject MyOTclObject
binding class name
binding variable
6. Let’s Bind theVariableLet’s Bind theVariable
Binding class name [see here and here]
http://www.ns2ultimate.com 6
static class MyObjectClass : public TclClass {
public:
MyObjectClass() : TclClass("MyOTclObject")
{}
TclObject* create(int, const char*const*) {
return (new MyObject());
}
} class_my_object;
7. Binding SyntaxBinding Syntax
Put the following command in the C++
constructor:
◦ otcl_var_name = the name of OTcl variable
◦ c_var_name = the name of C++ variable
Example:
http://www.ns2ultimate.com 7
bind(“<otcl_var_name>”,<c_var_name>);
MyObject::MyObject()
{
bind("ocount_",&count_);
count_ = 10;
}
8. Summary of Key StepsSummary of Key Steps
1. Create file otcl.cc and otcl.h with the
details explained earlier
2. Modify Makefile—including otcl.o into the
compilation list [see detail here]
3. run “make” from the command prompt
4. Create file otcl.tcl for testing
4.1 In the C++ constructor: set count_ to be 10
4.2 Create an OTcl object
4.3 Show the value of count_ on the screen
4.4 Change the C++ constructor: set count_ to be 20
4.5 Repeat 4.2 and 4.3
http://www.ns2ultimate.com 8
9. File otcl.tcl
Run the file otcl.tcl
See If It WorksSee If It Works
http://www.ns2ultimate.com 9
set ns [new Simulator]
set obj [new MyOTclObject]
puts "The variable count is [$obj set ocount_]"
10. Modify the constructor
Run the file otcl.tcl again
The change was made in the C++ domain (MyObject::count_); The effect was
carried over to the OTcl domain (MyOTclObject::ocount_)
See If It WorksSee If It Works
http://www.ns2ultimate.com 10
MyObject::MyObject()
{
bind("ocount_",&count_);
count_ = 20;
}
11. For more information aboutFor more information about
NS 2NS 2
Please see this book from Springer
T. Issaraiyakul and E. Hossain, “Introduction to
Network Simulator NS2”, Springer 2009
http://www.ns2ultimate.com 11