Chinaunix首页 | 论坛 | 博客
  • 博客访问: 106427
  • 博文数量: 36
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 10
  • 用 户 组: 普通用户
  • 注册时间: 2013-06-28 21:29
文章分类
文章存档

2015年(1)

2014年(30)

2013年(5)

我的朋友

分类: 系统运维

2014-08-14 15:30:58

E-mail:
 
     初略描述一下无线网络中的一些概念:(不准确,以后再来完善!)
 
     无线网络通信依靠的是射频信号,即电磁波;
     每个节点的工作模块大概可以分为:通信模块、电源管理模块。
     每个节点的通信具有一点的范围,称为通信半径,例如:550m。
     无线通信的主要问题:节点间干扰(信道竞争;隐藏节点和暴露节点问题)
                      无线网络组网(拓扑结构的建立)
                      数据回传到基站的路由策略;
                      动态侦测和动态拓扑条件下网络的可用性、可信性。  
     无线传感器网络方面的研究者一定很熟悉以上的问题。(有兴趣的读者请进一步查阅相关资料。)
 
    
     本实例是无线网络中的很基础的一个问题,即无线节点本身通信范围有限时,怎么通过邻居节点将数据在整个网络中进行传送。 (数据回传到基站的路由,就是在这基础之上进一步考虑如何使用策略来提高网路性能的)
     实例关键点:
          如何在无线节点上实现多跳,即如何让节点转发数据,充当路由器。
 
(本实例的TCL代码是针对 NS2--习题(1)设计的, 参考了Marc Grei‘s Tutorial,
  具体请参看博文: NS2 Tutorial----Marc Grei’s Tutorial,
  我将在今后修改时注明各种参考,我支持原创!   )
 
原题目:(场景定义:再次默认读者已过六级,不然也默认读者善于使用电子词典或Google翻译! 哈哈)
 
鉴于收到英国教授的邮件,他建议我不要让他的学生通过COPY和PASTE就有机会完成任务,从而没有真正地学习NS2,暂时把作业题隐去啦!!
(实际上,下面仅给出第二题的最后代码,第一题就是我发布的第二篇博客的要求。)
 
 TCL脚本的代码:

set val(simDur) 85.0         ;#simulation duration
set val(basename)  multi-hop ;#basename for this project or scenario
set val(statIntvl) 0.1 ;#statistics collection interval
set val(statStart) 0.5 ;
set val(trafStart) 0.5 ;#CBR start time
set val(cbrIntvl) 1.0  ;#CBR traffic interval
set val(chan)  [new Channel/WirelessChannel]    ;# channel model
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(ifqlen) 50                         ;# max packet in ifq
set val(ll)     LL                         ;# link layer type
set val(ant)    Antenna/OmniAntenna        ;# antenna model
set val(nn)     3                          ;# number of mobilenodes
set val(rp)             AODV                  ;# routing protocol
set val(topo_x_dim)     500
set val(topo_y_dim)     500

#Initialize and create output files
#Create a simulator instance
set ns [new Simulator]

#Crate a trace file and animation record
set tracefd [open $val(basename).tr w]
$ns trace-all $tracefd
set namtracefd [open $val(basename).nam w]
$ns namtrace-all-wireless $namtracefd $val(topo_x_dim) $val(topo_y_dim)

set outfd [open $val(basename).out w]
#Create Topology

# set up topography object
set topo       [new Topography]

$topo load_flatgrid $val(topo_x_dim) $val(topo_y_dim)

# Create God
#
create-god $val(nn)

#  Create the specified number of mobilenodes [$val(nn)] and "attach" them
#  to the channel.
 
# configure node
$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 OFF \
   -movementTrace OFF \
   -channel $val(chan)

for {set i 0} {$i < $val(nn) } {incr i} {
 set node($i) [$ns node]
 $node($i) random-motion 0              ;# disable random motion
}


$node(0) set X_ 155.0
$node(0) set Y_ 142.0
$node(0) set Z_ 0.0

$node(1) set X_ 410.0
$node(1) set Y_ 385.0
$node(1) set Z_ 0.0

$node(2) set X_ 340.0
$node(2) set Y_ 260.0
$node(2) set Z_ 0.0


$ns initial_node_pos $node(0) 10
$ns initial_node_pos $node(1) 10
$ns initial_node_pos $node(2) 10

#Modify these variables accordingly
set proto "tcp"
set src $node(0)
set dst $node(1)

if {$proto=="udp"} {
    #Create a udp agent on node0
    set udp [new Agent/UDP]
    $ns attach-agent $src $udp

    # Create a CBR traffic source on node0
    set cbr0 [new Application/Traffic/CBR]
    $cbr0 set packetSize_ 500
    $cbr0 set interval_ $val(cbrIntvl)
    $cbr0 set random_ 1
    $cbr0 attach-agent $udp

    #Create a Null agent (a traffic sink) on node1
    set sink0 [new Agent/LossMonitor]
    $ns attach-agent $dst $sink0

    #Connet source and dest Agents
    $ns connect $udp $sink0
    $ns at $val(trafStart) "$cbr0 start"
    $ns at $val(simDur) "$cbr0 stop"
} elseif {$proto=="tcp"} {
    #Create a tcp agent on the source node
    set tcp [new Agent/TCP]
    $tcp set class_ 2
    $ns attach-agent $src $tcp

    # Create a CBR traffic source on node0
    set ftp [new Application/FTP]
    $ftp attach-agent $tcp

    #Create a sink(a traffic sink) on the destination node
    set sink0 [new Agent/TCPSink]
    $ns attach-agent $dst $sink0

    #Connet source and dest Agents
    $ns connect $tcp $sink0
    $ns at $val(trafStart) "$ftp start"
}

 

$ns at 0.1 "$node(1) setdest 125.0 160.0 10.0"
$ns at 0.1 "$node(0) setdest 460.0 360.0 5.0"


#a procedure to record stats
proc record {} {
    global sink0 ns outfd val
    set bytes [$sink0 set bytes_]
    set now [$ns now]
    puts $outfd "$now $bytes"
    $sink0 set bytes_ 0
    $ns at [expr $now+$val(statIntvl)] "record"
}

#a procedure to close trace file and nam file
proc finish {} {

 global ns tracefd namtracefd basename val
 $ns flush-trace

 close $tracefd
 close $namtracefd
 exec nam multi-hop.nam &
 exit 0
}

#Schedule trigger events
$ns at $val(statStart) "record"

#Call the finish procedure after 5s (of simulated time)
$ns at $val(simDur) "finish"

#Run the simulation
$ns run

 
 
E-mail:
阅读(2042) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~