关闭

ns-leach.tcl 分析3

标签: dstjoinquerylist
1011人阅读 评论(0) 收藏 举报
分类:

当簇头发出了一个ADV类型的包时,其他的节点会接收这个包,并会将发送这个包的簇头的节点号按顺序先后记录在clusterChoices_中,还会计算每个簇头到接收节点的距离并记录在clusterDist_中。这样可以方便每个节点选簇的时候进行比较。具体的实现在在ns-leach.tcl中的recvADV_CH函数中。

findBestCluster

  if [$self isClusterHead?] {
    # If node is CH, determine code and create a TDMA schedule.
    set 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]
    $ns_ at [expr $now_ + $opt(ra_adv_total) + $opt(ra_join)] \
        "$self createSchedule"
  }

    如果这个节点是簇头节点,设置为当前的簇头,

    set numCodesAvail [expr 2 * $opt(spreading) - 1]
    set ClusterCode [expr int(fmod($myADVnum_, $numCodesAvail)) + 1]这两句没看懂

在一个随机时间后调用createSchedule,节点根据自己收到的clusterChoices,选择最近的簇头作为簇头节点。选择好簇头节点就informClusterHead。发送完数据就进入休眠状态。

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 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 sending 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 "$chID\t$TDMAschedule_"
  close $outf


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

}

recvADV_SCH给没个节点一个时间间隙发送数据

  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"

具体实现方法看不太懂。



0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:91685次
    • 积分:1231
    • 等级:
    • 排名:千里之外
    • 原创:27篇
    • 转载:35篇
    • 译文:0篇
    • 评论:42条
    NS2研究群
    66536296
    文章分类
    最新评论