1 # keepalive.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
32 Class KeepAlive
33 KeepAlive set BeaconInterval_ms 2000
34 Class KeepAliveSlave -superclass KeepAlive
35 Class KeepAliveMaster -superclass KeepAlive
36
37
38
39 Class KAChannel -superclass CBChannel/Local
40 KAChannel set ChannelID_ 1
41 Class KASChannel -superclass CBChannel/Local
42 Class KAMChannel -superclass CBChannel/Local
43
44
45
46 KASChannel instproc init { cb argv0 argv } {
47 $self next $cb -id [KAChannel set ChannelID_] -nolisten
48 $self set pid_ [pid]
49 $self set argv0_ $argv0
50 $self set argv_ $argv
51 }
52
53
54 KASChannel instproc send_alive { } {
55 $self instvar pid_ argv0_ argv_
56 $self send -apptype KeepAliveMaster alive $pid_ $argv0_ $argv_
57 }
58
59
60 KASChannel instproc send_dying { } {
61 $self instvar pid_
62 $self send -apptype KeepAliveMaster dying $pid_
63 }
64
65
66 KeepAliveSlave instproc init { cb argv0 argv } {
67 $self next
68 $self instvar cb_ channel_ after_id_
69 set cb_ $cb
70 set channel_ [$cb_ new_channel KASChannel $argv0 $argv]
71 $self send_alive
72 }
73
74
75 KeepAliveSlave instproc destroy { } {
76 $self instvar channel_ cb_ after_id_
77 $channel_ send_dying
78 $cb_ delete_channel channel_
79 after cancel $after_id_
80 $self next
81 }
82
83
84 KeepAliveSlave instproc send_alive { } {
85 puts "Sending alive: [pid]"
86 $self instvar channel_
87 $channel_ send_alive
88
89 # schedule the next alive beacon
90 $self set after_id_ [after [KeepAlive set BeaconInterval_ms] \
91 "$self send_alive"]
92 }
93
94
95 KeepAliveSlave instproc send_dying { } {
96 $self instvar channel_
97 $channel_ send_dying
98 }
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 KeepAliveMaster instproc init { cb } {
122 $self next
123 $self instvar cb_ channel_ applist_
124 set cb_ $cb
125 set channel_ [$cb_ new_channel KAMChannel $self]
126 set applist_ ""
127 $self periodic_check
128 }
129
130
131 KeepAliveMaster instproc destroy { } {
132 $self instvar channel_ cb_ after_id_
133 $cb_ delete_channel channel_
134 after cancel $after_id_
135 $self next
136 }
137
138
139
140
141 KeepAliveMaster instproc proc_alive { infoVar pid argv0 argv } {
142 puts "Alive: $pid $argv0"
143 $self instvar apps_ applist_ after_id_
144 if { ![info exists apps_($pid,argv0)] } {
145 # this is a new application
146 $self new_application $pid $argv0 $argv
147 lappend applist_ $pid
148 }
149 set apps_($pid,argv0) $argv0
150 set apps_($pid,argv) $argv
151 set apps_($pid,status) alive
152 }
153
154
155 KeepAliveMaster instproc proc_dying { infoVar pid } {
156 puts "Got dying $pid"
157 $self instvar applist_
158 if { [lsearch $applist_ $pid]==-1 } { return }
159 $self dismiss $pid
160 $self remove $pid
161 }
162
163
164 KeepAliveMaster instproc periodic_check { } {
165 $self instvar apps_ applist_
166 puts "Periodic check [clock seconds]"
167 foreach pid $applist_ {
168 if { $apps_($pid,status)=="no_beacon" } {
169 # I guess this app has died; maybe we should restart it
170 $self restart $pid $apps_($pid,argv0) $apps_($pid,argv)
171 $self remove $pid
172 } else {
173 set apps_($pid,status) "no_beacon"
174 }
175 }
176 set after_id_ [after [expr [KeepAlive set BeaconInterval_ms]*2] \
177 "$self periodic_check"]
178 }
179
180
181 KeepAliveMaster instproc remove { pid } {
182 $self instvar apps_ applist_
183 set idx [lsearch $applist_ $pid]
184 if { $idx==-1 } { return }
185
186 set applist_ [lreplace $applist_ $idx $idx]
187 unset apps_($pid,argv0)
188 unset apps_($pid,argv)
189 unset apps_($pid,status)
190 }
191
192
193
194
195
196
197
198
199 #virtual methods
200
201 KeepAliveMaster instproc new_application { pid argv0 argv } {
202 }
203
204
205 KeepAliveMaster instproc restart { pid argv0 argv } {
206 }
207
208
209 KeepAliveMaster instproc dismiss { pid } {
210 }
211
212
213
214
215
216
217 KAMChannel instproc init { cb kam } {
218 $self next $cb -id [KAChannel set ChannelID_]
219 $self register alive "$kam proc_alive"
220 $self register dying "$kam proc_dying"
221 }
222
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.