ns2 - TCP OVER NOISY LINKS AND QUEUE MONITORING

|
#Create the simulator instance
set ns [new Simulator]
$ns color 1 Blue
$ns color 2 Red

#open the NAM trace file
set nf [open out.nam w]
$ns namtrace-all $nf

#open the Trace file
set tf [open out.tr w]
set windowVsTime2 [open WindowVsTimeNReno w]
$ns trace-all $tf

#Define a 'finish' procedure
proc finish {} {
    global ns nf tf
    $ns flush-trace
    close $nf
    close $tf
    exec nam out.nam &
    exit 0
}

#create four nodes
set n0 [$ns node]
set n1 [$ns node]
set n2 [$ns node]
set n3 [$ns node]

$ns at 0.1 "$n1 label \"CBR\""
$ns at 0.1 "$n0 label \"FTP\""
# 노드 구분을 하기 위한 표식
#create links beween the nodes
$ns duplex-link $n0 $n2 2Mb 10ms DropTail
$ns duplex-link $n1 $n2 2Mb 10ms DropTail
$ns simplex-link $n2 $n3 0.07Mb 20ms DropTail
$ns simplex-link $n3 $n2 0.07Mb 20ms DropTail


#set Queue size of link (n2-n3) to 10
$ns queue-limit $n2 $n3 10   #node n2 와 n3 사이의 큐 사이즈

#monitor the queue for link (n2-n3). (for NAM)
$ns simplex-link-op $n2 $n3 queuePos 0.5
#nam 에서 큐가 쌓이는 각도를 설정 할수 있습니다. 0~2 값
#set error model on link n3 to n2
set loss_module [new ErrorModel]
$loss_module set rate_ 0.2
$loss_module ranvar [new RandomVariable/Uniform]
$loss_module drop-target [new Agent/Null]
$ns lossmodel $loss_module $n2 $n3
#손실률을 설정합니다.
#setup a TCP connection
set tcp [new Agent/TCP/Newreno]
$ns attach-agent $n0 $tcp
set sink [new Agent/TCPSink/DelAck]
$ns attach-agent $n3 $sink
$ns connect $tcp $sink
$tcp set fid_ 1

#setup a FTP over TCP connection
set ftp [new Application/FTP]
$ftp attach-agent $tcp
$ftp set type_ FTP

#setup a udp connection
set udp [new Agent/UDP]
$ns attach-agent $n1 $udp
set null [new Agent/Null]
$ns attach-agent $n3 $null
$ns connect $udp $null
$udp set fid_ 2

#setup a CBR over UDP connection
set cbr [new Application/Traffic/CBR]
$cbr attach-agent $udp
$cbr set type_ CBR
$cbr set packetSize_ 1000
$cbr set rate_ 0.01Mb
$cbr set random_ false

#Schedule events for the CBR and FTP agents
$ns at 0.1 "$cbr start"
$ns at 1.0 "$ftp start"
$ns at 624.0 "$ftp stop"
$ns at 624.5 "$cbr stop"

#printing the window size
proc plotWindow {tcpSource file} {
global ns
set time 0.01
set now [$ns now]
set cwnd [$tcpSource set cwnd_]
puts $file "$now $cwnd"
$ns at [expr $now+$time] "plotWindow $tcpSource $file"
}
$ns at 1.1 "plotWindow $tcp $windowVsTime2"

#sample the bottlenect queue every 0.1 sec. store the trace in qm.out
set qmon [$ns monitor-queue $n2 $n3 [open qm.out w] 0.1];
#monitor-queue는 4개의 속성을 가집니다. 첫번째와 두번째는 시작과 끝 노드, 세번째는#  output  trace file, 네번째는 빈도를 의미 합니다.
[$ns link $n2 $n3] queue-sample-timeout; # [$ns link $n2 $n3] start-tracing

#detach tcp and sink agents (now really necessary)
$ns at 624.5 "$ns detach-agent $n0 $tcp ; $ns detach-agent $n3 $sink"
# 노드에 연결했던 tcp와 sink를 분리합니다.
$ns at 625.0 "finish"
$ns run
사용자 삽입 이미지
                                               xls 자료
And