⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 agent-gen.tcl

📁 对IEEE 802.11e里的分布式信道接入算法EDCA进行改进
💻 TCL
📖 第 1 页 / 共 2 页
字号:
AgentGen instproc check-TCP-location { } {		$self instvar opts	$self create-stub-location srcstub	$self create-stub-location deststub	if {$opts(srcstub) != $opts(deststub)} {		if {[llength $opts(srcstub-list)] != [llength $opts(deststub-list)]} {			puts "Error: \#srcstubs donot match with \#deststubs - all stubs are now being considered"			set opts(srcstub) -1			set opts(deststub) -1		}	}}AgentGen instproc check-srm { type } {	set types { "" /Deterministic /Probabilistic /Adaptive }	foreach srmtype $types {		if { $type == "SRM$srmtype" } {			return 1		}	}	return -1}AgentGen instproc check-SRM-location { } {	$self instvar opts	$self create-stub-location srcstub	$self create-stub-location deststub}# XXX testing hack# Class TG# TG proc get-total-nodes { } {#	return 25# }AgentGen instproc getNodes { range } {	$self instvar opts	if { $range == "all" && $opts(totalnodes)} {		# total nodes provided at cmd line			return $opts(totalnodes)	} else {		#		# total nodes was not provided at cmd line so ask		# topology generator. and for all other topology		# semantics query topology generator as follows:		# all (all nodes), total-stubs (total # stubs),		# transit<num> (node number for that transit, e.g transit4 will return		# 3) , stub<num> (range of nodes in the stub, e.g stub2 returns the range		# 11-18).		#		if {[catch {set tg [ScenGen getTG]}]} {			puts stderr {Can't find topology generator..quiting!			Either run the topology generator first, or provide the total			number of nodes using the "totalnodes" option.			Use "agents -h" for more help.}			exit		}		return [$tg getNodes $range]	}}# should there be an SRMGen class (and TCPGen class..etc) !AgentGen instproc calc-num { num } {	set totalNodes [$self getNodes all]	if { $num == -1 } {		# if num is left out, assign a number		# equal to the number of nodes		set number $totalNodes	} elseif { [regexp {^([0-9]+)(%)$} $num all num per] } {		#		# check for percentage 		#		set number [expr $totalNodes * $num / 100]	} else { set number $num }	 #puts "num $number"	return $number}AgentGen instproc get-tcp-stubs { num } {	$self instvar opts	set pairs ""	for {set i 0} {$i < [llength $opts(srcstub-list)]} {incr i} {		set p [lindex $opts(srcstub-list) $i]		set sr [split [$self getNodes stub$p] :]		set q [lindex $opts(deststub-list) $i]		set dr [split [$self getNodes stub$q] :]		set temp [$self randomize-agent-pairs [lindex $sr 0] \				   [lindex $sr 1] [lindex $dr 0] [lindex $dr 1] $num]		set pairs [ concat $pairs $temp]	}		puts "pairs = $pairs"	return $pairs}AgentGen instproc get-srm-stubs { num srcnum } {	$self instvar opts	set srcnodes ""	set destnodes ""	for {set i 0} {$i < [llength $opts(srcstub-list)]} {incr i} {		set p [lindex $opts(srcstub-list) $i]		set sr [split [$self getNodes stub$p] :]		set src [$self randomize-agents [lindex $sr 0] [lindex $sr 1] \				      $srcnum]		set srcnodes [concat $scrnodes $src]	}	for {set i 0} {$i < [llength $opts(deststub-list)]} {incr i} {		set q [lindex $opts(deststub-list) $i]		set dr [split [$self getNodes stub$q] :]		set dest [$self randomize-agents [lindex $dr 0] \				       [lindex $dr 1] [expr $num - $srcnum]]		set destnodes [concat $destnodes $dest]	}	set opts(srcstub-list) $srcnodes	set opts(deststub-list) $destnodes}# should have SRMGen/TCPGen/'transport'Gen generate-agents...etc.AgentGen instproc generate-srm-agents { srm num srcnum src traffic } {	$self instvar opts	# puts "generate srm agents $srm $num $srcnum $src $traffic"	set totalNodes [$self getNodes all]	if { $opts(srcstub) == -1 || $opts(deststub) == -1} {		# we assume that num is less that totalNodes..X		# and  incase totalNodes < num, more than one srm src/recvr agent shall		# be attached to nodes.		set nodes [$self randomize-agents 0 $totalNodes $num]		set srcNodes [lrange $nodes 0 [expr $srcnum -1]]		set rcvrNodes [lrange $nodes $srcnum end]	} else {		$self get-srm-stubs $num $srcnum		set srcNodes $opts(srcstub-list)		set rcvrNodes $opts(deststub-list)	}set a_5 "\n # generate $num $srm agents, $srcnum of which are \n"set a_4 "# srcs $src/$traffic. Distribute randomly. \n"	set a_1 "\n set group \[Node allocaddr\] \n"	# create agents assume 1 srm agent per node# if we are going to use array names and array size, then# use unset to remove any left over arrays from previous commands# check that we don't need those arrays again# e.g. unset srm, unset srmsrc, unset s, unset src... etc XXX		# may create one loop for both srmsrc and srm .. later X	set a "foreach i \{$rcvrNodes\} \{ \n"	set b "\t set srm(\$i) \[new Agent/$srm\] \n"	# set the group ... and other params.. XXX	set c "\t \$srm(\$i) set dst_addr_ \$group \n"	set d "\t \$ns attach-agent \$n(\$i) \$srm(\$i) \n \} \n"	set str1 "$a_5 $a_4 $a_1 $a $b $c $d"	set ba "foreach i \{$srcNodes\} \{ \n"	set bb "\t set srmsrc(\$i) \[new Agent/$srm\] \n"	# set the group ... and other params.. XXX	set bc "\t \$srmsrc(\$i) set dst_addr_ \$group \n"	set bd "\t \$ns attach-agent \$n(\$i) \$srmsrc(\$i) \n"	# assign the srcs	set f "\t set s(\$i) \[new Agent/$src\] \n"		# traffic is only used with CBR UDP	if { $src == "CBR/UDP" } {	 set g "\t set traffic(\$i) \[new Traffic/$traffic\] \n"	 # assign the trfc params. later... burst, idle, rate..XXX	 set h "\t \$s(\$i) attach-traffic \$traffic(\$i) \n"	} else { set g ""; set h ""; }	set i "\t \$srmsrc(\$i) traffic-source \$s(\$i) \n \} \n"	set str2 "$ba $bb $bc $bd $f $g $h $i"	set str "$str1 $str2"	# puts "generate srm done"	return $str}AgentGen instproc commandLine { } {	$self instvar commandLine	return $commandLine}AgentGen instproc preamble-tcp { } {	set separator {#################}	set a "\n $separator \n"	set ab "\# The command line generating this part of the script is: \n"	set ac "\# [$self commandLine]"	set ae "\n\n \#XXX cuz we use \[array names src\], we unset it first.\n"		set b "catch \{unset src\} \n"	return "$a $ab $ac $a $ae $b"}AgentGen instproc preamble-srm { } {	set separator "#################"	set a "\n $separator \n"	set ab "# The command line generating this part of the script is: \n"	set ac "# [$self commandLine]"	#set ad $a	set ae "\n\n # XXX cuz we use array names of srm and srmsrc, we unset.\n"	set b "catch \{unset srmsrc\} \n"	set c "catch \{unset srm\} \n"	return "$a $ab $ac $a $ae $b $c"}# node-ranges to be extended to capture topological semanticsAgentGen instproc generate-tcp-agents { tcp src sink num } {	$self instvar opts	#	# puts "generate tcp $tcp $src $sink $num $node_ranges"	#	set totalNodes [$self getNodes all]	#	# assume there's an API to get the total num of nodes in 	# the topology simulated, also we may need an API to 	# get ranges of topological significance (like stub	# ranges..etc), topology type.. so on..	#	if { $opts(srcstub) == -1 || $opts(deststub) == -1 } {		#		# no specific stub given for src/dest location.		#		set pairs [$self randomize-agent-pairs 0 $totalNodes \			       0 $totalNodes $num]	} else {		#	 	# may use topology info (e.g. stubs) to get		# the pairs		#		set pairs [$self get-tcp-stubs $num]		#puts "tcp pairs = [list $pairs]"	}set a_5 "\n # generate $num connections of $tcp, using $sink \n"set a_4 "# sink types, and $src sources. Distribute randomly. \n"	set a_1 "\n set i 0 \n"	set a "foreach pair \{$pairs\} \{ \n"	set b "\t set sorc \[lindex \$pair 0\] \n"	set c "\t set dst \[lindex \$pair 1\] \n"	# set the fid	set d_1 "\t incr fid \n"	set d "\t set tcp(\$i) \[\$ns create-connection $tcp \$n(\$sorc) $sink \$n(\$dst) \$fid\] \n"	set e "\t set src(\$i) \[\$tcp(\$i) attach-source $src\] \n"	set f "\t incr i \n \}"	set str "$a_5 $a_4 $a_1 $a $b $c $d_1 $d $e $f"	return $str}# check for commonalities bet this and generate-src-start.. XAgentGen instproc generate-srm-patterns { join leave num \		start stop srcnum } {	#puts "generate srm patterns $join $leave $num $start \	#	$stop $srcnum"	# for now ignore the leave and stop until I know if	# if they apply to srm agents or not !! XXX		set ex {regexp {^([0-9]+)[\-]([0-9]+)$}}	set strstart "" ;	set done(start) 0;	set ext "-source"	foreach event { start } {	  if { [eval "$ex [set $event] all begin end"] } {		set str [$self randomize-time $begin $end $srcnum]set a_5 "\n # randomize $event times in the \[$begin,$end\] range\n"set a_4 "# for $srcnum srm sources. \n"		set a "\n set i 0 \n"		set ab "set times \{$str\} \n"		set b "foreach j \[array names srmsrc\] \{ \n"		set bb "\t set time \[lindex \$times \$i\] \n"		set c "\t \$ns at \$time \"\$srmsrc(\$j) $event$ext\" \n"		set d "\t incr i \n \} \n"		set str$event "$a_5 $a_4 $a $ab $b $bb $c $d"		set done($event) 1	  } 	}	if { !$done(start) } {set a_1 " # start all $srcnum srm sources at $start. \n"		set a "foreach i \[array names srmsrc\] \{ \n"set b "\t \$ns at $start \"\$srmsrc(\$i) start-source\" \n \} \n"		set strstart "$a_1 $a $b"	}	if { [eval "$ex $join all begin end"] } {		set str [$self randomize-time $begin $end $num]		set srcNodes [lrange $str 0 \			[expr [array size srmsrc] -1]]		set rcvrNodes [lrange $str [array size srmsrc] end]set a_1 "\n # randomize the join times for $num srm agents in\n" set a_2 "# the range \[$begin,$end\] \n"		set a "\n set i 0 \n"		set ab "set times \{$str\} \n"		set b "foreach j \[array names srmsrc\] \{ \n"		set bb "\t set time \[lindex \$times \$i\] \n"		set c "\t \$ns at \$time \"\$srmsrc(\$j) start\" \n"		set d "\t incr i \n \} \n"		set e "foreach j \[array names srm\] \{ \n"		set f "\t set time \[lindex \$times \$i\] \n"		set g "\t \$ns at \$time \"\$srm(\$j) start\" \n"		set h "\t incr i \n \} \n"	set strjoin "$a_1 $a_2 $a $ab $b $bb $c $d $e $f $g $h"	} else {		set a_1 "# srm agents join at $join\n"		set a "foreach j \[array names srm\] \{ \n"		set b "\t \$ns at $join \"\$srm(\$j) start\" \n \} \n"		set c "foreach j \[array names srmsrc\] \{ \n"	set d "\t \$ns at $join \"\$srmsrc(\$j) start\" \n \} \n"		set strjoin "$a_1 $a $b $c $d"	}	return "$strstart $strjoin"}AgentGen instproc generate-src-start { start stop num } {	# puts "generate src $start $stop $num"	set ex {regexp {^([0-9]+)[\-]([0-9]+)$}}	set strstart "" ; set strstop ""	set done(start) 0; set done(stop) 0	# assume the start and stop intervals don't overlap,	# we do not check if start is before stop for an agent XXX	foreach event { start stop } {	  if { [eval "$ex [set $event] all begin end"] } {		set str [$self randomize-time $begin $end $num]set a_2 "\n # randomize the $event time for the $num sources in\n" set a_1 "# the range \[$begin,$end\]\n"		set a "\n set i 0 \n"		set b "foreach time \{$str\} \{ \n"		set c "\t \$ns at \$time \"\$src(\$i) $event\" \n"		set d "\t incr i \n \} \n"		set str$event "$a_2 $a_1 $a $b $c $d"		set done($event) 1	  } 	}	if { $done(start) && ($done(stop) || !$stop) } {		return "$strstart $strstop"	}	# may want to clear up the src array before this run of	# code generation... to avoid left overs.. XXX	set a "foreach i \[array names src\] \{ \n"	set a_1 ""; set a_2 ""	if { !$done(start) } {		set a_2 "# start the sources at $start \n"		set b "\t \$ns at $start \"\$src(\$i) start\" \n"	} else { set b "" }	if { !$done(stop) && $stop } {		set a_1 "# stop the sources at $stop \n"		set c "\t \$ns at $stop \"\$src(\$i) stop\" \n"	} else { set c "" }	set d "\}"	set str "$a_2 $a_1 $a $b $c $d"	return "$strstart $strstop $str"}# the following takes as input the interval (first, last) from# which distinct 'number' of ints are randomly chosen and returned.# It is assumed that number <= intervalAgentGen instproc randomize-agents { first last number } {	set interval [expr $last - $first]	set result ""	#	# code below that checked for duplication of nodes 	# resulted in infinite loops where "interval" was much smaller than 	# "number". Hence will generate a warning instead, changing "number" 	# to value of "interval". -Padma (July 15, '98).	#	if {$number > [expr $interval * 2]} {		puts "\tinterval = $interval is much larger than number = $number		Changing number from $number to $interval"		set number $interval	}	set maxrval [expr pow(2,31)]	set intrval [expr $interval/$maxrval]	for { set i 0 } { $i < $number } { incr i } {		set randval [expr [ns-random] * $intrval]		set randNode [expr int($randval) + $first]				 if { [info exists done($randNode)] } { 			set i [expr $i - 1]			 #puts "do again, $randNode exists -> i = $i" 		} else { 			set done($randNode) 1 			lappend result $randNode 			#puts "result = $result" 		}	}	#puts "result = $result"		return $result}#### may need to check if 'number' of connections exceed the interval range.# takes in src and dst ranges (first,last) from which 'number' of# distinct [i.e. src != dst] src,dst pairs are chosen# the result is a list of pairsAgentGen instproc randomize-agent-pairs { srcfirst srclast \		dstfirst dstlast number	} {	set srcinterval [expr $srclast - $srcfirst]	set dstinterval [expr $dstlast - $dstfirst]	set result ""	set maxrval [expr pow(2,31)]	set srcintrval [expr $srcinterval/$maxrval]	set dstintrval [expr $dstinterval/$maxrval]	for { set i 0 } { $i < $number } { incr i } {		set srcrandval [expr [ns-random] * $srcintrval]		set srcrandNode [expr int($srcrandval) + $srcfirst]		while { 1 } {		  set dstrandval [expr [ns-random] * $dstintrval]		  set dstrandNode [expr int($dstrandval) + $dstfirst]		  if { $srcrandNode != $dstrandNode } {			break		  }		}		lappend result [list $srcrandNode $dstrandNode]	}	return $result}AgentGen instproc randomize-time { first last number } {	set times ""	set interval [expr $last - $first]	set maxrval [expr pow(2,31)]	set intrval [expr $interval/$maxrval]	for { set i 0 } { $i < $number } { incr i } {		set randtime [expr ([ns-random] * $intrval) + $first]		# XXX include only 6 decimals (i.e. usec)		lappend times [format "%.6f" $randtime]	}	return $times}proc mark-end { file } {	global AllCommandLines	# puts "mark end of file $file"	if { [catch {set f [open $file a]}] } {		puts "can't append to file $file"	}	set a_1 " \n \} ; # ending the generate-agents proc.\n\n"	set a "############## End of file marker ##########\n"	set b "# Following are the command lines used:"	puts $f "$a_1 $a $b"	foreach cmdLine $AllCommandLines {		puts $f " # $cmdLine"	}	flush $f	close $f}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -