Docsity

Lecture 9: Introduction to ns using Tcl and OTcl, Study notes of Computer Science

Partial preview of the text

Download Lecture 9: Introduction to ns using Tcl and OTcl and more Study notes Computer Science in PDF only on Docsity!

8T@!!! 8T@!!!

B

hqˆh‡rIr‡‚ xvtB hqˆh‡rIr‡‚ xvt

Xv‡r

!Xv‡r !

Lecture 9:Lecture 9: nsns TutorialTutorial

Dmitrii ZagorodnovDmitrii Zagorodnov

February 9, 2001 CSE 222 -- Lecture 9 – ns tutorial 2

P‰r

‰vr P‰r ‰vr

 Introduction to Tcl  Introduction to OTcl  Introduction to ns  ns installation  Midterm project

February 9, 2001 CSE 222 -- Lecture 9 – ns tutorial 3

UpyCv†‡‚

’ UpyCv†‡‚ ’

 Tcl = Tool Command Language  Simple, extendible, interpreted language  Tk is an extension for building GUI applications  Invented by John Ousterhout  1988 : first version of Tcl and Tk  1990 : presented at USENIX  1993 : first workshop at Berkeley  1994 - 1997 : Ousterhout at Sun  1997 - present : Ousterhout at Scriptics  Can be given a script or used interactively (tclsh)

February 9, 2001 CSE 222 -- Lecture 9 – ns tutorial 4

7h†vpT’‡h‘ 7h†vpT’‡h‘

 Scripts are commands separated by newlines or ; cmd arg arg ...  Variables are referenced using $ $var  Commands are nested using [ ] cmd [cmd arg ...] arg  Words are grouped into single argument using “” or { } puts “Hello, $name” while {$i < 10} { incr i }  Backslash is used to quote special characters (e.g. \n )  Pound character # is used for comments (note ;# )

February 9, 2001 CSE 222 -- Lecture 9 – ns tutorial 7

8‚‡

‚yT‡ ˆp‡ˆ r† 8‚‡ ‚yT‡ ˆp‡ˆ r†

if { ... } { ...} elseif { ... } else { ... }

while { $x > 0 } { ... }

foreach x { a b c d } { ... }

for {set i 0} {$i < 10} {incr i 1} { ... }

February 9, 2001 CSE 222 -- Lecture 9 – ns tutorial 8

Q

‚prqˆ r† Q ‚prqˆ r†

 Procedures: proc name arglist body proc add {a b} {return [expr $a + $b]}  Envoke just like a command add 4 5  Single, global scope for procedure names  Global variables must be brought into procedures proc foo { ... } { global ns ... }  “Qualified names” can be used to reference a variable in any scope ::x

February 9, 2001 CSE 222 -- Lecture 9 – ns tutorial 9

PUpy8yh††r† PUpy8yh††r†

 Create a class with Class command Class Bagel Bagel abagel or set abagel [new Bagel]  Methods are defined using instproc , instance variables using set or instvar. Use $self to refer to the object inside its methods. Bagel instproc toast { } { $self instvar toasted ... }  Constructors/destructors are called init and destroy

February 9, 2001 CSE 222 -- Lecture 9 – ns tutorial 10

Dur

v‡hprvPUpy Dur v‡hprvPUpy

 Classes can be extended using -superclass Class SpreadableBagel -superclass Bagel  One can trace the hierarchy using info **SpreadableBagel info heritage

Bagel Object**  Command next arranges for the next method with the same name up the inheritance tree to be invoked SpreadableBagel instproc init {args} { eval $self next $args ... }

February 9, 2001 CSE 222 -- Lecture 9 – ns tutorial 13

set ns [new Simulator]

set n0 [$ns node] set n1 [$ns node] set n2 [$ns node] set n3 [$ns node]

$ns duplex-link $n0 $n2 1Mb 10ms DropTail $ns duplex-link $n1 $n2 1Mb 10ms DropTail $ns duplex-link $n3 $n2 1Mb 10ms DropTail

Ir‡‚

xU‚ƒ‚y‚t’) Ir‡‚ xU‚ƒ‚y‚t’)

I‚qr†hqGvx†I‚qr†hqGvx†

February 9, 2001 CSE 222 -- Lecture 9 – ns tutorial 14

Ir‡‚

xU‚ƒ‚y‚t’) Ir‡‚ xU‚ƒ‚y‚t’)

6tr‡†hq6ƒƒyvph‡v‚†6tr‡†hq6ƒƒyvph‡v‚†

set udp0 [new Agent/UDP] $ns attach-agent $n0 $udp

set cbr0 [new Application/Traffic/CBR] $cbr0 set interval_ 0. $cbr0 attach-agent $udp

set null0 [new Agent/Null] $ns attach-agent $n3 $null

$ns connect $udp0 $null

cbr0 / udp

null

February 9, 2001 CSE 222 -- Lecture 9 – ns tutorial 15

Tpurqˆyvt@‰r‡† Tpurqˆyvt@‰r‡†

 Traffic flows can be started/stopped using at : $ns at 0.5 “$cbr0 start” $ns at 1.0 “$cbr0 stop”  The script is typically terminated by a finish() proc $ns at 5.0 “finish” proc finish { } { global ns fd $ns flush-trace; close $fd exec nam out.nam & exit 0 }

February 9, 2001 CSE 222 -- Lecture 9 – ns tutorial 16

6qqvtrhtr‡† 6qqvtrhtr‡†

Class Agent/Message/Receiver -superclass Agent/Message

Agent/Message/Receiver instproc handle msg {

$self instvar congested_ if $congested_ { set congested_ 0 } else { $self send "uncongested [lindex $msg 1]" }

}

February 9, 2001 CSE 222 -- Lecture 9 – ns tutorial 19

A‚

Ir‘‡Uv€r, A‚ Ir‘‡Uv€r,

 Domain Name System  Read and review Mockapetris and Dunlap paper from SIGCOMM ‘ 88  Read section 9.1 in the textbook