Request Packet Generation Code in NS2 Tcl/Tk

52 Views Asked by At
for {set i 0} {$i < $num_nodes} {incr i} {
   for {set j 0} {$j < 10} {incr j} {
     $ns at [expr 7.2+[expr rand()*3.0]] "$n($i) send_message 10 1 {[expr round([expr rand()*100000])]:[expr round([expr rand()*$numdata1])]:1:-1:-1} $MESSAGE_PORT"
     set generated1 [expr $generated1 + 1]
   }
}

I am building code for request packet generation in tcl ns2. i found these line from the internet, but I cannot understand this line:

$ns at [expr 7.2+[expr rand()*3.0]] "$n($i) send_message 10 1 {[expr round([expr rand()*100000])]:[expr round([expr rand()*$numdata1])]:1:-1:-1} $MESSAGE_PORT" 

please tell me what is the logic behind these above lines?

1

There are 1 best solutions below

0
glenn jackman On

First of all, that's bad use of nested expr. Cleaning that up:

 $ns at [expr {7.2+rand()*3.0}] "$n($i) send_message 10 1 {[expr {round(rand()*100000)}]:[expr {round(rand()*$numdata1)}]:1:-1:-1} $MESSAGE_PORT"

Looking at that word-by-word:

  • $ns
  • at
  • [expr {7.2+rand()*3.0}]
    • a random float value between 7.2 and 10.2
  • "$n($i) send_message 10 1 {[expr {round(rand()*100000)}]:[expr {round(rand()*$numdata1)}]:1:-1:-1} $MESSAGE_PORT"
    • a quoted string that is subject to variable and command substitutions (see the Tcl(n) man page)
    • looking further, we have:
      • $n($i) <== an associative array value at key $i
      • [expr {round(rand()*100000)}] <== a random integer between 0 and 100,000
      • [expr {round(rand()*$numdata1)}] <== a random integer between 0 and $numdata1

After Tcl substitutes all that, we might have something like

nsValue at 9.99934 "nValue send_message 10 1 {42:1234:1:-1:1} 8080"