《WSN中LEACH協(xié)議源碼分析》由會(huì)員分享,可在線閱讀,更多相關(guān)《WSN中LEACH協(xié)議源碼分析(10頁珍藏版)》請?jiān)谘b配圖網(wǎng)上搜索。
1、WSN中LEACH協(xié)議源碼分析分析(一)首先對wireless.tcl進(jìn)行分析,先對默認(rèn)的腳本選項(xiàng)進(jìn)行初始化:set opt(chan)Channel/WirelessChannelset opt(prop) Propagation/TwoRayGroundset opt(netif)Phy/WirelessPhyset opt(mac) Mac/802_11set opt(ifq) Queue/DropTail/PriQueueset opt(ll) LLset opt(ant) Antenna/OmniAntennaset opt(x) 0 ;# X dimension of the to
2、pographyset opt(y) 0 ;# Y dimension of the topographyset opt(cp) set opt(sc) ./mobility/scene/scen-670x670-50-600-20-2 ;# scenario fileset opt(ifqlen)50;# max packet in ifset opt(nn) 51 ;# number of nodesset opt(seed) 0.0set opt(stop) 10.0 ;# simulation timeset opt(tr) out.tr ;# trace fileset opt(rp
3、) dsdv ;# routing protocol scriptset opt(lm) on ;# log movement在這個(gè)wireless.tcl中設(shè)置了一些全局變量:# Initialize Global Variables#set ns_ new Simulatorset chan new $opt(chan)set prop new $opt(prop)set topo new Topographyset tracefd open $opt(tr) w$topo load_flatgrid $opt(x) $opt(y)$prop topography $topo這些初始化將在
4、后面的使用中用到,該文件最重要的是創(chuàng)建leach節(jié)點(diǎn):創(chuàng)建方法如下: elseif string compare $opt(rp) leach = 0 for set i 0 $i $opt(nn) incr i leach-create-mobile-node $i如果路由協(xié)議是leach協(xié)議,則在Uamps.tcl中調(diào)用leach-create-mobile-node方法創(chuàng)建leach節(jié)點(diǎn)。將在第二小節(jié)講如何創(chuàng)建leach節(jié)點(diǎn)。for set i 0 $i $begin_sleep_ set idle_energy expr $opt(Pidle) * expr $ns_ now - $b
5、egin_idle_ $self getER remove $idle_energy else set sleep_energy expr $opt(Psleep) * expr $ns_ now - $begin_sleep_ $self getER remove $sleep_energy set begin_sleep_ $ns_ now set begin_idle_ 0分析(六)在start中調(diào)用下面這個(gè)方法。Application/LEACH instproc decideClusterHead global ns_ chan opt node_ $self instvar ali
6、ve_ TDMAschedule_ $self instvar begin_idle_ begin_sleep_ # Check the alive status of the node. If the node has run out of # energy, it no longer functions in the network. set ISalive $self node set netif_(0) set alive_ if $alive_ = 1 if $ISalive = 0 puts Node $self nodeID is DEAD! $chan removeif $se
7、lf node set netif_(0) set alive_ 0 set opt(nn_) expr $opt(nn_) - 1 set ISalive $self node set netif_(0) set alive_ #從網(wǎng)絡(luò)接口netif中查看當(dāng)前節(jié)點(diǎn)狀 況如果節(jié)點(diǎn)存活,但是節(jié)點(diǎn)能量耗光,則$chan removeif $self node set netif_(0)將節(jié)點(diǎn)信道中移出,并將節(jié)點(diǎn)設(shè)置為死亡。節(jié)點(diǎn)的總數(shù)目減少一個(gè)。if $opt(eq_energy) = 1 # # Pi(t) = k / (N - k mod(r,N/k) # where k is the expe
8、cted number of clusters per round # N is the total number of sensor nodes in the network # and r is the number of rounds that have already passed. # set nn $opt(nn_) if expr $nn - $opt(num_clusters) * $round_ 1 set thresh 1 else set thresh expr double($opt(num_clusters) / expr $nn - $opt(num_cluster
9、s) * $round_ # Whenever round_ is 0, all nodes are eligible to be cluster-head. if $round_ = 0 $self hasnotbeenClusterHead # If node has been cluster-head in this group of rounds, it will not # act as a cluster-head for this round. if $self hasbeenClusterHead? set thresh 0 else # # Pi(t) = Ei(t) / E
10、total(t) * k # where k is the expected number of clusters per round, # Ei(t) is the nodes current energy, and Etotal(t) is the total # energy from all nodes in the network. # set Etotal 0 # Note! In a real network, would need a routing protocol to get this # information. Alternatively, each node cou
11、ld estimate Etotal(t) from # the energy of nodes in its cluster. for set id 0 $id expr $opt(nn)-1 incr id set app $node_($id) set rca_app_ set E $app getER query set Etotal expr $Etotal + $E set E $self getER query set thresh expr double(expr $E * $opt(num_clusters) / $Etotal 上面是對thresh的計(jì)算,當(dāng)(N - k m
12、od(r,N/k)1,則將thresh設(shè)置為1,否則節(jié)點(diǎn)thresh=k / (N - k mod(r,N/k),每個(gè)節(jié)點(diǎn)在一個(gè)1/p中都要成為簇頭節(jié)點(diǎn)一次。p=簇頭節(jié)點(diǎn)占所有節(jié)點(diǎn)的比例,在r=0的時(shí)候每個(gè)節(jié)點(diǎn)都有機(jī)會(huì)吃呢更為簇頭節(jié)點(diǎn)。如果節(jié)點(diǎn)成為過簇頭節(jié)點(diǎn)則thresh=0,則這個(gè)節(jié)點(diǎn)在1/p輪后才可以成為簇頭節(jié)點(diǎn)。 if $self getRandomNumber 0 1 getRandomNumber,則節(jié)點(diǎn)成為簇頭節(jié)點(diǎn)。然后調(diào)用advertiseClusterHead方法。set next_change_time_ expr $now_ + $opt(ch_change) $ns_ a
13、t $next_change_time_ $self decideClusterHead $ns_ at expr $now_ + $opt(ra_adv_total) $self findBestCluster當(dāng)節(jié)點(diǎn)成為簇頭節(jié)點(diǎn),則節(jié)點(diǎn)調(diào)用 advertiseClusterHead方法。 set chID $self nodeID set currentCH_ $chID pp Cluster Head $currentCH_ broadcasting ADV at time $ns_ now set mac_dst $MAC_BROADCAST set link_dst $LINK_BRO
14、ADCAST set msg list $currentCH_ set datasize expr $BYTES_ID * llength $msg # Send beacons opt(max_dist) meters so all nodes can hear. $self send $mac_dst $link_dst $ADV_CH $msg $datasize $opt(max_dist) $code_將該節(jié)點(diǎn)設(shè)置為簇頭節(jié)點(diǎn),設(shè)置當(dāng)前節(jié)點(diǎn)所處的簇號(hào)。然后發(fā)送數(shù)據(jù),廣播該節(jié)點(diǎn)為簇頭信息到全局網(wǎng)絡(luò)。在$ns_ at expr $now_ + $opt(ra_adv_total) $sel
15、f findBestCluster調(diào)用findBestCluster方法。分析(七)當(dāng)簇頭發(fā)出了一個(gè)ADV類型的包時(shí),其他的節(jié)點(diǎn)會(huì)接收這個(gè)包,并會(huì)將發(fā)送這個(gè)包的簇頭的節(jié)點(diǎn)號(hào)按順序先后記錄在clusterChoices_中,還會(huì)計(jì)算每個(gè)簇頭到接收節(jié)點(diǎn)的距離并記錄在clusterDist_中。這樣可以方便每個(gè)節(jié)點(diǎn)選簇的時(shí)候進(jìn)行比較。具體的實(shí)現(xiàn)在ns-leach.tcl中的recvADV_CH函數(shù)中。findBestCluster if $self isClusterHead? # If node is CH, determine code and create a TDMA schedule. s
16、et dist_ $opt(max_dist) set currentCH_ $nodeID set myADVnum_ $self mac set myADVnum_ # There are opt(spreading) - 1 codes available b/c need 1 code # for communication with the base station. set numCodesAvail expr 2 * $opt(spreading) - 1 set ClusterCode expr int(fmod($myADVnum_, $numCodesAvail) + 1
17、$ns_ at expr $now_ + $opt(ra_adv_total) + $opt(ra_join) $self createSchedule 如果這個(gè)節(jié)點(diǎn)是簇頭節(jié)點(diǎn),設(shè)置為當(dāng)前的簇頭, set numCodesAvail expr 2 * $opt(spreading) - 1 set ClusterCode expr int(fmod($myADVnum_, $numCodesAvail) + 1這兩句沒看懂在一個(gè)隨機(jī)時(shí)間后調(diào)用createSchedule,節(jié)點(diǎn)根據(jù)自己收到的clusterChoices,選擇最近的簇頭作為簇頭節(jié)點(diǎn)。選擇好簇頭節(jié)點(diǎn)就informClusterHea
18、d。發(fā)送完數(shù)據(jù)就進(jìn)入休眠狀態(tài)。Application/LEACH instproc createSchedule global ns_ opt ADV_SCH MAC_BROADCAST BYTES_ID $self instvar clusterNodes_ TDMAschedule_ $self instvar dist_ code_ now_ beginningE_ set numNodes llength $clusterNodes_ set chID $self nodeID if $numNodes = 0 set xmitOrder puts Warning! There are
19、 no nodes in this cluster ($chID)! $self SendMyDataToBS else # Set the TDMA schedule and send it to all nodes in the cluster. set xmitOrder $clusterNodes_ set msg list $xmitOrder set spreading_factor $opt(spreading) set datasize expr $spreading_factor * $BYTES_ID * llength $xmitOrder pp $chID sendin
20、g TDMA schedule: $xmitOrder at time $ns_ now pp Packet size is $datasize. set mac_dst $MAC_BROADCAST set link_dst $chID $self send $mac_dst $link_dst $ADV_SCH $msg $datasize $dist_ $code_ set TDMAschedule_ $xmitOrder set outf open $opt(dirname)/TDMAschedule.$now_.txt a puts $outf $chIDt$TDMAschedule
21、_ close $outf set outf open $opt(dirname)/startup.energy a puts $outf $ns_ nowt$chIDtexpr $beginningE_ - $self getER query close $outf在recvADV_SCH給每個(gè)節(jié)點(diǎn)一個(gè)時(shí)間間隙發(fā)送數(shù)據(jù) set frame_time_ expr expr 5 + llength join $order * $opt(ss_slot_time) set xmitTime_ expr $opt(ss_slot_time) * $ind set end_frm_time_ expr $frame_time_ - $xmitTime_ set xmitat expr $ns_ now + $xmitTime_ pp $nodeID scheduled to transmit at $xmitat. It is now $ns_ now. if expr $xmitat + $end_frm_time_ expr $next_change_time_ - 10 * $opt(ss_slot_time) $ns_ at $xmitat $self sendData具體實(shí)現(xiàn)方法看不太懂。