File: ns-sat.tcl

package info (click to toggle)
ns2 2.35%2Bdfsg-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 78,808 kB
  • sloc: cpp: 172,923; tcl: 107,130; perl: 6,391; sh: 6,143; ansic: 5,846; makefile: 816; awk: 525; csh: 355
file content (806 lines) | stat: -rw-r--r-- 25,191 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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
#
# Copyright (c) 1999 Regents of the University of California.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. All advertising materials mentioning features or use of this software
#    must display the following acknowledgement:
#       This product includes software developed by the MASH Research
#       Group at the University of California Berkeley.
# 4. Neither the name of the University nor of the Research Group may be
#    used to endorse or promote products derived from this software without
#    specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# Contributed by Tom Henderson, UCB Daedalus Research Group, June 1999

# @(#) $Header: /cvsroot/nsnam/ns-2/tcl/lib/ns-sat.tcl,v 1.13 2002/07/10 02:29:50 tomh Exp $


# ======================================================================
#
# The Node/SatNode class (a pure virtual class)
#
# ======================================================================


Node/SatNode instproc init args {
	eval $self next $args		;# parent class constructor

	$self instvar nifs_ 
	$self instvar phy_tx_ phy_rx_ mac_ ifq_ ll_ pos_ hm_ id_

	set nifs_	0		;# number of network interfaces
	# Create a drop trace to log packets for which no route exists
	set ns_ [Simulator instance]
	set trace_ [$ns_ get-ns-traceall]
	if {$trace_ != ""} {
		set dropT_ [$ns_ create-trace Sat/Drop $trace_ $self $self ""]
		$self set_trace $dropT_
	}
	$self cmd set_address $id_ ; # Used to indicate satellite node in array
}

Node/SatNode instproc reset {} {
        eval $self next 
	$self instvar hm_ instvar nifs_ phy_tx_ phy_rx_ mac_ ifq_ ll_
	set ns [Simulator instance]
	set now_ [$ns now]
        for {set i 0} {$i < $nifs_} {incr i} {
		$phy_tx_($i) reset
		$phy_rx_($i) reset
		if {[info exists mac_($i)]} {
			$mac_($i) reset
		}
		if {[info exists ll_($i)]} {
			$ll_($i) reset
		}
		if {[info exists ifq_($i)]} {
			$ifq_($i) reset
		}
	}
	if {$now_ == 0} {
		if {[info exists hm_]} {
			$ns at 0.0 "$self start_handoff"
		}
	}
}

Node/SatNode instproc set_next {node_} {
	$self instvar pos_
	$pos_ set_next [$node_ set pos_]
}

#
# Attach an agent to a node.  Pick a port and
# bind the agent to the port number.
# if portnumber is 255, default target is set to the routing agent
#
Node/SatNode instproc add-target {agent port } {

	$self instvar dmux_ 
	
	if { $port == [Node set rtagent_port_] } {			
		# Set the default target at C++ level.  
		[$self set classifier_] defaulttarget $agent
		$dmux_ install $port $agent
	} else {
		#
		# Send Target
		#
		$agent target [$self entry]

		#
		# Recv Target
		#
		$dmux_ install $port $agent
	}
}

# ======================================================================
#
# methods for creating SatNodes
#
# ======================================================================

Simulator instproc create-satnode {} {
	$self instvar satNodeType_ llType_ ifqType_ ifqlen_ macType_ \
		downlinkBW_ phyType_ channelType_
	if {![info exists satNodeType_]} {
		puts "Error: create-satnode called, but no satNodeType_; exiting"
		exit 1
	}
	# Case conversion (for backward compatibility)
	if {$satNodeType_ == "Polar"} {set satNodeType_ "polar"}
	if {$satNodeType_ == "Geo"} {set satNodeType_ "geo"}
	if {$satNodeType_ == "Terminal"} {set satNodeType_ "terminal"}
	if {$satNodeType_ == "Geo-repeater"} {set satNodeType_ "geo-repeater"}
	if {[lsearch {polar geo terminal geo-repeater} $satNodeType_] < 0} {
		puts "Error: undefined satNodeType: $satNodeType_; exiting"
		exit 1
	}
	# Create the satnode
	set tmp [$self newsatnode]
	# Add uplink and downlink if this is not a terminal
	if {$satNodeType_ == "polar" || $satNodeType_ == "geo"} {
		set linkargs "$llType_ $ifqType_ $ifqlen_ $macType_ \
		    $downlinkBW_ $phyType_"
		$self add-first-links $tmp gsl $linkargs $channelType_
	} elseif {$satNodeType_ == "geo-repeater"} {
		$self add-first-links $tmp gsl-repeater "" $channelType_
	}
	return $tmp
}

Simulator instproc newsatnode {} {
	$self instvar Node_ satNodeType_
	if ![info exists satNodeType_] {
		puts "Error: satNodeType_ does not exist in newsatnode; exiting"
		exit 1
	}
	set node [new Node/SatNode]
	# Create a routing agent for all types except the repeater node
	if {$satNodeType_ == "polar" || $satNodeType_ == "geo" || \
	    $satNodeType_ == "terminal"} {
		$node create-ragent
	}
	set Node_([$node id]) $node
	$node set ns_ $self
	if [$self multicast?] {
		$node enable-mcast $self
	}
	$self check-node-num
	return $node
}

Node/SatNode instproc set-position args {
	set ns_ [Simulator instance]
	set nodetype_ [$ns_ set satNodeType_]
	if {$nodetype_ == "polar"} {
		if {[llength $args] != 5 } {
			puts "Error:  satNodeType_ is polar, but number\
			      of position arguments incorrect: $args; exiting"
			exit 1
		}
		$self set pos_ [new Position/Sat/Polar $args]
		$self cmd set_position [$self set pos_]
		[$self set pos_] setnode $self
		# Set up handoff manager
		$self set hm_ [new HandoffManager/Sat]
		$self cmd set_handoff_mgr [$self set hm_]
		[$self set hm_] setnode $self
	} elseif {$nodetype_ == "geo" || $nodetype_ == "geo-repeater"} {
		if {[llength $args] != 1 } {
			puts "Error:  satNodeType_ is geo, but number\
			      of position arguments incorrect: $args; exiting"
			exit 1
		}
		$self set pos_ [new Position/Sat/Geo $args]
		$self cmd set_position [$self set pos_]
		[$self set pos_] setnode $self
	} elseif {$nodetype_ == "terminal"} {
		if {[llength $args] != 2 } {
			puts "Error:  satNodeType_ is terminal, but number\
		              of position arguments incorrect: $args; exiting"
			exit 1
		}
		$self set pos_ [new Position/Sat/Term $args]
		$self cmd set_position [$self set pos_]
		[$self set pos_] setnode $self
		$self set hm_ [new HandoffManager/Term]
		$self cmd set_handoff_mgr [$self set hm_]
		[$self set hm_] setnode $self
	} else {
		puts "Error:  satNodeType_ not set appropriately:\
		     $satNodeType_ exiting"
		exit 1
	}
}

## Backward compatibility code starts

# Wrapper method for making a polar satellite node that first creates a 
# satnode plus two link interfaces (uplink and downlink) plus two 
# satellite channels (uplink and downlink)
# Additional ISLs can be added later
Simulator instproc satnode-polar {alt inc lon alpha plane linkargs chan} {
        set tmp [$self satnode polar $alt $inc $lon $alpha $plane]
	$self add-first-links $tmp gsl $linkargs $chan
        return $tmp
}

# Wrapper method for making a geo satellite node that first creates a 
# satnode plus two link interfaces (uplink and downlink) plus two 
# satellite channels (uplink and downlink)
Simulator instproc satnode-geo {lon linkargs chan} {
        set tmp [$self satnode geo $lon]
	$self add-first-links $tmp gsl $linkargs $chan
	return $tmp
}

# Wrapper method for making a geo satellite repeater node that first creates a 
# satnode plus two link interfaces (uplink and downlink) plus two 
# satellite channels (uplink and downlink)
Simulator instproc satnode-geo-repeater {lon chan} {
        set tmp [$self satnode geo $lon]
	$self add-first-links $tmp gsl-repeater "" $chan
	return $tmp
}

# Wrapper method that does nothing really but makes the API consistent 
Simulator instproc satnode-terminal {lat lon} {
	$self satnode terminal $lat $lon
}

Simulator instproc satnode args {
	$self instvar Node_
	set node [new Node/SatNode]
	if {[lindex $args 0] == "polar" || [lindex $args 0] == "Polar"} {
		set args [lreplace $args 0 0]
		# Set up position object
		$node set pos_ [new Position/Sat/Polar $args]
		$node cmd set_position [$node set pos_]
		[$node set pos_] setnode $node
		# Set up handoff manager
		$node set hm_ [new HandoffManager/Sat]
		$node cmd set_handoff_mgr [$node set hm_]
		[$node set hm_] setnode $node
		$node create-ragent 
	} elseif {[lindex $args 0] == "geo" || [lindex $args 0] == "Geo"} {  
		set args [lreplace $args 0 0]
		$node set pos_ [new Position/Sat/Geo $args]
		$node cmd set_position [$node set pos_]
		[$node set pos_] setnode $node
		$node create-ragent
	} elseif {[lindex $args 0] == "geo-repeater" || [lindex $args 0] == "Geo-repeater"} {  
		set args [lreplace $args 0 0]
		$node set pos_ [new Position/Sat/Geo $args]
		$node cmd set_position [$node set pos_]
		[$node set pos_] setnode $node
	} elseif {[lindex $args 0] == "terminal" || [lindex $args 0] == "Terminal"} {  
		set args [lreplace $args 0 0]
		$node set pos_ [new Position/Sat/Term $args]
		$node cmd set_position [$node set pos_]
		[$node set pos_] setnode $node
		$node set hm_ [new HandoffManager/Term]
		$node cmd set_handoff_mgr [$node set hm_]
		[$node set hm_] setnode $node
		$node create-ragent
	} else {
		puts "Otcl error; satnode specified incorrectly: $args"
	}
	set Node_([$node id]) $node
	$node set ns_ $self
        if [$self multicast?] {
                $node enable-mcast $self
        }
	$self check-node-num
	return $node
}

## Backward compatibility code ends 

# ======================================================================
#
# methods for creating satellite links, channels, and error models
#
# ======================================================================

# Helper method for creating uplinks and downlinks for a satellite node
Simulator instproc add-first-links {node_ linktype linkargs chan} {
	$node_ set_downlink $chan
        $node_ set_uplink $chan
        # Add the interface for these channels and then attach the channels
	if {$linktype == "gsl-repeater"} {
		$node_ add-repeater $chan
	} else {
        	eval $node_ add-interface $linktype $linkargs
	}
        $node_ attach-to-outlink [$node_ set downlink_]
        $node_ attach-to-inlink [$node_ set uplink_]
}

#  Add network stack and attach to the channels 
Node/SatNode instproc add-gsl {ltype opt_ll opt_ifq opt_qlim opt_mac \
opt_bw opt_phy opt_inlink opt_outlink} {
	$self add-interface $ltype $opt_ll $opt_ifq $opt_qlim $opt_mac $opt_bw \
		$opt_phy 
	$self attach-to-inlink $opt_inlink
	$self attach-to-outlink $opt_outlink
}

# Add network stacks for ISLs, and create channels for them
Simulator instproc add-isl {ltype node1 node2 bw qtype qlim} {
	set opt_ll LL/Sat
	set opt_mac Mac/Sat
	set opt_phy Phy/Sat
	set opt_chan Channel/Sat
	set chan1 [new $opt_chan]
	set chan2 [new $opt_chan]
	$node1 add-interface $ltype $opt_ll $qtype $qlim $opt_mac $bw $opt_phy $chan1 $chan2
	$node2 add-interface $ltype $opt_ll $qtype $qlim $opt_mac $bw $opt_phy $chan2 $chan1
	if {$ltype == "crossseam"} {
		# Add another spare interface between node 1 and node 2
		$node1 add-interface $ltype $opt_ll $qtype $qlim $opt_mac $bw $opt_phy 
		$node2 add-interface $ltype $opt_ll $qtype $qlim $opt_mac $bw $opt_phy 
		
	}
}

Node/SatNode instproc add-repeater chan { 
	$self instvar nifs_ phy_tx_ phy_rx_ linkhead_ 

	set t $nifs_
	incr nifs_

	# linkhead_($t) is a simple connector that provides an API for
	# accessing elements of the network interface stack
	set linkhead_($t) [new Connector/LinkHead/Sat]
	set phy_tx_($t)	[new Phy/Repeater]		;# interface
	set phy_rx_($t)	[new Phy/Repeater]

	# linkhead maintains a collection of pointers
	$linkhead_($t) setnode $self
	$linkhead_($t) setphytx $phy_tx_($t)
	$linkhead_($t) setphyrx $phy_rx_($t)
	$linkhead_($t) set_type "gsl-repeater"
	$linkhead_($t) set type_ "gsl-repeater"

	$phy_rx_($t) up-target $phy_tx_($t)
	$phy_tx_($t) linkhead $linkhead_($t)
	$phy_rx_($t) linkhead $linkhead_($t)
	$phy_tx_($t) node $self		;# Bind node <---> interface
	$phy_rx_($t) node $self		;# Bind node <---> interface
}

#
#  The following sets up link layer, mac layer, interface queue 
#  and physical layer structures for the satellite nodes.
#  Propagation model is an optional argument
#
Node/SatNode instproc add-interface args { 

	$self instvar nifs_ phy_tx_ phy_rx_ mac_ ifq_ ll_ drophead_ linkhead_

	global ns_ MacTrace opt

	set t $nifs_
	incr nifs_

	# linkhead_($t) is a simple connector that provides an API for
	# accessing elements of the network interface stack
	set linkhead_($t) [new Connector/LinkHead/Sat]

	set linktype 	[lindex $args 0]
	set ll_($t)	[new [lindex $args 1]]		;# link layer
	set ifq_($t)	[new [lindex $args 2]]		;# interface queue
	set qlen	[lindex $args 3]
	set mac_($t)	[new [lindex $args 4]]		;# mac layer
	set mac_bw	[lindex $args 5]
	set phy_tx_($t)	[new [lindex $args 6]]		;# interface
	set phy_rx_($t)	[new [lindex $args 6]]		;# interface
	set inchan 	[lindex $args 7]
	set outchan 	[lindex $args 8]
	set drophead_($t) [new Connector]	;# drop target for queue
	set iif_($t) [new NetworkInterface]
	

	#
	# Local Variables
	#
	#set nullAgent_ [$ns_ set nullAgent_]
	set linkhead $linkhead_($t)
	set phy_tx $phy_tx_($t)
	set phy_rx $phy_rx_($t)
	set mac $mac_($t)
	set ifq $ifq_($t)
	set ll $ll_($t)
	set drophead $drophead_($t)
	set iif $iif_($t)

	# linkhead maintains a collection of pointers
	$linkhead setnode $self
	$linkhead setll $ll
	$linkhead setmac $mac
	$linkhead setqueue $ifq
	$linkhead setphytx $phy_tx
	$linkhead setphyrx $phy_rx
	$linkhead setnetinf $iif
	$self addlinkhead $linkhead; # Add NetworkInterface to node's list
	$linkhead target $ll; 
	$linkhead set_type $linktype
	$linkhead set type_ $linktype

	# 
	# NetworkInterface
	#
	$iif target [$self entry]

	#
	# Link Layer
	#
	$ll mac $mac; # XXX is this needed?
	$ll up-target $iif
	$ll down-target $ifq
	$ll set delay_ 0ms; # processing delay between ll and ifq
	$ll setnode $self

	#
	# Interface Queue
	#
	$ifq target $mac
	$ifq set limit_ $qlen
        $drophead target [[Simulator instance] set nullAgent_]
        $ifq drop-target $drophead


	#
	# Mac Layer
	#
	$mac netif $phy_tx; # Not used by satellite code at this time
	$mac up-target $ll
	$mac down-target $phy_tx
	$mac set bandwidth_ $mac_bw; 
	#$mac nodes $opt(nn)
	
	#
	# Physical Layer
	#
	$phy_rx up-target $mac
	$phy_tx linkhead $linkhead
	$phy_rx linkhead $linkhead
	$phy_tx node $self		;# Bind node <---> interface
	$phy_rx node $self		;# Bind node <---> interface

	# If we have a channel to attach this link to (for an point-to-point
	# ISL), do so here
	if {$outchan != "" && $inchan != ""} {
		$phy_tx channel $outchan
		$phy_rx channel $inchan
		# Add to list of Phys receiving on the channel
		$inchan addif $phy_rx
	}
	return $t
}

Node/SatNode instproc set_uplink {chan} {
	$self instvar uplink_
	set uplink_ [new $chan]
	$self cmd set_uplink $uplink_
}

Node/SatNode instproc set_downlink {chan} {
	$self instvar downlink_
	set downlink_ [new $chan]
	$self cmd set_downlink $downlink_
}

#  Attaches channel to the phy indexed by "index" (by default, the first one)
Node/SatNode instproc attach-to-outlink {chan {index 0} } {
	$self instvar phy_tx_ mac_
	$phy_tx_($index) channel $chan
	#$mac_($index) channel $chan; # For mac addr resolution (rather than ARP)
}

#  Attaches channel to the phy indexed by "index" (by default, the first one)
Node/SatNode instproc attach-to-inlink { chan {index 0}} {
	$self instvar phy_rx_ 
	$phy_rx_($index) channel $chan
	$chan addif $phy_rx_($index)
}

#  Attaches error model to interface "index" (by default, the first one)
Node/SatNode instproc interface-errormodel { em { index 0 } } {
	$self instvar mac_ ll_ em_ linkhead_
	$mac_($index) up-target $em
	$em target $ll_($index)
	$em drop-target [new Agent/Null]; # otherwise, packet is only marked
	set em_($index) $em
	$linkhead_($index) seterrmodel $em
} 

# ======================================================================
#
# methods to add tracing support to Mac/Sat
#
# ======================================================================

Mac/Sat instproc init args {
	eval $self next $args           ;# parent class constructor

	set ns_ [Simulator instance]
	set trace_ [$ns_ get-ns-traceall]
	if {$trace_ != ""} {
		set dropT_ [$ns_ create-trace Sat/Drop $trace_ $self $self ""]
		$self set_drop_trace $dropT_
		set collT_ [$ns_ create-trace Sat/Collision $trace_ $self $self ""]
		$self set_coll_trace $collT_
	}
}

# ======================================================================
#
# methods for routing 
#
# ======================================================================

# Create a network interface (for one uplink and downlink), add routing agent
# At least one interface must be created before routing agent is added
Node/SatNode instproc create-ragent {} {
	set ragent [new Agent/SatRoute]
        $self attach $ragent 255; # attaches to default target of classifier  
        $ragent set myaddr_ [$self set id_]
	$self set_ragent $ragent; # sets pointer at C++ level
	$ragent set_node $self; # sets back pointer in ragent to node
}

# When running all routing in C++, we want a dummy OTcl routing
#
Class Agent/rtProto/Dummy -superclass Agent/rtProto

Agent/rtProto/Dummy proc init-all args {
	# Nothing
}

# ======================================================================
#
# methods for wired/wireless integration
#
# ======================================================================

# For wired routing to work, the following are needed:
# - the link_(src:dst) array must point to the LinkHead at the top of the link
# - the call "link_(src:dst) up?" must return "up"
# - the call "link_(src:dst) cost?" must return the current cost
# However, in wireless case, the same linkhead may be used to connect to
# multiple destinations on the channel.  Therefore, a new data structure
# is needed that can perform the above functions


Class Connector/RoutingHelper -superclass Connector

Simulator instproc sat_link_up {src dst cost handle queue_handle} {
	$self instvar link_

	global slink_
	set slink_($src:$dst) $self; # what is this?

	if {![info exists link_($src:$dst)]} {
		set link_($src:$dst) [new Connector/RoutingHelper]
	}
	if {[$link_($src:$dst) info class] == "Connector/RoutingHelper"} {
		$link_($src:$dst) set cost_ $cost
		$link_($src:$dst) set up_ "up"
		$link_($src:$dst) set queue_ $queue_handle
		$link_($src:$dst) target $handle
		$link_($src:$dst) set head_ $handle
	} else {
		puts -nonewline "link_(${src}:${dst}) have non-connector "
		puts "[$link_($src:$dst) info class]"
		exit 1
	}
}

# Upcall to enable wired routing
# Normally, in satellite routing we insert directly to adjacency table 
# in C++, but we upcall here to allow the OTcl link_ array to be populated

Simulator instproc sat_link_destroy {src dst} {
	$self instvar link_

	global slink_
	if {[info exists slink_($src:$dst)]} {
		unset slink_($src:$dst)
	}

	if {[info exists link_($src:$dst)]} {
		delete $link_($src:$dst)
		unset link_($src:$dst)
	} else {
		# This warning pops up when running sat-teledesic.tcl script,
		# which uses crossseam ISLs (not yet debugged)
		puts -nonewline "Warning: trying to delete a link_ "
		puts "link_(${src}:${dst}) that doesn't exist at [$self now]"
	}
}

Connector/RoutingHelper instproc up? {} {
	$self instvar up_
	return $up_
}

Connector/RoutingHelper instproc queue {} {
	$self instvar queue_
	return $queue_
}

Connector/RoutingHelper instproc head {} {
	$self instvar head_
	return $head_
}

Connector/RoutingHelper instproc cost? {} {
	$self instvar cost_
	return $cost_
}                                                     
# XXX this is because we can't support all of these nam calls
Connector/RoutingHelper instproc dump-nam-queueconfig {} { return 0}

# ======================================================================
#
# methods for tracing
#
# ======================================================================

Simulator instproc trace-all-satlinks {f} {
	$self instvar Node_
	foreach nn [array names Node_] {
		if {![$Node_($nn) info class Node/SatNode]} {
			continue; # Not a SatNode
		}
		$Node_($nn) trace-all-satlinks $f
	}
}

# All satlinks should have an interface indexed by nifs_
Node/SatNode instproc trace-all-satlinks {f} {
	$self instvar nifs_ enqT_ rcvT_ linkhead_
	for {set i 0} {$i < $nifs_} {incr i} {
		if {[$linkhead_($i) set type_] == "gsl-repeater"} {
			continue;
		}
		if {[info exists enqT_($i)]} {
			puts "Tracing already exists on node [$self id]"
		} else {
			$self trace-outlink-queue $f $i
		}
		if {[info exists rcvT_($i)]} {
			puts "Tracing already exists on node [$self id]"
		} else {
			$self trace-inlink-queue $f $i
		}
	}
}

# Set up trace objects around first output queue for packets destined to node
Node/SatNode instproc trace-outlink-queue {f {index_ 0} } {
	$self instvar id_ enqT_ deqT_ drpT_ mac_ ll_ ifq_ drophead_ 
	
	set ns [Simulator instance]
	set fromNode_ $id_
	set toNode_ -1

	set enqT_($index_) [$ns create-trace Sat/Enque $f $fromNode_ $toNode_]
	$enqT_($index_) target $ifq_($index_)
	$ll_($index_) down-target $enqT_($index_)

	set deqT_($index_) [$ns create-trace Sat/Deque $f $fromNode_ $toNode_]
	$deqT_($index_) target $mac_($index_)
	$ifq_($index_) target $deqT_($index_)

	set drpT_($index_) [$ns create-trace Sat/Drop $f $fromNode_ $toNode_]
	$drpT_($index_) target [$drophead_($index_) target]
	$drophead_($index_) target $drpT_($index_)
	$ifq_($index_) drop-target $drpT_($index_)
}

# Trace element between mac and ll tracing packets between node and node
Node/SatNode instproc trace-inlink-queue {f {index_ 0} } {
	$self instvar id_ rcvT_ mac_ ll_ phy_rx_ em_ errT_    

	set ns [Simulator instance]
	set toNode_ $id_
	set fromNode_ -1

	if {[info exists em_($index_)]} {
		# if error model, then chain mac -> em -> rcvT -> ll
		# First, set up an error trace on the ErrorModule
		set errT_($index_) [$ns create-trace Sat/Error $f $fromNode_ $toNode_]
		$errT_($index_) target [$em_($index_) drop-target]
		$em_($index_) drop-target $errT_($index_)
		set rcvT_($index_) [$ns create-trace Sat/Recv $f $fromNode_ $toNode_]
		$rcvT_($index_) target [$em_($index_) target]
		$em_($index_) target $rcvT_($index_)
	} else {
		# if no error model, then insert between mac and ll
		set rcvT_($index_) [$ns create-trace Sat/Recv $f $fromNode_ $toNode_]
		$rcvT_($index_) target [$mac_($index_) up-target]
		$mac_($index_) up-target $rcvT_($index_)
	}
	
}


###########
# TRACE MODIFICATIONS
##########

# This creates special satellite tracing elements
# See __FILE__.cc
# Support for Enque, Deque, Recv, Drop, Generic (not Session)

Class Trace/Sat/Hop -superclass Trace/Sat
Trace/Sat/Hop instproc init {} {
        $self next "h"
}

Class Trace/Sat/Enque -superclass Trace/Sat
Trace/Sat/Enque instproc init {} {
        $self next "+"
}

Trace/Sat/Deque instproc init {} {
        $self next "-"
}

Class Trace/Sat/Recv -superclass Trace/Sat
Trace/Sat/Recv instproc init {} {
        $self next "r"
}

Class Trace/Sat/Drop -superclass Trace/Sat
Trace/Sat/Drop instproc init {} {
        $self next "d"
}

Class Trace/Sat/Error -superclass Trace/Sat
Trace/Sat/Error instproc init {} {
        $self next "e"
}

Class Trace/Sat/Collision -superclass Trace/Sat
Trace/Sat/Collision instproc init {} {
	$self next "c"
}

Class Trace/Sat/Generic -superclass Trace/Sat
Trace/Sat/Generic instproc init {} {
        $self next "v"
}


# ======================================================================
#
# Defaults for bound variables 
#
# ======================================================================

Node/SatNode set dist_routing_ false; # distributed routing not yet supported
Position/Sat set time_advance_ 0; # time offset to start of simulation 
Position/Sat/Polar set plane_ 0
HandoffManager/Term set elevation_mask_ 0
HandoffManager/Term set term_handoff_int_ 10
HandoffManager/Sat set sat_handoff_int_ 10
HandoffManager/Sat set latitude_threshold_ 70
HandoffManager/Sat set longitude_threshold_ 0
HandoffManager set handoff_randomization_ false 
SatRouteObject set metric_delay_ true
SatRouteObject set data_driven_computation_ false
SatRouteObject set wiredRouting_ false
Mac/Sat set trace_drops_ true
Mac/Sat set trace_collisions_ true
Mac/Sat/UnslottedAloha set mean_backoff_ 1s; # mean backoff time upon collision
Mac/Sat/UnslottedAloha set rtx_limit_ 3; # Retransmission limit 
Mac/Sat/UnslottedAloha set send_timeout_ 270ms; # Timer interval for new sends

Agent/SatRoute set myaddr_       0        ;# My address
Mac/Sat set bandwidth_ 2Mb