File: http-ex.tcl

package info (click to toggle)
ns2 2.35%2Bdfsg-2.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 78,780 kB
  • ctags: 27,490
  • sloc: cpp: 172,923; tcl: 107,130; perl: 6,391; sh: 6,143; ansic: 5,846; makefile: 816; awk: 525; csh: 355
file content (214 lines) | stat: -rwxr-xr-x 5,018 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#!/bin/sh
# the next line finds ns \
nshome=`dirname $0`; [ ! -x $nshome/ns ] && [ -x ../../ns ] && nshome=../..
# the next line starts ns \
export nshome; exec $nshome/ns "$0" "$@"

if [info exists env(nshome)] {
	set nshome $env(nshome)
} elseif [file executable ../../ns] {
	set nshome ../..
} elseif {[file executable ./ns] || [file executable ./ns.exe]} {
	set nshome "[pwd]"
} else {
	puts "$argv0 cannot find ns directory"
	exit 1
}
set env(PATH) "$nshome/bin:$env(PATH)"
#source $nshome/tcl/lan/ns-lan.tcl
source $nshome/tcl/http/http.tcl

set opt(trsplit) "-"
set opt(tr)	out
set opt(namtr)	""
set opt(stop)	20
set opt(node)	3
set opt(seed)	0

set opt(bw)	1.8Mb
set opt(delay)	1ms
set opt(ll)	LL
set opt(ifq)	Queue/DropTail
set opt(mac)	Mac/Csma/Cd
set opt(chan)	Channel
set opt(tcp)	TCP/Reno
set opt(sink)	TCPSink

# number of traffic source (-1 means the number of nodes $opt(node))
set opt(telnet) 0
set opt(cbr)	0
set opt(ftp)	0
set opt(http)	-1

set opt(webModel) $nshome/tcl/http/data
set opt(maxConn) 4
set opt(phttp) 0

if {$argc == 0} {
	puts "Usage: $argv0 \[-stop sec\] \[-seed value\] \[-node numNodes\]"
	puts "\t\[-tr tracefile\] \[-g\]"
	puts "\t\[-ll lltype\] \[-ifq qtype\] \[-mac mactype\]"
	puts "\t\[-bw $opt(bw)] \[-delay $opt(delay)\]"
	exit 1
}

proc getopt {argc argv} {
	global opt
	for {set i 0} {$i < $argc} {incr i} {
		set key [lindex $argv $i]
		if ![string match {-*} $key] continue
		set key [string range $key 1 end]
		set val [lindex $argv [incr i]]
		set opt($key) $val
		if [string match {-[A-z]*} $val] {
			incr i -1
			continue
		}
		switch $key {
			ll  { set opt($key) LL/$val }
			ifq { set opt($key) Queue/$val }
			mac { set opt($key) Mac/$val }
		}
	}
}


proc finish {} {
	global env nshome pwd
	global ns opt trfd

	$ns flush-trace
	close $trfd
	foreach key {node bw delay ll ifq mac seed} {
		lappend comment $opt($key)
	}
	exec perl $nshome/bin/trsplit -tt r -pt tcp -c "$comment" \
			$opt(trsplit) $opt(tr) >& $opt(tr)-bw
	exec head -1 $opt(tr)-bw >@ stdout

	if [info exists opt(g)] {
		catch "exec xgraph -nl -M -display $env(DISPLAY) \
				[lsort [glob $opt(tr).*]] &"
	}
	exit 0
}


proc create-trace {} {
	global ns opt

	if {[info exists opt(f)] || [info exists opt(g)]} {
		set opt(trsplit) "-f"
	}
	if [file exists $opt(tr)] {
		exec touch $opt(tr).
		eval 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
}


proc create-topology {} {
	global ns opt
	global lan node source

	# Nodes:
	#  0		a router from LAN to external network
	#  1 to n	nodes on the LAN
	#  n+1		node outside of the LAN,
	set num $opt(node)
	set node(0)  [$ns node]
	for {set i 1} {$i <= $num} {incr i} {
		set node($i) [$ns node]
		lappend nodelist $node($i)
	}
	set node($i) [$ns node]
	$ns duplex-link $node(0) $node($i) 10Mb 50ms DropTail

	set lan [$ns newLan $nodelist $opt(bw) $opt(delay) -llType $opt(ll) \
			-ifqType $opt(ifq) -macType $opt(mac)]
	$lan addNode $node(0) $opt(bw) $opt(delay)

	set ifq0 [[$lan netIface $node(0)] set ifq_]
	$ifq0 set limit_ [expr $opt(node) * [$ifq0 set limit_]]
}


proc create-source {} {
	global ns opt
	global lan node source

	if {$opt(webModel) != ""} {
		create-webModel $opt(webModel)
	}
	# make node($num+1) the source of all connection
	set src $node(0)
	foreach ttype {http ftp cbr telnet} {
		set num $opt($ttype)
		if {$num < 0} {
			set num $opt(node)
		}
		for {set i 0} {$i < $num} {incr i} {
			set dst $node([expr 1 + $i % $opt(node)])
			new_$ttype $i $src $dst
		}
	}
}

proc create-webModel path {
	Http setCDF rvClientTime 0
#	Http setCDF rvClientTime $path/HttpThinkTime.cdf
	Http setCDF rvReqLen $path/HttpRequestLength.cdf
	Http setCDF rvNumImg $path/HttpConnections.cdf
	set rv [Http setCDF rvRepLen $path/HttpReplyLength.cdf]
	Http setCDF rvImgLen $rv
}

proc new_http {i server client} {
	global ns opt http webm
	set webopt "-srcType $opt(tcp) -snkType $opt(sink) -phttp $opt(phttp) -maxConn $opt(maxConn)"
	set http($i) [eval new Http $ns $client $server $webopt]
	$ns at [expr 0 + $i/1000.0] "$http($i) start"
}

proc new_cbr {i src dst} {
	global ns opt cbr
	set cbr($i) [$ns create-connection CBR $src Null $dst 0]
	$ns at [expr 0.0001 + $i/1000.0] "$cbr($i) start"
}

proc new_ftp {i src dst} {
	global ns opt ftp
	set tcp [$ns create-connection $opt(tcp) $src $opt(sink) $dst 0]
	set ftp($i) [$tcp attach-source FTP]
	$ns at [expr 0.0002 + $i/1000.0] "$ftp($i) start"
}

proc new_telnet {i src dst} {
	global ns opt telnet
	set tcp [$ns create-connection $opt(tcp) $src $opt(sink) $dst 0]
	set telnet($i) [$tcp attach-source Telnet]
	$ns at [expr 0.0003 + $i/1000.0] "$telnet($i) start"
}


## MAIN ##
getopt $argc $argv
if {$opt(seed) >= 0} { ns-random $opt(seed) }
if [info exists opt(qsize)] { Queue set limit_ $opt(qsize) }

set ns [new Simulator]
set trfd [create-trace]

create-topology
$lan trace $ns $trfd

create-source
$ns at $opt(stop) "finish"
$ns run