-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpanda.py
executable file
·126 lines (109 loc) · 3.39 KB
/
panda.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import os, signal
import time
import sys
from sys import stdout
from threading import Thread
import dbus
from dbus.mainloop.glib import DBusGMainLoop
import gobject
class PingContainer:
def __init__(self, mod, ticks):
self.ticks = ticks
self.mod = mod
def getTicks(self):
return self.ticks
def setTicks(self, t):
self.ticks = t
def tick(self):
self.ticks -= 1
return self.ticks <= 0
def done(self):
self.mod.ping()
class PandaService(Thread):
def __init__(self, ports, modules, dbus):
Thread.__init__(self)
self.ports = ports
self.mods = modules
self.dbus = None
self.tickers = []
self.pings = []
self.running = True
self.dbus = dbus
for mod in self.mods:
mod.register(self)
def getDBus(self):
return self.dbus
def registerTicker(self, mod):
self.tickers.append(mod)
def pingModuleIn(self, mod, ticks):
if not self.pings:
self.pings.append(PingContainer(mod, ticks))
else:
i = 0
after = self.pings[i]
t = after.getTicks()
while t < ticks and i < len(self.pings) - 1:
i += 1
after = self.pings[i]
t += after.getTicks()
if i >= len(self.pings):
self.pings.append(PingContainer(mod, t - ticks))
else:
after.setTicks(t - ticks)
before = 0
if i > 0:
before = self.pings[i-1].getTicks()
self.pings.insert(i, PingContainer(mod, ticks - before))
def cancel(self):
self.running = False
def run(self):
try:
while self.running:
refresh = False
if self.pings:
nextPing = self.pings[0]
if nextPing.tick():
# God, this is horrible. Sorry.
while self.pings and self.pings[0].getTicks() <= 0:
self.pings.pop(0).done()
refresh = True
for mod in self.tickers:
if mod.tick():
refresh = True
if refresh:
self.redraw()
time.sleep(1)
except Exception as e:
print "Exception is: %s" % (e)
return
def redraw(self):
line = '[' + ','.join(filter(None, [p.getView() for p in self.ports])) + '],'
stdout.write(line.encode('utf-8'))
stdout.flush()
class PandaMain:
def __init__(self, ports, modules):
self.ports = ports
self.modules = modules
def run(self):
svc = None
mainloop = None
try:
gobject.threads_init()
DBusGMainLoop(set_as_default=True)
bus = dbus.SessionBus()
svc = PandaService(self.ports, self.modules, bus)
svc.start()
mainloop = gobject.MainLoop()
mainloop.run()
except Exception as e:
print "Exception: %s" % (e)
if svc:
svc.cancel()
finally:
if mainloop:
mainloop.quit()
if svc:
svc.cancel()
svc.join()