20100712-OTcl Command -- Getting Started

13
NS2: NS2: OTcl Command – Getting OTcl Command – Getting Started Started by Teerawat Issariyakul http://www.ns2ultimate.com July 2010 http://www.ns2ultimate.com 1

description

 

Transcript of 20100712-OTcl Command -- Getting Started

Page 1: 20100712-OTcl Command -- Getting Started

NS2: NS2: OTcl Command – Getting OTcl Command – Getting StartedStarted

by Teerawat Issariyakul

http://www.ns2ultimate.com

July 2010

http://www.ns2ultimate.com 1

Page 2: 20100712-OTcl Command -- Getting Started

OutlineOutlineIntroductionMotivationWhat we would like to doOTcl Command

http://www.ns2ultimate.com 2

Page 3: 20100712-OTcl Command -- Getting Started

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

Page 4: 20100712-OTcl Command -- Getting Started

MotivationMotivationOTcl is the place where users do the

programmingSometimes, users need to call C++

function

http://www.ns2ultimate.com 4

delay_

MyObject

programmer

C++

show_delay(){show “delay_” on the screen}

MyOTclObject

OTcl

show-delaybinding function

binding class name

Page 5: 20100712-OTcl Command -- Getting Started

What we would like to doWhat we would like to do

http://www.ns2ultimate.com 5

C++:oclass = MyObject (see

above)

ovariable = delay_

ofunction: show_delay()

OTcl:

oclass = MyOTclObject (see

above)

ovariable = none

oOTcl command: show-delay

Binding class name [see here and here]

delay_

MyObject

C++

show_delay(){show “delay_” on the screen}

MyOTclObject

OTcl

show-delaybinding function

binding class name

(see above)

Page 6: 20100712-OTcl Command -- Getting Started

OTcl Command: OTcl Command: IntroductionIntroductionCan be invoked from the OTcl domain

(show-delay)

Is bound to an OTcl object (object)

Execute C++ domain (show_delay())

http://www.ns2ultimate.com 6

Page 7: 20100712-OTcl Command -- Getting Started

OTcl Command: C++ filesOTcl Command: C++ files In the following, I will use files in the

previous post: otcl.cc, otcl.h, otcl.tclAdd a C++ variable delay_ to class MyObject

http://www.ns2ultimate.com 7

class MyObject : public TclObject {public: MyObject(); virtual ~MyObject(){};protected: int count_; double delay_; double speed_; double virtual_time_; int is_running_; int command(int argc, const char*const* argv); };

otcl.h

Page 8: 20100712-OTcl Command -- Getting Started

OTcl Command: C++ filesOTcl Command: C++ files In the following, I will use files in the

previous post: otcl.cc, otcl.h, otcl.tclAdd a C++ variable delay_ to class MyObject

Constructor: Set the initial value of delay_ to be 7.7

Do not bind the variable

Next step to use OTcl command to show the value of the variable delay_ on the screen

http://www.ns2ultimate.com 8

MyObject::MyObject(){

delay_ = 7.7;}

otcl.cc

Page 9: 20100712-OTcl Command -- Getting Started

OTcl Command: DefinitionOTcl Command: DefinitionDefine within C++ class (e.g. MyObject)Define by function “command(…)”Example: Insert the following into the file otcl.cc

Do not forget to add a C++ variable delay_ to class MyObject http://www.ns2ultimate.com 9

int MyObject::command(int argc, const char*const* argv) {

if (argc==2) {if (strcmp(argv[1], "show-delay") == 0) {

printf("Delay is %g\n", delay_);return (TCL_OK);

}}return TclObject::command(argc, argv);

};

OTcl command name

what this OTcl command do

Page 10: 20100712-OTcl Command -- Getting Started

Summary of Key StepsSummary of Key Steps1. Use files otcl.cc and otcl.h in the

previous post2. Add the details as stated earlier3. run “make” to incorporate C++

changes in to NS2 [see detail here]4. Create file otcl.tcl for testing

4.1 Create an OTcl object object

4.2 Invoke the OTcl command “show-delay” associated with object

4.4 Change the C++ constructor: set delay_ to be 9.9

4.5 Repeat 4.2 and 4.3http://www.ns2ultimate.com 10

Page 11: 20100712-OTcl Command -- Getting Started

File otcl.tcl

Run the file otcl.tcl

OTcl Command: OTcl Command: InvocationInvocation

http://www.ns2ultimate.com 11

set ns [new Simulator]set obj [new MyOTclObject]$obj show-delay

Page 12: 20100712-OTcl Command -- Getting Started

Modify the constructor

Run the file otcl.tcl again

OTcl Command: OTcl Command: InvocationInvocation

http://www.ns2ultimate.com 12

MyObject::MyObject(){

delay_ = 9.9;}

Page 13: 20100712-OTcl Command -- Getting Started

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 13