⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nsscript3.htm

📁 介绍了网络仿真软件NS2的使用
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0051)http://www.isi.edu/nsnam/ns/tutorial/nsscript3.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>VI. Network dynamics</H1>
<P>[<A href="http://www.isi.edu/nsnam/ns/tutorial/nsscript2.html">Previous 
section</A>] [<A href="http://www.isi.edu/nsnam/ns/tutorial/nsnew.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 I am going to show you an example for a dynamic network where 
the routing adjusts to a link failure. On the way there I'll show you how you 
can keep a larger number of nodes in a Tcl array instead of giving each node its 
own name. </P>
<HR>
<A name=first>
<P><STRONG>VI.1. Creating a larger topology</STRONG><BR>I suggest you call the 
Tcl script for this example 'example3.tcl'. You can already insert the <A 
href="http://www.isi.edu/nsnam/ns/tutorial/examples/template.tcl">template</A> 
from <A href="http://www.isi.edu/nsnam/ns/tutorial/nsscript1.tcl">section 
IV.1</A> into the file. </P>
<P>As always, the topology has to be created first, though this time we take a 
different approach which you will find more comfortable when you want to create 
larger topologies. The following code creates seven nodes and stores them in the 
array n(). 
<TABLE cellPadding=5 bgColor=#eeeeee>
  <TBODY>
  <TR>
    <TD><CODE><PRE>
for {set i 0} {$i &lt; 7} {incr i} {
  set n($i) [$ns node]
}
</PRE></CODE></TD></TR></TBODY></TABLE>You have certainly seen 'for' loops in 
other programming languages before, and I am sure you understand the structure 
at once. Note that arrays, just like other variables in Tcl, don't have to be 
declared first. </P>
<P>Now we're going to connect the nodes to create a circular topology. The 
following piece of code might look a bit more complicated at first. 
<TABLE cellPadding=5 bgColor=#eeeeee>
  <TBODY>
  <TR>
    <TD><CODE><PRE>
for {set i 0} {$i &lt; 7} {incr i} {
  $ns duplex-link $n($i) $n([expr ($i+1)%7]) 1Mb 10ms DropTail
}
</PRE></CODE></TD></TR></TBODY></TABLE>This 'for' loop connects all nodes with 
the next node in the array with the exception of the last node, which is being 
connected with the first node. To accomplish that, I used the '%' (modulo) 
operator. </P>
<P>When you run the script now, the topology might look a bit strange in nam at 
first, but after you hit the 're-layout' button it should look like the picture 
below. </P>
<P><IMG height=384 alt="Nam snap shot" src="nsscript3.files/namss8.gif" 
width=425> </P>
<HR>
<A name=second>
<P><STRONG>VI.2. Link failure</STRONG><BR>The next step is to send some data 
from node n(0) to node n(3). 
<TABLE cellPadding=5 bgColor=#eeeeee>
  <TBODY>
  <TR>
    <TD><CODE><PRE>#Create a UDP agent and attach it to node n(0)
set udp0 [new Agent/UDP]
$ns attach-agent $n(0) $udp0

# Create a CBR traffic source and attach it to udp0
set cbr0 [new Application/Traffic/CBR]
$cbr0 set packetSize_ 500
$cbr0 set interval_ 0.005
$cbr0 attach-agent $udp0

set null0 [new Agent/Null]
$ns attach-agent $n(3) $null0

$ns connect $udp0 $null0

$ns at 0.5 "$cbr0 start"
$ns at 4.5 "$cbr0 stop"
</PRE></CODE></TD></TR></TBODY></TABLE>The code above should look familiar to 
you by now. The only difference to the last sections is that now we have to use 
the node array elements. </P>
<P>If you start the script, you will see that the traffic takes the shortest 
path from node 0 to node 3 through nodes 1 and 2, as could be expected. Now we 
add another interesting feature. We let the link between node 1 and 2 (which is 
being used by the traffic) go down for a second. 
<TABLE cellPadding=5 bgColor=#eeeeee>
  <TBODY>
  <TR>
    <TD><CODE><PRE>
$ns rtmodel-at 1.0 down $n(1) $n(2)
$ns rtmodel-at 2.0 up $n(1) $n(2)
</PRE></CODE></TD></TR></TBODY></TABLE>It is probably not too hard to understand 
these two lines. Now you can start the script again and you will see that 
between the seconds 1.0 and 2.0 the link will be down, and all data that is sent 
from node 0 is lost. </P>
<P><IMG height=385 alt="Nam snap shot" src="nsscript3.files/namss9.gif" 
width=425> </P>
<P>Now I will show you how to use dynamic routing to solve that 'problem'. Add 
the following line at the beginning of your Tcl script, after the simulator 
object has been created. 
<TABLE cellPadding=5 bgColor=#eeeeee>
  <TBODY>
  <TR>
    <TD><CODE><PRE>
$ns rtproto DV
</PRE></CODE></TD></TR></TBODY></TABLE></P>
<P>Start the simulation again, and you will see how at first a lot of small 
packets run through the network. If you slow nam down enough to click on one of 
them, you will see that they are 'rtProtoDV' packets which are being used to 
exchange routing information between the nodes. When the link goes down again at 
1.0 seconds, the routing will be updated and the traffic will be re-routed 
through the nodes 6, 5 and 4. </P>
<P><IMG height=385 alt="Nam snap shot" src="nsscript3.files/namss10.gif" 
width=425> </P>
<P>You can download the full example <A 
href="http://www.isi.edu/nsnam/ns/tutorial/examples/example3.tcl">here</A>. </P>
<HR>

<P>[<A href="http://www.isi.edu/nsnam/ns/tutorial/nsscript2.html">Previous 
section</A>] [<A href="http://www.isi.edu/nsnam/ns/tutorial/nsnew.html">Next 
section</A>] [<A href="http://www.isi.edu/nsnam/ns/tutorial/nsindex.html">Back 
to the index</A>] </P>ns-users <BR>
<ADDRESS><A 
href="mailto:ns-users@isi.edu">ns-users@isi.edu</A></ADDRESS></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -