+ All Categories
Home > Documents > Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning...

Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning...

Date post: 13-Mar-2020
Category:
Upload: others
View: 0 times
Download: 0 times
Share this document with a friend
73
Programming Essentials Programming Essentials Programming Essentials Programming Essentials Textbook: T. Issariyakul and E. Hossain, Introduction to Network Simulator NS2, Springer 2008. 1
Transcript
Page 1: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

Programming EssentialsProgramming EssentialsProgramming EssentialsProgramming Essentials

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

Page 2: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

Outline• Tcl/Otcl Tcl/Otcl

– Tcl– Regular Expression– OTcl

• Unix Essentials• Unix Essentials– Redirection Command– AWK– Grep

• Summary

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

y

2

Page 3: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l: O tlinTcl: Outline• Overview, Syntax, and ExampleOverview, Syntax, and Example• Data type: String, List, Associative

array, Handley• Input/Output, • Mathematical functions• Control Structures• Procedure

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

Page 4: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l: O iTcl: Overview• An Interpreter: An Interpreter:

– Interpret instructions lines by lines

– No need for compilation

• Core language for NS2

• Strength: Simplicity

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

Page 5: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

N t ti nNotation C d t• >> : Command prompt

• foo : A string “foo” Th l t d i i bl • <foo> : The value stored in variable

foo• [foo] : Optional placement of “foo”• [foo] : Optional placement of foo

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

Page 6: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l P I tiTcl: Program Invocation• Program invocationProgram invocation

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

N i E T l i – No input arg = Enter Tcl environment; Take command line by line

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

white spaces

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

Page 7: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l P I tiTcl: Program Invocation• <filename> = Tcl script or Simualtion p

script

F ithi th i l ti i t• From within the simulation script– The input argument <args> is stored in

an array $argvan array $argv – The number of input arguments is

stored in a variable $argcT i th ( {0 1 }) i – To retrieve nth (={0,1,…}) input argument, execute

lindex $argv $n

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

$ g $

7

Page 8: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l: S nt xTcl: Syntax• Position based (not keyword-based)( y ) First word = Tcl command

• Each word separated by a white space• Each word separated by a white space

• Termination = EOL or Semicolon “;”Termination EOL or Semicolon ;

• $ = Interpretation of variablep

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

Page 9: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l: S nt xTcl: Syntax• Grouping p g

– “<str>”: evaluation of <str>– {<str>}: <str> with value substitution

[<str>]: <str> without value substitution– [<str>]: <str> without value substitution– (<str>): the index <str>

• \ = disable the special character

t• # = comment

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

Page 10: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l Si l E lTcl: Simple Example• Convert temperature from Convert temperature from

Fahrenheit (Tf) to Celsius (Tc): T = f(T ) = [ (T 32) *5/9 ] – Tc = f(Tf) = [ (Tf -32) 5/9 ]

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

Page 11: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l D T S iTcl: Data Type--String• Main Data Type = string list associative arrayMain Data Type = string, list, associative array• String: Example

# var tcl

t l h t l

# var.tcl

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

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

Quiz: $c stores ( 10 + 15 )11

Page 12: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l S iTcl: String• Default type

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

• unset <name> Remove the value stored in <name>

• append <varname> <val1> <val2> …– Append <val1> <val2> … to the variable <name>Append val1 val2 … to the variable name

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

Page 13: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l S iTcl: StringFrom the previous example insertFrom the previous example, insert

unset cputs $cputs $c

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

Page 14: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l S iTcl: String• Command: string, format, scang, ,

• Command string: – string match <pattern> <string>:

• Return 1 if matched; 0 otherwise• <pattern> Use Regular Expression

– string tolower: change to lower case

h – string toupper: change to upper case

– string length: return no. of

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

string length return no. of characters

14

Page 15: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l S iTcl: String• Command string (cont.):g ( )– string first <str>: return the first

occurrence of <str>

– string last <str>: return the lastoccurrence of <str>

– string range <str> <f> <l>: return the characters in <str> between <f>position and <l> position

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

Page 16: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l S iTcl: String• Command

format <f t> < 1> < 2> format <fmt> <v1> <v2> …

– similar to printf in C++p– Return the formated string

Format Meaning Format Meaningg g%s String %x or %X Hexadecimal%c Ascii Equivalent

(of input integer)%f Floating point

(of input integer)%d or %i Integer %e or %E Scientific

Representationd l Ei h

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

%u Unsigned value %g or %G Either %f or %e%o Octal

16

Page 17: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l S iTcl: String• Command Command

scan <str> <fmt> <v1> <v2> …

– Reverse of format– Parse <str>– Look for format <fmt> – Place relevant values in <v1> <v2> …– Regular Expression can be used in <str>

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

Page 18: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l LiTcl: List• Similar to arraysSimilar to arrays

• Use {…} to Use {…} to – Create a list– Create a sublist– {} = empty list

C d • Commands: set, list, lappend, split, join, llength, lindex, linsert, lreplace

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

, p

18

Page 19: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l LiTcl: List• List Creation: Use eitherList Creation: Use either

– set mylist [list 1 2 3]set mylist “1 2 3”– set mylist “1 2 3”

– set mylist {1 2 3}

M b t i l R t i th th• Member retrieval: Retrieve the nth

(={0,1,…}) member of the listlindex $mylist <n>

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

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

Page 20: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l LiTcl: List• Member setting:Member setting:

lset mylist <n> <value>

G i l/ l i • Group retrieval/replace setting: Return a list containing the nth to mth

b f h limember of the listlrange $mylist <n> <m>

lreplace $mylist <n> <m> <e1> <e2>

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

Page 21: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l LiTcl: List

• List Appending:l d $ li t $ th li tlappend $mylist $anotherlist

• String Lengthllength $mylistllength $mylist

• Convert List to String:join <list1> <list2>join <list1> <list2>

• Convert String to List:spit <str> <separator>

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

spit <str> <separator>

21

Page 22: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l A i i ATcl: Associative Array• An array with string indices y g• Creation:

• set price(apple) 10• set price(orange) 20

• array <name> <arr> [<pattern>]: • Return a list containing all index values of <arr>. u g u f .• Return only indices which match the <pattern>.

• array get <arr>: Return indices and values of <arr>array get <arr>: Return indices and values of <arr>

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

Page 23: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l H dlTcl: Handle• An object return by Tcl j y

• Channel: a file, a serial port, or a TCP socket• Graphic: a graphic object

h f • http: a ref. to a URL

• We shall focus on “channel”• We shall focus on channel

• Discuss later in file accessDiscuss later in file access

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

Page 24: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l I /OTcl: Input/Output• Tcl Channel: Interface to the outside Tcl Channel Interface to the outside

world (4 types)1. Standard input: stdin ??

??2. Standard output: stdout3. Standard error: stderr {r,w,a,r+,w+,a+}

??

4. File channel– To obtain the channel

open <filename> [<acc type>]open <filename> [<acc_type>]

– To close the fileclose <channel>

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

Page 25: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l I /OTcl: Input/Output• gets: Read from the channelgets: Read from the channel

gets <channel> <var>

• puts: Write to the channelputs Write to the channelputs [-nonewline] [<channel>] <string>

• Example: Copy input txt to output txtExample: Copy input.txt to output.txtWait for “end of line”

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

Write to the output file (output.txt)25

Page 26: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l M h iTcl: Mathematics• incr: Incrementincr: Increment• expr: Tell Tcl that the following is a

mathematical expression.mathematical expression.

expr <operand1> <operator> <operand2>f ti ( )expr <function>(<arg>)

Example: • Example: – 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)

26

Page 27: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l M h iTcl: Mathematics• Lists of operators:Lists of operators:

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

Page 28: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l M h iTcl: Mathematics• Lists of functions:Lists of functions:

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

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

[1] http://www.tcl.tk/man/tcl8.5/TclCmd/mathfunc.htm[2] C. Flynt, “Tcl/Tk: A developer guide,” Morgan Kaufmann, 2003.

28

Page 29: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l C l STcl: Control Structure• if/else/elseifif/else/elseif

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

Page 30: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l C l STcl: Control Structure• switchswitch

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

Page 31: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l C l STcl: Control Structure• while/for/foreachwhile/for/foreach

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

Page 32: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l P dTcl: Procedures• Definition:Definition:

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

}

• Invocation:<proc_name> <val1> … <valn>

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

Page 33: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l P dTcl: Procedures• Global scope: useGlobal scope: use

global <varname1> <varname2> …

• Global Information Variable:

Variable MeaningVariable Meaning$argv A list of input argument$ Th b f l ts i “$ ”$argc The number of elements in “$argv”$env An associative array of environment

variables

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

variables

33

Page 34: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

Outline• Tcl/Otcl Tcl/Otcl

– Tcl– Regular Expression– OTcl

• Unix Essentials• Unix Essentials– Redirection Command– AWK– Grep

• Summary

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

y

34

Page 35: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

R l E iRegular Expression• Syntax for String MatchingSyntax for String Matching• Tcl, Perl, AWK, etc.• Terminologygy

– Rules = Piece: Matching rule– Target String: A string to be search– Atom: A character in a target stringAtom: A character in a target string– Count Modifier: Modify the number of matching

• Example: h ‘ ’ h “ h h b ” Is there any ‘n’ [or more] in the string “This is the beginning”

Matched!!

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

Matched!!

35

Page 36: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

AAtomAtom MeaningX A character X[ ] A h t ithi t [a-x] Any character within a to x. Any character (e.g., “a.c” match “abc”)^ Beginning of the target string only (e g “^ ”)Beginning of the target string only (e.g., ^a )$ End of the target string only (e.g., “z$”)\ Inhibit special character\ Inhibit special character

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

Page 37: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

• SyntaxCount Modifier

yModifier Meaningx? • Zero or One occurrence of xfx+ • One or More occurrence of xx* • Zero or More occurrence of x

• ExampleRegex 0 1 2 or moregColou?r Color Colour N/AColou+r N/A Colour Colouur

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

Colou*r Color Colour Colouur

37

Page 38: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

Range

Range Meaning[abc] • “a”, “b”, or “c”[a-z] • Any character from “a” to “z”[^a] • Any character except “a”y p[^a-z] • Any character except “a” to “z”

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

Page 39: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l I iTcl Invocation• Find a match• Find a match

regexp [<opt>] <pattern> <str> <varF> <varS1> <varS2> …

– <opt>: option– <pattern>: pattern to be matched– <str>: given string– <varF>: put the result of the entire match here< 1> < 2>– <var1>, <var2>, …

• <pattern> may contain several parts, each enclosed by (…).• Put 1st, 2nd ,… part in <var1>, <var2>, …

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

Page 40: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

T l I iTcl Invocation• Substitution• Substitution

regsub [<opt>] <pattern> <str> <substr> <var>

– <substr>: string to substitute the matched string– <var>: put the modified string here

• ExampleExample– regexp ABcdEF{([A-Z]*)((a-z)*[A-Z]*)} a b c d e– regsub “wrung” “This is wrong!!” result

Wh t t d i d ?

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

– What are stored in $a, $b,$c, $d, $e, and $result?

40

Page 41: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

Outline• Tcl/Otcl Tcl/Otcl

– Tcl– Regular Expression– OTcl

• Unix Essentials• Unix Essentials– Redirection Command– AWK– Grep

• Summary

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

y

41

Page 42: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

OT l O liOTcl: Outline• OverviewOverview• Example• Syntax• Useful methods

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

Page 43: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

OT l O iOTcl: Overview• Object oriented Tcl (OTcl)Object oriented Tcl (OTcl)• Class:

A ll ti f thi– A collection of common things– Attributes State or Memory Instvar– Methods What to do Instproc

• Objects and Instances• Inheritance

– Base class = Mother class = Parent class

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

– Derived class = child class

43

Page 44: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

OT l E lOTcl: Example• Base Class = Class NodeBase Class = Class Node

– Instvar = stateInstproc = ( )– Instproc = recv(…)

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

class NodeI t – Instvar = location

– Instproc = move(…)

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

Page 45: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

OTcl: ExampleBASE CLASS

Class Node

recv(pkt)

state

recv(pkt)

Derive

Class Mobile

location move(x,y)

state recv_(pkt)

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

DERIVED CLASS45

Page 46: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

OT l E lOTcl: Example• Constructor: Creating an ObjectConstructor Creat ng an Object

• InvocationObject instantiation

Return the value stored in

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

Set the instvar “state” to be 0

the instvar “state”Invoke the

instproc “move”46

Page 47: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

OT l M i Ch i iOTcl: Main Characteristics• Declaration AnywhereDeclaration Anywhere• Constructor = init • Destructor = destroyy• Call through object or class• Instvar $self = itself• Instproc next: call the same instproc of

the mother class

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

Page 48: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

OT l SOTcl: Syntax• Class DeclarationClass <classname> [-superclass <Baseclassname>]

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

• Instvar access: set– Retrieve the value:

$<object> set <instvar name>j _

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

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

Page 49: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

OT l SOTcl: Syntax• Instproc declarationInstproc declaration

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

}

• Note: <argi> can be replaced with { i d f } where d f is the {<argi> <def>}, where <def> is the default value, automatically assigned if the value is not specified at the if the value is not specified at the invocation.

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

Page 50: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

OT l SOTcl: Syntax• Instproc invocationInstproc invocation

<object> <proc_name> <val1> … <valn>

• Object construction– OTcl: <classname> <object name>– OTcl: <classname> <object_name>– NS2: new <classname>

d t thi• new does two things– alloc{…}: Allocate memory

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

– init{…}: Necessary initialization50

Page 51: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

OT l U f l P dOTcl: Useful Procedures• next: Invoke the instproc (with the same name) of next Invoke the instproc (with the same name) of

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

i f R t th i f ti f • info: Return the information of – Instvar: <varname> info <options>

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

Page 52: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

OT l U f l P dOTcl: Useful Procedures• info: Return the information of info Return the information of

– Class: <classname> info <options>

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

Page 53: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

OT l U f l P dOTcl: Useful Procedures• class: Change the classclass: Change the class• init: Constructor• instvar: Declare an instvar• instproc: Declare an instproc• next: Invoke instproc of the mother class

l • proc: Declare a procedure• set: Set or return the value of an instvar

t The opposite of set• unset: The opposite of set• unknown: Invoked when no matched instproc• superclass: Define the superclass

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

superclass: Define the superclass

53

Page 54: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

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

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

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

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

Page 55: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

Outline• Tcl/Otcl Tcl/Otcl

– Tcl– Regular Expression– OTcl

• Unix Essentials• Unix Essentials– Basic Command– Grepp– AWK

• Summary

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

y

55

Page 56: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

U i B i dUnix: Basic commands• Basic CommandsBasic Commandsls cd pwd chmod

more/less mv cp rm

• RedirectionPi ( )– Pipe (|):

“a | b” Use input of a as an input of b– Write Redirection (>) :

“a > b.txt” Write output of a in the file b.txt– Append Redirection (>>):

“a >> b.txt” Append outputof a to the file b.txt

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

a >> b.txt Append outputof a to the file b.txt

56

Page 57: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

U i GUnix: Grep• Look for a <pattern> through a set of filesLook for a <pattern> through a set of files

grep [option] <pattern> [<filename>]

• <pattern> can be either “text” or “regular expression”p

• Most frequently usedq ygrep -r <pattern> *

– Look for a <pattern> in all sub-directory

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

– r = recursive

57

Page 58: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

Outline• Tcl/Otcl Tcl/Otcl

– Tcl– Regular Expression– OTcl

• Unix Essentials• Unix Essentials– Redirection Command– Grepp– AWK

• Summary

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

y

58

Page 59: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

AWK O iAWK: Overview• Developed by Aho Weinberger and Developed by Aho, Weinberger, and

Kernighan (thus AWK)

• awk gawk (gnu extenstion)

• Main purpose:Ma n purposeExtract words from a structured file

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

Page 60: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

AWK E lAWK: Example• Input: infile txtInput: infile.txt

• Output: Lines with ‘EnQ’Output: Lines with EnQ

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

Page 61: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

AWK SAWK: Syntax• Invocation Invocation

Tell AWK “what to do”!!Tell AWK what to do !!

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

Page 62: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

R d d Fi ldRecords and Fields• A Record = A line in a data fileA Record = A line in a data file• Fields

Withi d– Within a record– Separated by “delimiter”

• A delimiter – A white space by default– Modified by “-F” option– E g “-F:” Use “:” as a delimiter

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

E.g., F: Use : as a delimiter

62

Page 63: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

E lExamples

• Record = Entire LineD li i Whi • Delimiter = White space

• Fields (stored in variables)• $1 = EnQ• $2 = 0.164• $3 = FromNode

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

Page 64: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

AWK S iAWK Script• SyntaxSyntax

• Key steps1 Do <initialization>– 1. Do <initialization>

– 2. For each record– If <pattern> is matched, do <actions>

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

If <pattern> is matched, do <actions>– 3. Do <final actions>

64

Page 65: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

PPatterns• Recall:Recall:

Th f • Three types of patterns:1. Empty: Match everythingp y y g2. Regular expression: Enclosed by

/…/

3. Boolean: ==, >, <, >=, <=

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

Page 66: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

P E lAWK script:

Pattern: ExamplesOutput: AWK script:

BEGIN{print “Begin Program”;}/EnQ/ {print “Found EnQ”;}

Output: Begin ProgramFound EnQ

/DeQ/ {print “Found DeQ”;}if $2 > 0.17 {print “$2”;}END{print “End Program”;}

Found DeQFound EnQFound DeQ {p g ;} QFound EnQ 0.172Found DeQ 0.172F d E Q 0 178Found EnQ 0.178End Program

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

Page 67: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

A iActions• A statement ends with “;”A statement ends with ;• Basic Actions+ (addition) ++ (increment)

- (subtraction) -- (decrement)* (multiplication) = (assignment)/ (division) % (modulo)

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

Page 68: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

A i OActions: Output• Basic printing: Basic printing:

print <item1> <item2> …

• Print with a specified format: • Print with a specified format: printf(<format>,<item1>,<item2> …)

• Write to a file (>): • Write to a file (>): print “test” > myfile.txt

App nd t fil (>>): • Append to a file (>>): print “test” >> myfile.txt

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

Page 69: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

E lExample• Input: infile txt Show lines with Input: infile.txt Show lines with

EnQ

• >> awk /EnQ/ infile.txt

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

Page 70: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

V i blVariables• No DeclarationNo Declaration• Type: Floating point or String

A i U “ ”• Assign: Use “=”• Every text is variable, unlessy• Enclosed by “…” String• Enclosed by [ ] ArrayEnclosed by […] Array• Enclosed by […] Associative Array

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

Page 71: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

C l SControl Structure• Main Structures: If While forMain Structures: If, While, for

• Breaking keywords

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

Page 72: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

Outline• Tcl/Otcl Tcl/Otcl

– Tcl– Regular Expression– OTcl

• Unix Essentials• Unix Essentials– Redirection Command– Grepp– AWK

• Summary

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

y

72

Page 73: Programming Essentials - UBC · 2009-10-07 · ^ Beginning of the target string only (e g Beginning of the target string only (e.g., “^a”)) $ End of the target string only (e.g.,

SSummary• Tcl InterpreterTcl Interpreter• C++ Compiler• Q: Main difference? Pros and Cons ?• Regular expression Pattern matching• OTcl = Object oriented version of Tcl• Unix basic commands• Redirection commands: |, >, >>• Grep: Pattern finding command• Grep: Pattern finding command• AWK: Extract info based on given pattern

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


Recommended