📄 nsscript5.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0051)http://www.isi.edu/nsnam/ns/tutorial/nsscript5.html -->
<HTML><HEAD><TITLE>Marc Greis' Tutorial for the UCB/LBNL/VINT Network Simulator "ns"</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META content="MSHTML 6.00.2900.3199" name=GENERATOR></HEAD>
<BODY text=#000000 bgColor=#ffffff>
<H1 align=center>IX. Running Wireless Simulations in ns</H1>
<P>[<A href="http://www.isi.edu/nsnam/ns/tutorial/nsscript4.html">Previous
section</A>] [<A href="http://www.isi.edu/nsnam/ns/tutorial/nsscript6.html">Next
section</A>] [<A href="http://www.isi.edu/nsnam/ns/tutorial/nsindex.html">Back
to the index</A>] </P>
<P>In this section, you are going to learn to use the mobile wireless simulation
model available in ns. The section consists of two parts. In the <A
href="http://www.isi.edu/nsnam/ns/tutorial/nsscript5.html#first">first</A>
subsection, we discuss how to create and run a simple 2-node wireless network
simulation. In <A
href="http://www.isi.edu/nsnam/ns/tutorial/nsscript5.html#second">second</A>
subsection, we will extend our example (in subsection 1) to create a relatively
more complex wireless scenario. </P>
<P><I>IMPORTANT: This tutorial chapter uses new node APIs which are available as
of ns-2.1b6, released January 18, 2000. If you have an earlier version of ns you
must upgrade to use these features. </I></P>
<HR>
<A name=first></A>
<P><STRONG>IX.1. Creating a simple wireless scenario </STRONG><BR></P>
<P>We are going to simulate a very simple 2-node wireless scenario. The topology
consists of two mobilenodes, node_(0) and node_(1). The mobilenodes move about
within an area whose boundary is defined in this example as 500mX500m. The nodes
start out initially at two opposite ends of the boundary. Then they move towards
each other in the first half of the simulation and again move away for the
second half. A TCP connection is setup between the two mobilenodes. Packets are
exchanged between the nodes as they come within hearing range of one another. As
they move away, packets start getting dropped. </P>
<P>Just as with any other ns simulation, we begin by creating a tcl script for
the wireless simulation. We will call this file simple-wireless.tcl. If you want
to download a copy of simple-wireless.tcl click <A
href="http://www.isi.edu/nsnam/ns/tutorial/examples/simple-wireless.tcl">here</A>.
</P>
<P>A mobilenode consists of network components like Link Layer (LL), Interface
Queue (IfQ), MAC layer, the wireless channel nodes transmit and receive signals
from etc. For details about these network components see section 1 of chapter 15
of <A href="http://www.isi.edu/nsnam/ns/ns-documentation.html">ns Notes &
Documentation (now renamed as ns Manual)</A>. At the beginning of a wireless
simulation, we need to define the type for each of these network components.
Additionally, we need to define other parameters like the type of antenna, the
radio-propagation model, the type of ad-hoc routing protocol used by mobilenodes
etc. See comments in the code below for a brief description of each variable
defined. The array used to define these variables, val() is not global as it
used to be in the earlier wireless scripts. For details and available optional
values of these variables, see chapter 15 (mobile networking in ns) of <A
href="http://www.isi.edu/nsnam/ns/ns-documentation.html">ns documentation</A>.
We begin our script simple-wireless.tcl with a list of these different
parameters described above, as follows:
<TABLE cellPadding=5 bgColor=#eeeeee>
<TBODY>
<TR>
<TD><CODE><PRE># ======================================================================
# 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
set val(netif) Phy/WirelessPhy ;# network interface type
set val(mac) Mac/802_11 ;# MAC type
set val(rp) DSDV ;# ad-hoc routing protocol
set val(nn) 2 ;# number of mobilenodes
</PRE></CODE></TD></TR></TBODY></TABLE></P>
<P>Next we go to the main part of the program and start by creating an instance
of the simulator,
<TABLE cellPadding=5 bgColor=#eeeeee>
<TBODY>
<TR>
<TD><CODE><PRE>set ns_ [new Simulator]
</PRE></CODE></TD></TR></TBODY></TABLE>Then setup trace support by opening file
simple.tr and call the procedure trace-all {} as follows:
<TABLE cellPadding=5 bgColor=#eeeeee>
<TBODY>
<TR>
<TD><CODE><PRE>set tracefd [open simple.tr w]
$ns_ trace-all $tracefd
</PRE></CODE></TD></TR></TBODY></TABLE>Next create a topology object that keeps
track of movements of mobilenodes within the topological boundary.
<TABLE cellPadding=5 bgColor=#eeeeee>
<TBODY>
<TR>
<TD><CODE><PRE>set topo [new Topography]
</PRE></CODE></TD></TR></TBODY></TABLE>We had earlier mentioned that mobilenodes
move within a topology of 500mX500m. We provide the topography object with x and
y co-ordinates of the boundary, (x=500, y=500) :
<TABLE cellPadding=5 bgColor=#eeeeee>
<TBODY>
<TR>
<TD><CODE><PRE>$topo load_flatgrid 500 500
</PRE></CODE></TD></TR></TBODY></TABLE></P>
<P>The topography is broken up into grids and the default value of grid
resolution is 1. A diferent value can be passed as a third parameter to
load_flatgrid {} above.
<P>Next we create the object God, as follows:
<TABLE cellPadding=5 bgColor=#eeeeee>
<TBODY>
<TR>
<TD><CODE><PRE>create-god $val(nn)
</PRE></CODE></TD></TR></TBODY></TABLE>
<P>Quoted from CMU document on god, "God (General Operations Director) is the
object that is used to store global information about the state of the
environment, network or nodes that an omniscent observer would have, but that
should not be made known to any participant in the simulation." Currently, God
object stores the total number of mobilenodes and a table of shortest number of
hops required to reach from one node to another. The next hop information is
normally loaded into god object from movement pattern files, before simulation
begins, since calculating this on the fly during simulation runs can be quite
time consuming. However, in order to keep this example simple we avoid using
movement pattern files and thus do not provide God with next hop information.
The usage of movement pattern files and feeding of next hop info to God shall be
shown in the example in the next sub-section. </P>
<P>The procedure create-god is defined in ~ns/tcl/mobility/com.tcl, which allows
only a single global instance of the God object to be created during a
simulation. In addition to the evaluation functionalities, the God object is
called internally by MAC objects in mobilenodes. So even though we may not
utilise God for evaluation purposes,(as in this example) we still need to create
God. </P><A name=newAPI></A>
<P>Next, we create mobilenodes. The node creation APIs have been revised and
here we shall be using the new APIs to create mobilenodes. <BR>IMPORTANT NOTE:
The new APIs are not available with ns2.1b5 release. Download the daily snapshot
version if the next release (2.1b6 upwards) is not as yet available. </P>
<P>First, we need to configure nodes before we can create them. Node
configuration API may consist of defining the type of addressing
(flat/hierarchical etc), the type of adhoc routing protocol, Link Layer, MAC
layer, IfQ etc. The configuration API can be defined as follows:
<TABLE cellPadding=5 bgColor=#eeeeee>
<TBODY>
<TR>
<TD><CODE><PRE> (parameter examples)
# $ns_ node-config -addressingType flat or hierarchical or expanded
# -adhocRouting DSDV or DSR or TORA
# -llType LL
# -macType Mac/802_11
# -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)
# -rxPower (in W)
# -txPower (in W)
# -agentTrace ON or OFF
# -routerTrace ON or OFF
# -macTrace ON or OFF
# -movementTrace ON or OFF
</PRE></CODE></TD></TR></TBODY></TABLE>All default values for these options are
NULL except: <BR>addressingType: flat </P>
<P>We are going to use the default value of flat addressing; Also lets turn on
only AgentTrace and RouterTrace; You can experiment with the traces by turning
all of them on. AgentTraces are marked with AGT, RouterTrace with RTR and
MacTrace with MAC in their 5th fields. MovementTrace, when turned on, shows the
movement of the mobilenodes and the trace is marked with M in their 2nd field.
</P>
<P>The configuration API for creating mobilenodes looks as follows:
<TABLE cellPadding=5 bgColor=#eeeeee>
<TBODY>
<TR>
<TD><CODE><PRE># 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
</PRE></CODE></TD></TR></TBODY></TABLE></P>
<P>Next we create the 2 mobilenodes as follows:
<TABLE cellPadding=5 bgColor=#eeeeee>
<TBODY>
<TR>
<TD><CODE><PRE> for {set i 0} {$i < $val(nn) } {incr i} {
set node_($i) [$ns_ node ]
$node_($i) random-motion 0 ;# disable random motion
}
</PRE></CODE></TD></TR></TBODY></TABLE>The random-motion for nodes is disabled
here, as we are going to provide node position and movement(speed &
direction) directives next. </P>
<P>Now that we have created mobilenodes, we need to give them a position to
start with,
<TABLE cellPadding=5 bgColor=#eeeeee>
<TBODY>
<TR>
<TD><CODE><PRE>#
# 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
</PRE></CODE></TD></TR></TBODY></TABLE>Node0 has a starting position of (5,2)
while Node1 starts off at location (390,385). </P>
<P>Next produce some node movements,
<TABLE cellPadding=5 bgColor=#eeeeee>
<TBODY>
<TR>
<TD><CODE><PRE>#
# 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"
</PRE></CODE></TD></TR></TBODY></TABLE>$ns_ at 50.0 "$node_(1) setdest 25.0 20.0
15.0" means at time 50.0s, node1 starts to move towards the destination
(x=25,y=20) at a speed of 15m/s. This API is used to change direction and speed
of movement of the mobilenodes. </P>
<P>Next setup traffic flow between the two nodes as follows:
<TABLE cellPadding=5 bgColor=#eeeeee>
<TBODY>
<TR>
<TD><CODE><PRE># 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"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -