#!/usr/bin/env python
"""
-@todo Look into switching from POLL_TIME = min(F * 2^n, MAX) to POLL_TIME = min(CONST + F * 2^n, MAX)
-@todo Look into supporting more states that have a different F and MAX
+@todo Look into supporting more states
"""
import logging
class GeometricStateStrategy(object):
def __init__(self, init, min, max):
- assert 0 < init or init == UpdateStateMachine.INFINITE_PERIOD
- assert 0 < min or min == UpdateStateMachine.INFINITE_PERIOD
+ assert 0 < init and init < max and init != UpdateStateMachine.INFINITE_PERIOD
+ assert 0 < min and min != UpdateStateMachine.INFINITE_PERIOD
assert min < max or max == UpdateStateMachine.INFINITE_PERIOD
self._min = min
self._max = max
self._init = init
- self._current = min / 2
+ self._current = 0
def initialize_state(self):
self._current = self._min / 2
_IS_DAEMON = True
- def __init__(self, updateItems):
+ def __init__(self, updateItems, name=""):
+ self._name = name
self._updateItems = updateItems
self._state = self.STATE_ACTIVE
assert self._timeoutId is None
for strategy in self._strategies.itervalues():
strategy.initialize_state()
- self._timeoutId = gobject.idle_add(self._on_timeout)
+ if self._strategy.timeout != self.INFINITE_PERIOD:
+ self._timeoutId = gobject.idle_add(self._on_timeout)
_moduleLogger.info("%s Starting State Machine" % (self._name, ))
def stop(self):
+ _moduleLogger.info("%s Stopping State Machine" % (self._name, ))
self._stop_update()
def close(self):
+ assert self._timeoutId is None
self._callback = None
def set_state(self, newState):
return self._state
def reset_timers(self):
+ _moduleLogger.info("%s Resetting State Machine" % (self._name, ))
self._reset_timers()
@property
def _strategy(self):
return self._strategies[self._state]
- @property
- def _name(self):
- return "/".join(type(s).__name__ for s in self._updateItems)
-
- @gobject_utils.async
@gtk_toolbox.log_exception(_moduleLogger)
def _request_reset_timers(self, *args):
self._reset_timers()
self._strategy.initialize_state()
self._schedule_update()
+ @gtk_toolbox.log_exception(_moduleLogger)
def _on_timeout(self):
- _moduleLogger.info("%s Update" % (self._name))
+ self._timeoutId = None
+ self._schedule_update()
for item in self._updateItems:
try:
item.update(force=True)
except Exception:
_moduleLogger.exception("Update failed for %r" % item)
- self._timeoutId = None
- self._schedule_update()
return False # do not continue