LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 08-24-2013, 03:58 AM   #1
eng_mohamed_sc
Member
 
Registered: Jul 2013
Posts: 49

Rep: Reputation: Disabled
Lightbulb LEACH-C code


please explain this for me
sorry I'm a beginner at NS2
what is $self mean is it define a method
is set now_ : for define variable named now_ so what is [$ns_ now]?

$self setCode $opt(bsCode)
$self WakeUp
set now_ [$ns_ now]
set nodeID [$self nodeID]
set X [$self getX]
set Y [$self getY]
set E [[$self getER] query]
set mac_dst $MAC_BROADCAST
set link_dst $LINK_BROADCAST
set msg[list[list[list $X $Y $E]]]
set datasize [expr $BYTES_ID * [llength[list $X $Y $E]]]
set dist [nodeToBSDist [$self node] $bs]

set beginningE_ $E
 
Old 08-24-2013, 04:15 AM   #2
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,513

Rep: Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641
! Please do not make multiple threads with the same topic (leach, leach-c) !


Quote:
what is [$ns_ now]
Please read the the files to know that it is a time definition :
$ cd ns-2.34/mit/
$ grep -Rn "ns_ now" *

-
 
Old 08-24-2013, 05:43 AM   #3
eng_mohamed_sc
Member
 
Registered: Jul 2013
Posts: 49

Original Poster
Rep: Reputation: Disabled
ok please can you explain all the following code by adding comments and how can I understand NS2 language?



$self setCode $opt(bsCode)
$self WakeUp
set now_ [$ns_ now]
set nodeID [$self nodeID]
set X [$self getX]
set Y [$self getY]
set E [[$self getER] query]
set mac_dst $MAC_BROADCAST
set link_dst $LINK_BROADCAST
set msg[list[list[list $X $Y $E]]]
set datasize [expr $BYTES_ID * [llength[list $X $Y $E]]]
set dist [nodeToBSDist [$self node] $bs]

set beginningE_ $E
 
Old 08-24-2013, 08:34 AM   #4
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,513

Rep: Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641
# 3 .

Probably read the documentation.
$ cd ns-2.34/doc/ && make
.. Created are everything.pdf, everything.ps, everything.ps.gz .
* The ns-2.34 doc/ files are dated 2009.

* Some online manuals may be outdated. ( Google .. ns2 manual ..).
This one was edited 2011, and should be OK :
http://www.isi.edu/nsnam/ns/doc/

-
 
Old 08-25-2013, 04:48 AM   #5
eng_mohamed_sc
Member
 
Registered: Jul 2013
Posts: 49

Original Poster
Rep: Reputation: Disabled
Arrow Leach-c protocol explanation

Please add comment at each line to understand its mean

# Determine code for each cluster from BS information.
foreach element $clusters {
if {[lsearch $CHnodes $element] == -1} {
set CHnodes [lappend CHnodes $element]
}
}
$self setCode [expr [lsearch $CHnodes $my_ch] + 1]

set outf [open $opt(dirname)/startup.energy a]
puts $outf "[$ns_ now]\t$id\t[expr $beginningE_ - [[$self getER] query]]"
close $outf

# Determine slot in TDMA schedule from BS information.
set i 0
set sch ""
foreach element $clusters {
if {$element == $my_ch} {lappend sch $i}
incr i
}
set TDMAschedule_ [join $sch]
set clusterNodes_ $TDMAschedule_
set frame_time_ [expr [expr 5 + [llength $TDMAschedule_]] * \
$opt(ss_slot_time)]

puts "Node $id's CH is $my_ch, code is $code_ at time [$ns_ now]"
if {$my_ch == $id} {
# Node is a CH for this round. Record TDMA schedule.
puts "CH $id: TDMAschedule is $TDMAschedule_"
puts "*******************************************"
$self WakeUp
$self setClusterHead
set dist_ $opt(max_dist)
set outf [open $opt(dirname)/TDMAschedule.[expr round($now_)].txt a]
puts $outf "$my_ch\t$TDMAschedule_"
close $outf
if {[llength $TDMAschedule_] == 1} {
puts "Warning! There are no nodes in this cluster ($id)!"
$self SendMyDataToBS
}
} elseif {$my_ch > -1} {
# Node is a cluster member for this round. Schedule a data
# transmission to the cluster-head during TDMA slot.
$self unsetClusterHead
set dist_ [nodeDist [$self node] $node_($my_ch)]
set ind [lsearch $TDMAschedule_ $id]
if {$ind < 0} {
puts "ERROR!!!! $id does not have a transmit time!"
exit 0
}
set xmitTime_ [expr $opt(ss_slot_time) * $ind]
set end_frm_time_ [expr $frame_time_ - $xmitTime_]
set xmitat [expr [$ns_ now] + $xmitTime_]
if {[expr $xmitat + $end_frm_time_] < \
[expr $next_change_time_ - 10 * $opt(ss_slot_time)]} {
$ns_ at $xmitat "$self sendData"
}
$self GoToSleep
}

# For LEACH-C, clusters are rotated at the beginning of each round.
if {$opt(rcapp) == "LEACH/LEACH-C"} {
$ns_ at $next_change_time_ "$self advertiseInfo"
}

}
 
Old 08-25-2013, 04:58 AM   #6
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen DK
Distribution: PCLinuxOS2023 Fedora38 + 50+ other Linux OS, for test only.
Posts: 17,513

Rep: Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641Reputation: 3641
Please read #2 http://www.linuxquestions.org/questi...-4175474501/#2
"".. do not make multiple threads with the same topic (leach, leach-c) !""

You can use the 'Report' button to ask a mod to merge this thread
with one of your other {leach, leach-c} threads.
 
  


Reply

Tags
ns2



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
leach.out total energy increase leach ns2.34 linuxUser123linux Linux - Software 1 07-13-2013 04:54 AM
source code of leach protocol MAHAKR Linux - Newbie 1 06-11-2011 03:48 AM
how to write ns code that implements LEACH in a network _man_ouer Linux - General 1 05-02-2010 03:26 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 01:48 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration