1 # agent-mbgw.tcl --
2 #
3 # FIXME: This file needs a description here.
4 #
5 # Copyright (c) 1997-2002 The Regents of the University of California.
6 # All rights reserved.
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions are met:
10 #
11 # A. Redistributions of source code must retain the above copyright notice,
12 # this list of conditions and the following disclaimer.
13 # B. Redistributions in binary form must reproduce the above copyright notice,
14 # this list of conditions and the following disclaimer in the documentation
15 # and/or other materials provided with the distribution.
16 # C. Neither the names of the copyright holders nor the names of its
17 # contributors may be used to endorse or promote products derived from this
18 # software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
21 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
24 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #
31 # @(#) $Header: /usr/mash/src/repository/mash/mash-1/tcl/mbgw/agent-mbgw.tcl,v 1.19 2002/02/03 04:27:36 lim Exp $ (UCB)
32
33
34 import AnnounceListenManager/AS/Service/MeGa SDPParser AddressBlock \
35 AnnounceListenManager/AS/Client/MeGa NetworkManager
36
37 Class MBGWAgent
38
39 MBGWAgent instproc init { app } {
40 $self next
41
42 $self instvar app_ sessions_
43 set app_ $app
44 set sessions_ ""
45
46 set megaspec [$self get_option asCtrl]
47 set bw [$self get_option asCtrlBW]
48 set sspec [$app_ resource megaSession]
49 $self instvar gwal_ cliental_
50 if { $sspec != "" } {
51 set gwal_ [new AnnounceListenManager/AS/Service/MeGa $self \
52 $megaspec $bw $sspec mbgw mb]
53 }
54 set megaclient [$app_ get_option megaClient]
55 if { $megaclient != "" } {
56 set localbw [$app_ get_option localSessionBW]
57 if { $localbw == "" } {
58 set localbw [$app_ get_option maxSessionBW]
59 }
60 set sname [$self get_option megaSession]
61 set sspec [$self get_option sessionSpec]
62 set rportspec [$self get_option megaRecvPort]
63 set ofmt [$self get_option megaFormat]
64
65 set bw [expr 0.02*$localbw]
66 set cliental_ [new AnnounceListenManager/AS/Client/MeGa \
67 $self $megaclient $bw sdgw sdp \
68 $sname $sspec $rportspec $ofmt]
69 }
70 $self set sdp_ [new SDPParser]
71 $self instvar checkInt_
72 set checkInt_ [$app_ resource startupIdleTime]
73 after $checkInt_ "$self check_active 1"
74 $self init_network
75
76 if [info exists cliental_] {
77 $cliental_ start 1
78 }
79 if [info exists gwal_] {
80 #$gwal_ start 1
81 $gwal_ announce [$gwal_ send_announcement]
82 }
83 }
84
85 MBGWAgent instproc destroy {} {
86 $self instvar gwal_ cliental_
87 delete $gwal_
88 if [info exists cliental_] {
89 delete $cliental_
90 }
91 foreach s $sessions_ {
92 delete $s
93 }
94
95 $self next
96 }
97
98 MBGWAgent instproc set_maxchannel n {
99 }
100
101 MBGWAgent instproc reset_spec { spec } {
102 $self instvar sessbyname_
103 set ab [new AddressBlock $spec]
104 $sessbyname_(global) reset $ab
105 delete $ab
106 }
107
108 MBGWAgent instproc add_session { sname spec } {
109 $self instvar sessbyname_ sessions_
110
111 set s [new Session/MBGW $self]
112 if { $spec != "none" } {
113 set ab [new AddressBlock $spec]
114 $s reset $ab
115 delete $ab
116 }
117
118 set sessbyname_($sname) $s
119 $s set sname_ $sname
120 lappend sessions_ $s
121 }
122
123 MBGWAgent instproc init_network {} {
124 $self instvar app_ dataref_ ctrlref_
125
126 set dataref_ [new Reflector 1024]
127 set ctrlref_ [new Reflector 1024]
128
129 $self add_session global [$app_ get_option defaultGlobalSpec]
130 $self add_session local [$app_ get_option defaultLocalSpec]
131 }
132
133 MBGWAgent instproc have_network {} {
134 $self instvar sessbyname_
135 set nm [$sessbyname_(global) set netmgr_]
136 if { $nm == "" } {
137 return 0
138 } else {
139 return 1
140 }
141 }
142
143 MBGWAgent instproc data-net n {
144 $self instvar dataref_
145 $dataref_ add-network $n
146 }
147
148 MBGWAgent instproc ctrl-net n {
149 $self instvar ctrlref_
150 $ctrlref_ add-network $n
151 }
152
153 MBGWAgent instproc mega_unregister { atype aspec addr srv_name srv_inst } {
154 if { $atype != "client" } {
155 return
156 }
157 $self check_active 0
158 }
159
160 MBGWAgent instproc check_active { timer } {
161 $self instvar sessbyname_ gwal_ sdp_
162
163 # Check if there exists a client that is listening on our address.
164 # If so, we are "active".
165
166 set clist [$gwal_ set agentbytype_(client)]
167 set s $sessbyname_(local)
168 set nm [$s set netmgr_]
169 if { $nm == "" } {
170 if $timer {
171 after $checkInt_ "$self check_active 1"
172 }
173 }
174 set net [$nm set net_(0)]
175 set dn [$net data-net]
176 if [in_multicast [$dn addr]] {
177 set lspec [$dn addr]/[$dn rport]:[$dn sport]/[$dn ttl]
178 } else {
179 set lspec [localaddr]/[$dn rport]:[$dn sport]/[$dn ttl]
180 }
181 foreach c $clist {
182 set msg [$sdp_ parse [lindex [$gwal_ agenttab $c] 1]]
183 set media [lindex [$msg set allmedia_] 0]
184 set addr [lindex [$media field_value c] 2]
185 delete $msg
186 if { $addr == $lspec } {
187 # puts "mbgw alive!"
188 if $timer {
189 $self instvar checkInt_
190 after $checkInt_ "$self check_active 1"
191 }
192 return
193
194 }
195 }
196 puts "mbgw: no clients, exiting."
197 exit 0
198 }
199
200 MBGWAgent instproc network {} {
201 if ![$self have_network] {
202 return none
203 }
204 $self instvar sessbyname_
205 set network [$sessbyname_(global) set netmgr_]
206 return [$network data-net 0]
207 }
208
209 MBGWAgent instproc session-addr {} {
210 if ![$self have_network] {
211 return none
212 }
213 return [[$self network] addr]
214 }
215
216 MBGWAgent instproc session-port {} {
217 if ![$self have_network] {
218 return none
219 }
220 return [[$self network] port]
221 }
222
223 MBGWAgent instproc session-rport {} {
224 if ![$self have_network] {
225 return none
226 }
227 return [[$self network] rport]
228 }
229
230 MBGWAgent instproc session-sport {} {
231 if ![$self have_network] {
232 return none
233 }
234 return [[$self network] sport]
235 }
236
237 MBGWAgent instproc session-ttl {} {
238 if ![$self have_network] {
239 return none
240 }
241 return [[$self network] ttl]
242 }
243
244 Class Session/MBGW
245
246 Session/MBGW instproc init { agent } {
247 $self next
248 $self instvar agent_ ofmt_ netmgr_
249 set agent_ $agent
250 set ofmt_ null
251 set netmgr_ ""
252 }
253
254 Session/MBGW instproc destroy {} {
255 $self instvar netmgr_
256 if { $netmgr != "" } {
257 delete $netmgr_
258 }
259 }
260
261 Session/MBGW instproc data-net { net n } {
262 $self instvar agent_
263 $agent_ data-net $net
264 }
265
266 Session/MBGW instproc ctrl-net { net n } {
267 $self instvar agent_
268 $agent_ ctrl-net $net
269 }
270
271 Session/MBGW instproc reset { ab } {
272 $self instvar agent_
273 set nm [new NetworkManager $ab $self $agent_]
274
275 # Turn on loopback since multiple gateways could be running on same
276 # host.
277 set net [$nm set net_(0)]
278 set net [$net data-net]
279 $net loopback 1
280
281 $self instvar netmgr_
282 if { $netmgr_ != "" } {
283 delete $netmgr_
284 }
285 set netmgr_ $nm
286 }
287
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.