How to implement the tcl script for DSR protocol for 50 node in Ns-2?
Also i need the code for DSDV protocol i am doing a performance evaluation for them.
The DSDV c++ code : ns-2.3x/dsdv/{ dsdv.cc, dsdv.h, rtable.cc, rtable.h }.
Tcl scripts with DSR : All scripts with a line like ...
set opt(adhocRouting) DSR
or
set opt(rp) DSR
or
set val(rp) DSR
DSR examples : dsr-examples-04.2015.tar.gz
https://drive.google.com/file/d/0B7S255p3kFXNNFJzNGxRd25Zd3c/view?usp=sharing
DSDV examples : dsdv-examples.tar.gz
https://drive.google.com/file/d/0B7S255p3kFXNTHdTcnpHVFRRZzQ/view?usp=sharing
You don't "implement the tcl script".
Tcl scripts are just to be run, with the executable 'ns' : ns file.tcl
And most files.tcl can be run from a random folder, any location ( outside ns2.) Some files.tcl will require to be run from a particular folder, e.g.
ns-2.3x/tcl/ex/
ns2
#----------------------------------------------------------------
# Definition of the physical layer
#----------------------------------------------------------------
set val(chan) Channel/WirelessChannel
set val(prop) Propagation/TwoRayGround
set val(netif) Phy/WirelessPhy
set val(mac) Mac/802_11
set val(ifq) Queue/DropTail/PriQueue
set val(ll) LL
set val(ant) Antenna/OmniAntenna
#-----------------------------------------------------------------
# Scenario parameters
#------------------------------------------------------------------
set val(x) 2000 ;# X dimension of the topography
set val(y) 2000 ;# Y dimension of the topography
set val(ifqlen) 100 ;# max packet in queue
set val(seed) 0.0 ;#random seed
set val(adhocRouting) DSR ;
set val(nn) 50 ;# how many nodes are simulated
set val(cp) < cbr file >
set val(sc) < scen file >
set val(stop) 200 ;# simulation time
#---------------------------------------------------------------------
# Set up simulator objects
#---------------------------------------------------------------------
# create simulator instance
set ns_ [new Simulator]
# setup topography object
set topo [new Topography]
# create trace object for ns and nam
set tracefd [open out.tr w]
$ns_ use-newtrace ;# use the new wireless trace file format
set namtrace [open out.nam w]
$ns_ trace-all $tracefd
$ns_ namtrace-all-wireless $namtrace $val(x) $val(y)
# define topology
$topo load_flatgrid $val(x) $val(y)
# Create God
set god_ [create-god $val(nn)]
$ns_ node-config -adhocRouting $val(adhocRouting) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channelType $val(chan) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace ON
# Create the specified number of nodes [$val(nn)] and "attach" them
# to the channel.
for {set i 0} {$i < $val(nn) } {incr i} {
set node_($i) [$ns_ node]
#$ns at 0.0 "$node_(0) color blue"
$node_($i) random-motion 0 ;# disable random motion
}
for {set i 0} {$i < $val(nn) } {incr i} {
$node_($i) color blue
$ns_ at 0.0 "$node_($i) color cyan"
}
# Define node movement model
puts "Loading connection pattern..."
source $val(cp)
# Define traffic model
puts "Loading scenario file..."
source $val(sc)
# Define node initial position in nam
for {set i 0} {$i < $val(nn)} {incr i} {
# 50 defines the node size in nam, must adjust it according to your scenario
# The function must be called after mobility model is defined
# puts "Processing node $i"
$ns_ initial_node_pos $node_($i) 50
}
#
# Tell nodes when the simulation ends
#
for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ at $val(stop).0 "$node_($i) reset";
}
$ns_ at $val(stop).0002 "puts \"NS EXITING...\" ; $ns_ halt"
# dump the initial simulation info to the trace file
puts $tracefd "M 0.0 nn $val(nn) x $val(x) y $val(y) rp $val(adhocRouting)"
puts $tracefd "M 0.0 sc $val(sc) cp $val(cp) seed $val(seed)"
puts $tracefd "M 0.0 prop $val(prop) ant $val(ant)"
puts "Starting Simulation..."
$ns_ run
To generate node traffic and scenario file use this link
change "set val(adhocRouting) DSR ;" to "set val(adhocRouting) DSDV ;" to perform DSDV protocol
Related
I'm setting up a net environment that there are n nodes surrounded by a circle with DSR protocol. However, when I fixed most of the errors, the most confused error that I couldn't solve like the title. How to fix 'can't read "node_(0)": no such variable' error? Didn't I already define it in the for loop at line 86?
I can't find a solution to this problem and when I changed
set node_($i) [$ns node]
to
set node_($i) [$ns_ node]
Here's the full code.
set val(chan) Channel/WirelessChannel
set val(prop) Propagation/TwoRayGround
set val(netif) Phy/WirelessPhy
set val(mac) Mac/802_11
set val(ifq) CMUPriQueue
set val(ll) LL
set val(ant) Antenna/OmniAntenna
set val(ifqlan) 50
set val(nn) 0
set val(rp) DSR
set val(x) 1000
set val(y) 1000
set val(r) 400
proc usage {} \
{
global argv0
puts "\nusage: $argv0 \[-nn node\] \[-r r\] \[-x x\] \[-y y\]\n"
puts "note: \[-nn nodes\] is essential, and the others are optional.\n"
}
proc getval {argc argv} \
{
global val
lappend vallist nn r x y z
for {set i 0} {$i < $argc} {incr i} {
set arg [lindex $argv $i]
if {[string range $arg 0 0] != "-"} continue
set name [string range $arg 1 end]
set val($name) [lindex $argv[expr $i+1]]
}
}
getval $argc $argv
if {$val(nn) == 0} {
usage
exit
}
set ns [new Simulator]
set tracefd [open circle.tr w]
$ns trace-all $tracefd
set namtracefd [open circle.nam w]
$ns namtrace-all-wireless $namtracefd $val(x) $val(y)
proc finish {} \
{
global ns tracefd namtracefd
$ns flush-trace
#close the trace file
close $tracefd
close $namtracefd
#execute nam on the trace file
exec nam circle.nam &
exit 0
}
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
create-god $val(nn)
$ns node-config -addressType def\
-adhocRouting $val(rp)\
-llType $val(ll)\
-macType $val(mac)\
-ifqType $val(ifq)\
-ifqLan $val(ifqlan)\
-antType $val(ant)\
-propType $val(prop)\
-phyType $val(netif)\
-channelType $val(chan)\
-topoInstance $topo\
-agenttrace ON\
-routertrace ON\
-mactrace OFF\
-movementtrace OFF
##################################
for {set i 0} {$i < $val(nn)} {incr i} {
set node_($i) [$ns node]
$node_($i) random-motion 0
$node_($i) set X_ [expr $val(r) * cos($i * 2 * 3.14159 / $val(nn))]
$node_($i) set Y_ [expr $val(r) * sin($i * 2 * 3.14159 / $val(nn))]
$node_($i) set Z_ 0
$ns initial_node_pos $node_($i) [expr $val(x) / 10]
}
##################################
set tcp [new Agent/UDP]
$ns attach-agent $node_(0) $tcp
set null [new Agent/Null]
$ns attach-agent $node_([expr $val(nn) / 2]) $null
set cbr [new Application/Traffic/CBR]
$cbr set packetSize_ 5000
$cbr set interval_ 0.05
$cbr attach-agent $tcp
$ns connect $tcp $null
$ns at 0.1 "$cbr start"
$ns at 3.0 "$cbr stop"
$ns at 5.0 "finish"
$ns run
When i type ns circle.tcl -nn 12, I expect the output:
num_node is set 12
warning: Please use -channel as shown in tcl/ex/wireless-mitf.tcl
INITIALIZE THE LIST xListHead
channel.cc:sendUp - Calc highestAntennaZ_and distCST_
SORTING LISTS ...DONE!
Please help me, I've been stuck for a long time.
That error will be produced whenever val(nn) is not greater than zero, as then the loop that creates the nodes:
for {set i 0} {$i < $val(nn)} {incr i} {
set node_($i) [$ns node]
$node_($i) random-motion 0
$node_($i) set X_ [expr $val(r) * cos($i * 2 * 3.14159 / $val(nn))]
$node_($i) set Y_ [expr $val(r) * sin($i * 2 * 3.14159 / $val(nn))]
$node_($i) set Z_ 0
$ns initial_node_pos $node_($i) [expr $val(x) / 10]
}
will simply decide that it has nothing to do and not execute the body of the loop even once. What might cause that? Well, if we look at the argument parsing procedure:
proc getval {argc argv} \
{
global val
lappend vallist nn r x y z
for {set i 0} {$i < $argc} {incr i} {
set arg [lindex $argv $i]
if {[string range $arg 0 0] != "-"} continue
set name [string range $arg 1 end]
set val($name) [lindex $argv[expr $i+1]]
}
}
we can see a number of problems, of which the biggest is this (on the line with set val($name):
lindex $argv[expr $i+1]
The problem here is the lack of a space between $argv and the expression evaluation; that concatenates the two strings before feeding the result into lindex as a single argument! (lindex with a single argument just returns that argument, using the principle of “do nothing, gracefully”.) That's not even syntactically correct in all cases, but probably was in the cases you tried and resulted in val(nn) being set to something like the string -nn 101. Now, the < operator (in the node generation loop) will use ASCII ordering whenever either side looks non-numeric and that string has extra garbage in it so it is definitely non-numeric. Oh dear. (- is ASCII character code 45 and 0 is ASCII character code 48, so - comes before 0.) This isn't what you wanted!
Here's a fixed version of the argument parser:
proc getval {argc argv} {
global val
for {set i 0} {$i < $argc} {incr i} {
set arg [lindex $argv $i]
if {[string index $arg 0] eq "-"} {
set val([string range $arg 1 end]) [lindex $argv [incr i]]
}
}
}
Remember, in Tcl the spaces are important!
Add four lines, and the simulation can be run.
set val(ifqlen) 50 ;# added (Line 7)
set val(nn) 10 ;# added (Line 10)
.
-movementtrace OFF \
-ifqLen $val(ifqlen) ;# added (Line 85)
.
for {set i 0} {$i < [expr $val(nn)]} {incr i} {
set node_($i) [$ns node] ;# added (Line 89)
Reason for set val(nn) 10 : I didn't get the [$argv]'s to work. ( $ ns file.tcl -nn 10 or $ ns file.tcl nn 10 )
Link to the edited file : https://www.dropbox.com/s/m7zsnga6wf29r95/2Ezio-Auditore-DSR.tcl?dl=0
All ~3500 ns2 simulations ex https://drive.google.com/drive/folders/0B7S255p3kFXNSmRYb2lGcDRUdWs?usp=sharing
EDIT : The 'argument parser' by #Donal Fellows was added, can now run with $ ns file.tcl -nn 12. New example https://www.dropbox.com/s/br6qaeg5kj73k14/4-circle-Ezio-Auditore.tar.gz?dl=0
ns2 analyzing scripts https://drive.google.com/drive/folders/1rU_MFAEl1GCLMTJPLR3zbxPxkQQHkQ7T?usp=sharing
After successfully applied /home/uday/ns-allinone-2.35/ns-2.35/DYMO-03-all__ns235.patch
When i tried to run the following dymoum/ns/test/test_2n.tcl code it gives error and i can't solve that error .I tried my best but i failed ...Kindly anyone look at the code and help me to solve error.The test code is given below:
# ======================================================================
# Define options
# ======================================================================
set opt(chan) Channel/WirelessChannel ;# channel type
set opt(prop) Propagation/TwoRayGround ;# radio-propagation model
set opt(netif) Phy/WirelessPhy ;# network interface type
set opt(mac) Mac/802_11 ;# MAC type
set opt(ifq) Queue/DropTail/PriQueue ;# interface queue type
set opt(ll) LL ;# link layer type
set opt(ant) Antenna/OmniAntenna ;# antenna model
set opt(ifqlen) 50 ;# max packet in ifq
set opt(nn) 2 ;# number of mobilenodes
set opt(adhocRouting) DYMOUM ;# routing protocol
set opt(cp) "" ;# connection pattern file
set opt(sc) "" ;# node movement file.
set opt(x) 400 ;# x coordinate of topology
set opt(y) 600 ;# y coordinate of topology
set opt(seed) 0.0 ;# seed for random number gen.
set opt(stop) 40 ;# time to stop simulation
set opt(cbr-start) 10.0
# ============================================================================
#
# check for random seed
#
if {$opt(seed) > 0} {
puts "Seeding Random number generator with $opt(seed)\n"
ns-random $opt(seed)
}
#
# create simulator instance
#
set ns_ [new Simulator]
#
# control DYMOUM behaviour from this script
#
Agent/DYMOUM set debug_ true
Agent/DYMOUM set no_path_acc_ true
Agent/DYMOUM set reissue_rreq_ false
Agent/DYMOUM set s_bit_ true
Agent/DYMOUM set hello_ival_ 1
#
# open traces
#
set tracefd [open test_2n.tr w]
set namtrace [open test_2n.nam w]
$ns_ trace-all $tracefd
$ns_ namtrace-all-wireless $namtrace $opt(x) $opt(y)
#
# create topography object
#
set topo [new Topography]
#
# define topology
#
$topo load_flatgrid $opt(x) $opt(y)
#
# create God
#
create-god $opt(nn)
#
# configure mobile nodes
#
$ns_ node-config -adhocRouting $opt(adhocRouting) \
-llType $opt(ll) \
-macType $opt(mac) \
-ifqType $opt(ifq) \
-ifqLen $opt(ifqlen) \
-antType $opt(ant) \
-propType $opt(prop) \
-phyType $opt(netif) \
-channelType $opt(chan) \
-topoInstance $topo \
-wiredRouting OFF \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF
for {set i 0} {$i < $opt(nn)} {incr i} {
set node_($i) [$ns_ node]
}
#
# positions
#
$node_(0) set X_ 200.0
$node_(0) set Y_ 200.0
$node_(0) set Z_ 0.0
$node_(1) set X_ 300.0
$node_(1) set Y_ 200.0
$node_(1) set Z_ 0.0
#
# setup UDP connection
#
set udp [new Agent/UDP]
set null [new Agent/Null]
$ns_ attach-agent $node_(0) $udp
$ns_ attach-agent $node_(1) $null
$ns_ connect $udp $null
set cbr [new Application/Traffic/CBR]
$cbr set packetSize_ 512
$cbr set rate_ 20Kb
$cbr attach-agent $udp
$ns_ at $opt(cbr-start) "$cbr start"
#
# print (in the trace file) routing table and other
# internal data structures on a per-node basis
# TODO: implement this
#$ns_ at 15.0 "[$node_(0) agent 255] print_rtable"
#
# source connection-pattern and node-movement scripts
#
if { $opt(cp) == "" } {
puts "*** NOTE: no connection pattern specified."
set opt(cp) "none"
} else {
puts "Loading connection pattern..."
source $opt(cp)
}
if { $opt(sc) == "" } {
puts "*** NOTE: no scenario file specified."
set opt(sc) "none"
} else {
puts "Loading scenario file..."
source $opt(sc)
puts "Load complete..."
}
#
# define initial node position in nam
#
for {set i 0} {$i < $opt(nn)} {incr i} {
$ns_ initial_node_pos $node_($i) 20
}
#
# tell all nodes when the simulation ends
#
for {set i 0} {$i < $opt(nn) } {incr i} {
$ns_ at $opt(stop).0 "$node_($i) reset";
}
$ns_ at $opt(stop).0002 "puts \"NS EXITING...\" ; $ns_ halt"
$ns_ at $opt(stop).0001 "stop"
proc stop {} {
global ns_ tracefd namtrace
$ns_ flush-trace
close $tracefd
close $namtrace
}
#
# begin simulation
#
puts "Starting Simulation..."
$ns_ run
It shows error:
root#uday-HP-15-Notebook-PC:/home/uday/Downloads/ns-allinone-2.35/ns-2.35/dymoum/ns/test# ns test_2n.tcl
invalid command name "Agent/DYMOUM"
while executing
"Agent/DYMOUM set debug_ true"
(file "test_2n.tcl" line 42)
Ref. stackoverflow → NS-2.34 DYMO patch
tar xvf ns-allinone-2.35_gcc5.tar.gz
https://drive.google.com/file/d/0B7S255p3kFXNVVlxR0ZNRGVORjQ/view?usp=sharing
cd ns-allinone-2.35/
patch -p0 < DYMO-03-all__ns235.patch
./install
cd ns-2.35/
cp ns ns235-dymo
sudo cp ns235-dymo /usr/local/bin/
------------ Simulation ------------
ns235-dymo test_2n.tcl
num_nodes is set 2
warning: Please use -channel as shown in tcl/ex/wireless-mitf.tcl
INITIALIZE THE LIST xListHead
*** NOTE: no connection pattern specified.
*** NOTE: no scenario file specified.
Starting Simulation...
DYMOUM agent started in node 0
no_path_acc=1
reissue_rreq=0
s_bit=1
hello_ival=1
node 0: hello_send: sending HELLO
DYMOUM agent started in node 1
no_path_acc=1
reissue_rreq=0
s_bit=1
hello_ival=1
node 1: hello_send: sending HELLO
.
.
node 0: process_data: route to dst 1 updated
node 1: tap: route to src 0 updated
NS EXITING...
Your errors : Seems you are using a wrong copy of ns i.e. an 'ns' with no Dymoum ... or you have a failed build. The safe way : Use an executable "ns-name" which is present in a system PATH = /usr/local/bin/.
Dymoum simulation examples : Dymoum-examples.tar.gz https://drive.google.com/drive/folders/0B7S255p3kFXNSmRYb2lGcDRUdWs?usp=sharing
Ref. comment 1. : OK with Ubuntu 14.04-amd64 installed to hard-disk. Same file sizes for test_2n.nam (89.5kB), test_2n.tr (64kB), and same 486 lines of text output : https://www.dropbox.com/s/5v84a9ubnip3m1h/Dymo-test_2n.tcl.txt?dl=0
Ref. comment 2 - 3. : New patch Dymo-blackhole-idsAODV_ns235.patch https://drive.google.com/file/d/1S63-743G9Et86yXWhaYkpSR_3wOAsUHG/view?usp=sharing
The new executable ns235_Dymo-blackhole-idsAODV was tested OK with Dymoum simulations, blackholeaodv2005~2013-examples-05.17.tar.gz, idsAODV-examples-05.17.tar.gz https://drive.google.com/drive/folders/0B7S255p3kFXNSmRYb2lGcDRUdWs?usp=sharing
I am working on ns2.35-ubuntu10.04. I want to divide the sensor field into 9 squares of equal size by using TCL script. This is my first attempt but there is no result. (The steps of division is delimited by the star mark and attachment picture.)
# Define options
set val(chan) Channel/WirelessChannel ; # channel type
set val(prop) Propagation/TwoRayGround ; # radio-propagation model
set val(netif) Phy/WirelessPhy ; # network interface type
set val(mac) Mac/802_11 ; # MAC type
set val(ifq) Queue/DropTail/PriQueue ; # interface queue type
set val(ll) LL ; # link layer type
set val(ant) Antenna/OmniAntenna ; # antenna model
set val(ifqlen) 50 ; # max packet in ifq
set val(rp) AODV ; # routing protocol
set val(x) 900 ; # X dimension of topography
set val(y) 900 ; # Y dimension of topography
set val(stop) 10 ; # time of simulation end
set val(Y) 0;
set val(X) 0;
set val(nn) 30 ;
#Creating simulation:
set ns [new Simulator]
#Creating nam and trace file:
set tracefd [open k.tr w]
set namtrace [open k.nam w]
$ns trace-all $tracefd
$ns namtrace-all-wireless $namtrace $val(x) $val(y)
# set up topography object
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
set god_ [create-god $val(nn)]
# configure the nodes
$ns node-config -adhocRouting $val(rp) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channelType $val(chan) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace OFF \
############ BEGIN ############
set $val(X) [expr $val(x)/300]
set $val(Y) [expr $val(y)/300]
for {set i 0} {$i<=$val(X)} {incr i}
{
for {set j 0} {$j<=$val(Y)} {incr i}
{
for {set b 0} {$b < $val(nn) } { incr b }
{
set node($b) [$ns node]
}
}
}
############ END ############
#stop procedure..
proc stop {} {
global ns tracefd namtrace
$ns flush-trace
close $tracefd
close $namtrace
exec nam k.nam &
}
$ns at $val(stop) "stop"
$ns run
Looking at just the code you highlighted:
set $val(X) [expr $val(x)/300]
set $val(Y) [expr $val(y)/300]
for {set i 0} {$i<=$val(X)} {incr i}
{
for {set j 0} {$j<=$val(Y)} {incr i}
{
for {set b 0} {$b < $val(nn) } { incr b }
{
set node($b) [$ns node]
}
}
}
The main problem is that you've put open braces on lines of their own. Don't do that in Tcl; the language enormously prefers One True Brace style (because newlines are significant, being command terminators). If we change to this (the indentation is just for clarity):
set $val(X) [expr $val(x)/300]
set $val(Y) [expr $val(y)/300]
for {set i 0} {$i<=$val(X)} {incr i} {
for {set j 0} {$j<=$val(Y)} {incr i} {
for {set b 0} {$b < $val(nn)} {incr b} {
set node($b) [$ns node]
}
}
}
Then the code will run. It will probably not produce quite what you expect though, since you're not using either the i or the j value in the innermost loop. When you make a node, you need to somehow tell it where it is in the grid.
Also, you're using three nested loops, so you're (probably) making a 3D grid. Is this what you intend? (As you're also just saving information about one dimension in the key, that probably means you're using just one tiny fraction of that grid and completely losing track of the other undifferentiated nodes.)
Hy guy i tried to invoke this tcl in the terminal but it gave me this error
num_nodes is set 0
warning: Please use -channel as shown in tcl/ex/wireless-mitf.tcl
can't read "node_(0)": no such variable
while executing
"$node_(0) set X_ 133.516460138239"
(file "/tmp/mob.tcl" line 4)
invoked from within
"source.orig /tmp/mob.tcl"
("uplevel" body line 1)
invoked from within
"uplevel source.orig [list $fileName]"
invoked from within
"if [$instance_ is_http_url $fileName] {
set buffer [$instance_ read_url $fileName]
uplevel eval $buffer
} else {
uplevel source.orig [list $fileName]
..."
(procedure "source" line 8)
invoked from within
"source "/tmp/mob.tcl" "
(file "mobilita_source.tcl" line 125)
And the tcl file is
# Define ions
set val(chan) Channel/WirelessChannel ;# channel type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model
set val(netif) Phy/WirelessPhy ;# network interface type
set val(mac) Mac/802_11 ; # MAC type
Mac/802_11 set RTSThreshold_ 500 ;
Mac/802_11 set dataRate_ 24Mb ;
Mac/802_11 set basicRate_ 6Mb ;
Mac/802_11 set CWMin_ 31 ;
Mac/802_11 set CWMAX_ 1023 ;
Mac/802_11 set SlotTime_ 0.000009 ;
Mac/802_11 set SIFS_ 0.000016 ;
Mac/802_11 set ShortRetryLImit_ 7 ;
Mac/802_11 set LOngRetryLimit_ 7 ;
set val(ifq) Queue/DropTail/PriQueue ;# interface queue type
set val(ifqlen) 50 ;# max packet in ifq
set val(ll) LL ;# link layer type
set val(ant) Antenna/OmniAntenna ;# antenna model
set val(adhocRouting) AODV ;# routing protocol
set val(x) 250 ;# X dimension of the topography
set val(y) 250 ;# Y dimension of the topography
set val(tr) esercizio1.tr ;# trace file
set val(rate) [lindex $argv 0] ;
set val(nn) [lindex $argv 1] ;# how many nodes are simulated
set val(stop) [lindex $argv 3] ;# simulation time
set val(seed) 0.0
# Main Program
# Initialize Global Variables
# create simulator instance
set ns_ [new Simulator]
# define topology
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
# create trace object for ns and nam
set tracefd [open $val(tr) w]
$ns_ trace-all $tracefd
$ns_ use-newtrace
# Create God
set god_ [create-god $val(nn)]
# define how node should be created
#global node setting
$ns_ node-config -adhocRouting $val(adhocRouting) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-channelType $val(chan) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace ON
# Create the specified number of nodes [$val(nn)] and "attach" them to the channel
for {set i 0} {$i < $val(nn) } {incr i} {
set node_($i) [$ns_ node]
}
# Define node positions
source "/tmp/mob.tcl"
# Define traffic flows
source "traffic"
for {set i 0} {$i < 4 } {incr i} {
$cbr_($i) set interval_ [ expr 1 /$rate ]
}
# Tell nodes when the simulation ends
for {set i 0} {$i < $val(nn) } {incr i} {
$ns_ at $val(stop).000000001 "$node_($i) reset";
}
$ns_ at $val(stop).000000001 "puts \"NS EXITING...\" ; $ns_ halt"
puts "Starting Simulation..."
$ns_ run
i invoked the ns mobilita_source from terminal but it describe me this so i think it could be for the path /tmp in the mobilita_souce.tcl file. In tmp i noticed that the file is present.
The variable node_(0) is not set, so the variable is unreadable. That means that the code to set it did not run, perhaps because the variable val(nn) contains 0, or perhaps because it contains something other value — possibly non-numeric — that sorts less than 1; the empty string is such a value. The value of val(nn) comes from lindex $argv 1, so it is the second argument after the Tcl script that you passed to the executable to invoke the program (assuming it's tclsh-like).
If there are insufficient arguments, you'll get an empty string from the lindex (for backward-compatibility reasons, and out-of-range lindex produces an empty string instead of throwing an error). You are advised to explicitly check that sufficient arguments were passed in:
if {[llength $argv] < 4} {
error "missing arguments, needs: rate, nn, ??, seed"
}
You can then (assuming Tcl 8.5 or 8.6) use lassign to write those to variables; it's a little clearer than a bunch of set … [lindex $argv …] and easier to get correct:
# your posted code ignores one argument...
lassign $argv val(rate) val(nn) ?? val(seed)
In 8.4 and before (outdated versions now) then you'd instead do:
foreach $argv {val(rate) val(nn) ?? val(seed)} break
But that's really not very clear.
I have two tcl files. one is main tcl file(main.tcl) and another one is like helper file(help.tcl) for main tcl file.I want to execute that helperfile from main file while execution of main file.I use the command "source filename" for this.but i got some errors.here i post both tcl files and what is the error occured on execution. Please help me to solve this...
Error
num_nodes is set 4
warning: Please use -channel as shown in tcl/ex/wireless-mitf.tcl
INITIALIZE THE LIST xListHead
Enter Source Node (0 to 4)
0
Enter Destination Node (0 to 4)
3
SORTING LISTS ...DONE!
channel.cc:sendUp - Calc highestAntennaZ_ and distCST_
highestAntennaZ_ = 1.5, distCST_ = 716.1
ns: proc1: can't read "n0": no such variable
while executing
"attach-CBR-traffic $n0 $sink1 64 0.07"
invoked from within
"set inf0_1[attach-CBR-traffic $n0 $sink1 64 0.07]"
(file "final.tcl" line 1)
invoked from within
"source.orig final.tcl"
("uplevel" body line 1)
invoked from within
"uplevel source.orig [list $fileName]"
invoked from within
"if [$instance_ is_http_url $fileName] {
set buffer [$instance_ read_url $fileName]
uplevel eval $buffer
} else {
uplevel source.orig [list $fileName]
..."
(procedure "source" line 8)
invoked from within
"source final.tcl"
(procedure "proc1" line 6)
invoked from within
"proc1"
In the file Main.tcl:
set val(chan) Channel/WirelessChannel ;# Channel Type
set val(prop) Propagation/TwoRayGround ;# radio-propagation model
set val(netif) Phy/WirelessPhy ;# network interface type
set val(mac) Mac/802_11 ;# MAC type
set val(ifq) CMUPriQueue ;# interface queue type
set val(ll) LL ;# link layer type
set val(ant) Antenna/OmniAntenna ;# antenna model
set val(ifqlen) 340 ;# max packet in ifq
set val(nn) 4 ;# number of mobilenodes
set val(rp) DSR ;# routing protocol
set val(x) 630 ;# X axis distance
set val(y) 570 ;# Y axis distance
set opt(energymodel) EnergyModel ;# Initial Energy
set opt(initialenergy) 100 ;# Initial energy in Joules
set ns [new Simulator]
set nam1 [open nam11.nam w]
$ns namtrace-all-wireless $nam1 $val(x) $val(y)
set trace1 [open trace11.tr w]
$ns trace-all $trace1
$ns use-newtrace
set topo [new Topography]
$topo load_flatgrid $val(x) $val(y)
create-god $val(nn)
Phy/WirelessPhy set Pt_ 0.81
$ns node-config -adhocRouting $val(rp) \
-llType $val(ll) \
-macType $val(mac) \
-ifqType $val(ifq) \
-ifqLen $val(ifqlen) \
-antType $val(ant) \
-propType $val(prop) \
-phyType $val(netif) \
-topoInstance $topo \
-agentTrace ON \
-routerTrace ON \
-macTrace ON \
-movementTrace ON \
-rxPower 0.36 \
-txPower 0.14 \
-channelType $val(chan) \
-initialEnergy $opt(initialenergy) \
-energyModel $opt(energymodel) \
for { set i 0 } {$i < $val(nn)} { incr i } {
set n($i) [$ns node]
$n($i) random-motion 0
$n($i) color black
$ns initial_node_pos $n($i) 30
}
for { set i 0 } { $i < $val(nn)} { incr i } {
$ns at 0.0 "$n($i) color black"
$ns at 0.0 "$n($i) label-color maroon"
}
$ns at 0.0 "$n(0) setdest 220.00 210 3000"
$ns at 0.0 "$n(1) setdest 320.00 310 3000"
$ns at 0.0 "$n(2) setdest 420.00 110 3000"
$ns at 0.0 "$n(3) setdest 520.00 210 3000"
for {set i 0} {$i<$val(nn)} {incr i} {
set sink($i) [new Agent/LossMonitor]
$ns attach-agent $n($i) $sink($i)
}
proc attach-CBR-traffic { node sink size itval} {
#Get an instance of the simulator
set ns_ [Simulator instance]
set udp [new Agent/UDP]
$ns_ attach-agent $node $udp
#Create a CBR agent and attach it to the node
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set packetSize_ $size ;#sub packet size
$cbr set interval_ $itval
#Attach CBR source to sink;
$ns_ connect $udp $sink
return $cbr
}
set cbr001 [attach-CBR-traffic $n(0) $sink(1) 256 0.082]
$ns at 1.0 "$cbr001 start"
$ns at 1.001 "$cbr001 stop"
set cbr001 [attach-CBR-traffic $n(1) $sink(3) 256 0.082]
$ns at 1.002 "$cbr001 start"
$ns at 1.003 "$cbr001 stop"
# neighbor node calculation
set nbr [open Neighbor w]
puts $nbr "\t\t\t\t\tNeighbor Detail"
puts $nbr "\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
puts $nbr "\tNode\t\tNb node\t\tNode-Xpos\tNode-Ypos\tNb-Xpos\t\tNb-Ypos\t\tDistance(d)"
puts $nbr "\t~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
close $nbr
#~~~~~~~~~~~~~~~~ Calculation of neighbor nodes of all node~~~~~~~~~~~~~~~~~~~
proc distance { n1 n2 nd1 nd2} {
global c n bnd
set nbr [open Neighbor a]
set x1 [expr int([$n1 set X_])]
set y1 [expr int([$n1 set Y_])]
set x2 [expr int([$n2 set X_])]
set y2 [expr int([$n2 set Y_])]
set d [expr int(sqrt(pow(($x2-$x1),2)+pow(($y2-$y1),2)))]
if {$d<300} {
if {$nd2!=$nd1} {
puts $nbr "\t$nd1\t\t$nd2\t\t$x1\t\t$y1\t\t$x2\t\t$y2\t\t$d"
}
}
close $nbr
}
for {set i 0} {$i <$val(nn)} {incr i} {
for {set j 0} {$j <$val(nn)} {incr j} {
$ns at 1.002 "distance $n($i) $n($j) $i $j"
}
}
# For Input
set f 1
while {$f} {
puts "Enter Source Node (0 to 4)"
gets stdin src
puts "Enter Destination Node (0 to 4)"
gets stdin dst
if {$src>=0 && $src<5 && $dst>=0 && $dst<5 && $src!=$dst} {
set f 0
set sd [open srcdst w]
puts $sd "$src $dst"
close $sd
} else {
puts "\n\t---------Enter Proper Input-------\n"
}
}
#minimum
proc proc1 {} {
exec awk -f distance.awk srcdst Neighbor &
exec awk -f mm.awk snd &
exec awk -f mnode.awk min snd &
exec awk -f final.awk srcdst mnode &
source final.tcl
}
$ns at 1.5 "proc1"
proc proc2 {} {
global ns trace1 nam1
$ns flush-trace
close $trace1
close $nam1
exec nam nam11.nam &
exit 0
}
$ns at 3.0 "proc2"
$ns run
In the file final.tcl:
set inf0_1[attach-CBR-traffic $n0 $sink1 64 0.07]
$ns at2.1 "$inf0_1 start"
$ns at2.3 "$inf0_1 stop"
$ns at2.1 "$n(0) color green4"
$ns at2.4 "$ns trace-annotate\"Node-0 send data to its neighbor1\""
set inf1_3[attach-CBR-traffic $n1 $sink3 64 0.07]
$ns at2.1 "$inf1_3 start"
$ns at2.3 "$inf1_3 stop"
$ns at2.1 "$n(1) color green4"
$ns at2.4 "$ns trace-annotate\"Node-1 send data to its neighbor3\""
There doesn't seem to be anywhere in your main.tcl file where you source help.tcl. However, in the error message it appears that the file you claim to be help.tcl is actually named final.tcl. Assuming this is what you mean then I can diagnose the error.
The problem is that in your final.tcl file, the first line tries to execute this statement:
set inf0_1 [attach-CBR-traffic $n0 $sink1 64 0.07]
# ^
# |
# BTW, unrelated to the error, there is also a missing space here which
# is probably not what you intended.
The problem with that statement is that the variables n0 and sink1 are undefined.
Updated Answer
OK, there seem to be lots of misconceptions in your understanding of Tcl. I highly recommend reading the documentation of the Tcl language. Fortunately for you, unlike C or Perl or almost any other language, the complete documentation of tcl syntax is only a single page: http://www.tcl.tk/man/tcl/TclCmd/Tcl.htm. Note specifically rule number 8 (variable substitution).
First thing you need to know that's wrong with your script is what the error message tells you: you have not defined the variable n0 anywhere in your program (nor have you defined sink1). There is however a global variable n which happens to be an array. From you comment it looks like this is what you mean when you're trying to access n0. If so, the correct syntax is given in rule number 8: arrays are accessed using the arrayname(index) syntax. Before we go further I'd like to remind you that what Tcl calls arrays are technically associative arrays (in other languages they are sometimes known as hashes or maps) so the index can be a string. Now, having said that, the correct syntax for accessing n is:
set inf0_1 [attach-CBR-traffic $n(0) $sink(1) 64 0.07]
# ^ ^
But this still won't work. If you try this it will still complain about n and sink being undefined. That's because of how tcl scoping rules work. Inside a function, only local variables are visible. Global variables are not available inside functions unless you explicitly ask tcl to import them. To access global variables you have two options:
First, the traditional way: use the global command to import the global variables. You can either do it in the proc that sources the final.tcl file:
proc proc1 {} {
global n sink
# ...
source final.tcl
}
or you can do it in the final.tcl file itself:
global n sink
set inf0_1 [attach-CBR-traffic $n(0) $sink(1) 64 0.07]
# ...
The second method is to access the global variables using the global namespace ::. This way you don't need to import the variables using the global command:
set inf0_1 [attach-CBR-traffic $::n(0) $::sink(1) 64 0.07]
Fixing all the issues I highlighted above should get your code running (assuming I've guessed your intentions correctly). However I strongly suggest reading the tcl man pages and familiarize yourself with tcl to avoid future bugs. At minimum you should understand the syntax rules, the man page for set and the man page for proc:
http://www.tcl.tk/man/tcl/TclCmd/Tcl.htm
http://www.tcl.tk/man/tcl/TclCmd/set.htm
http://www.tcl.tk/man/tcl/TclCmd/proc.htm
The following page is also a good introduction to tcl: An Introduction to Tcl Scripting