本文共 2767 字,大约阅读时间需要 9 分钟。
#Create a simulator objectset ns [new Simulator]#Define different colors for data flows (for NAM)$ns color 0 blue$ns color 1 red$ns color 2 white#Create four nodesset n0 [$ns node]set n1 [$ns node]set n2 [$ns node]set n3 [$ns node]# set trace file(预先定义)set f [open out.tr w]$ns trace-all $fset nf [open out.nam w] #Open the NAM trace file$ns namtrace-all $nf#Create links between the nodes (双向链接)$ns duplex-link $n0 $n2 5Mb 2ms DropTail$ns duplex-link $n1 $n2 5Mb 2ms DropTail$ns duplex-link $n2 $n3 1.5Mb 10ms DropTail
它有点类似于FIFO(先入先出)的存储方式。Drop Tail最大的优点是原理简单。
#Set Queue Size of link (n2-n3) to 10$ns queue-limit $n2 $n3 10 #设置n2与n3之间链路上队列缓冲区的最大值为10
#Give node position (for NAM)$ns duplex-link-op $n0 $n2 orient right-up # n0在n2的下方45°$ns duplex-link-op $n1 $n2 orient right-down # n1在n2的上方45°$ns duplex-link-op $n2 $n3 orient right # n2的右边是n3#队列位置定义了队列与水平的夹角(水平线以下度数为正)#right-up 45#right-down -45#right 0 右
#Monitor the queue for link (n2-n3). (for NAM)$ns duplex-link-op $n2 $n3 queuePos 0.5#duplex-link-op 设置双工链路属性
1. **queuePos 0.5表示包从上到下进入队列** (上图即为从上到下) 2. queuePos 0表示包从右到左进入队列 3. queuePos 1表示包从左到右进入队列 4. queuePos 1.5表示包从下到上进入队列 5. queuePos 2==queuePos 0
因此queuePos N表示包进入队列时的角度,角度 =(N×π) % 2π
set udp0 [new Agent/UDP]$ns attach-agent $n0 $udp0set cbr0 [new Application/Traffic/CBR]$cbr0 attach-agent $udp0set udp1 [new Agent/UDP]$ns attach-agent $n3 $udp1$udp1 set class_ 1 #udp1的流量是红色set cbr1 [new Application/Traffic/CBR]$cbr1 attach-agent $udp1set null0 [new Agent/Null] #这是接收器(sink)$ns attach-agent $n3 $null0set null1 [new Agent/Null] #也是接收器(sink)$ns attach-agent $n1 $null1$ns connect $udp0 $null0$ns connect $udp1 $null1$ns at 1.0 "$cbr0 start"$ns at 1.1 "$cbr1 start"
time=1.0
time=1.14
#Setup a TCP connectionset tcp [new Agent/TCP] #创建一个tcp代理$tcp set class_ 2 #设置tcp流的颜色为白色(n0)set sink [new Agent/TCPSink] #Sink:接收器(n3)$ns attach-agent $n0 $tcp$ns attach-agent $n3 $sink$ns connect $tcp $sinkset ftp [new Application/FTP]$ftp attach-agent $tcp$ns at 1.2 "$ftp start"
t=1.23
$ns at 1.35 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"#把节点和代理分离开,把代理重置为空代理puts [$cbr0 set packetSize_] #在命令行打印cbr0产生包恒定的大小puts [$cbr0 set interval_] #在命令行打印cbr0包之间的间隔#这个模拟程序会执行3s$ns at 3.0 "finish"#Define a 'finish' procedureproc finish {} { global ns f nf $ns flush-trace close $f close $nf #Close the NAM trace file puts "running nam..." exec nam out.nam & #Execute NAM on the trace file exit 0}#即关闭所有nam文件,将out.nam以可视化方式实现
转载于:https://blog.51cto.com/12059878/2063978