1 # application-servmgr.tcl --
2 #
3 # Implementation of a simple service manager. This is a small GUI
4 # program that lists available services, and allow users to kill
5 # the services.
6 #
7 # Copyright (c) 1996-2002 The Regents of the University of California.
8 # All rights reserved.
9 #
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions are met:
12 #
13 # A. Redistributions of source code must retain the above copyright notice,
14 # this list of conditions and the following disclaimer.
15 # B. Redistributions in binary form must reproduce the above copyright notice,
16 # this list of conditions and the following disclaimer in the documentation
17 # and/or other materials provided with the distribution.
18 # C. Neither the names of the copyright holders nor the names of its
19 # contributors may be used to endorse or promote products derived from this
20 # software without specific prior written permission.
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
23 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 # ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
26 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #
33 # @(#)
34
35 import Application
36 import CDcSession
37 import CServiceManager
38 import FontInitializer
39
40 #---------------------------------------------------------------------
41 # Class:
42 # ServiceManagerApplication
43 # Description:
44 # Implementation of a simple service manager. This is a small GUI
45 # program that lists available services, and allow users to kill
46 # the services.
47 #---------------------------------------------------------------------
48 Class ServiceManagerApplication -superclass Application
49
50 #---------------------------------------------------------------------
51 # Method:
52 # ServiceManagerApplication init
53 # Description:
54 # Initialized UI and create a service manager and sds session to listen
55 # to service announcement.
56 #---------------------------------------------------------------------
57 ServiceManagerApplication instproc init { winPath argv } {
58 $self instvar win_
59
60 $self next "servmgr"
61 set options [$self options]
62 $self InitArguments $options
63 $self InitResources $options
64 set argv [$options parse_args $argv]
65
66 frame $winPath
67 frame $winPath.main -relief ridge
68 frame $winPath.quit -relief ridge
69 label $winPath.quit.l -text "DC Service Manager" \
70 -font [$self get_option smallfont]
71 button $winPath.quit.b -text "Quit" \
72 -font [$self get_option smallfont]\
73 -command "$self Quit"
74 pack $winPath.quit.l $winPath.quit.b -side left -fill both
75 pack $winPath $winPath.main $winPath.quit
76 set win_ $winPath.main
77
78 # create the discovery service session
79 $self instvar dc_session_
80
81 set inetServiceAddr [$options get_option optServiceAddress]
82 set iServicePort [$options get_option optServicePort]
83 set iServiceTTL [$options get_option optServiceTTL]
84
85 set dc_session_ [new CDcSession $inetServiceAddr $iServicePort \
86 $iServicePort $iServiceTTL]
87 $dc_session_ StartQuery
88 $dc_session_ UpdateInfo "$self ServiceUpdateCallback"
89
90 $self instvar service_mgr_
91 if {[catch {new CServiceManager "ClientApp" "ServiceManager" 0} service_mgr_]} {
92 puts stderr "ERROR: servmgr: Unable to create service manager"
93 exit
94 }
95 $service_mgr_ Attach $self
96 }
97
98
99 #---------------------------------------------------------------------
100 # Method:
101 # ServiceManagerApplication Quit
102 # Description:
103 # Called when user click on the Quit button. Cleanup the session
104 # object and the manager object.
105 #---------------------------------------------------------------------
106 ServiceManagerApplication instproc Quit { } {
107 $self instvar dc_session_
108 $self instvar service_mgr_
109 delete $dc_session_
110 delete $service_mgr_
111 exit
112 }
113
114
115 #---------------------------------------------------------------------
116 # Method:
117 # ServiceManagerApplication InitArguments
118 # ServiceManagerApplication InitResources
119 # Description:
120 # Initialize default arguments. The only one we support are arguments
121 # to configure SDS session.
122 #---------------------------------------------------------------------
123 ServiceManagerApplication instproc InitArguments { options } {
124 # for the service discovery service
125 $options register_option -sa optServiceAddress
126 $options register_option -sp optServicePort
127 $options register_option -st optServiceTTL
128 }
129
130 ServiceManagerApplication instproc InitResources { options } {
131 new FontInitializer [$self options]
132 # for the service discovery service
133 $options add_default optServiceAddress "224.4.6.8"
134 $options add_default optServicePort "12344"
135 $options add_default optServiceTTL "16"
136 }
137
138
139 #---------------------------------------------------------------------
140 # Method:
141 # ServiceManagerApplication Kill
142 # Description:
143 # Called when user wants to kill a service at $host/$port. We first
144 # establish a connection with the service, then send a QUIT command
145 # to the service.
146 #---------------------------------------------------------------------
147 ServiceManagerApplication instproc Kill { host port } {
148 $self instvar service_mgr_
149 if [catch {$service_mgr_ NewService $host $port} service] {
150 puts "ERROR: servmgr: Cannot kill. No such service $host at port $port"
151 return
152 }
153 $service Send "QUIT" {}
154 }
155
156
157 #---------------------------------------------------------------------
158 # Method:
159 # ServiceManagerApplication NewConnection
160 # Description:
161 # Callback when a new connection is established. Nothing to see
162 # here. Move along.
163 #---------------------------------------------------------------------
164 ServiceManagerApplication instproc NewConnection { service } {
165 }
166
167
168 #---------------------------------------------------------------------
169 # Method:
170 # ServiceManagerApplication ServiceUpdateCallback
171 # Description:
172 # This is called whenever a new service announcement is received.
173 # We update the UI that shows a list of services.
174 #---------------------------------------------------------------------
175 ServiceManagerApplication instproc ServiceUpdateCallback { } {
176 $self instvar dc_session_
177 $self instvar win_
178 set font [$self get_option smallfont]
179 set tree [$dc_session_ set m_data]
180 set i 0
181 set button_list ""
182 destroy $win_
183 frame $win_
184 foreach {host_port service_data} $tree {
185 set host [lindex $host_port 0]
186 set host [lindex [split $host .] 0]
187 set type "Unknown"
188 foreach item $service_data {
189 foreach {key value} $item {
190 if {$key == "TYPE:"} {
191 set type $value
192 break
193 }
194 }
195 }
196 frame $win_.host$i -relief ridge
197 label $win_.host$i.l -text "$host $type" -font $font -relief ridge
198 button $win_.host$i.b -text x -command "$self Kill $host_port" -font $font
199 pack $win_.host$i.l $win_.host$i.b -side left -fill both
200 append button_list "$win_.host$i "
201 incr i
202 }
203 eval pack $win_ $button_list
204 }
205
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.