1 # powersw-srv.tcl --
2 #
3 # FIXME: This file needs a description here.
4 #
5 # Copyright (c) 1996-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/applications/powerswitch-server/powersw-srv.tcl,v 1.9 2002/02/03 04:22:21 lim Exp $
32
33
34 import SerialChannel/Powerswitch-Microgreen-Duet UDPServer Rendezvous Trace
35
36 #Trace on
37 #Trace add PowerSwitchServer
38
39
40 # PowerSwitch RPC-style interface
41 #
42 Class UDPServer/PowerSw -superclass UDPServer
43
44 #
45 UDPServer/PowerSw public init {addrspec parent {mtu 1500}} {
46 $self instvar parent_ pending_announce_ getting_own_msgs_
47 set parent_ $parent
48 set pending_announce_ -1
49 set getting_own_msgs_ 0
50
51 eval [list $self] next $addrspec $mtu
52 }
53
54 #
55 UDPServer/PowerSw private recv {addr port data size} {
56 puts "Msg: $addr/$port \[$size\]: $data"
57
58 $self instvar parent_ pending_announce_ getting_own_msgs_
59
60 set theMethod [lindex $data 0]
61 switch $theMethod {
62 0 {
63 if {!$getting_own_msgs_} {
64 puts "hearing own updates: need to disable mcast loopback"
65 set getting_own_msgs_ 1
66 }
67 ## don't need to schedule another update -- this msg from self
68 return
69 }
70 set_power { $parent_ set_power "$data" }
71 default { puts "bad msg: no action, but send state update" }
72 }
73
74 if {$pending_announce_ != -1} {
75 after cancel $pending_announce_
76 }
77 set pending_announce_ [after 500 $self announce_reply]
78 }
79
80 #
81 UDPServer/PowerSw private announce_reply {} {
82 $self instvar parent_ pending_announce_
83
84 set p [$parent_ set powerswitch_]
85 set state [$p array get plugState_]
86 puts "$state"
87
88 $self announce "$state"
89 after 10 $self announce [list $state]
90 after 40 $self announce [list $state]
91 set pending_announce_ -1
92 }
93
94
95 #
96 # a UI-less server that accepts RPC-like commands and
97 # returns results for controlling a powerswitch
98 #
99 Class PowerSwitchServer
100
101 #
102 PowerSwitchServer public init {addrspec} {
103 Trc $class "--> ${class}::$proc"
104
105 $self instvar powerswitch_ al_
106
107 set powerswitch_ [new SerialChannel/Powerswitch-Microgreen-Duet 1024]
108 set dv [$self get_option serialdevicename]
109 if {$dv != ""} {
110 $powerswitch_ close
111 $powerswitch_ device $dv
112 $powerswitch_ open
113 }
114
115 set al_ [new UDPServer/PowerSw $addrspec $self]
116
117 set raddr [$self get_option rendez]
118 if {$raddr != ""} {
119 $self start_rendez $raddr $addrspec
120 }
121
122 }
123
124 #
125 PowerSwitchServer public start_rendez {raddr ctrladdr} {
126 Trc $class "--> ${class}::$proc"
127 $self instvar rv_
128 set rv_ [new Rendezvous $raddr]
129 set m "will-provide: mash-object=[$self info class] ctrlspec=$ctrladdr"
130 set uid [$self get_option uniqid]
131 if {$uid != ""} {set m "$m uniqid=$uid"}
132 puts "starting rv msgs on $raddr"
133 $rv_ start $m
134 }
135
136 #
137 PowerSwitchServer public destroy {} {
138 $self instvar al_ powerswitch_
139
140 delete $al_
141 delete $avswitch_
142
143 eval [list $self] next
144 }
145
146 #
147 PowerSwitchServer private set_power {data} {
148 Trc $class "--> ${class}::$proc"
149 $self instvar powerswitch_
150
151 # 'set_power number onOff'
152 if {[llength $data] != 3} {
153 puts "bad params to set_power: $data"
154 return
155 }
156
157 set num [lindex $data 1]
158 set onOff [lindex $data 2]
159
160 Trc $class "set_power"
161 $powerswitch_ set_power $num $onOff
162 Trc $class "done power"
163 $powerswitch_ set plugState_($num) $onOff
164 Trc $class "<-- ${class}::$proc"
165 }
166
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.