Adding support for coroutines for async ops through trampolines
[theonering] / hand_tests / sm.py
1 #!/usr/bin/env python
2
3 import threading
4 import datetime
5 import time
6 import logging
7
8 import gtk
9
10 import sys
11 sys.path.insert(0,"../src")
12 import gvoice.state_machine as state_machine
13
14
15 class _I(object):
16
17         def __init__(self, startTime):
18                 self._startTime = startTime
19
20         def update(self, force = False):
21                 print "%s\t%r: force=%r" % (datetime.datetime.now() - self._startTime, self, force)
22
23
24 def loop(state):
25
26         def actual():
27                 while state[0]:
28                         gtk.main_iteration(block=False)
29                         time.sleep(0.1)
30
31         return actual
32
33
34 def main():
35         logging.basicConfig(level=logging.DEBUG)
36         startTime = datetime.datetime.now()
37
38         state = [True]
39         mainLoop = threading.Thread(target=loop(state))
40         mainLoop.setDaemon(False)
41         mainLoop.start()
42         try:
43                 state_machine.StateMachine._IS_DAEMON = False
44
45                 regular = _I(startTime)
46                 print "Regular:", regular
47
48                 sm = state_machine.UpdateStateMachine([regular])
49                 sm.set_state_strategy(
50                         state_machine.StateMachine.STATE_DND,
51                         state_machine.NopStateStrategy(),
52                 )
53                 sm.set_state_strategy(
54                         state_machine.StateMachine.STATE_IDLE,
55                         state_machine.ConstantStateStrategy(state_machine.to_milliseconds(seconds=30)),
56                 )
57                 sm.set_state_strategy(
58                         state_machine.StateMachine.STATE_ACTIVE,
59                         state_machine.GeometricStateStrategy(
60                                 state_machine.to_milliseconds(seconds=3),
61                                 state_machine.to_milliseconds(seconds=3),
62                                 state_machine.to_milliseconds(seconds=20),
63                         ),
64                 )
65                 print "Starting", datetime.datetime.now() - startTime
66                 sm.start()
67                 time.sleep(60.0) # seconds
68                 print "Reseting timers", datetime.datetime.now() - startTime
69                 sm.reset_timers()
70                 time.sleep(60.0) # seconds
71                 print "Switching to IDLE", datetime.datetime.now() - startTime
72                 sm.set_state(state_machine.StateMachine.STATE_IDLE)
73                 time.sleep(10.0) # seconds
74                 print "Stopping", datetime.datetime.now() - startTime
75                 sm.stop()
76         finally:
77                 state[0] = False
78
79
80 if __name__ == "__main__":
81         main()