[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Học NS2



chào tất cả các bạn , hiện tại mình đang mô phỏng bên NS2
mình có đoạn code này , khi mình cho chạy file.tcl thì nó ko hiển thị file nam được. mong các bạn xem và giúp mình với nhé.
nó có chạy ra 2 file là xgraph rồi các bạn ạ 
#--------- So do ket noi mang  ----------------------------------------------#
#       n0                             
#         \   Bottleneck               Dich
#    n1--R0-----------------------R1 
#         /                            
#       n2                         
#

# Cac luong xcp va tcp dung chung router xcp
Queue/XCP set tcp_xcp_on_ 1
Agent/TCP set minrto_ 1

# Tao doi tuong mo phong
set ns          [new Simulator]
$ns             use-scheduler Heap

# Tao file trace
set f_all [open out.tr w]
$ns trace-all $f_all

#-----------------------------------------------------------------------------
#	Dinh nghia cac ham su dung trong chuong trinh chinh
#-----------------------------------------------------------------------------

# Tao tham so cho hang RED
proc set-red-params { qsize } {
	Queue/RED set thresh_ [expr 0.6 * $qsize]
	Queue/RED set maxthresh_ [expr 0.8 * $qsize]
	Queue/RED set q_weight_ 0.001
	Queue/RED set linterm_ 10
	Queue/RED set bytes_ false ;
	Queue/RED set queue_in_bytes_ false ;
	Agent/TCP set old_ecn_ true
	Queue/RED set setbit_ true
}
#----------------------------------------------------------------------------
# Tao topo mang
proc create-topology2 { BW delay qtype qsize numSideLinks deltaDelay } {
    global ns
 
    # Thiet lap kich co hang den kich co duong truyen 
    # Gia su rang kich co goi la 1000Bytes
    if { $qsize == 0 } { set qsize [expr round([expr ($BW / 8) * 2 * $delay])] }
    
    #Khai bao 2 nut R0 R1
    set i 0
    while { $i < 2 } {
	global R$i
	set R$i [$ns node]
	incr i
   }
  	$R0 color red
	$R1 color blue
	$R0 label "That co chai"
	$R1 label "Dich"

    # Khai bao link:   nodes           bw            delay          queue
    $ns duplex-link $R0 $R1 [set BW]Mb [set delay]ms $qtype
    $ns queue-limit $R0 $R1 $qsize
    $ns queue-limit $R1 $R0 $qsize

# Xu li hang bi that co chai, cho phep thiet lap cac tham so RED
    global Bottleneck rBottleneck
    set  Bottleneck  [[$ns link $R0 $R1] queue] 
    set  rBottleneck [[$ns link $R1 $R0] queue]
    global l rl
    set  l  [$ns link $R0 $R1]  
    set  rl [$ns link $R1 $R0]
    global all_links 
    set all_links "$l $rl "

# Duong truyen nha'nh co cung bang thong nhu duong that co chai
    set i 0
    while { $i < $numSideLinks } {
	global n$i q$i rq$i l$i rl$i
	set n$i  [$ns node]
	$ns duplex-link [set n$i]  $R0  [set BW]Mb [expr $delay + $i* $deltaDelay]ms $qtype
	$ns queue-limit [set n$i]  $R0  $qsize
	$ns queue-limit $R0 [set n$i]   $qsize
	set  q$i   [[$ns link [set n$i]  $R0] queue] 
	set  rq$i  [[$ns link $R0 [set n$i]] queue]
	set  l$i    [$ns link [set n$i]  $R0] 
	set  rl$i   [$ns link $R0 [set n$i]]
	set all_links "$all_links [set l$i] [set rl$i]"
	incr i
    }
}
#------------------------------------------------------------------------------------------

#------Lop GeneralSender: can thiet cho phia goi ftp------------------------------

Class GeneralSender  -superclass Agent 

GeneralSender instproc init { id node rcvrTCP otherparams } {
    global  ns
    $self next
    $self instvar tcp_ id_ ftp_ node_ tcp_rcvr_ tcp_type_
    set id_ $id
    set node_ $node
    if { [llength $otherparams] > 1 } {
	set TCP [lindex $otherparams 1]
    } else { 
	set TCP "TCP/Reno"
    }
    set   tcp_type_ $TCP
    set   tcp_ [new Agent/$TCP]
    $tcp_ set  packetSize_ 1000
    $tcp_ set  class_  $id
    set   ftp_ [new Source/FTP]
    $ftp_ set agent_ $tcp_
    $ns   attach-agent $node $tcp_
    $ns   connect $tcp_  $rcvrTCP
    set   tcp_rcvr_ $rcvrTCP
    set   startTime [lindex $otherparams 0]
    $ns   at $startTime "$ftp_ start"
    puts " Khoi tao phia goi $id_ tai thoi gian t= $startTime s" 
}

GeneralSender instproc trace-xcp parameters {
    $self instvar tcp_ id_ tcpTrace_
    global ftracetcp$id_ ns
    set ftracetcp$id_ [open  xcp$id_.tr  w]
    set tcpTrace_ [set ftracetcp$id_]
    $tcp_ attach-trace [set ftracetcp$id_]
    if { -1 < [lsearch $parameters cwnd]  } { $tcp_ tracevar cwnd_ }
    }

#-----------------------------------------------------------------------------------------

#------------------------Thuc hien chuc nang ve do thi----------------------#
# do thi bien trace xcp

proc plot-xcp { TraceName nXCPs  PlotTime what } {
    
	exec rm -f xgraph_cwnd.tcp
	set f [open xgraph_cwnd.tcp w]
	set a cwnd
   
    puts $f "TitleText: $TraceName"
    puts $f "Device: Postscript"
    foreach i $nXCPs {

# trace TCP duoc flushed khi cac nguon ngung 
	exec rm -f temp.tcp 
           exec touch temp.tcp
	global ftracetcp$i 
	if [info exists ftracetcp$i] { flush [set ftracetcp$i] }
        set result [exec awk -v PlotTime=$PlotTime -v what=$what {
	    {
		if (( $6 == what ) && ($1 > PlotTime)) {
		    print $1, $7 >> "temp.tcp";
		}
	    }
	} xcp$i.tr]
	puts $f \"$what$i 
	exec cat temp.tcp >@ $f
	puts $f "\n"
	flush $f
   }
    close $f

    exec xgraph -P -bg white -fg black -zg black -x "Thoi gian (s)" -y "cwnd (KBytes)" xgraph_$a.tcp &
    return 
}
#------------------------------------------------------------------------------------

proc plot-xcp-queue { PlotTime traceFile } {
    
    exec rm -f xcp_queue
    exec rm -f temp.q temp.a temp.p temp.avg_enqueued temp.avg_dequeued temp.x temp.y 
    exec touch temp.q temp.a temp.p temp.avg_enqueued temp.avg_dequeued temp.x temp.y 
    exec awk -v PT=$PlotTime {
	{
	    if (($1 == "q" && NF>2) && ($2 > PT)) {
		print $2, $3 >> "temp.q"
	    }
	    else if (($1 == "a" && NF>2) && ($2 > PT)) {
		print $2, $3 >> "temp.a"
	    }
	    else if (($1 == "p" && NF>2) && ($2 > PT)) {
		print $2, $3 >> "temp.p"
	    }
	}
    }  $traceFile
    set ff [open xcp_queue w]
    puts $ff "TitleText: Trang thai hang DROPTAIL XCP"
    puts $ff "Device: Postscript \n"
    puts $ff \"queue
    exec cat temp.q >@ $ff  
    puts $ff \n\"ave_queue
    exec cat temp.a >@ $ff
    puts $ff \n\"prob_drop
    exec cat temp.p >@ $ff
    close $ff
    exec xgraph  -P -bg white -fg black -zg black -x "Thoi gian (s)" -y "Queue (goi)" xcp_queue &
}
#-------------------------------------------------------------------------------------

proc plot-red-queue { PlotTime traceFile } {
    exec rm -f red_queue
    exec rm -f temp.q temp.a temp.p temp.avg_enqueued temp.avg_dequeued temp.x temp.y 
    exec touch temp.q temp.a temp.p temp.avg_enqueued temp.avg_dequeued temp.x temp.y 
    exec awk -v PT=$PlotTime {
	{
	    if (($1 == "Q" && NF>2) && ($2 > PT)) {
		print $2, $3 >> "temp.q"
	    }
	    else if (($1 == "a" && NF>2) && ($2 > PT)) {
		print $2, $3 >> "temp.a"
	    }
	    else if (($1 == "p" && NF>2) && ($2 > PT)) {
		print $2, $3 >> "temp.p"
	    }
	}
    }  $traceFile
    set ff [open red_queue w]
    puts $ff "TitleText: Trang thai hang RED TCP"
    puts $ff "Device: Postscript \n"
    puts $ff \"queue
    exec cat temp.q >@ $ff  
    puts $ff \n\"ave_queue
    exec cat temp.a >@ $ff
    puts $ff \n\"prob_drop
    exec cat temp.p >@ $ff
    close $ff
    exec xgraph  -P -bg white -fg black -zg black -x "Thoi gian (s)" -y "Queue (goi)" red_queue &
}
#---------------------------------------------------------------------------------------
proc plot-red {varname filename PlotTime} {
    exec rm -f $filename
    exec touch $filename
    
    set result [exec awk -v PlotTime=$PlotTime -v what=$varname -v file=$filename {
	{
	    if (( $1 == what ) && ($2 > PlotTime)) {
		print $2, $3 >> file ;
	    }  
	}
    } ft_red_Bottleneck.tr]

    exec xgraph -P -bg white -fg black -zg black -x "Thoi gian (s)" -y "Hieu qua su dung" -t "Hieu qua su dung router XCP khi co 1 luong TCP" $filename
}

#-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- MO PHONG -*-*-*-*-*-*-*-*--*-*-*-*-*
set seed   472904
set qType  XCP
set BW     20; #  Mb/s
set nXCPs  3; # so luong
set delay  10; # ms
set rtg [new RNG]
$rtg seed       $seed

# thiet lap bo dem den kich co duong dan
set  qSize  [expr round([expr ($BW / 8.0) * 4 * $delay * 1.0])];

puts "\n CAC THONG SO MO PHONG\n"
puts " Bang thong    = $BW Mb/s\n"
puts " Do tre            = $delay ms\n"
puts " Kich co hang = $qSize KBytes\n"

set tracedXCPs       "0 1 2 3 "
set SimStopTime      30
set PlotTime         0

#------------------------------Khoi tao mo phong----------------------------------#
# Goi ham tao topo
set-red-params $qSize
create-topology2 $BW $delay $qType $qSize $nXCPs 0.0
foreach link $all_links {
    set queue [$link queue]
    switch $qType {
	"XCP" {
		$queue set-link-capacity [[$link set link_] set bandwidth_];
	}
	"DropTail/XCP" {
		$queue set-link-capacity-Kbytes [expr [[$link set link_] set bandwidth_] / 8000];
	}
	    default {
		    puts "Incorrect qType $qType"
		    exit 0
	    }
    }
}

# Tao cac nguon:
# Tao 3 nguon XCP
set i 0
while { $i < $nXCPs  } {
    set StartTime [expr [$rtg integer 1000] * 0.001 * (0.01 * $delay) + $i  * 0.0] 
    set rcvr_XCP      [new Agent/TCPSink/XCPSink]
    $ns attach-agent  $R1 $rcvr_XCP
    set src$i         [new GeneralSender $i [set n$i] $rcvr_XCP "$StartTime TCP/Reno/XCP"]
	[[set src$i] set tcp_]  set  packetSize_ 1000
	[[set src$i] set tcp_]  set  window_     [expr $qSize * 10]
	$ns color $i Blue
	incr i
}
# Tao 1 nguon TCP tu node 0
set StartTime 0.0900000000000000023
set rcvr_TCP [new Agent/TCPSink]
$ns attach-agent $R1 $rcvr_TCP
set src$i [new GeneralSender $i $n0 $rcvr_TCP "$StartTime TCP/Reno"]
[[set src$i] set tcp_]  set  packetSize_ [expr 100 * ($i +10)]  # don vi Byte
[[set src$i] set tcp_]  set  window_     [expr $qSize * 10]	#  KByte
$ns color $i Red
incr i

#------------------------------------- Trace --------------------------------------------#
set i 0

# Trace cac nguon
foreach i $tracedXCPs {
	[set src$i] trace-xcp "cwnd"
}

# Trace cac hang
foreach queue_name "Bottleneck rBottleneck" {
	set queue [set "$queue_name"]
	switch $qType {
		"XCP" {
			global "ft_red_$queue_name"
			global "tcp_$queue_name"
			set "ft_red_$queue_name" [open ft_red_[set queue_name].tr w]
			$queue attach [set ft_red_$queue_name]
			set "tcp_$queue_name" [open tcp.tr w]
			set tcpq [$queue set vq_(1)]
			$tcpq attach [set tcp_$queue_name]
			$tcpq trace curq_
			$tcpq trace ave_
			$tcpq trace prob1_
			
		}
		"DropTail/XCP" {}
	}
}
#------------------------ Chay mo phong----------------------------------#

proc flush-files {} {
    set files "f_all ft_red_Bottleneck ft_red_rBottleneck"
    global tracedXCPs
    foreach file $files {
        global $file
        if {[info exists $file]} {
            flush [set $file]
            close [set $file]
        }   
    }
    foreach i $tracedXCPs {
	global src$i
	    set file [set src$i tcpTrace_]
	if {[info exists $file]} {
           flush [set $file]
           close [set $file]
        }   
    }
}
proc finish {} {
    global ns 
    if {[info exists f]} {
	$ns flush-trace
	close $f
    } 
   	$ns halt
}

$ns at $SimStopTime "finish"
$ns run
flush-files

#-------------- Goi cac ham ve do thi------------------------#
set PostProcess 1
if { $PostProcess } {
	set TraceName "XCP va TCP cung ton tai"
	plot-xcp $TraceName  $tracedXCPs  0.0  "cwnd_"
	plot-xcp-queue  $PlotTime   ft_red_Bottleneck.tr
	plot-red-queue  $PlotTime   tcp.tr
	plot-red "u" Hieuqua 0.0
}