4 @todo Look into switching from POLL_TIME = min(F * 2^n, MAX) to POLL_TIME = min(CONST + F * 2^n, MAX)
5 @todo Look into supporting more states that have a different F and MAX
12 import util.go_utils as gobject_utils
13 import util.coroutines as coroutines
17 _moduleLogger = logging.getLogger("gvoice.state_machine")
20 def _to_milliseconds(**kwd):
21 if "milliseconds" in kwd:
22 return kwd["milliseconds"]
23 elif "seconds" in kwd:
24 return kwd["seconds"] * 1000
25 elif "minutes" in kwd:
26 return kwd["minutes"] * 1000 * 60
27 raise KeyError("Unknown arg: %r" % kwd)
30 class StateMachine(object):
32 STATE_ACTIVE = 0, "active"
33 STATE_IDLE = 1, "idle"
36 _ACTION_UPDATE = "update"
37 _ACTION_RESET = "reset"
40 _INITIAL_ACTIVE_PERIOD = int(_to_milliseconds(seconds=10))
41 _FINAL_ACTIVE_PERIOD = int(_to_milliseconds(minutes=10))
42 _IDLE_PERIOD = int(_to_milliseconds(minutes=30))
47 def __init__(self, initItems, updateItems):
48 self._initItems = initItems
49 self._updateItems = updateItems
51 self._state = self.STATE_ACTIVE
52 self._timeoutId = None
53 self._currentPeriod = self._INITIAL_ACTIVE_PERIOD
54 self._set_initial_period()
56 self._callback = coroutines.func_sink(
57 coroutines.expand_positional(
58 self._request_reset_timers
66 @gtk_toolbox.log_exception(_moduleLogger)
68 _moduleLogger.info("Starting State Machine")
69 for item in self._initItems:
73 _moduleLogger.exception("Initial update failed for %r" % item)
74 self._schedule_update()
75 return False # do not continue
78 _moduleLogger.info("Stopping an already stopped state machine")
81 def set_state(self, newState):
82 oldState = self._state
83 _moduleLogger.info("Transitioning from %s to %s" % (oldState, newState))
85 self._state = newState
91 def reset_timers(self):
95 def request_reset_timers(self):
99 @gtk_toolbox.log_exception(_moduleLogger)
100 def _request_reset_timers(self, *args):
103 def _set_initial_period(self):
104 self._currentPeriod = self._INITIAL_ACTIVE_PERIOD / 2 # We will double it later
106 def _schedule_update(self):
107 assert self._timeoutId is None
108 nextTimeout = self._calculate_step(self._state, self._currentPeriod)
109 nextTimeout = int(nextTimeout)
110 if nextTimeout != self._INFINITE_PERIOD:
111 self._timeoutId = gobject.timeout_add(nextTimeout, self._on_timeout)
112 _moduleLogger.info("Next update in %s ms" % (nextTimeout, ))
113 self._currentPeriod = nextTimeout
115 def _stop_update(self):
116 if self._timeoutId is None:
118 gobject.source_remove(self._timeoutId)
119 self._timeoutId = None
121 def _reset_timers(self):
122 if self._timeoutId is None:
123 return # not started yet
125 self._set_initial_period()
126 self._schedule_update()
128 def _on_timeout(self):
129 _moduleLogger.info("Update")
130 for item in self._updateItems:
132 item.update(force=True)
134 _moduleLogger.exception("Update failed for %r" % item)
135 self._timeoutId = None
136 self._schedule_update()
137 return False # do not continue
140 def _calculate_step(cls, state, period):
141 if state == cls.STATE_ACTIVE:
142 return min(period * 2, cls._FINAL_ACTIVE_PERIOD)
143 elif state == cls.STATE_IDLE:
144 return cls._IDLE_PERIOD
145 elif state == cls.STATE_DND:
146 return cls._INFINITE_PERIOD
148 raise RuntimeError("Unknown state: %r" % (state, ))