Otcl Programming basics syntax

download Otcl Programming basics syntax

of 8

Transcript of Otcl Programming basics syntax

  • 8/20/2019 Otcl Programming basics syntax

    1/19

     

    # Study of OTCL programming syntax

    # ------------------------first---------------------#

    #set a value to a variable:"set a 1"note in Tcl,variables are not

    typed.The value assigned to a variable determines the type of

    the variable.

    set a 1

    # declare a variable named "a" with a value "1"

    # warning dont use captial S alphabet for "set" keyword

    # By using Tcl's substitution operator "$" will replace

    "variableName" with the content of that variable

    # $variablename

     puts $a

    # print a value

    # line by line debugging - otcl interpreter

    #TclCL linkage between c++ and otcl programming language

    # users write program in otcl script language and saves in

    filename.tcl and runs using tcl filename.tcl (tcl8.4 installed) or

    ns filename.tcl (ns2 installed)

    # use ctrl+c for type mode in or return to terminal window

  • 8/20/2019 Otcl Programming basics syntax

    2/19

     

    #----------------------second-----------------------#

    # To use the value assigned to a variable ,we need to use the $

    sign:set x $a

    set x $a

    # declare a variable named "x" with a value equal to the value

    of the variable "a"

     puts $x

    #gedit : if c variable is not set (i.e set c $c is not present)

    #terminal displays: can't read "c": no such variable

    # # --> write a comment

    #----------------------third-----------------------#

    #A mathematical operation is done using the expression

    command expr as follows: set b [expr $x +-*/% $a]

    #declare a variable named "b" with a value equal to the sum of

    "x" and "a"

    set b [expr $x + $a]

     puts "add a with b and the addition b : $b"

    # Puts "text" print out the word "text"

    # if x Puts "The value of x is $x" -> print out "The value of x is

    0"

    set b [expr $x - $a]

     puts "Substract a from x and the substraction b : $b"

  • 8/20/2019 Otcl Programming basics syntax

    3/19

     

    set b [expr $x * $a]

     puts "x multiply by a and the multiplication b : $b"

    set b [expr $x / $a]

     puts "x divided by a and the division b : $b"

    set b [expr $x % $a]

     puts "x divided by a and the remainder is i.e modulas b : $b"

    #use curved brackets for complex operation

    set b [expr (($x+$a)/$a)%$a]

     puts "complex equation value is $b"

     puts "b complex linear equation : $b"

    #---------------------fourth------------------------#

    # To comment a line,use the symbol #

     puts "comment a line use # followed by statements"

    #--------------------fifth--------------------------#

    #To create a file, we need to give it a name that will identify it

     by our system. Then we need to assign a pointer to it that will

     be used within the Tcl program in order to relate it:

    set tclFilePointer [open ourFile.txt w]

    # create a file, say "filename" or "ourFile" or "out1" ... and

    assign a pointer to it within tcl program in order to relate it

    "tclFilePointer" or "file1" ...

  • 8/20/2019 Otcl Programming basics syntax

    4/19

     

    set file1 [open out1.tr w]

    # i.e., set filel [open out1.tr w] -

    # define a file named "file1" and assign it to "out1.tr"

    #--------------------sixth--------------------------#

    #• The command puts is used for printing an output. Note:

    each time the "puts" command is used, a new line is started.

    # To avoid new line, one has to add -nonewline after the "puts"

    command.

    #If we want to print into a file (say the one we defined above)

    we type

    # puts $filel "text"

    # Tabulating is done by inserting \t.

    #To print an output, use the command puts "". For instance, to

     print into our File.txt the previous variable b with its tabulated

    value, use the following line:

    #puts $tclFilePointer "b \t a"

    #puts $tclFilePointer "b \t $a"

     puts $tclFilePointer "$a \t $x"

    set a [expr $a+1]

    set b [expr $b+2]

     puts $tclFilePointer "$a \t $x"

    set a [expr $a+1]

    set b [expr $b+2]

  • 8/20/2019 Otcl Programming basics syntax

    5/19

     

     puts $tclFilePointer "$a \t $x"

    set a [expr $a+1]

    set b [expr $b+2]

     puts $tclFilePointer "$a \t $x"

     puts $tclFilePointer "[expr ($a + 1)] \t [expr ($x + 2)]"

    #puts $tclFilePointer "$x [expr (2 * $x)]"

    close $tclFilePointer

    #puts $file1 "b \t a"

     puts $file1 "$a \t $x"

    set a [expr $a+1]

    set b [expr $b+2]

     puts $file1 "$a \t $x"

    set a [expr $a+1]

    set b [expr $b+2]

     puts $file1 "$a \t $x"

    set a [expr $a+1]

    set b [expr $b+2]

     puts $file1 "$a \t $x"

     puts $file1 "[expr ($a + 1)] \t [expr ($x + 2)]"

    #puts $file1 "$x [expr (2 * $x)]"

    close $file1

    #-------------------seventh-------------------------#

  • 8/20/2019 Otcl Programming basics syntax

    6/19

     

    #To execute a unix/Linux command from the Tcl script, use

    the command exec.

    #For example, to plot the curve of the data contained in our

    File.txt

    #e.g., use: exec gnuplot ourFile.txt &

    # & is to allow the command gnuplot or xgraph to execute in

    the background.

    # exec gnuplot our.txt &

    #exec xgraph data.tr & -> execute the program "xgraph",

    #which takes the file "data.tr" as an input

    #exec xgraph ourFile.txt &

    #exec xgraph out1.tr &

    #-------------------eigth--------------------------#

    #if { expression } {

    # some commands

    #} else {

    #some commands

    #}

    # The if command can be nested with other "ifs and with

    "else"s that can appear in "" part.

  • 8/20/2019 Otcl Programming basics syntax

    7/19

     

     Note that when testing equality, we should use "==" and (not

    "=" ie., The inequality is written with !=).

    #for { set i 0 } { $i < 5 } { incr i } {

    #some commands

    #}

    #Proc example {x1 x1 ....} {

    # some commands ...

    # return $something

    #}

    #warning: invalid command name "If" use "if"

    if {$x > $a} {

     puts "print a $a"

    } else {

     puts "print x $x"

    }

    #warning: invalid command name "For" use "for"

    for { set i 0 } { $i < 5 } { incr i } {

     puts "print a $a"

    }

  • 8/20/2019 Otcl Programming basics syntax

    8/19

     

    #Proc example {x1 x1 ....} {

    # some commands ...

    # return $something

    #}

    #Create a procedure for doing arithmedtic operations

     proc test {} {

    set a 43

    set b 27

    set c [expr $a * $b]

    set d [expr [expr $a - $b] * $c]

     puts "c = $c d = $d"

    for {set k 0} {$k < 10} {incr k} {

    if {$k < 5} {

     puts "k < 5, pow = [expr pow($d,$k)]"

    } else {

     puts "k >= 5, mod = [expr $d % $k]"

    }

    }

    }

    #calling the procedure

    test

    #------------nineth-------

  • 8/20/2019 Otcl Programming basics syntax

    9/19

     

    # tcl allows to create procedures. They can return some value

    in which case they contain a "return" command. The general

    form of a procedure which we name "blue" is

    #proc blue { pari par2 ... } {

    #global varl var2

    #

    #return $something

    #}

    #The procedure receives some parameters that can be objects,

    files or variables. In our case these are named pari, par'2, etc.

    These parameters will be used within the procedures with

    these names. The procedure is called by typing blue x y ...

    where the values of x and y will be used by the procedure for

     pari and par2. If pari and par2 are changed within the

     procedure, this will not affect the values of x and y. On the

    other hand, if we wish the procedure to be able to affect

    directly variables external to it, we have to declare these

    variables as "global". In the above example these are varl and

    var2.

    # obtain prime number upto 11

  • 8/20/2019 Otcl Programming basics syntax

    10/19

     

    # Usage: ns prime.tcl NUMBER

    # NUMBER is the number up to which we want to obtain the

     prime numbers

    #

    if {$argc != 1} {

    # Must get a single argument or program fails.

     puts stderr "ERROR! ns called with wrong number of

    arguments!($argc)"

    exit 1

    } else {

    set j [lindex $argv 0]

    # it is not one ie 1 index : it is l alphabet index

    }

     proc prime {j} {

    # Computes all the prime numbers till j

    for {set a 2} {$a

  • 8/20/2019 Otcl Programming basics syntax

    11/19

     

    } else {

     puts "$a is a prime number"

    }

    }

    }

     prime $j

     Ns2 programming

    #-->--> Create a scheduler

    #- set ns [new Simulator]

    set ns [new Simulator]

    # To be able to use nam, we should Create a nam trace

    datafile.

    set namfile [open results/versuch1.nam w]

    $ns namtrace-all $namfile

    #-->--> Creation of Network Topology

  • 8/20/2019 Otcl Programming basics syntax

    12/19

     

    # i.e., (0)

    # i.e., (1) (2)

    #--> option1 : Create Nodes

    #- set n_0 [$ns node]

    #- set n_1 [$ns node]

    #- set n_2 [$ns node]

    # i.e., (0)

    # i.e., (2) (1)

    #--> option2 : Create Nodes (using a loop)

    for { set i 0 } { $i < 3 } { incr i } {

    set n_$i [$ns node]

    }

    # examples: create 6 nodes

    # After that, we should create the nodes

    #for { set i 0 } { $i < 6 } { incr i } {

    #set node($i) [$ns node]

    #}

    #-->--> Creation of Network Topology

    #--> Create links between the nodes

  • 8/20/2019 Otcl Programming basics syntax

    13/19

     

    #- $ns $n_0 $n_1

    #--> : duplex-link, simplex-link

    #--> : in Mb

    #--> : in ms

    #--> : DropTail, RED, CBQ, FQ, SFQ, DRR

    # example: Creation of Network Topology

    # i.e., (0)

    # i.e., / \

    # i.e., (1) --- (2)

    #--> Create links between the nodes of our example (3 nodes)

    $ns simplex-link $n_0 $n_1 1Mb 5ms DropTail

    $ns simplex-link $n_0 $n_2 1Mb 5ms DropTail

    $ns duplex-link $n_1 $n_2 10Mb 25ms DropTail

    #--> Define the properties of the links between the nodes

    #- $ns duplex-link-op $n_0 $n_1

    #- : orient, color, queuePos, label

    #- orient: the orientation of a link (up, down, right, left, right-

    up, right-down, left-up, left-down)

    #- color: the color of the link (black, green, red,...etc)

  • 8/20/2019 Otcl Programming basics syntax

    14/19

     

    #- queuePos: angle of the queue line with horizontal (default

    0.5)

    #- Label: label of the link

    #--> example: Creation of Network Topology

    # i.e., (0)---(1)

    # i.e., \ /

    # i.e., (2)

    #--> Define the orientation of the links between the nodes of

    our example

    #- $ns duplex-link-op $n_0 $n_1 orient right

    #- $ns duplex-link-op $n_0 $n_2 orient right-down

    #- $ns duplex-link-op $n_1 $n_2 orient left-down

    # example: six nodes: After that, we should connect the nodes

    with each other

    #$ns duplex-link $node(0) $node(2) 1.0Mb 20.0ms DropTail

    #$ns duplex-link $node(1) $node(2) 1.0Mb 20.0ms DropTail

    #$ns duplex-link $node(2) $node(5) 1.0Mb 20.0ms DropTail

    #$ns simplex-link $node(5) $node(2) 0.125Mb 20.0ms

    DropTail

    #$ns duplex-link $node(3) $node(5) 1.0Mb 20.0ms DropTail

    #$ns duplex-link $node(4) $node(5) 1.0Mb 20.0ms DropTail

  • 8/20/2019 Otcl Programming basics syntax

    15/19

     

    #-->--> Connection and Traffic

    # i.e., (0)-----------------------------(1)

    # . .

    # . connect the agents . .

    # Src-agent.......................Dest-agent

    # (UDP,TCP) (Null,LossMonitor,TCPSINK)

    # Transport layer protocol

    # .

    # .

    # .

    # Application

    # (CBR,FTP, HTTP(Hypertext Transfer Protocol),

    # SMTP(Simple Mail Transfer Protocol for email)

    # TELNET(for remote virtual terminal)

    # Domain Name Service (DNS for mapping comprehensible

    # host names to their network addresses)

    # File Transfer Protocol (FTP for file transfer))

    # Define an application on the top of the src-agent

    #--> Creation of TCP/UDP agents connection

    #--> UDP agent

    set Srcagent [new Agent/UDP]

    $ns attach-agent $n_0 $Srcagent

  • 8/20/2019 Otcl Programming basics syntax

    16/19

     

    set Destagent [new Agent/Null]

    $ns attach-agent $n_1 $Destagent

    $ns connect $Srcagent $Destagent

    #--> TCP agent

    #--> set Src-agent [new Agent/TCP]

    #--> $ns attach-agent $n_0 $Src-agent

    #--> set Dest-agent [new Agent/TCPSink]

    #--> $ns attach-agent $n_1 $Dest-agent

    #--> $ns connect $Src-agent $Dest-agent

    #--> Creation of Traffic

    #--> FTP

    # set src [new Application/FTP]

    # $src attach-agent $Src-agent

    #--> Telnet

    # set src [new Application/Telnet]

    # $src attach-agent $Src-agent

    #--> CBR

    # set src [new Application/Traffic/CBR]

    # $src attach-agent $Src-agent

  • 8/20/2019 Otcl Programming basics syntax

    17/19

     

    #--> Exponential or Pareto on-off

    # set src [new Application/Traffic/Exponential]

    # set src [new Application/Traffic/Pareto]

    # $src attach-agent $Src-agent

    #-->--> Parameterize a Traffic Source

    #example: --> CBR

    set src [new Application/Traffic/CBR]

    $src attach-agent $Srcagent

    $src set interval_ 40ms

    $src set packetSize_ 500

    # examples: six nodes: After that, we have to create the agents

    #set agent(0) [new Agent/UDP]

    #$ns attach-agent $node(1) $agent(0)

    #$agent(0) set fid_ 6

    #$ns color 6 "red„ 

    #set sink(0) [new Agent/Null]

    #$ns attach-agent $node(4) $sink(0)

    #$ns connect $agent(0) $sink(0)

    # examples: six nodes: After that, we have to create traffic

    source and add it to the agent

    # set traffic_source(0) [new Application/Traffic/CBR]

  • 8/20/2019 Otcl Programming basics syntax

    18/19

     

    #$traffic_source(0) set interval_ 0.001950

    #$traffic_source(0) set paketSize_ 230

    #$traffic_source(0) attach-agent $agent(0)

    #-->--> Schedule an event

    # A scheduler maintains a list of events and their execution

    time.

    # During a asimulation it moves along a simulation clock and

    executes

    # events in the list chronologically.

    # Start and Stop a Traffic Source

    #- $ns at

    #- Example: $ns at 10.0 "record_data"

    $ns at 10.0 "$src start"

    $ns at 100.0 "$src stop"

    # examples: six nodes: Now, we have to schedule starting and

    stopping the traffic source

    #$ns at 3.0 "$traffic_source(0) start"

    #$ns at 100.0 "$traffic_source(0) stop"

    # examples: six nodes: Now, we have to start the finish

     procedure

  • 8/20/2019 Otcl Programming basics syntax

    19/19

     

     proc finish {} {

    global ns namfile

    $ns flush-trace

    close $namfile

    exec nam results/versuch1.nam &

    exit 0

    }

    # After that, we have to schedule the stop procedure

    $ns at 110.000000 "finish"

    #-->--> Start the scheduler

    $ns run

    #--> After that the file should be saved "filename.tcl"

    #--> The executing of the example is through writing: "ns

    filename.tcl" in linux commands window (Console)