tcl/otcl tutorialtcl/otcl tutorial tcl ttcl cl tcl - ubc...

38
Tcl/OTcl Tutorial Tcl/OTcl Tutorial Tcl/OTcl Tutorial Tcl/OTcl Tutorial Tcl/OTcl Tutorial Tcl/OTcl Tutorial Tcl/OTcl Tutorial Tcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 1 ed pared Prepa P P ook: T. Iss y by at rwat Tcl T cl T cl T cl T cl T cl T cl T Tee Tcl cl cl cl cl cl cl ul yakul sariya Issa

Upload: trantuyen

Post on 11-Mar-2018

268 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

Tcl/OTcl TutorialTcl/OTcl TutorialTcl/OTcl TutorialTcl/OTcl TutorialTcl/OTcl TutorialTcl/OTcl TutorialTcl/OTcl TutorialTcl/OTcl Tutorial

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 1ed

pared

Prepa

PPook: T. Iss

yby

at rw

atTcl Tcl Tcl Tcl Tcl Tcl Tcl T

Tee

Tcl clclclclclcl

ulya

kul

sariy

a

Issa

Page 2: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

Outline• Tcl

– OverviewV i bl d D t T– Variables and Data Type

– Input/Output– Mathematics– Control Structure– Procedures

OT l• OTcl – Class, Inheritance, Members, and Syntax– An ExampleAn Example– Useful procedures

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 2

ere

pared

plel proc

Prepa

PPook: T. Iss

yby

tance,

at

erwat

Tee

ulya

kul

sariy

a

Issa

Page 3: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

lTcl• An Interpreter: p

– Interpret instructions lines by lines

– No need for compilation• Core language for NS2• Core language for NS2• Strength: Simplicity

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 3ed

th:

pared

Prepa

PPook: T. Iss

g

by

guag: Si

at

erwatctions

Teer comp

ulya

kul

sariy

a

Issa

Page 4: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

Tcl• Program invocation

>> tclsh [<filename> [<args>] ]>> tclsh [<filename> [<args>] ]>> ns [<filename> [<args>] ]

– No input arg = Enter Tcl environment; Take– No input arg = Enter Tcl environment; Take command line by line

– <filename> = an input Tcl file name– <args> = input arguments separated by white

spaces

• In the file <filename>, – <args> is stored in the internal variable $argv<args> is stored in the internal variable $argv– To retrieve nth (={0,1,…}) input argument,

executel d $ $

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

lindex $argv $n4

d

e sto

pared

is stoetrievcute

Prepa

PPook: T. Iss

yby

<filen

at

[<enviro

erwaenvir

t Tcl f

Teegument

ulya

kul

sariy

aargs

Iss

> [<argargs

Page 5: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

Tcl: Simple ExampleTcl: Simple Example

• Convert temperature from Fahrenheit (Tf) to Celsius (Tc): ( f) ( c)– Tc = f(Tf) = [ (Tf -32) *5/9 ]

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 5PPrepook: T. Iss

atlsiu

erwat32) *5

ulya

kul

sariy

ale

Issa

rom s

Page 6: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

Tcl: Simple ExampleTcl: Simple Example

• Convert temperature from Fahrenheit (Tf) to Celsius (Tc): ( f) ( c)– Tc = f(Tf) = [ (Tf -32) *5/9 ]

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 6

par

Prepa

PPook: T. Iss

atlsiu

erwat32) *5

ulya

kul

sariy

ale

Issa

rom s

Page 7: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

Tcl: Variable and Data TypeTcl: Variable and Data Type

• Everything is String.# var.tcl

>> tclsh var.tcl( 10 + 15 )( 25 )

Quiz: $c stores ( 10 + 15 )

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 7

pared

Prepa

PPook: T. Iss

y

at

erwat

erwerwTeeeeee

ulak

ulTyp

sariyTy

Issa

Page 8: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

Tcl: Variable and Data TypeTcl: Variable and Data Type

• Referencing value $– name = “name”– $name = the value stored in the variable whose

name is “name”

• set: Value assignment / Retrieval– set <name> <expr> set the value of $<name> to <expr>– $<name> set return the value stored in $<name>

• unset: Remove the value stored in a variable– unset <name> Remove the value stored in <name>

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

unset name Remove the value stored in name

8P

ed se

pared: Rem

nset <

Prepa

ook: T. Iss

nsety

g

by <expr>

at

erwatred in

Teegnmen

ulak

ulTyp

sariyTy

Issa

Page 9: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

T l V i bl d D t TTcl: Variable and Data Type

• From the previous example, insertunset cputs $cp

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 9ed

pared

Prepa

PPook: T. Iss

b

at

m

wat

ulya

kul

T

sariy

aTyT

Issa

mple

Page 10: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

Tcl: Variable and Data Type• Bracketing

Tcl: Variable and Data Type• Bracketing

– {}: Interpret as it is– “”: Interpolate the expression Interpolate the express on– []: Do this first– (): Indexing

• From the previous example, insert

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 10PPrepook: T. Iss

yby

atssion

erwass on

Teeous e

ulya

kul

ype

sariy

aType

Issa

Page 11: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

Tcl: Variable and Data Type

V i bl i l b l b d f lt

Tcl: Variable and Data Type

• Variable is global by default• Arrayy

– Regular array - Associative array

• List = A collection of itemsList A collection of items• Example: a list “mylist” which contain

th it 1 2 d 3Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

three items: 1, 2, and 311P

paredd

coA comple:

i

Prepa

ook: T. Iss

hreeyby

l

at

erwatAssoc

Tee

ulya

kul

ype

sariy

alt

Type

Issafault

Page 12: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

T l V i bl d D t TLi t C ti U ith

Tcl: Variable and Data Type• List Creation: Use either

– set mylist [1 2 3]y– set mylist “1 2 3”– set mylist {1 2 3}set mylist {1 2 3}

• Member retrieval: Retrieve the nth

( {0 1 }) b f h li(={0,1,…}) member of the listlindex $mylist <n>lindex $mylist n

“$Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

• Quiz: lindex “$mylist 1” returns ( 2 )12

ed

l

pared

l

Prepa

ook: T. Iss

Quiz: by

ybymemat

erwat

}

Tee

}ieval

ulya

kul

sariy

aTypT

Issaer

Page 13: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

T l V i bl d D t TM b tti

Tcl: Variable and Data Type• Member setting:

lset $mylist <n> <value>$ y• Group retrieval setting: Return a

list containing the nth to mth memberlist containing the nth to mth member of the list

lrange $mylist <n> <m>• List Appending:• List Appending:

lappend $mylist $anotherlist

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 13den

pare

ppenlappe

Prepa

PPook: T. Iss

yby ange

atn> <v

erwatetting

e nt

Teethe n

ulya

kul

sariy

aTypT

Issa

Page 14: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

Outline• Tcl

– OverviewV i bl d D t T– Variables and Data Type

– Input/Output– Mathematics– Control Structure– Procedures

OT l• OTcl – Class, Inheritance, Members, and Syntax– An ExampleAn Example– Useful procedures

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 14

ere

pared

plel proc

Prepa

PPook: T. Iss

yby

tance,

at

erwat

Tee

ulya

kul

sariy

a

Issa

Page 15: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

T l I t/O t tTcl: Input/OutputT l Ch l I f h id• Tcl Channel: Interface to the outside world (4 types)

1 S d d i di1. Standard input: stdin2. Standard output: stdout3. Standard error: stderr4. File channel {r,w,a, r+, w+, a+}

– To obtain the channelopen <filename> [<acc_type>]

– To close the fileclose <channel>

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 15

ope

paredose th

Prepa

PPook: T. Iss

ybythe chner

at rw

attdout

Tee: stder

ulya

kul

sariy

at

Issahe ou

Page 16: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

Tcl: Input/Output• gets: Read from the channel

gets <channel> <var>gets <channel> <var>• puts: Write to the channel

puts [-nonewline] [<channel>] <string>• Example: Copy input.txt to output.txtExample opy nput.txt to output.txt

Wait for “end of line”end of line

Write to the output

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

Write to the output file (output.txt)

16P

re

Prepa

ook: T. Iss

y

at

el

rwatchann

t.txt

Tee

t.tx

T

ulya

kul

sariy

a>

Issaar>

Page 17: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

T l M h iTcl: Mathematics

• expr: Tell Tcl that the following is a mathematical expressionmathematical expression.

– expr <operand1> <operator> <operand2>– expr <function>(<arg>)

• Example: E mp– Operator: expr $a + 3– Function: expr log10(10)

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

Function: expr log10(10)

17P

ed

ared

ratorunctio

Prepa

ook: T. Iss

unctyby

at

fxpre

erwaexpre

Tee<operat>(<arg>

ulya

kul

sariy

as

Issa

ollow

Page 18: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

Tcl: Mathematics• Lists of operators:

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 18

paPrep

aPPook: T. Iss

at at

ulya

kul

sariy

acs

Issa

Page 19: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

Tcl: MathematicsTcl Mathematics• Lists of functions:

– double returns a floating point– rand returns a random number in [0,1]– srand returns a random integer– wide returns an integer part– hypot returns

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

See http://www.tcl.tk/man/tcl8.5/TclCmd/mathfunc.htm19

ed etu

paredretur

ypot r

Prepa

PPook: T. Iss

S

yby rns a r

rns

Tes a flo

ulya

kul

sariy

a

ssa

Page 20: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

Outline• Tcl

– OverviewV i bl d D t T– Variables and Data Type

– Input/Output– Mathematics– Control Structure– Procedures

OT l• OTcl – Class, Inheritance, Members, and Syntax– An ExampleAn Example– Useful procedures

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 20

ere

pared

plel proc

Prepa

PPook: T. Iss

yby

tance,

at

erwat

Tee

ulya

kul

sariy

a

Issa

Page 21: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

Tcl: Control StructureTcl: Control Structure• if/else/elseif

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 21

pare

Prepa

PPook: T. Iss

ulya

kul

e

sariy

are

Issa

Page 22: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

Tcl: Control StructureTcl: Control Structure• switch

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 22ed

pared

Prepa

PPook: T. Iss

ulya

kul

e

sariy

are

Issa

Page 23: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

Tcl: Control StructureTcl: Control Structure• while/for/foreach

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 23

par

Prepa

PPook: T. Iss

ulya

kul

e

sariy

are

Issa

Page 24: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

Tcl: ProceduresTcl: Procedures• Definition:

proc <proc_name> { <arg1> … <argn> } {<actions><actions>return <returned_value>

}

• Invocation:set myvar [ <proc name> <val1> <valn> ]set myvar [ <proc_name> <val1> … <valn> ]

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 24d

n[ <

pare

ar [ <

Prepa

PPook: T. Iss

yby

at

erwat

_valu

Tee

ulya

kul

sariy

a

Issa

… <ar

Page 25: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

Outline• Tcl

– OverviewV i bl d D t T– Variables and Data Type

– Input/Output– Mathematics– Control Structure– Procedures

OT l• OTcl – Class, Inheritance, Members, and Syntax– An ExampleAn Example– Useful procedures

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 25

ere

pared

plel proc

Prepa

PPook: T. Iss

yby

tance,

at

erwat

Tee

ulya

kul

sariy

a

Issa

Page 26: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

OTcl: Main Components• Object oriented version of Tcl

Class = Definition for a group of objects• Class = Definition for a group of objects• I.E., we can instantiate objects from a

classclass.• Class Characterization

M b i bl i t– Member variable = instvars– Member procedure = instprocs

I h it• Inheritance– Base class = Mother class = Parent class

D i d cl ss child cl ss

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

– Derived class = child class

26P

ed pr

paredance

e clasi

Prepa

ook: T. Iss

Derivybby

ableocedu

at

ofgrou

erwaa grou

iate ob

Teerizati

ulya

kul

sariy

anents

Issa

Tcl

Page 27: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

OT l S tOTcl: Syntax• Class Declaration

Class <classname> [-superclass <Baseclassname>][ p ]

• Instvar declaration: Declared in an instproc$self instvar <name1> <name2> …

• Instvar access: set– Retrieve the value:

$<object> set <instvar name>$<object> set <instvar_name>– Set the value:

$<object> set <instvar_name> <value>

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 27

pareed

he

redhe valu

Prepa

PPook: T. Iss

yby ss: set

value:

atass <

erwat

eclare

Teetvar <na

ulya

kul

sariy

a

Issa

B

Page 28: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

OTcl: SyntaxOTcl: Syntax• Instproc declaration

<classname> instproc <proc_name> {<arg1> <argn>} {{<arg1> … <argn>} {

<body>}

• Note: <argi> can be replace with { <argi>• Note: <argi> can be replace with { <argi> <def> }, where <def> is the default

l t ti ll i d if thvalue, automatically assigned if the value is not specified at the invocation.

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 28

padwh

tredautoue is n

Prepa

PPook: T. Iss

c

by

gi> cahere

at

p

erwat

Tee

ulya

kul

sariy

a

Issa

roc_

Page 29: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

OTcl: SyntaxOTcl: Syntax

• Instproc invocation<object> <proc name> <val1> <valn><object> <proc_name> <val1> … <valn>

• Object construction– Basic: <classname> <object_name>– NS2: new <classname>NS2: new classname

• new does two thingsll { } ll– alloc{…}: Allocate memory

– init{…}: Necessary initialization

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

y

29ed

s t}

paredc{…}: A

nit{…}

Prepa

PPook: T. Iss

ycby c

two t

te> <v

erwae> <v

tion

Teeame> <ss

ulya

kul

sariy

a

Issa

Page 30: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

Outline• Tcl

– OverviewV i bl d D t T– Variables and Data Type

– Input/Output– Mathematics– Control Structure– Procedures

OT l• OTcl – Class, Inheritance, Members, and Syntax– An ExampleAn Example– Useful procedures

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 30

ere

pared

plel proc

Prepa

PPook: T. Iss

yby

tance,

at

erwat

Tee

ulya

kul

sariy

a

Issa

Page 31: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

OTcl: ExampleOTcl: Example• Base Class = Class NodeBase Class Class Node

– Instvar = stateI st ( )– Instproc = recv(…)

• Derived Class = Class Mobile– Derive all instvars and instprocs from

class Nodeclass Node– Instvar = location

Instproc = move( )– Instproc = move(…)

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 31dc =

pare

roc =

Prepa

PPook: T. Iss

yby

locat

Teat

erwat

ass M

eetvars a

ulya

kul

sariy

a

Issa

Page 32: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

OTcl: ExampleBASE CLASS pClass Node

t t

recv(pkt)

state

Derive

Cl M b l

Derive

Class Mobile

( )

location

move(x,y)

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

DERIVED CLASS32PPreook: T. Iss

ded d(

pareare

dare

ove(x y

eppa

epVED CLA

yyyby

bybybTee

ulya

kul

ariya

Page 33: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

OTcl: ExampleOTcl Example• Constructor

• InvocationObject instantiationObject instantiation

R t th

Set the instvar

Return the value stored in the instvar Invoke the

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008.

“state” to be 0 “state”Invoke the instproc “move”

33Ppa

rednstvarar

Prepa

ook: T. Iss

to be

erwa

TeeT

ulya

kul

sariy

a

Page 34: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

Outline• Tcl

– OverviewV i bl d D t T– Variables and Data Type

– Input/Output– Mathematics– Control Structure– Procedures

OT l• OTcl – Class, Inheritance, Members, and Syntax– An ExampleAn Example– Useful procedures

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 34

ere

pared

plel proc

Prepa

PPook: T. Iss

yby

tance,

at

erwat

Tee

ulya

kul

sariy

a

Issa

Page 35: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

OT l U f l P dOTcl: Useful Procedures• next: Invoke the instproc (with the same name) of n t n o th nstproc (w th th sam nam ) of

the base class.$self next [<args>][ g ]

• info: Return the information of – Instvar: <varname> info <options>

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 35PPrepook: T. Iss

atargs

erwatgion of

<option

ee

ulya

kul

sariy

aurese sam

Iss

sa

]

Page 36: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

OT l U f l P dOTcl: Useful Proceduresi f R h i f i f• info: Return the information of – Class: <classname> info <options>

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 36

paPrep

aPPook: T. Iss

at

ns>

at

ulya

kul

sariy

aures

Issa

Page 37: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

OT l U f l P dOTcl: Useful Procedures• More on procedure pertaining to

– Class: Visitftp://ftp.tns.lcs.mit.edu/pub/otcl/doc/class.html

– Object: Visitftp://ftp tns lcs mit edu/pub/otcl/doc/object htmlftp://ftp.tns.lcs.mit.edu/pub/otcl/doc/object.html

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 37ed

pared

Prepa

PPook: T. Iss

m

by s.m

at

erwat/pub/o

Teet

ulya

kul

sariy

aures

Issa

tainin

Page 38: Tcl/OTcl TutorialTcl/OTcl Tutorial Tcl TTcl cl Tcl - UBC ECEteerawat/publications/NS2/WA-TclTorial.pdfTcl/OTcl TutorialTcl/OTcl Tutorial Textbook: T. Issariyakul and E. Hossain, Introduction

Summary• Tcl

– OverviewV i bl d D t T– Variables and Data Type

– Input/Output– Mathematics– Control Structure– Procedures

OT l• OTcl – Class, Inheritance, Members, and Syntax– An ExampleAn Example– Useful procedures

Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 38

ere

pared

plel proc

Prepa

PPook: T. Iss

yby

tance,

at

erwat

Tee

ulya

kul

sariy

a

Issa