NS2相关学习——在ns中模拟无线场景

之前学习的都是有线场景下的NS2相关应用,现在开始,终于要学习无线啦!无线是我研究的重点,要好好学习呀!


在本节中,我们将学习使用ns中提供的移动无线仿真模型。 该部分由两部分组成。 在第一小节中,我们讨论如何创建和运行一个简单的2节点无线网络仿真。 在第二部分中,我们将扩展我们的示例(在第1小节中),创建一个比较复杂的无线方案。

1、创建简单的无线场景

我们将模拟一个非常简单的2个节点无线场景。 拓扑由两个移动节点(node_(0)和node_(1))组成。 在这个例子中边界定义为500mX500m的区域。 节点最初在边界的两个相对端开始相对移动。在两个移动终端之间建立TCP连接。 节点之间的数据包在彼此的听觉范围内进行交换。 当它们离开时,数据包开始下降。

首先,我们创建一个tcl脚本,命名为simple-wireless.tcl。

移动节点由网络组件组成,网络组件包括链路层(LL),接口队列(IfQ),MAC层,无线信道节点发送和接收信号(有关这些网络组件的详细信息参见请参见ns手册第15章第1节)。 在无线模拟开始时,我们需要为每个这些网络组件定义类型。此外,我们需要定义其他参数,如天线类型,无线电传播模型,移动终端使用的自组织路由协议的类型等。

下面我们开始我们的脚本——simple-wireless.tcl,这个脚本列出了上面说的那些参数,具有一定的参考意义。

# ======================================================================
# Define options
# ======================================================================
set val(chan)         Channel/WirelessChannel  ;# channel type 通道类型
set val(prop)         Propagation/TwoRayGround ;# radio-propagation model 无线传播模型
set val(ant)          Antenna/OmniAntenna      ;# Antenna type 天线类型
set val(ll)           LL                       ;# Link layer type 链路层类型
set val(ifq)          Queue/DropTail/PriQueue  ;# Interface queue type 接口队列类型
set val(ifqlen)       50                       ;# max packet in ifq Ifq的大小
set val(netif)        Phy/WirelessPhy          ;# network interface type 网络接口类型
set val(mac)          Mac/802_11               ;# MAC type MAC类型
set val(rp)           DSDV                     ;# ad-hoc routing protocol ad-hoc路由协议
set val(nn)           2                        ;# number of mobilenodes 移动节点的数目
接下来我们来到程序的主要部分,首先创建模拟器的一个实例

set ns_    [new Simulator]


然后通过打开文件simple.tr设置跟踪支持,并调用过程trace-all {},如下所示:

set tracefd     [open simple.tr w]
$ns_ trace-all $tracefd    

接下来创建一个拓扑对象,跟踪拓扑边界内的移动节点的移动。

set topo	[new Topography]

我们之前提到,移动节点在500mX500m的拓扑内移动。 我们提供边界的x和y坐标(x = 500,y = 500):

$topo load_flatgrid 500 500

整个区域被分解为网格,网格分辨率的默认值为1.可以将不同的值作为第三个参数传递给load_flatgrid {}。
下面我们创造god对象(可以理解为一个很厉害的具有上帝视角的对象)
create-god $val(nn)
god(通用运营总监)是用于存储关于全局环境、网络、节点的所有信息,但是对于模拟参与者来说是透明的。目前,god对象存储了所有移动节点的数量,一个节点到达另外一个节点最小跳数表。因为在模拟运行期间计算下一跳信息相当耗时,所以下一跳信息通常在模拟开始之前从运动模式文件中加载到god对象中。 然而,为了保持这个例子简单,我们避免使用运动模式文件,因此不能向上帝提供下一跳信息。 运动模式文件的使用和向上帝馈送下一跳信息将在下一小节的示例中显示。
程序create-god在〜ns / tcl / mobility / com.tcl中定义,它只允许在仿真期间创建God对象的单个全局实例。 除了评估功能之外,上帝的对象是由移动节点中的MAC对象内部调用的。 所以即使我们不能利用god的评估目的(如本例),我们仍然需要创造god。
接下来,我们创建移动节点。 节点创建API已被修改,在这里我们将使用新的API来创建移动代码。
首先,我们需要在创建节点之前配置节点。 节点配置API可以包括定义寻址类型(平面/层次等),自适应路由协议的类型,链路层,MAC层,IfQ等。配置API可以定义如下:
 (parameter examples)
# $ns_ node-config -addressingType flat or hierarchical or expanded
#                  -adhocRouting   DSDV or DSR or TORA                ###设置移动节点所需要的路由协议
#                  -llType	   LL                                 ###设置移动节点的逻辑链路层
#                  -macType	   Mac/802_11                         ###设置移动节点的MAC层
#                  -propType	   "Propagation/TwoRayGround"         ###设置移动节点的无线信号传输模型
#                  -ifqType	   "Queue/DropTail/PriQueue"          ###设置移动节点的队列类型
#                  -ifqLen	   50                                 ####设置移动节点的队列长度
#                  -phyType	   "Phy/WirelessPhy"                  ####设置移动节点的物理层模型
#                  -antType	   "Antenna/OmniAntenna"              ####设置移动节点的天线类型
#                  -channelType    "Channel/WirelessChannel"          ####设置移动节点的无线信道类型
#                  -topoInstance   $topo                              ####设置移动节点的拓扑对象
#                  -energyModel    "EnergyModel"                      ###设置节点的能量模型
#                  -initialEnergy  (in Joules)                        ###设置节点初始能量,单位是J(焦耳)
#                  -rxPower        (in W)                             ###设置节点的接收功率,单位是瓦(w)
#                  -txPower        (in W)                             ###设置节点的发送功率,单位是瓦(w)
#                  -agentTrace     ON or OFF                          ###是否打开应用层的trace
#                  -routerTrace    ON or OFF                          ###是否打开路由层的trace
#                  -macTrace       ON or OFF                          ###是否打开mac层的trace
#                  -movementTrace  ON or OFF                          ###是否打开节点位置和移动信息的trace

用于创建移动节点的配置API如下所示:
# Configure 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) \
                         -topoInstance $topo \
                         -channelType $val(chan) \
                         -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       ;# disable random motion 这个节点不能乱动
        }    

下面给移动节点他们开始的位置
#
# Provide initial (X,Y, for now Z=0) co-ordinates for node_(0) and node_(1)
#
$node_(0) set X_ 5.0
$node_(0) set Y_ 2.0
$node_(0) set Z_ 0.0

$node_(1) set X_ 390.0
$node_(1) set Y_ 385.0
$node_(1) set Z_ 0.0
Node0的起始位置为(5,2),而Node1在位置(390,385)处开始。

接下来产生节点移动
#
# Node_(1) starts to move towards node_(0)
#
$ns_ at 50.0 "$node_(1) setdest 25.0 20.0 15.0"
$ns_ at 10.0 "$node_(0) setdest 20.0 18.0 1.0"

# Node_(1) then starts to move away from node_(0)
$ns_ at 100.0 "$node_(1) setdest 490.0 480.0 15.0"
$ ns_ at 50.0“$ node_(1)setdest 25.0 20.0 15.0”表示在时刻50.0s时,node1以15m / s的速度开始向目标移动(x = 25,y = 20)。 该API用于改变移动节点的移动方向和速度。
接下来,在两个节点之间建立数据流。
# TCP connections between node_(0) and node_(1)

set tcp [new Agent/TCP]
$tcp set class_ 2
set sink [new Agent/TCPSink]
$ns_ attach-agent $node_(0) $tcp    ###node0是源节点
$ns_ attach-agent $node_(1) $sink   ###node1是目标节点
$ns_ connect $tcp $sink             ###吧源和目标连接起来
set ftp [new Application/FTP]       ###使用的应用层协议是ftp
$ftp attach-agent $tcp              ###把应用层协议和传输层协议连接起来 
$ns_ at 10.0 "$ftp start" 
这将在两个两个节点之间建立起来一个TCP连接。

然后,我们需要在模拟结束时定义停止时间,并告诉移动节点复位,实际上重置其内部网络组件,
#
# Tell nodes when the simulation ends
#
for {set i 0} {$i < $val(nn) } {incr i} {
    $ns_ at 150.0 "$node_($i) reset";
}
$ns_ at 150.0001 "stop"
$ns_ at 150.0002 "puts \"NS EXITING...\" ; $ns_ halt"
proc stop {} {
    global ns_ tracefd
    close $tracefd
}
在时刻150.0s时模拟停止,节点在此时被重置。在150.0002s时调用“$ ns_ halt”。 调用stop {}来清除跟踪并关闭跟踪文件。

最后执行“开始模拟”的命令。
puts "Starting Simulation..."
$ns_ run

保存文件simple-wireless.tcl。

完整的脚本如下所示:
# Copyright (c) 1997 Regents of the University of California.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#      This product includes software developed by the Computer Systems
#      Engineering Group at Lawrence Berkeley Laboratory.
# 4. Neither the name of the University nor of the Laboratory may be used
#    to endorse or promote products derived from this software without
#    specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# simple-wireless.tcl
# A simple example for wireless simulation

# ======================================================================
# 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(nn)             2                          ;# number of mobilenodes
set val(rp)             DSDV                       ;# routing protocol

# ======================================================================
# Main Program
# ======================================================================


#
# Initialize Global Variables
#
set ns_		[new Simulator]
set tracefd     [open simple.tr w]
$ns_ trace-all $tracefd

# set up topography object
set topo       [new Topography]

$topo load_flatgrid 500 500

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

#
#  Create the specified number of mobilenodes [$val(nn)] and "attach" them
#  to the channel. 
#  Here two nodes are created : node(0) and node(1)

# 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) \
			 -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		;# disable random motion
	}

#
# Provide initial (X,Y, for now Z=0) co-ordinates for mobilenodes
#
$node_(0) set X_ 5.0
$node_(0) set Y_ 2.0
$node_(0) set Z_ 0.0

$node_(1) set X_ 390.0
$node_(1) set Y_ 385.0
$node_(1) set Z_ 0.0

#
# Now produce some simple node movements
# Node_(1) starts to move towards node_(0)
#
$ns_ at 50.0 "$node_(1) setdest 25.0 20.0 15.0"
$ns_ at 10.0 "$node_(0) setdest 20.0 18.0 1.0"

# Node_(1) then starts to move away from node_(0)
$ns_ at 100.0 "$node_(1) setdest 490.0 480.0 15.0" 

# Setup traffic flow between nodes
# TCP connections between node_(0) and node_(1)

set tcp [new Agent/TCP]
$tcp set class_ 2
set sink [new Agent/TCPSink]
$ns_ attach-agent $node_(0) $tcp
$ns_ attach-agent $node_(1) $sink
$ns_ connect $tcp $sink
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ns_ at 10.0 "$ftp start" 

#
# Tell nodes when the simulation ends
#
for {set i 0} {$i < $val(nn) } {incr i} {
    $ns_ at 150.0 "$node_($i) reset";
}
$ns_ at 150.0 "stop"
$ns_ at 150.01 "puts \"NS EXITING...\" ; $ns_ halt"
proc stop {} {
    global ns_ tracefd
    $ns_ flush-trace
    close $tracefd
}

puts "Starting Simulation..."
$ns_ run

在与tcl脚本同一个目录文件下,产生了一个trace文件。

trace文件中,AgentTraces在其第5个字段中标有AGT,RouterTrace和MACTrack标有RTR。


链接:http://www.isi.edu/nsnam/ns/tutorial/

  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值