16 def __init__(self, begin, end):
18 self.prepare_time = 10
23 if self.state == self.StateWait:
24 return self.begin - self.prepare_time
25 elif self.state == self.StatePrepare:
31 return self.getTime() < o.getTime()
33 def activate(self, event):
34 print "[timer.py] timer %s got activated (%d)!" % (self.description, event)
38 # the time between "polls". We do this because
39 # we want to account for time jumps etc.
40 # of course if they occur <100s before starting,
41 # it's not good. thus, you have to repoll when
42 # you change the time.
44 # this is just in case. We don't want the timer
45 # hanging. we use this "edge-triggered-polling-scheme"
46 # anyway, so why don't make it a bit more fool-proof?
51 self.processed_timers = [ ]
54 self.timer.timeout.get().append(self.calcNextActivation)
55 self.lastActivation = time.time()
57 self.calcNextActivation()
59 def addTimerEntry(self, entry, noRecalc=0):
60 # we either go trough Prepare/Start/End-state if the timer is still running,
61 # or skip it when it's alrady past the end.
62 if entry.end > time.time():
63 bisect.insort(self.timer_list, entry)
65 self.calcNextActivation()
67 bisect.insort(self.processed_timers, entry)
69 def setNextActivation(self, when):
70 delay = int((when - time.time()) * 1000)
71 print "[timer.py] next activation: %d (in %d ms)" % (when, delay)
73 self.timer.start(delay, 1)
76 def calcNextActivation(self):
77 if self.lastActivation > time.time():
78 print "[timer.py] timewarp - re-evaluating all processed timers."
79 tl = self.processed_timers
80 self.processed_timers = [ ]
82 self.addTimerEntry(x, noRecalc=1)
84 self.processActivation()
85 self.lastActivation = time.time()
87 min = int(time.time()) + self.MaxWaitTime
89 # calculate next activation point
90 if len(self.timer_list):
91 w = self.timer_list[0].getTime()
95 self.setNextActivation(min)
97 def timeChanged(self, timer):
98 self.timer_list.remove(timer)
99 self.addTimerEntry(timer)
101 def doActivate(self, w):
103 self.timer_list.remove(w)
105 if w.state < TimerEntry.StateEnded:
106 bisect.insort(self.timer_list, w)
108 bisect.insort(self.processed_timers, w)
110 def processActivation(self):
111 t = int(time.time()) + 1
113 # we keep on processing the first entry until it goes into the future.
114 while len(self.timer_list) and self.timer_list[0].getTime() < t:
115 self.doActivate(self.timer_list[0])