slixmpp/sleekxmpp/thirdparty/statemachine.py

295 lines
12 KiB
Python
Raw Normal View History

2010-10-14 01:15:21 +00:00
"""
SleekXMPP: The Sleek XMPP Library
Copyright (C) 2010 Nathanael C. Fritz
This file is part of SleekXMPP.
See the file LICENSE for copying permission.
"""
2010-10-14 01:15:21 +00:00
import threading
import time
import logging
log = logging.getLogger(__name__)
class StateMachine(object):
def __init__(self, states=None):
if not states: states = []
self.lock = threading.Condition()
2010-10-21 02:43:53 +00:00
self.__states = []
2010-10-20 23:57:47 +00:00
self.addStates(states)
self.__default_state = self.__states[0]
self.__current_state = self.__default_state
2010-10-20 23:57:47 +00:00
def addStates(self, states):
self.lock.acquire()
try:
for state in states:
if state in self.__states:
raise IndexError("The state '%s' is already in the StateMachine." % state)
2010-10-21 02:43:53 +00:00
self.__states.append(state)
finally:
self.lock.release()
def transition(self, from_state, to_state, wait=0.0, func=None, args=None, kwargs=None):
2010-10-20 23:57:47 +00:00
'''
Transition from the given `from_state` to the given `to_state`.
2010-10-20 23:57:47 +00:00
This method will return `True` if the state machine is now in `to_state`. It
will return `False` if a timeout occurred the transition did not occur.
If `wait` is 0 (the default,) this method returns immediately if the state machine
2010-10-20 23:57:47 +00:00
is not in `from_state`.
If you want the thread to block and transition once the state machine to enters
`from_state`, set `wait` to a non-negative value. Note there is no 'block
indefinitely' flag since this leads to deadlock. If you want to wait indefinitely,
2010-10-20 23:57:47 +00:00
choose a reasonable value for `wait` (e.g. 20 seconds) and do so in a while loop like so:
::
while not thread_should_exit and not state_machine.transition('disconnected', 'connecting', wait=20 ):
pass # timeout will occur every 20s unless transition occurs
if thread_should_exit: return
# perform actions here after successful transition
This allows the thread to be responsive by setting `thread_should_exit=True`.
The optional `func` argument allows the user to pass a callable operation which occurs
within the context of the state transition (e.g. while the state machine is locked.)
If `func` returns a True value, the transition will occur. If `func` returns a non-
True value or if an exception is thrown, the transition will not occur. Any thrown
exception is not caught by the state machine and is the caller's responsibility to handle.
If `func` completes normally, this method will return the value returned by `func.` If
values for `args` and `kwargs` are provided, they are expanded and passed like so:
2010-10-20 23:57:47 +00:00
`func( *args, **kwargs )`.
'''
if not args:
args = []
if not kwargs:
kwargs = {}
2010-10-20 23:57:47 +00:00
return self.transition_any((from_state,), to_state, wait=wait,
2010-10-21 02:43:53 +00:00
func=func, args=args, kwargs=kwargs)
def transition_any(self, from_states, to_state, wait=0.0, func=None, args=None, kwargs=None):
2010-10-20 23:57:47 +00:00
'''
Transition from any of the given `from_states` to the given `to_state`.
'''
if not args:
args = []
if not kwargs:
kwargs = {}
2010-10-20 23:57:47 +00:00
if not isinstance(from_states, (tuple, list, set)):
raise ValueError("from_states should be a list, tuple, or set")
2010-10-20 23:57:47 +00:00
for state in from_states:
if not state in self.__states:
2010-10-21 02:43:53 +00:00
raise ValueError("StateMachine does not contain from_state %s." % state)
if not to_state in self.__states:
2010-10-21 02:43:53 +00:00
raise ValueError("StateMachine does not contain to_state %s." % to_state)
2010-10-20 23:57:47 +00:00
if self.__current_state == to_state:
return True
2010-10-20 23:57:47 +00:00
start = time.time()
while not self.lock.acquire(False):
time.sleep(.001)
if (start + wait - time.time()) <= 0.0:
log.debug("==== Could not acquire lock in %s sec: %s -> %s ", wait, self.__current_state, to_state)
2010-10-20 23:57:47 +00:00
return False
while not self.__current_state in from_states:
# detect timeout:
remainder = start + wait - time.time()
if remainder > 0:
self.lock.wait(remainder)
else:
log.debug("State was not ready")
self.lock.release()
return False
2010-10-20 23:57:47 +00:00
try: # lock is acquired; all other threads will return false or wait until notify/timeout
if self.__current_state in from_states: # should always be True due to lock
# Note that func might throw an exception, but that's OK, it aborts the transition
return_val = func(*args,**kwargs) if func is not None else True
# some 'false' value returned from func,
2010-10-20 23:57:47 +00:00
# indicating that transition should not occur:
if not return_val:
return return_val
2010-10-20 23:57:47 +00:00
log.debug(' ==== TRANSITION %s -> %s', self.__current_state, to_state)
2010-10-21 02:43:53 +00:00
self._set_state(to_state)
2010-10-20 23:57:47 +00:00
return return_val # some 'true' value returned by func or True if func was None
else:
2010-10-21 02:43:53 +00:00
log.error("StateMachine bug!! The lock should ensure this doesn't happen!")
2010-10-20 23:57:47 +00:00
return False
finally:
self.lock.notify_all()
2010-10-20 23:57:47 +00:00
self.lock.release()
def transition_ctx(self, from_state, to_state, wait=0.0):
'''
Use the state machine as a context manager. The transition occurs on /exit/ from
the `with` context, so long as no exception is thrown. For example:
2010-10-20 23:57:47 +00:00
::
with state_machine.transition_ctx('one','two', wait=5) as locked:
if locked:
# the state machine is currently locked in state 'one', and will
# transition to 'two' when the 'with' statement ends, so long as
2010-10-20 23:57:47 +00:00
# no exception is thrown.
print 'Currently locked in state one: %s' % state_machine['one']
else:
# The 'wait' timed out, and no lock has been acquired
print 'Timed out before entering state "one"'
print 'Since no exception was thrown, we are now in state "two": %s' % state_machine['two']
The other main difference between this method and `transition()` is that the
state machine is locked for the duration of the `with` statement. Normally,
after a `transition()` occurs, the state machine is immediately unlocked and
2010-10-20 23:57:47 +00:00
available to another thread to call `transition()` again.
'''
if not from_state in self.__states:
2010-10-21 02:43:53 +00:00
raise ValueError("StateMachine does not contain from_state %s." % from_state)
if not to_state in self.__states:
2010-10-21 02:43:53 +00:00
raise ValueError("StateMachine does not contain to_state %s." % to_state)
2010-10-20 23:57:47 +00:00
return _StateCtx(self, from_state, to_state, wait)
2010-10-21 02:43:53 +00:00
def ensure(self, state, wait=0.0, block_on_transition=False):
2010-10-20 23:57:47 +00:00
'''
Ensure the state machine is currently in `state`, or wait until it enters `state`.
'''
2010-10-21 02:43:53 +00:00
return self.ensure_any((state,), wait=wait, block_on_transition=block_on_transition)
2010-10-20 23:57:47 +00:00
def ensure_any(self, states, wait=0.0, block_on_transition=False):
'''
Ensure we are currently in one of the given `states` or wait until
we enter one of those states.
Note that due to the nature of the function, you cannot guarantee that
2010-10-20 23:57:47 +00:00
the entirety of some operation completes while you remain in a given
state. That would require acquiring and holding a lock, which
2010-10-20 23:57:47 +00:00
would mean no other threads could do the same. (You'd essentially
be serializing all of the threads that are 'ensuring' their tasks
occurred in some state.
2010-10-20 23:57:47 +00:00
'''
if not (isinstance(states,tuple) or isinstance(states,list)):
2010-10-20 23:57:47 +00:00
raise ValueError('states arg should be a tuple or list')
for state in states:
if not state in self.__states:
2010-10-21 02:43:53 +00:00
raise ValueError("StateMachine does not contain state '%s'" % state)
2010-10-20 23:57:47 +00:00
# if we're in the middle of a transition, determine whether we should
# 'fall back' to the 'current' state, or wait for the new state, in order to
2010-10-20 23:57:47 +00:00
# avoid an operation occurring in the wrong state.
# TODO another option would be an ensure_ctx that uses a semaphore to allow
2010-10-20 23:57:47 +00:00
# threads to indicate they want to remain in a particular state.
self.lock.acquire()
2010-10-20 23:57:47 +00:00
start = time.time()
while not self.__current_state in states:
2010-10-20 23:57:47 +00:00
# detect timeout:
remainder = start + wait - time.time()
if remainder > 0:
self.lock.wait(remainder)
else:
self.lock.release()
return False
self.lock.release()
2010-10-20 23:57:47 +00:00
return True
def reset(self):
# TODO need to lock before calling this?
2010-10-20 23:57:47 +00:00
self.transition(self.__current_state, self.__default_state)
def _set_state(self, state): #unsynchronized, only call internally after lock is acquired
self.__current_state = state
return state
def current_state(self):
'''
Return the current state name.
'''
return self.__current_state
def __getitem__(self, state):
'''
Non-blocking, non-synchronized test to determine if we are in the given state.
Use `StateMachine.ensure(state)` to wait until the machine enters a certain state.
'''
return self.__current_state == state
def __str__(self):
2010-10-21 02:43:53 +00:00
return "".join(("StateMachine(", ','.join(self.__states), "): ", self.__current_state))
2010-10-20 23:57:47 +00:00
2010-10-14 01:15:21 +00:00
class _StateCtx:
2010-10-21 02:43:53 +00:00
def __init__(self, state_machine, from_state, to_state, wait):
2010-10-20 23:57:47 +00:00
self.state_machine = state_machine
self.from_state = from_state
self.to_state = to_state
self.wait = wait
self._locked = False
def __enter__(self):
start = time.time()
while not self.state_machine[self.from_state] or not self.state_machine.lock.acquire(False):
2010-10-20 23:57:47 +00:00
# detect timeout:
remainder = start + self.wait - time.time()
if remainder > 0:
self.state_machine.lock.wait(remainder)
else:
2010-10-21 02:43:53 +00:00
log.debug('StateMachine timeout while waiting for state: %s', self.from_state)
2010-10-20 23:57:47 +00:00
return False
self._locked = True # lock has been acquired at this point
self.state_machine.lock.clear()
log.debug('StateMachine entered context in state: %s',
2010-10-21 02:43:53 +00:00
self.state_machine.current_state())
2010-10-20 23:57:47 +00:00
return True
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_val is not None:
log.exception("StateMachine exception in context, remaining in state: %s\n%s:%s",
2010-10-21 02:43:53 +00:00
self.state_machine.current_state(), exc_type.__name__, exc_val)
2010-10-20 23:57:47 +00:00
if self._locked:
if exc_val is None:
log.debug(' ==== TRANSITION %s -> %s',
2010-10-20 23:57:47 +00:00
self.state_machine.current_state(), self.to_state)
2010-10-21 02:43:53 +00:00
self.state_machine._set_state(self.to_state)
2010-10-20 23:57:47 +00:00
self.state_machine.lock.notify_all()
2010-10-20 23:57:47 +00:00
self.state_machine.lock.release()
return False # re-raise any exception
2010-10-14 01:15:21 +00:00
if __name__ == '__main__':
2010-10-20 23:57:47 +00:00
def callback(s, s2):
print((1, s.transition('on', 'off', wait=0.0, func=callback, args=[s,s2])))
print((2, s2.transition('off', 'on', func=callback, args=[s,s2])))
return True
2010-10-14 01:15:21 +00:00
2010-10-20 23:57:47 +00:00
s = StateMachine(('off', 'on'))
s2 = StateMachine(('off', 'on'))
print((3, s.transition('off', 'on', wait=0.0, func=callback, args=[s,s2]),))
print((s.current_state(), s2.current_state()))