ns-2 간단한 예제2
노드의 구성은 아래와 같다.

이번 예제는 1번 예제에서 노드들만 추가된 것으로 소스는 아래에서 다운 받을 수 있다.
주의사항: 줄번호가 들어간 것은 설명의 편의상 넣은 것이므로 파일로 다운받기 바랍니다.
1 #Create a simulator object
2 set ns [new Simulator]
2 set ns [new Simulator]
3 #Define different colors for data flows
4 $ns color 1 Blue
5 $ns color 2 Red
4 $ns color 1 Blue
5 $ns color 2 Red
6 #Open the nam trace file
7 set nf [open out.nam w]
8 $ns namtrace-all $nf
7 set nf [open out.nam w]
8 $ns namtrace-all $nf
9 #Define a 'finish' procedure
10 proc finish {} {
11 global ns nf
12 $ns flush-trace
13 #Close the trace file
14 close $nf
15 #Execute nam on the trace file
16 exec nam out.nam &
17 exit 0
18 }
10 proc finish {} {
11 global ns nf
12 $ns flush-trace
13 #Close the trace file
14 close $nf
15 #Execute nam on the trace file
16 exec nam out.nam &
17 exit 0
18 }
19 #Create four nodes
20 set n0 [$ns node]
21 set n1 [$ns node]
22 set n2 [$ns node]
23 set n3 [$ns node]
20 set n0 [$ns node]
21 set n1 [$ns node]
22 set n2 [$ns node]
23 set n3 [$ns node]
24 #Create links between the nodes
25 $ns duplex-link $n0 $n2 1Mb 10ms DropTail
26 $ns duplex-link $n1 $n2 1Mb 10ms DropTail
27 $ns duplex-link $n3 $n2 1Mb 10ms DropTail
25 $ns duplex-link $n0 $n2 1Mb 10ms DropTail
26 $ns duplex-link $n1 $n2 1Mb 10ms DropTail
27 $ns duplex-link $n3 $n2 1Mb 10ms DropTail
28 $ns duplex-link-op $n0 $n2 orient right-down
29 $ns duplex-link-op $n1 $n2 orient right-up
30 $ns duplex-link-op $n2 $n3 orient right
29 $ns duplex-link-op $n1 $n2 orient right-up
30 $ns duplex-link-op $n2 $n3 orient right
31 #Monitor the queue for the link between node 2 and node 3
32 $ns duplex-link-op $n2 $n3 queuePos 0.5
32 $ns duplex-link-op $n2 $n3 queuePos 0.5
33 #Create a UDP agent and attach it to node n0
34 set udp0 [new Agent/UDP]
35 $udp0 set class_ 1
36 $ns attach-agent $n0 $udp0
34 set udp0 [new Agent/UDP]
35 $udp0 set class_ 1
36 $ns attach-agent $n0 $udp0
37 # Create a CBR traffic source and attach it to udp0
38 set cbr0 [new Application/Traffic/CBR]
39 $cbr0 set packetSize_ 500
40 $cbr0 set interval_ 0.005
41 $cbr0 attach-agent $udp0
38 set cbr0 [new Application/Traffic/CBR]
39 $cbr0 set packetSize_ 500
40 $cbr0 set interval_ 0.005
41 $cbr0 attach-agent $udp0
42 #Create a UDP agent and attach it to node n1
43 set udp1 [new Agent/UDP]
44 $udp1 set class_ 2
45 $ns attach-agent $n1 $udp1
43 set udp1 [new Agent/UDP]
44 $udp1 set class_ 2
45 $ns attach-agent $n1 $udp1
46 # Create a CBR traffic source and attach it to udp1
47 set cbr1 [new Application/Traffic/CBR]
48 $cbr1 set packetSize_ 500
49 $cbr1 set interval_ 0.005
50 $cbr1 attach-agent $udp1
47 set cbr1 [new Application/Traffic/CBR]
48 $cbr1 set packetSize_ 500
49 $cbr1 set interval_ 0.005
50 $cbr1 attach-agent $udp1
51 #Create a Null agent (a traffic sink) and attach it to node n3
52 set null0 [new Agent/Null]
53 $ns attach-agent $n3 $null0
52 set null0 [new Agent/Null]
53 $ns attach-agent $n3 $null0
54 #Connect the traffic sources with the traffic sink
55 $ns connect $udp0 $null0
56 $ns connect $udp1 $null0
55 $ns connect $udp0 $null0
56 $ns connect $udp1 $null0
57 #Schedule events for the CBR agents
58 $ns at 0.5 "$cbr0 start"
59 $ns at 1.0 "$cbr1 start"
60 $ns at 4.0 "$cbr1 stop"
61 $ns at 4.5 "$cbr0 stop"
62 #Call the finish procedure after 5 seconds of simulation time
63 $ns at 5.0 "finish"
58 $ns at 0.5 "$cbr0 start"
59 $ns at 1.0 "$cbr1 start"
60 $ns at 4.0 "$cbr1 stop"
61 $ns at 4.5 "$cbr0 stop"
62 #Call the finish procedure after 5 seconds of simulation time
63 $ns at 5.0 "finish"
64 #Run the simulation
65 $ns run
65 $ns run
4~5줄은 nam 에서의 색상을 지정한 것이다.
28~32줄은 nam 에서의 위치를 지정한 것이다.
그 밖의 토플로지를 구성하고, 연결을 설정하는 등의 예는 예제1과 같다.
한가지 특이한 사항은 시뮬레이션을 돌려보면 파랑색 패킷만 드롭이 되는데, 이는 DropTail 큐를 사용하였을 때의 문제점으로 SFQ를 사용하면 해결된다. DropTail 큐는 큐가 다 찼을 경우 마지막으로 들어오는 녀석을 드롭시키는데 여기서는 큐가 다찼을 경우 파랑색이 들어올 차례면 파랑색이 드롭된다. 이때 큐에서 하나의 패킷이 처리되어도 빨간색이 자리를 차리하게 되므로 결국 계속적으로 파랑색만 드롭되게 된다.
28~32줄은 nam 에서의 위치를 지정한 것이다.
그 밖의 토플로지를 구성하고, 연결을 설정하는 등의 예는 예제1과 같다.
한가지 특이한 사항은 시뮬레이션을 돌려보면 파랑색 패킷만 드롭이 되는데, 이는 DropTail 큐를 사용하였을 때의 문제점으로 SFQ를 사용하면 해결된다. DropTail 큐는 큐가 다 찼을 경우 마지막으로 들어오는 녀석을 드롭시키는데 여기서는 큐가 다찼을 경우 파랑색이 들어올 차례면 파랑색이 드롭된다. 이때 큐에서 하나의 패킷이 처리되어도 빨간색이 자리를 차리하게 되므로 결국 계속적으로 파랑색만 드롭되게 된다.
http://www.joon.pe.kr/blog/trackback/3