File: dynamics.tcl

package info (click to toggle)
ns2 2.35%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 78,120 kB
  • sloc: cpp: 172,923; tcl: 107,127; perl: 6,391; sh: 6,143; ansic: 5,846; makefile: 812; awk: 525; csh: 355
file content (452 lines) | stat: -rw-r--r-- 10,856 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#
#  Copyright (c) 1997 by the University of Southern California
#  All rights reserved.
#
#  This program is free software; you can redistribute it and/or
#  modify it under the terms of the GNU General Public License,
#  version 2, as published by the Free Software Foundation.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program; if not, write to the Free Software Foundation, Inc.,
#  59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
#
#  The copyright of this module includes the following
#  linking-with-specific-other-licenses addition:
#
#  In addition, as a special exception, the copyright holders of
#  this module give you permission to combine (via static or
#  dynamic linking) this module with free software programs or
#  libraries that are released under the GNU LGPL and with code
#  included in the standard release of ns-2 under the Apache 2.0
#  license or under otherwise-compatible licenses with advertising
#  requirements (or modified versions of such code, with unchanged
#  license).  You may copy and distribute such a system following the
#  terms of the GNU GPL for this module and the licenses of the
#  other code concerned, provided that you include the source code of
#  that other code when and as the GNU GPL requires distribution of
#  source code.
#
#  Note that people who make modified versions of this module
#  are not obligated to grant this special exception for their
#  modified versions; it is their choice whether to do so.  The GNU
#  General Public License gives permission to release a modified
#  version without this exception; this exception also makes it
#  possible to release a modified version which carries forward this
#  exception.

#
# Maintainer: <kannan@isi.edu>.
#

# The API for this code is still somewhat fluid and subject to change.
# Check the documentation for details.

Class rtQueue

Simulator instproc rtmodel { dist parms args } {
	set ret ""
	if { [rtModel info subclass rtModel/$dist] != "" } {
		$self instvar  rtModel_
		set ret [eval new rtModel/$dist $self]
		eval $ret set-elements $args
		eval $ret set-parms $parms
		set trace [$self get-ns-traceall]
		if {$trace != ""} {
			$ret trace $self $trace
		}
		set trace [$self get-nam-traceall]
		if {$trace != ""} {
			$ret trace $self $trace "nam"
		}
		if [info exists rtModel_] {
			lappend rtModel_ $ret
		} else {
			set rtModel_ $ret
		}
	}
	return $ret
}

Simulator instproc rtmodel-configure {} {
    $self instvar rtq_ rtModel_
    if [info exists rtModel_] {
	set rtq_ [new rtQueue $self]
	foreach m $rtModel_ {
	    $m configure
	}
    }
}

Simulator instproc rtmodel-at {at op args} {
    set parms [list $op $at]
    eval $self rtmodel Manual [list $parms] $args
}

Simulator instproc rtmodel-delete model {
    $self instvar rtModel_
    set idx [lsearch -exact $rtModel_ $model]
    if { $idx != -1 } {
	delete $model
	set rtModel_ [lreplace $rtModel_ $idx $idx]
    }
}

#
rtQueue instproc init ns {
    $self next
    $self instvar ns_
    set ns_ $ns
}

rtQueue instproc insq-i { interval obj iproc args } {
    $self instvar rtq_ ns_
    set time [expr $interval + [$ns_ now]]
    if ![info exists rtq_($time)] {
	$ns_ at $time "$self runq $time"
    }
    lappend rtq_($time) "$obj $iproc $args"
    return $time
}

rtQueue instproc insq { at obj iproc args } {
    $self instvar rtq_ ns_
    if {[$ns_ now] >= $at} {
	puts stderr "$proc: Cannot set event in the past"
	set at ""
    } else {
	if ![info exists rtq_($at)] {
	    $ns_ at $at "$self runq $at"
	}
	lappend rtq_($at) "$obj $iproc $args"
    }
    return $at
}

rtQueue instproc delq { time obj } {
    $self instvar rtq_
    set ret ""
    set nevent ""
    if [info exists rtq_($time)] {
	foreach event $rtq_($time) {
	    if {[lindex $event 0] != $obj} {
		lappend nevent $event
	    } else {
		set ret $event
	    }
	}
	set rtq_($time) $nevent		;# XXX
    }
    return ret
}

rtQueue instproc runq { time } {
    $self instvar rtq_
    set objects ""
    foreach event $rtq_($time) {
	set obj   [lindex $event 0]
	set iproc [lindex $event 1]
	set args  [lrange $event 2 end]
	eval $obj $iproc $args
	lappend objects $obj
    }
    foreach obj $objects {
	$obj notify
    }
    unset rtq_($time)
}

#
Class rtModel

rtModel set rtq_ ""

rtModel instproc init ns {
    $self next
    $self instvar ns_ startTime_ finishTime_
    set ns_ $ns
    set startTime_ [$class set startTime_]
    set finishTime_ [$class set finishTime_]
}

rtModel instproc set-elements args {
    $self instvar ns_ links_ nodes_
    if { [llength $args] == 2 } {
	set n0 [lindex $args 0]
	set n1 [lindex $args 1]
	set n0id [$n0 id]
	set n1id [$n1 id]
	
	set nodes_($n0id) $n0
	set nodes_($n1id) $n1
	set links_($n0id:$n1id) [$ns_ link $n0 $n1]
	set links_($n1id:$n0id) [$ns_ link $n1 $n0]
    } else {
	set n0 [lindex $args 0]
	set n0id [$n0 id]
	set nodes_($n0id) $n0
	foreach nbr [$n0 set neighbor_] {
	    set n1 $nbr
	    set n1id [$n1 id]
	    
	    set nodes_($n1id) $n1
	    set links_($n0id:$n1id) [$ns_ link $n0 $n1]
	    set links_($n1id:$n0id) [$ns_ link $n1 $n0]
	}
    }
}

rtModel instproc set-parms args {
    $self instvar startTime_ upInterval_ downInterval_ finishTime_

    set cls [$self info class]
    foreach i {startTime_ upInterval_ downInterval_ finishTime_} {
	if [catch "$cls set $i" $i] {
	    set $i [$class set $i]
	}
    }

    set off "-"
    set up  "-"
    set dn  "-"
    set fin "-"

    switch [llength $args] {
	4 {
	    set off [lindex $args 0]
	    set up  [lindex $args 1]
	    set dn  [lindex $args 2]
	    set fin [lindex $args 3]
	}
	3 {
	    set off [lindex $args 0]
	    set up  [lindex $args 1]
	    set dn  [lindex $args 2]
	}
	2 {
	    set up [lindex $args 0]
	    set dn [lindex $args 1]
	}
    }
    if {$off != "-" && $off != ""} {
	set startTime_ $off
    }
    if {$up != "-" && $up != ""} {
	set upInterval_ $up
    }
    if {$dn != "-" && $dn != ""} {
	set downInterval_ $dn
    }
    if {$fin != "-" && $fin != ""} {
	set finishTime_ $fin
    }
}

rtModel instproc configure {} {
    $self instvar ns_ links_
    if { [rtModel set rtq_] == "" } {
	rtModel set rtq_ [$ns_ set rtq_]
    }

    foreach l [array names links_] {
	$links_($l) dynamic
    }
    $self set-first-event
}

rtModel instproc set-event-exact {fireTime op} {
    $self instvar ns_ finishTime_
    if {$finishTime_ != "-" && $fireTime > $finishTime_} {
	if {$op == "up"} {
	    [rtModel set rtq_] insq $finishTime_ $self $op
	}
    } else {
	[rtModel set rtq_] insq $fireTime $self $op
    }
}

rtModel instproc set-event {interval op} {
    $self instvar ns_
    $self set-event-exact [expr [$ns_ now] + $interval] $op
}

rtModel instproc set-first-event {} {
    $self instvar startTime_ upInterval_
    $self set-event [expr $startTime_ + $upInterval_] down
}

rtModel instproc up {} {
    $self instvar links_
    foreach l [array names links_] {
	$links_($l) up
    }
}

rtModel instproc down {} {
    $self instvar links_
    foreach l [array names links_] {
	$links_($l) down
    }
}

rtModel instproc notify {} {
    $self instvar nodes_ ns_
    foreach n [array names nodes_] {
	$nodes_($n) intf-changed
    }
    [$ns_ get-routelogic] notify
}

rtModel instproc trace { ns f {op ""} } {
    $self instvar links_
    foreach l [array names links_] {
	$links_($l) trace-dynamics $ns $f $op
    }
}

#
# Exponential link failure/recovery models
#

Class rtModel/Exponential -superclass rtModel

rtModel/Exponential instproc set-first-event {} {
	global rtglibRNG

	$self instvar startTime_ upInterval_
	$self set-event [expr $startTime_ + [$rtglibRNG exponential] * $upInterval_] down
}

rtModel/Exponential instproc up { } {
	global rtglibRNG

	$self next
	$self instvar upInterval_
	$self set-event [expr [$rtglibRNG exponential] * $upInterval_] down
}

rtModel/Exponential instproc down { } {
	global rtglibRNG

	$self next
	$self instvar downInterval_
	$self set-event [expr [$rtglibRNG exponential] * $downInterval_] up
}

#
# Deterministic link failure/recovery models
#

Class rtModel/Deterministic -superclass rtModel

rtModel/Deterministic instproc up { } {
	$self next
	$self instvar upInterval_
	$self set-event $upInterval_ down
}

rtModel/Deterministic instproc down { } {
	$self next
	$self instvar downInterval_
	$self set-event $downInterval_ up
}

#
# Route Dynamics instantiated through a trace file.
# Invoked through:
#
#    $ns_ rtmodel Trace $traceFile $node1 [$node2 ... ]
#

Class rtModel/Trace -superclass rtModel

rtModel/Trace instproc get-next-event {} {
    $self instvar tracef_ links_
    while {[gets $tracef_ event] >= 0} {
	set toks [split $event]
	if [info exists links_([lindex $toks 3]:[lindex $toks 4])] {
	    return $toks
	}
    }
    return ""
}

rtModel/Trace instproc set-trace-events {} {
    $self instvar ns_ nextEvent_ evq_
    
    set time [lindex $nextEvent_ 1]
    while {$nextEvent_ != ""} {
	set nextTime [lindex $nextEvent_ 1]
	if {$nextTime < $time} {
	    puts stderr "event $nextEvent_  is before current time $time. ignored."
	    continue
	}
	if {$nextTime > $time} break
	if ![info exists evq_($time)] {
	    set op [string range [lindex $nextEvent_ 2] 5 end]
	    $self set-event-exact $time $op
	    set evq_($time) 1
	}
	set nextEvent_ [$self get-next-event]
    }
}


rtModel/Trace instproc set-parms traceFile {
    $self instvar tracef_ nextEvent_
    if [catch "open $traceFile r" tracef_] {
	puts stderr "cannot open $traceFile"
    } else {
	set nextEvent_ [$self get-next-event]
	if {$nextEvent_ == ""} {
	    puts stderr "no relevant events in $traceFile"
	}
    }
}

rtModel/Trace instproc set-first-event {} {
    $self set-trace-events
}

rtModel/Trace instproc up {} {
    $self next
    $self set-trace-events
}

rtModel/Trace instproc down {} {
    $self next
    $self set-trace-events
}

#
# One-shot route dynamics events
# Invoked through:
#
#	$ns_ link-op $op $at $node1 [$node2 ...]
# or
#	$ns_ rtmodel Manual {$op $at} $node1 [$node2 ...]
#
Class rtModel/Manual -superclass rtModel

rtModel/Manual instproc set-first-event {} {
    $self instvar op_ at_
    $self set-event-exact $at_ $op_ ;# you could concievably set a finishTime_?
}

rtModel/Manual instproc set-parms {op at} {
    $self instvar op_ at_
    set op_ $op
    set at_ $at
}

rtModel/Manual instproc notify {} {
    $self next
    delete $self		;# XXX wierd code alert.
	# If needed, this could be commented out, on the assumption that
	# manual settings will be very limited, and hence not a sufficient
	# drag on memory resources.  For now, play it safe (or is it risky?)
}