forked from etotheipi/BitcoinArmory
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qt4reactor.py
361 lines (275 loc) · 10.2 KB
/
qt4reactor.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# Copyright (c) 2001-2011 Twisted Matrix Laboratories.
# See LICENSE_qt4reactor for details.
"""
This module provides support for Twisted to be driven by the Qt mainloop.
In order to use this support, simply do the following::
| app = QApplication(sys.argv) # your code to init Qt
| import qt4reactor
| qt4reactor.install()
alternatively:
| from twisted.application import reactors
| reactors.installReactor('qt4')
Then use twisted.internet APIs as usual. The other methods here are not
intended to be called directly.
If you don't instantiate a QApplication or QCoreApplication prior to
installing the reactor, a QCoreApplication will be constructed
by the reactor. QCoreApplication does not require a GUI so trial testing
can occur normally.
Twisted can be initialized after QApplication.exec_() with a call to
reactor.runReturn(). calling reactor.stop() will unhook twisted but
leave your Qt application running
API Stability: stable
Maintainer: U{Glenn H Tarbox, PhD<mailto:[email protected]>}
Previous maintainer: U{Itamar Shtull-Trauring<mailto:[email protected]>}
Original port to QT4: U{Gabe Rudy<mailto:[email protected]>}
Subsequent port by therve
"""
import sys
import time
from zope.interface import implements
from twisted.internet.interfaces import IReactorFDSet
from twisted.python import log, runtime
from twisted.internet import posixbase
from twisted.python.runtime import platformType, platform
try:
from PyQt4.QtCore import QSocketNotifier, QObject, SIGNAL, QTimer, QCoreApplication
from PyQt4.QtCore import QEventLoop
except ImportError:
from PySide.QtCore import QSocketNotifier, QObject, SIGNAL, QTimer, QCoreApplication
from PySide.QtCore import QEventLoop
class TwistedSocketNotifier(QObject):
"""
Connection between an fd event and reader/writer callbacks.
"""
def __init__(self, parent, reactor, watcher, socketType):
QObject.__init__(self, parent)
self.reactor = reactor
self.watcher = watcher
fd = watcher.fileno()
self.notifier = QSocketNotifier(fd, socketType, parent)
self.notifier.setEnabled(True)
if socketType == QSocketNotifier.Read:
self.fn = self.read
else:
self.fn = self.write
QObject.connect(self.notifier, SIGNAL("activated(int)"), self.fn)
def shutdown(self):
self.notifier.setEnabled(False)
self.disconnect(self.notifier, SIGNAL("activated(int)"), self.fn)
self.fn = self.watcher = None
self.notifier.deleteLater()
self.deleteLater()
def read(self, fd):
if not self.watcher:
return
w = self.watcher
# doRead can cause self.shutdown to be called so keep a reference to self.watcher
def _read():
#Don't call me again, until the data has been read
self.notifier.setEnabled(False)
why = None
try:
why = w.doRead()
inRead = True
except:
inRead = False
log.err()
why = sys.exc_info()[1]
if why:
self.reactor._disconnectSelectable(w, why, inRead)
elif self.watcher:
self.notifier.setEnabled(True) # Re enable notification following sucessfull read
self.reactor._iterate(fromqt=True)
log.callWithLogger(w, _read)
def write(self, sock):
if not self.watcher:
return
w = self.watcher
def _write():
why = None
self.notifier.setEnabled(False)
try:
why = w.doWrite()
except:
log.err()
why = sys.exc_info()[1]
if why:
self.reactor._disconnectSelectable(w, why, False)
elif self.watcher:
self.notifier.setEnabled(True)
self.reactor._iterate(fromqt=True)
log.callWithLogger(w, _write)
class QtReactor(posixbase.PosixReactorBase):
implements(IReactorFDSet)
def __init__(self):
self._reads = {}
self._writes = {}
self._notifiers = {}
self._timer = QTimer()
self._timer.setSingleShot(True)
QObject.connect(self._timer, SIGNAL("timeout()"), self.iterate)
if QCoreApplication.instance() is None:
# Application Object has not been started yet
self.qApp=QCoreApplication([])
self._ownApp=True
else:
self.qApp = QCoreApplication.instance()
self._ownApp=False
self._blockApp = None
posixbase.PosixReactorBase.__init__(self)
def _add(self, xer, primary, type):
"""
Private method for adding a descriptor from the event loop.
It takes care of adding it if new or modifying it if already added
for another state (read -> read/write for example).
"""
if xer not in primary:
primary[xer] = TwistedSocketNotifier(None, self, xer, type)
def addReader(self, reader):
"""
Add a FileDescriptor for notification of data available to read.
"""
self._add(reader, self._reads, QSocketNotifier.Read)
def addWriter(self, writer):
"""
Add a FileDescriptor for notification of data available to write.
"""
self._add(writer, self._writes, QSocketNotifier.Write)
def _remove(self, xer, primary):
"""
Private method for removing a descriptor from the event loop.
It does the inverse job of _add, and also add a check in case of the fd
has gone away.
"""
if xer in primary:
notifier = primary.pop(xer)
notifier.shutdown()
def removeReader(self, reader):
"""
Remove a Selectable for notification of data available to read.
"""
self._remove(reader, self._reads)
def removeWriter(self, writer):
"""
Remove a Selectable for notification of data available to write.
"""
self._remove(writer, self._writes)
def removeAll(self):
"""
Remove all selectables, and return a list of them.
"""
rv = self._removeAll(self._reads, self._writes)
return rv
def getReaders(self):
return self._reads.keys()
def getWriters(self):
return self._writes.keys()
def callLater(self,howlong, *args, **kargs):
rval = super(QtReactor,self).callLater(howlong, *args, **kargs)
self.reactorInvocation()
return rval
def reactorInvocation(self):
self._timer.stop()
self._timer.setInterval(0)
self._timer.start()
def _iterate(self, delay=None, fromqt=False):
"""See twisted.internet.interfaces.IReactorCore.iterate.
"""
self.runUntilCurrent()
self.doIteration(delay, fromqt)
iterate = _iterate
def doIteration(self, delay=None, fromqt=False):
'This method is called by a Qt timer or by network activity on a file descriptor'
if not self.running and self._blockApp:
self._blockApp.quit()
self._timer.stop()
delay = max(delay, 1)
if not fromqt:
self.qApp.processEvents(QEventLoop.AllEvents, delay * 1000)
if self.timeout() is None:
timeout = 0.1
elif self.timeout() == 0:
timeout = 0
else:
timeout = self.timeout()
self._timer.setInterval(timeout * 1000)
self._timer.start()
def runReturn(self, installSignalHandlers=True):
self.startRunning(installSignalHandlers=installSignalHandlers)
self.reactorInvocation()
def run(self, installSignalHandlers=True):
if self._ownApp:
self._blockApp = self.qApp
else:
self._blockApp = QEventLoop()
self.runReturn()
self._blockApp.exec_()
class QtEventReactor(QtReactor):
def __init__(self, *args, **kwargs):
self._events = {}
super(QtEventReactor, self).__init__()
def addEvent(self, event, fd, action):
"""
Add a new win32 event to the event loop.
"""
self._events[event] = (fd, action)
def removeEvent(self, event):
"""
Remove an event.
"""
if event in self._events:
del self._events[event]
def doEvents(self):
handles = self._events.keys()
if len(handles) > 0:
val = None
while val != WAIT_TIMEOUT:
val = MsgWaitForMultipleObjects(handles, 0, 0, QS_ALLINPUT | QS_ALLEVENTS)
if val >= WAIT_OBJECT_0 and val < WAIT_OBJECT_0 + len(handles):
event_id = handles[val - WAIT_OBJECT_0]
if event_id in self._events:
fd, action = self._events[event_id]
log.callWithLogger(fd, self._runAction, action, fd)
elif val == WAIT_TIMEOUT:
pass
else:
#print 'Got an unexpected return of %r' % val
return
def _runAction(self, action, fd):
try:
closed = getattr(fd, action)()
except:
closed = sys.exc_info()[1]
log.deferr()
if closed:
self._disconnectSelectable(fd, closed, action == 'doRead')
def timeout(self):
t = super(QtEventReactor, self).timeout()
return min(t, 0.01)
def iterate(self, delay=None):
"""See twisted.internet.interfaces.IReactorCore.iterate.
"""
self.runUntilCurrent()
self.doEvents()
self.doIteration(delay)
def posixinstall():
"""
Install the Qt reactor.
"""
p = QtReactor()
from twisted.internet.main import installReactor
installReactor(p)
def win32install():
"""
Install the Qt reactor.
"""
p = QtEventReactor()
from twisted.internet.main import installReactor
installReactor(p)
if runtime.platform.getType() == 'win32':
#from win32event import CreateEvent, MsgWaitForMultipleObjects
#from win32event import WAIT_OBJECT_0, WAIT_TIMEOUT, QS_ALLINPUT, QS_ALLEVENTS
install = win32install
else:
install = posixinstall
__all__ = ["install"]