20100905 ns2: passing values between c++ and otcl

17
NS2: Passing Values NS2: Passing Values between C++ and OTcl between C++ and OTcl domains domains by Teerawat Issariyakul http://www.ns2ultimate.com September 2010 http://www.ns2ultimate.com 1

Upload: teerawat-issariyakul

Post on 16-Dec-2014

3.221 views

Category:

Education


1 download

DESCRIPTION

 

TRANSCRIPT

Page 1: 20100905 NS2: Passing Values Between C++ and OTcl

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

Page 2: 20100905 NS2: Passing Values Between C++ and OTcl

OutlineOutlineIntroductionMotivationExecuting OTcl statements

◦ C++ OTcl result(<constant string>)

resultf(<string with printf format>)

◦ OTcl C++: result()

http://www.ns2ultimate.com 2

Page 3: 20100905 NS2: Passing Values Between C++ and OTcl

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: 20100905 NS2: Passing Values Between C++ and OTcl

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

Page 5: 20100905 NS2: Passing Values Between C++ and OTcl

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

Page 6: 20100905 NS2: Passing Values Between C++ and OTcl

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

Page 7: 20100905 NS2: Passing Values Between C++ and OTcl

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);

};

Page 8: 20100905 NS2: Passing Values Between C++ and OTcl

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"

Page 9: 20100905 NS2: Passing Values Between C++ and OTcl

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

Page 10: 20100905 NS2: Passing Values Between C++ and OTcl

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

Page 11: 20100905 NS2: Passing Values Between C++ and OTcl

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

Page 12: 20100905 NS2: Passing Values Between C++ and OTcl

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

Page 13: 20100905 NS2: Passing Values Between C++ and OTcl

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

Page 14: 20100905 NS2: Passing Values Between C++ and OTcl

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"

Page 15: 20100905 NS2: Passing Values Between C++ and OTcl

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

Page 16: 20100905 NS2: Passing Values Between C++ and OTcl

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.

Page 17: 20100905 NS2: Passing Values Between C++ and OTcl

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