ns2 to attach more than one applications to one agent - tcl

Can anyone tell me how should I fix this problem? I have a problem in attaching more than one applications to one agent. (I am running ns2.35 on Ubuntu12.10)
There are two nodes(Source and Destination) in my environment and here are some features:
I attached a loss-monitor agent on Destination node.
I attached an udp agent on Source node.
I attached 9 applications on the udp agent by following declaration:
set nExpGen 9
for {set i 1} {$i <= $nExpGen} {incr i} {
set eee($i) [new Application/Traffic/Exponential]
$eee($i) attach-agent $udp
$ns connect $eee($i) $lmt
#nExpGen= number of exponential generators
#eee = exponential application
#lmt = loss-monitor agent
I got errors "cant read agent address: no such variable.." when running my tcl file ( see [error message])
Did I use the wrong way to attach these applications to the agent? How can I fix that?
Thank you all in advance.
[error messages]
can't read "agent_addr_": no such variable
while executing
"subst $[subst $var]"
(procedure "_o40" line 5)
(Object next line 5)
invoked from within
"_o40 next agent_addr_"
("eval" body line 1)
invoked from within
"eval $self next $args"
(procedure "_o40" line 11)
(Application/Traffic set line 11)
invoked from within
"$dst set agent_addr_"
(procedure "_o3" line 2)
(Simulator simplex-connect line 2)
invoked from within
"$self simplex-connect $dst $src"
(procedure "_o3" line 10)
(Simulator connect line 10)
invoked from within
"$ns connect $eee($i) $lmt"
("for" body line 4)
invoked from within
"for {set i 1} {$i <= $nExpGen} {incr i} {
set eee($i) [new Application/Traffic/Exponential]
$eee($i) attach-agent $udp
$ns con..."
(file "myTest3.tcl" line 47)

I got the solution:
The following statement build connections between two "Agents" (here $A and $B).
$ns connect $A $B
So, for this question, I shall connect my UDP agent and LossMonitor agent (outside my for loop).
$ns connect $udp $lmt
Connecting an "application" to an "agent" causes compiling error.

Related

invalid command name "Agent/LeachAgent"

I try to implement a simple scenario for LEACH protocol but I get this error:
Creating Sensors ...
invalid command name "Agent/LeachAgent"
while executing
"Agent/LeachAgent create _o2340 "
invoked from within
"catch "$className create $o $args" msg"
invoked from within
"if [catch "$className create $o $args" msg] {
if [string match "__FAILED_SHADOW_OBJECT_" $msg] {
delete $o
return ""
}
global errorInfo
error "class $..."
(procedure "new" line 3)
invoked from within
"new Agent/LeachAgent"
("for" body line 3)
invoked from within
"for {set i 1} {$i <= $val(nsn)} {incr i} {
set agent($i) [new Agent/LeachAgent]
$ns attach-agent $node_($i) $agent($i)
$agent($i) set packetSize_..."
(file "newleach3.tcl" line 187)
I use ubuntu 16.04 and ns-allinone-2.35 . when I ran my tcl file for the first time, i did not get this error.
Change your script to use Agent/RCAgent/LeachAgent as class name:
set agent($i) [new Agent/RCAgent/LeachAgent]
From what I can see, there is no Agent/LeachAgent in ns-allinone-2.35.
I was not successful in writing a correct code for attaching Leach protocol in my scenario, but I found that mannasim has a Mannasim Script Generator (MSG). It is a front-end for TCL simulation scripts easy creation.

Running .tcl file multiple times with different start and stop times for traffic generators

I've created the following code to run a tcl script multiple times in NS2.The code is executed many times but the start and stop times for the traffic generator doesn't change from one run to the other .What is wrong with my code?
set ns [new Simulator]
for {set i 1} {$i<5} {incr i} {
.....
set min 0
set max 10
set min1 10
set max1 20
set start [expr rand()*($max-$min)]
set stop [expr rand()*($max1-$min1)+$min1]
$ns at $start "ftp start"
$ns at $stop "ftp stop"
puts $outfile "$start $stop"
}
$ns run
Thanks in advance !

Tcl error in xilinx isim simulator

I am trying to write a tcl script for some simulations in isim. the script would run two nested loops and generate a file in each iteration. when running the simulation with this scrips the simulator generates the error:
TCL batch file "D:...subbyte.tcl" encountered an error in line 2. Simulator is exiting.
but all the output files are generated and saved. I am totally confused with it.
Thanks for any help in advance!
run 100 ns
for {set i 0} { $i<1} {incr i} {
for {set j 0} { $j<256} {incr j} {
saif open -scope uut -file "saifsubbyte $i$j .saif" -allnets
run 1 ns
saif close
run 19 ns
}
}

tcl error in set nf[open out.nam w]

the following code
#Create a simulator object
set ns [new Simulator]
#Open the nam trace file
set nf[open out.nam w]
$ns namtrace-all $nf
#Define a 'finish' procedure
proc finish {} {
global ns nf
$ns flush-trace
#Close the trace file
close $nf
#Execute namon the trace file
exec nam–a out.nam&
exit 0
}
#Create two nodes
set n0 [$ns node]
set n1 [$ns node]
#Create a duplex link between the nodes
$ns duplex-link $n0 $n1 1Mb 10ms DropTail
#Call the finish procedure after 5 seconds of simulation time
$ns at 5.0 "finish"
#Run the simulation
$ns run
produces this error
can't read "nffile5": no such variable
while executing
"set nf[open out.nam w]"
(file "Desktop/sample.tcl" line 4)
it is my first time in tcl. so what is the problem.
i just run this by this statement: > ns sample.tcl
You're missing a space after the variable name:
set nf [open out.nam w]
Tcl is very dependent on proper use of whitespace. The entire language can be described in 12 rules, listed here. Rule 3: "Words of a command are separated by white space (except for newlines, which are command separators)."
What was actually happening in your script:
Tcl breaks the command into 2 words: the command set and the argument nf[open out.nam w]
the command in brackets is executed and the result (the filehandle name returned is file5) is substituted.
the set command is executed with its one argument, nffile5
when given a single argument, the set command will return the value of the given variable.
since you never assigned a variable by that name, set returns an error.

What is wrong with this statement

for {set count 0} {$count<$num_of_UEs} { incr count } {
puts $count
set tcp$count [new Agent/TCP]
#$tcp$count set fid_ $count
#$tcp$count set prio_ 2
}
My problem is with the line#$tcp$count set fid_ $count
When I try to execute it it says
can't read "tcp": no such variable
while executing
"$tcp$count set fid_ $count"
("for" body line 4)
invoked from within
"for {set count 0} {$count<$num_of_UEs} { incr count } {
puts $count
set tcp$count [new Agent/TCP]
$tcp$count set fid_ $count
$tcp$coun..."
It says Can't read tcp, well it shouldnt read tcp it should read it as tcp0 in the first iteration and tcp1 in the second and so on.
What amI doing wrong?
thanks
EDIT:
I tried using arrays, thanks for the TIP. It worked for most parts but in one specific case
when I did this:
for {set count 0} {$count<$num_of_UEs} { incr count } {
set ftp($count) [new Application/FTP]
$ftp($count) attach-agent $tcp($count)
}
It gives me the following error:
Came here 0
(_o180 cmd line 1)
invoked from within
"_o180 cmd target _o99"
invoked from within
"catch "$self cmd $args" ret"
invoked from within
"if [catch "$self cmd $args" ret] {
set cls [$self info class]
global errorInfo
set savedInfo $errorInfo
error "error when calling class $cls: $args" $..."
(procedure "_o180" line 2)
(SplitObject unknown line 2)
invoked from within
"$agent target [[$self node] entry]"
(procedure "_o98" line 2)
(RtModule attach line 2)
invoked from within
"$m attach $agent $port"
(procedure "_o97" line 4)
(Node add-target line 4)
invoked from within
"$self add-target $agent $port"
(procedure "_o97" line 15)
(Node attach line 15)
invoked from within
"$node attach $agent"
(procedure "_o3" line 2)
(Simulator attach-agent line 2)
invoked from within
"$ns attach-agent $node2 $tcp($count)"
("for" body line 3)
invoked from within
"for {set count 0} {$count<$num_of_UEs} { incr count } {
puts "Came here $count"
$ns attach-agent $node2 $tcp($count)
}"
(file "hsexample.tcl" line 104)
I know its a long one, but I would really appreciate your help
The problem is that the parsing of the variable name after the $ stops at the first non-alphanumeric character (except for cases that I'll mention in a moment). This means that $tcp$count is interpreted as the string that is the concatenation of the contents of the tcp variable and the count variable (only one of which you've defined).
The best way of dealing with this is to use Tcl's associative arrays:
for {set count 0} {$count<$num_of_UEs} { incr count } {
puts $count
set tcp($count) [new Agent/TCP]
$tcp($count) set fid_ $count
$tcp($count) set prio_ 2
}
The ( is a special case in variable access syntax handling; it starts processing an associative array access (which goes on to the matching ), assuming no unquoted spaces).
(The other special case in variable names is ::, which is Tcl's namespace separator.)
I your big error traceback, I see:
if [catch "$self cmd $args" ret] {
Do you really have an instance method named "cmd" (or whatever terminology for the OO system you appear to use)
Do you want:
if {[catch {$self $cmd $args} ret]} { ...
If you do it as you do you are trying to get the contents of the (non existent variable) tcp and the content of the variable count. What you want is to get the content of tcp$count. You can achieve this using the set command. Simply do:
[set tcp$count] set fid_ $count