~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Open Mash Cross Reference
mash/tcl/common/mash-softstate.tcl

Component: ~ [ mash ] ~ [ apps ] ~ [ gsm ] ~ [ lib ] ~ [ otcl ] ~ [ srm ] ~ [ tcl8.3 ] ~ [ tclcl ] ~ [ tk8.3 ] ~ [ tutorials ] ~

  1 import MashTimer
  2 import MashLog
  3 
  4 Class MashSoftState
  5 
  6 #----------------------------------------------------------------------
  7 # Class:
  8 #   MashSoftState
  9 # Description:
 10 #   A MashSoftState object is an abstraction for a soft state variable, 
 11 #   commonly used in writing light-weight, loosely coupled protocol.  
 12 #   It contains a data value, a timeout value t, and a callback f.  
 13 #   When the value is set, a timer is started.  The timer timeout after 
 14 #   t seconds, after which the value expires, and callback f is called.  
 15 #----------------------------------------------------------------------
 16 #
 17 # The following interface is provided by this class. 
 18 # - set_value : set the value of the state & the timer is reset.
 19 # - get_value : retrieve the value of the state & the timer is unchange.
 20 # - refresh   : reset the timer without changing the value
 21 # - expire    : force the timer the expire but keep the value unchanged.
 22 # - is_expired : test if the timer has expired.
 23 #
 24 
 25 #----------------------------------------------------------------------
 26 # Method:
 27 #   MashSoftState init
 28 # Description:
 29 #   Create a new soft-state with value $value and timeout $timeout.
 30 #   $callback will be called if $timeout has passed without a refresh.
 31 #----------------------------------------------------------------------
 32 MashSoftState public init { value timeout callback } {
 33     $self instvar value_ timeout_ timer_ callback_ expired_
 34     set value_ $value
 35     set timeout_ $timeout
 36     set callback_ $callback
 37     set timer_ [new MashTimer "once" $timeout "$self expire"]
 38     set expired_ 0
 39 }
 40 
 41 
 42 #----------------------------------------------------------------------
 43 # Method:
 44 #   MashSoftState set_value
 45 # Description:
 46 #   Set the value of the state.  The soft state is refreshed.  If the
 47 #   state has expired, "unexpire" the state.
 48 #----------------------------------------------------------------------
 49 MashSoftState public set_value { value } {
 50     $self instvar value_ expired_
 51     set value_ $value
 52     $self refresh
 53     set expired_ 0
 54 }
 55 
 56 
 57 #----------------------------------------------------------------------
 58 # Method:
 59 #   MashSoftState get_value
 60 # Description:
 61 #   Return the value of the state.
 62 #----------------------------------------------------------------------
 63 MashSoftState public get_value { } {
 64     $self instvar value_
 65     return $value_ 
 66 }
 67 
 68 
 69 #----------------------------------------------------------------------
 70 # Method:
 71 #   MashSoftState refresh
 72 # Description:
 73 #   Refresh the soft state.
 74 #----------------------------------------------------------------------
 75 MashSoftState public refresh { } {
 76     $self instvar timer_ timeout_ callback_
 77     $timer_ resched
 78 }
 79 
 80 
 81 #----------------------------------------------------------------------
 82 # Method:
 83 #   MashSoftState expire
 84 # Description:
 85 #   Called when this state expired.
 86 #----------------------------------------------------------------------
 87 MashSoftState public expire { } {
 88     $self instvar timer_ callback_ expired_
 89     $timer_ cancel
 90     set expired_ 1
 91     eval $callback_
 92 }
 93 
 94 
 95 #----------------------------------------------------------------------
 96 # Method:
 97 #   MashSoftState is_expired
 98 # Description:
 99 #   Test if the current state has expired.  
100 #----------------------------------------------------------------------
101 MashSoftState public is_expired { } {
102     $self instvar expired_
103     return $expired_
104 }
105 
106 
107 #----------------------------------------------------------------------
108 # Method:
109 #   MashSoftState destroy
110 # Description:
111 #   Delete the timer.
112 #----------------------------------------------------------------------
113 MashSoftState public destroy {} {
114     $self instvar timer_
115     delete $timer_
116     $self next
117 }
118 
119 
120 #----------------------------------------------------------------------
121 # Class:
122 #   MashSoftState/Adaptive
123 # Description:
124 #   Maintain average refresh interval I, and timeout after N*I second.
125 #----------------------------------------------------------------------
126 Class MashSoftState/Adaptive -superclass MashSoftState
127 
128 MashSoftState/Adaptive instproc init { value timeout callback {N 8} } {
129     $self next $value $timeout $callback
130 
131     $self instvar n_
132     set n_ $N
133 
134     $self instvar last_refresh_
135     set last_refresh_ [clock clicks -milliseconds]
136 
137     $self instvar avg_diff_
138     set avg_diff_ [expr {$timeout/$N}]
139 }
140 
141 
142 #----------------------------------------------------------------------
143 # Method:
144 #   MashSoftState/Adaptive refresh
145 # Description:
146 #   Refresh the soft state.
147 #----------------------------------------------------------------------
148 MashSoftState/Adaptive public refresh { } {
149     $self instvar timer_ timeout_ callback_ last_refresh_
150     set now [clock clicks -milliseconds]
151     set diff [expr {$now - $last_refresh_}]
152     set last_refresh_ $now
153 
154     $self instvar avg_diff_ n_
155     set avg_diff_ [expr {0.88*$avg_diff_ + 0.12*$diff}]
156 
157     set t [expr {$n_*$avg_diff_}]
158     $timer_ cancel
159     $timer_ sched $t
160     #MashLog info "SS: resched: self=$self:to=$t:n=$n_:d=$avg_diff_"
161 }
162 
163 #----------------------------------------------------------------------
164 # Method:
165 #   MashSoftState/Adaptive timeout
166 # Description:
167 #   Refresh the soft state.
168 #----------------------------------------------------------------------
169 MashSoftState/Adaptive public expire { } {
170     $self instvar callback_
171     #MashLog info "SS: expire: self=$self:cb=$callback_"
172     $self next
173 }
174 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.