Sunteți pe pagina 1din 15

################# ## DEFINITIONS ## ################# ################# # imports vlan.tcl # puts "sourcing vlan.tcl..." # source vlan.

tcl ################################# # VARIOUS VARIABLES AND CONSTANTS ################################# # Standard .tr/.nam file definitions set opt(tr) out_STUD-NAME.tr

set opt(namtr) "STUD-NAME.nam" # Enviroment settings set bandw 10 set paqsize 500 # Sampling interval set samplint 0.1 # Usage requirement settings set Filesize 2048000 set Querysize 100000 set sesionD 180 set Recmail 50 set Sendmail 30 set Prints 25 set Queries 120 set Files_Ses 75 set Dfiles_ses 40 # Stop time set opt(stoptime) ################### 1.2

# LAN parameters ################### # Number of workstation in LAN set opt(wkst) 20

# Delays on LAN and File-, Print-, Mail-server links set opt(landelay) set opt(Fdelay) 2ms set opt(Pdelay) 2ms set opt(Mdelay) 2ms # Other LAN parameters set opt(ll) set opt(ifq) set opt(mac) set opt(chan) set opt(bw) LL Queue/DropTail Mac/802_3 Channel "[expr $bandw]Mb" 1ms

###################### # PROCEDURES ######################

# # PROCEDURE flushing trace and invoking nam # proc finish {} { global ns opt $ns flush-trace exec nam $opt(namtr) & exit 0 } # # PROCEDURE creating trace files #

proc create-trace {} { global ns opt if [file exists $opt(tr)] { catch "exec rm -f $opt(tr) $opt(tr)-bw [glob $opt(tr).*]" } set trfd [open $opt(tr) w] $ns trace-all $trfd if {$opt(namtr) != ""} { $ns namtrace-all [open $opt(namtr) w] } return $trfd }

# # PROCEDURE for closing output files with recorded sequences and calling Xgraph # proc closegraph {} { global f file # # Close the output files # for {set i 1} {$i < 5} {incr i} { close $f($i) } # # Call xgraph to display the results of various recorded values # such as Bytes, CH UTIL, Dropped packets, Sent Frames # for {set i 1} {$i < 5} {incr i} { exec xgraph $file($i) -geometry 800x400 & }

return 0 exit 0 } # # PROCEDURE for creating topology # proc create-topology {} { global ns opt global lan lannode fileserv printserv mailserv # # Create LAN workstations # set num $opt(wkst) for {set i 0} {$i < $num} {incr i} { set lannode($i) [$ns node] lappend nodelist $lannode($i) } set lan [$ns make-lan $nodelist $opt(bw) \ $opt(landelay) $opt(ll) $opt(ifq) $opt(mac) $opt(chan)] ####################### # Create Server Nodes # ####################### # # File Server and link C1/C2 # set fileserv [$ns node] $fileserv shape box $fileserv color red $fileserv label "File Server" $ns duplex-link $fileserv $lannode(1) $opt(bw) $opt(Fdelay) DropTail $ns duplex-link-op $fileserv $lannode(1) orient right

#$ns duplex-link-op $fileserv $lannode(1) color "green" $ns duplex-link-op $fileserv $lannode(1) label "C1 & C2" # # Printer Server and link C3 # set printserv [$ns node] $printserv shape box $printserv color green $printserv label "Printer Server" $ns duplex-link $printserv $lannode(2) $opt(bw) $opt(Pdelay) DropTail $ns duplex-link-op $printserv $lannode(2) orient down #$ns duplex-link-op $printserv $lannode(2) color "green" $ns duplex-link-op $printserv $lannode(2) label "C3" # # Mail Server and link C4/C5 # set mailserv [$ns node] $mailserv shape box $mailserv color blue $mailserv label "Mail Server" $ns duplex-link $mailserv $lannode(8) $opt(bw) $opt(Mdelay) DropTail $ns duplex-link-op $mailserv $lannode(8) orient down #$ns duplex-link-op $mailserv $lannode(8) color "green" $ns duplex-link-op $mailserv $lannode(8) label "C4 & C5" # # Just a label to draw in nam for all LAN workstation $lannode(5) label "Workstations" } # # PROCEDURE attaching CBR traffic between nodes "nodex" and "sink" which # is generated with interval "interv" and agent's class "agntcls"

# proc attach-traffic {nodex sink interv agntcls} { global paqsize # # Get an instance of the simulator # set ns [Simulator instance] # # Create an UDP transport agent named "source" and attach it to the node named "nodex" # set udp [new Agent/UDP] $ns attach-agent $nodex $udp # # Create CBR traffic source # set cbr [new Application/Traffic/CBR] $cbr set packetSize_ $paqsize $cbr set interval_ $interv $udp set class_ $agntcls # # Attach traffic source to the traffic generator # $cbr attach-agent $udp # # Connect the source and the sink of transport protocol # $ns connect $udp $sink return $cbr } # # PROCEDURES for calculation of interval parameters

# proc WSF { } { global Querysize sesionD Queries paqsize Dfiles_ses Filesize set W [expr $Queries*$Querysize\$sesionD] set X [expr $Dfiles_ses*$Filesize\$sesionD] set A [expr $X + $W] return [expr $paqsize/($A/60)] } proc FW { } { global Filesize Files_Ses sesionD paqsize set W [expr $Files_Ses*$Filesize/$sesionD] return [expr $paqsize/($W/60)] }

proc WSP { } { global Filesize sesionD Prints paqsize set W [expr $Prints*$Filesize/$sesionD] return [expr $paqsize/($W/60)] } proc WSM { } { global Filesize sesionD Sendmail paqsize set W [expr $Sendmail*$Filesize/$sesionD] return [expr $paqsize/($W/60)] } proc MW { } { global Filesize sesionD Recmail paqsize set W [expr $Recmail*$Filesize/$sesionD] return [expr $paqsize/($W/60)] } # # PROCEDURES for calculation results

# # # Calculates KBytes transmited by adding Bytes of lost Frames and received bytes # proc Bytestrans { bytes packl } { global paqsize return [expr ($bytes+($packl*$paqsize/1000))] } # # Calculates Frames transmited by using lost and received packets # proc framestrans { packrec packl } { return [expr ($packrec+$packl)] } # # Calculates drop rates (in %) transmited by using lost and received packets # proc DropRate { packrec packl } { set temp [expr $packrec+$packl] #Avoid Div/0 if { $temp > 0 } { return [expr $packl*100/$temp] } return 0 } # # Calculates Chanel Utilization in % # proc ChanelUtil { bytes packl } { global bandw paqsize samplint set temp [expr ($bytes+($packl * $paqsize/1000))]

set ability [expr $bandw*1000*$samplint/8] return [expr ($temp * 100) / $ability] } # # PROCEDURE for periodical recording sink values and calculation # the requested info # proc record {} { global Fsink Msink FS_sink PS_sink MS_sink f samplint opt global T_bw T_lp T_rp # # Get an instance of the simulator # set ns [Simulator instance] # # How many bytes, and packets have been received by the traffic sinks? # How many packets have lost? # for {set i 0} {$i < $opt(wkst)} {incr i} { # For File server set Fbw($i) [$Fsink($i) set bytes_] set Flp($i) [$Fsink($i) set nlost_] set Frp($i) [$Fsink($i) set npkts_]

# For Mail server set Mbw($i) [$Msink($i) set bytes_] set Mlp($i) [$Msink($i) set nlost_] set Mrp($i) [$Msink($i) set npkts_]

# For File server set FS_bw($i) [$FS_sink($i) set bytes_]

set FS_lp($i) [$FS_sink($i) set nlost_] set FS_rp($i) [$FS_sink($i) set npkts_]

# For Printer server set PS_bw($i) [$PS_sink($i) set bytes_] set PS_lp($i) [$PS_sink($i) set nlost_] set PS_rp($i) [$PS_sink($i) set npkts_]

# For Mail server set MS_bw($i) [$MS_sink($i) set bytes_] set MS_lp($i) [$MS_sink($i) set nlost_] set MS_rp($i) [$MS_sink($i) set npkts_] } # # Get the current time # set now [$ns now] # # Sum sinks # set T_bw 0 set T_lp 0 set T_rp 0 for {set i 0} {$i < $opt(wkst)} {incr i} { set temp_bw [expr $Fbw($i) + $Mbw($i) + $FS_bw($i) + $PS_bw($i) + $MS_bw($i)] set T_bw [expr $T_bw + ($temp_bw/1000)] set temp_lp [expr $Flp($i) + $Mlp($i) + $FS_lp($i) + $PS_lp($i) + $MS_lp($i)] set T_lp [expr $T_lp + $temp_lp] set temp_rp [expr $Frp($i) + $Mrp($i) + $FS_rp($i) + $PS_rp($i) + $MS_rp($i)] set T_rp [expr $T_rp + ($temp_rp)] }

# # Record Bytes/Sec transm at given time # puts $f(1) "$now [Bytestrans $T_bw $temp_lp]" puts $f(2) "$now [framestrans $T_rp $T_lp]" puts $f(3) "$now [DropRate $T_rp $T_lp]" puts $f(4) "$now [ChanelUtil $T_bw $T_lp]" # # Reset the bytes_ values and packet values on the traffic sinks # for {set i 0} {$i < $opt(wkst)} {incr i} { $Fsink($i) set bytes_ 0 $Fsink($i) set nlost_ 0 $Fsink($i) set npkts_ 0 $Msink($i) set bytes_ 0 $Msink($i) set nlost_ 0 $Msink($i) set npkts_ 0 $FS_sink($i) set bytes_ 0 $FS_sink($i) set nlost_ 0 $FS_sink($i) set npkts_ 0 $PS_sink($i) set bytes_ 0 $PS_sink($i) set nlost_ 0 $PS_sink($i) set npkts_ 0 $MS_sink($i) set bytes_ 0 $MS_sink($i) set nlost_ 0 $MS_sink($i) set npkts_ 0 } # # Re-schedule the procedure for next run # if { $now+$samplint < $opt(stoptime) } {

$ns at [expr $now+$samplint] "record" } } ############### ## MAIN CODE ## ###############

set ns [new Simulator] set trfd [create-trace] ################################################### ## Define colour codes ################################################### $ns color 1 Blue $ns color 2 Red $ns color 3 Green $ns color 4 Yellow $ns color 5 Pink $ns color 6 Black $ns color 7 chocolate $ns color 8 purple ################################################### ## Call procedure "create-topology" ################################################### create-topology ################################################### ## Create and open the output files for writing ################################################### set file(1) "ctp143n-cw2_STUD-NAME_Bytes_[expr $opt(wkst)].tr" set file(2) "ctp143n-cw2_STUD-NAME_Frames_[expr $opt(wkst)].tr" set file(3) "ctp143n-cw2_STUD-NAME_Drop_[expr $opt(wkst)].tr" set file(4) "ctp143n-cw2_STUD-NAME_ChUt_[expr $opt(wkst)].tr"

for {set i 1} {$i < 5} {incr i} { set temp $file($i) set f($i) [open $temp w] } ##################################################### ## Create Sources and Sinks ##################################################### ## ## Create LossMonitor agents (traffic sinks) and attach them to appropiate nodes ## set num $opt(wkst) for {set i 0} {$i < $num} {incr i} { set Fsink($i) [new Agent/LossMonitor] set Msink($i) [new Agent/LossMonitor] set FS_sink($i) [new Agent/LossMonitor] set PS_sink($i) [new Agent/LossMonitor] set MS_sink($i) [new Agent/LossMonitor] $ns attach-agent $lannode($i) $Fsink($i) $ns attach-agent $lannode($i) $Msink($i) $ns attach-agent $fileserv $FS_sink($i) $ns attach-agent $printserv $PS_sink($i) $ns attach-agent $mailserv $MS_sink($i) } ## ## Get intervals parameters ## #set wf [WSF] #set wp [WSP] #set wm [WSM] #set Fw [FW] #set Mw [MW]

#puts " $wf $wp $wm $Fw $Mw " # UNFORTUNATELY TCL RETURNS INTEGERS AND THERE IS DIV/0 # SETTING INTERVALS MANNUALLY FROM EXCEL set wf 0.057 set wp 0.105 set wm 0.088 set Fw 0.035 set Mw 0.053 ## ## Create Sources ## for {set i 0} {$i < $opt(wkst)} {incr i} { set Fappl($i) [attach-traffic $lannode($i) $FS_sink($i) $wf 2] set Pappl($i) [attach-traffic $lannode($i) $PS_sink($i) $wp 3] set Mappl($i) [attach-traffic $lannode($i) $MS_sink($i) $wm 8] set FS_app($i) [attach-traffic $fileserv $Fsink($i) $Fw 5] set MS_app($i) [attach-traffic $mailserv $Msink($i) $Mw 1] } ##################################################### ## Start trafic ##################################################### for {set i 0} {$i < $opt(wkst)} {incr i} { $ns at 0.1 "$Fappl($i) start" $ns at 0.1 "$Pappl($i) start" $ns at 0.1 "$Mappl($i) start" $ns at 0.2 "$FS_app($i) start" $ns at 0.2 "$MS_app($i) start" } ##################################################### ## Initialise Sum variable for bw, lost packets, received packets #####################################################

set T_bw 0 set T_lp 0 set T_rp 0 ##################################################### ## Perform recording procedure starting from 0.2s ##################################################### $ns at 0.2 "record" ##################################################### ## Stop traffic ##################################################### for {set 2 0} {$i < $opt(wkst)} {incr i} { $ns at $opt(stoptime) "$Fappl($i) stop" $ns at $opt(stoptime) "$Pappl($i) stop" $ns at $opt(stoptime) "$Mappl($i) stop" $ns at $opt(stoptime) "$FS_app($i) stop" $ns at $opt(stoptime) "$MS_app($i) stop" } ##################################################### ## Close files and terminate simulation ##################################################### $ns at 0.0 "$ns set animation-rate 0.5ms" # $ns at $opt(stop) "finish" $ns at 1.4 "closegraph" $ns at 1.5 "finish" ############################################## ## NOW DO EVERYTHING WHAT IS DESCRIBED ABOVE # ############################################## $ns run

S-ar putea să vă placă și