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)
56 self.calcNextActivation()
58 def addTimerEntry(self, entry):
59 # we either go trough Prepare/Start/End-state if the timer is still running,
60 # or skip it when it's alrady past the end.
61 if entry.end > time.time():
62 bisect.insort(self.timer_list, entry)
63 self.calcNextActivation()
65 bisect.insort(self.processed_timers, entry)
67 def setNextActivation(self, when):
68 delay = int((when - time.time()) * 1000)
69 print "[timer.py] next activation: %d (in %d ms)" % (when, delay)
71 self.timer.start(delay, 1)
74 def calcNextActivation(self):
75 self.processActivation()
77 min = int(time.time()) + self.MaxWaitTime
79 # calculate next activation point
80 if len(self.timer_list):
81 w = self.timer_list[0].getTime()
85 self.setNextActivation(min)
87 def timeChanged(self, timer):
88 self.timer_list.remove(timer)
89 bisect.insort(self.timer_list, timer)
91 def doActivate(self, w):
93 self.timer_list.remove(w)
95 if w.state < TimerEntry.StateEnded:
96 bisect.insort(self.timer_list, w)
98 bisect.insort(self.processed_timers, w)
100 def processActivation(self):
101 t = int(time.time()) + 1
103 # we keep on processing the first entry until it goes into the future.
104 while len(self.timer_list) and self.timer_list[0].getTime() < t:
105 self.doActivate(self.timer_list[0])