invalid command name "Agent/LeachAgent" - tcl

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.

Related

How can I fix this error invalid command name "_o10 in NS2

You are missing the MIH / MIPv6 addition provided by ns-2.29-nist-mob-022707.tgz.This is for ns-2.29,but I usens-allinone-2.35.Is it the same procedure? Thanks!
This is my code link
https://drive.google.com/file/d/1Aan1OeAh5tIeDrt5MjgFTqvNSeahNWVy/view?usp=sharing
While running my tcl script for new protocol in NS2 it shows error as:
num_nodes is set 10
invalid command name "Agent/MIHUser/IFMNGMT/MIPV6/Handover/Handover1"
while executing
"Agent/MIHUser/IFMNGMT/MIPV6/Handover/Handover1 create _o216 "
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/MIHUser/IFMNGMT/MIPV6/Handover/Handover1"
invoked from within
"set handover [new Agent/MIHUser/IFMNGMT/MIPV6/Handover/Handover1]"
(file "bear2.tcl" line 177)
How can I modify it?

How to get name of TCL test from the test itself

I was wondering how you would find the name of the test you're running in tcl from the test itself? I couldn't find this on google.
I'm calling another proc and passing the name of the test that is calling it, as an argument. So I would like to know which tcl command can do that for me.
This isn't an encouraged use caseā€¦ but you can use info frame 1 to get the information if you use it directly inside the test.
proc example {contextTest} {
puts "Called from $contextTest"
return ok
}
tcltest::test foo-1.1 {testing the foo} {
example [lindex [dict get [info frame 1] cmd] 1]
} ok
This assumes that you're using Tcl 8.5 or later, but Tcl 8.5 is the oldest currently-supported Tcl version so that's a reasonable restriction.
I read your comments ("source ... instade of my test name") as follows: You seem to source the Tcl script file containing the tests (and Donal's instrumented tcltest), rather than batch-running the script from the command line: tclsh /path/to/your/file.tcl In this setting, there will be an extra ("eval") stack frame which distorts introspection.
To turn Donal's instrumentation more robust, I suggest actually walking the Tcl stack and watching out for a valid tcltest frame. This could look as follows:
package req tcltest
proc example {} {
for {set i 1} {$i<=[info frame]} {incr i} {
set frameInfo [info frame $i]
set frameType [dict get $frameInfo type]
set cmd [dict get $frameInfo cmd]
if {$frameType eq "source" && [lindex $cmd 0] eq "tcltest::test"} {
puts "Called from [lindex $cmd 1]"
return ok
}
}
return notok
}
tcltest::test foo-1.1 {testing the foo} {
example
} ok
This will return "Called from foo-1.1" both, when called as:
$ tclsh test.tcl
Called from foo-1.1
and
$ tclsh
% source test.tcl
Called from foo-1.1
% exit
The Tcl version used (8.5, 8.6) is not relevant. However, your are advised to upgrade to 8.6, 8.5 has reached its end of life.

Why can't I access errorInfo and errorCode

I have the following code:
$ cat ~/tmp/2.tcl
set zero 0
proc p1 {} {
if {[catch {expr 1/$zero} err]} {
puts "errorCode=$errorCode"
puts "errorInfo=$errorInfo"
}
}
p1
When I source it, I get error accessing errorCode:
$ tclsh ~/tmp/2.tcl
can't read "errorCode": no such variable
while executing
"puts "errorCode=$errorCode""
(procedure "p1" line 3)
invoked from within
"p1"
(file "~/tmp/2.tcl" line 9)
I tried changing to $::errorCode, but did not help.
Can you see what is wrong?
The errorInfo and errorCode variables are globals. You should either use the global command to bring them into scope or use their fully-qualified names (i.e., precede with ::).
It might be easier to pick the information out of the result options dictionary (a new feature in 8.5).
Starting from Tcl 8.5 [catch] doesn't set the errorCode and errorInfo global variables. (As Donal has pointed out, it still does, so they can be accessed as $::errorCode and $::errorInfo). And in addition it puts their values into a dictionary which name is to be specified as the third argument. The following code
#!/usr/bin/tclsh
set zero 0
proc p1 {} {
if {[catch {expr 1/$zero} err opts] == 1} {
puts "errorCode=[dict get $opts -errorcode]"
puts "errorInfo=[dict get $opts -errorinfo]"
}
}
p1
prints
errorCode=NONE
errorInfo=can't read "zero": no such variable
while executing
"expr 1/$zero"
in Tcl 8.5.19, and
errorCode=TCL READ VARNAME
errorInfo=can't read "zero": no such variable
while executing
"expr 1/$zero"
in Tcl8.6.6.
You'd probably want to use $::zero in the division after which the result would be
errorCode=ARITH DIVZERO {divide by zero}
errorInfo=divide by zero
while executing
"expr 1/$::zero"

ns2 to attach more than one applications to one agent

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.

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