Updating todos and logging
[theonering] / src / gvoice / state_machine.py
index d26a57c..511ccde 100644 (file)
@@ -1,8 +1,7 @@
 #!/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
@@ -65,13 +64,13 @@ class ConstantStateStrategy(object):
 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
@@ -151,7 +150,8 @@ class UpdateStateMachine(StateMachine):
 
        _IS_DAEMON = True
 
-       def __init__(self, updateItems):
+       def __init__(self, updateItems, name=""):
+               self._name = name
                self._updateItems = updateItems
 
                self._state = self.STATE_ACTIVE
@@ -171,13 +171,16 @@ class UpdateStateMachine(StateMachine):
                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):
@@ -194,6 +197,7 @@ class UpdateStateMachine(StateMachine):
                return self._state
 
        def reset_timers(self):
+               _moduleLogger.info("%s Resetting State Machine" % (self._name, ))
                self._reset_timers()
 
        @property
@@ -204,11 +208,6 @@ class UpdateStateMachine(StateMachine):
        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()
@@ -237,13 +236,13 @@ class UpdateStateMachine(StateMachine):
                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