Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

_glibbase.py 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. # -*- test-case-name: twisted.internet.test -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. This module provides base support for Twisted to interact with the glib/gtk
  6. mainloops.
  7. The classes in this module should not be used directly, but rather you should
  8. import gireactor or gtk3reactor for GObject Introspection based applications,
  9. or glib2reactor or gtk2reactor for applications using legacy static bindings.
  10. """
  11. import sys
  12. from zope.interface import implementer
  13. from twisted.internet import base, posixbase, selectreactor
  14. from twisted.internet.interfaces import IReactorFDSet
  15. from twisted.python import log
  16. from ._signals import _UnixWaker
  17. def ensureNotImported(moduleNames, errorMessage, preventImports=[]):
  18. """
  19. Check whether the given modules were imported, and if requested, ensure
  20. they will not be importable in the future.
  21. @param moduleNames: A list of module names we make sure aren't imported.
  22. @type moduleNames: C{list} of C{str}
  23. @param preventImports: A list of module name whose future imports should
  24. be prevented.
  25. @type preventImports: C{list} of C{str}
  26. @param errorMessage: Message to use when raising an C{ImportError}.
  27. @type errorMessage: C{str}
  28. @raise ImportError: with given error message if a given module name
  29. has already been imported.
  30. """
  31. for name in moduleNames:
  32. if sys.modules.get(name) is not None:
  33. raise ImportError(errorMessage)
  34. # Disable module imports to avoid potential problems.
  35. for name in preventImports:
  36. sys.modules[name] = None
  37. class GlibWaker(_UnixWaker):
  38. """
  39. Run scheduled events after waking up.
  40. """
  41. def doRead(self) -> None:
  42. super().doRead()
  43. self.reactor._simulate()
  44. @implementer(IReactorFDSet)
  45. class GlibReactorBase(posixbase.PosixReactorBase, posixbase._PollLikeMixin):
  46. """
  47. Base class for GObject event loop reactors.
  48. Notification for I/O events (reads and writes on file descriptors) is done
  49. by the gobject-based event loop. File descriptors are registered with
  50. gobject with the appropriate flags for read/write/disconnect notification.
  51. Time-based events, the results of C{callLater} and C{callFromThread}, are
  52. handled differently. Rather than registering each event with gobject, a
  53. single gobject timeout is registered for the earliest scheduled event, the
  54. output of C{reactor.timeout()}. For example, if there are timeouts in 1, 2
  55. and 3.4 seconds, a single timeout is registered for 1 second in the
  56. future. When this timeout is hit, C{_simulate} is called, which calls the
  57. appropriate Twisted-level handlers, and a new timeout is added to gobject
  58. by the C{_reschedule} method.
  59. To handle C{callFromThread} events, we use a custom waker that calls
  60. C{_simulate} whenever it wakes up.
  61. @ivar _sources: A dictionary mapping L{FileDescriptor} instances to
  62. GSource handles.
  63. @ivar _reads: A set of L{FileDescriptor} instances currently monitored for
  64. reading.
  65. @ivar _writes: A set of L{FileDescriptor} instances currently monitored for
  66. writing.
  67. @ivar _simtag: A GSource handle for the next L{simulate} call.
  68. """
  69. # Install a waker that knows it needs to call C{_simulate} in order to run
  70. # callbacks queued from a thread:
  71. _wakerFactory = GlibWaker
  72. def __init__(self, glib_module, gtk_module, useGtk=False):
  73. self._simtag = None
  74. self._reads = set()
  75. self._writes = set()
  76. self._sources = {}
  77. self._glib = glib_module
  78. self._gtk = gtk_module
  79. posixbase.PosixReactorBase.__init__(self)
  80. self._source_remove = self._glib.source_remove
  81. self._timeout_add = self._glib.timeout_add
  82. def _mainquit():
  83. if self._gtk.main_level():
  84. self._gtk.main_quit()
  85. if useGtk:
  86. self._pending = self._gtk.events_pending
  87. self._iteration = self._gtk.main_iteration_do
  88. self._crash = _mainquit
  89. self._run = self._gtk.main
  90. else:
  91. self.context = self._glib.main_context_default()
  92. self._pending = self.context.pending
  93. self._iteration = self.context.iteration
  94. self.loop = self._glib.MainLoop()
  95. self._crash = lambda: self._glib.idle_add(self.loop.quit)
  96. self._run = self.loop.run
  97. def _handleSignals(self):
  98. # First, install SIGINT and friends:
  99. base._SignalReactorMixin._handleSignals(self)
  100. # Next, since certain versions of gtk will clobber our signal handler,
  101. # set all signal handlers again after the event loop has started to
  102. # ensure they're *really* set. We don't call this twice so we don't
  103. # leak file descriptors created in the SIGCHLD initialization:
  104. self.callLater(0, posixbase.PosixReactorBase._handleSignals, self)
  105. # The input_add function in pygtk1 checks for objects with a
  106. # 'fileno' method and, if present, uses the result of that method
  107. # as the input source. The pygtk2 input_add does not do this. The
  108. # function below replicates the pygtk1 functionality.
  109. # In addition, pygtk maps gtk.input_add to _gobject.io_add_watch, and
  110. # g_io_add_watch() takes different condition bitfields than
  111. # gtk_input_add(). We use g_io_add_watch() here in case pygtk fixes this
  112. # bug.
  113. def input_add(self, source, condition, callback):
  114. if hasattr(source, "fileno"):
  115. # handle python objects
  116. def wrapper(ignored, condition):
  117. return callback(source, condition)
  118. fileno = source.fileno()
  119. else:
  120. fileno = source
  121. wrapper = callback
  122. return self._glib.io_add_watch(
  123. fileno, condition, wrapper, priority=self._glib.PRIORITY_DEFAULT_IDLE
  124. )
  125. def _ioEventCallback(self, source, condition):
  126. """
  127. Called by event loop when an I/O event occurs.
  128. """
  129. log.callWithLogger(source, self._doReadOrWrite, source, source, condition)
  130. return True # True = don't auto-remove the source
  131. def _add(self, source, primary, other, primaryFlag, otherFlag):
  132. """
  133. Add the given L{FileDescriptor} for monitoring either for reading or
  134. writing. If the file is already monitored for the other operation, we
  135. delete the previous registration and re-register it for both reading
  136. and writing.
  137. """
  138. if source in primary:
  139. return
  140. flags = primaryFlag
  141. if source in other:
  142. self._source_remove(self._sources[source])
  143. flags |= otherFlag
  144. self._sources[source] = self.input_add(source, flags, self._ioEventCallback)
  145. primary.add(source)
  146. def addReader(self, reader):
  147. """
  148. Add a L{FileDescriptor} for monitoring of data available to read.
  149. """
  150. self._add(reader, self._reads, self._writes, self.INFLAGS, self.OUTFLAGS)
  151. def addWriter(self, writer):
  152. """
  153. Add a L{FileDescriptor} for monitoring ability to write data.
  154. """
  155. self._add(writer, self._writes, self._reads, self.OUTFLAGS, self.INFLAGS)
  156. def getReaders(self):
  157. """
  158. Retrieve the list of current L{FileDescriptor} monitored for reading.
  159. """
  160. return list(self._reads)
  161. def getWriters(self):
  162. """
  163. Retrieve the list of current L{FileDescriptor} monitored for writing.
  164. """
  165. return list(self._writes)
  166. def removeAll(self):
  167. """
  168. Remove monitoring for all registered L{FileDescriptor}s.
  169. """
  170. return self._removeAll(self._reads, self._writes)
  171. def _remove(self, source, primary, other, flags):
  172. """
  173. Remove monitoring the given L{FileDescriptor} for either reading or
  174. writing. If it's still monitored for the other operation, we
  175. re-register the L{FileDescriptor} for only that operation.
  176. """
  177. if source not in primary:
  178. return
  179. self._source_remove(self._sources[source])
  180. primary.remove(source)
  181. if source in other:
  182. self._sources[source] = self.input_add(source, flags, self._ioEventCallback)
  183. else:
  184. self._sources.pop(source)
  185. def removeReader(self, reader):
  186. """
  187. Stop monitoring the given L{FileDescriptor} for reading.
  188. """
  189. self._remove(reader, self._reads, self._writes, self.OUTFLAGS)
  190. def removeWriter(self, writer):
  191. """
  192. Stop monitoring the given L{FileDescriptor} for writing.
  193. """
  194. self._remove(writer, self._writes, self._reads, self.INFLAGS)
  195. def iterate(self, delay=0):
  196. """
  197. One iteration of the event loop, for trial's use.
  198. This is not used for actual reactor runs.
  199. """
  200. self.runUntilCurrent()
  201. while self._pending():
  202. self._iteration(0)
  203. def crash(self):
  204. """
  205. Crash the reactor.
  206. """
  207. posixbase.PosixReactorBase.crash(self)
  208. self._crash()
  209. def stop(self):
  210. """
  211. Stop the reactor.
  212. """
  213. posixbase.PosixReactorBase.stop(self)
  214. # The base implementation only sets a flag, to ensure shutting down is
  215. # not reentrant. Unfortunately, this flag is not meaningful to the
  216. # gobject event loop. We therefore call wakeUp() to ensure the event
  217. # loop will call back into Twisted once this iteration is done. This
  218. # will result in self.runUntilCurrent() being called, where the stop
  219. # flag will trigger the actual shutdown process, eventually calling
  220. # crash() which will do the actual gobject event loop shutdown.
  221. self.wakeUp()
  222. def run(self, installSignalHandlers=True):
  223. """
  224. Run the reactor.
  225. """
  226. self.callWhenRunning(self._reschedule)
  227. self.startRunning(installSignalHandlers=installSignalHandlers)
  228. if self._started:
  229. self._run()
  230. def callLater(self, *args, **kwargs):
  231. """
  232. Schedule a C{DelayedCall}.
  233. """
  234. result = posixbase.PosixReactorBase.callLater(self, *args, **kwargs)
  235. # Make sure we'll get woken up at correct time to handle this new
  236. # scheduled call:
  237. self._reschedule()
  238. return result
  239. def _reschedule(self):
  240. """
  241. Schedule a glib timeout for C{_simulate}.
  242. """
  243. if self._simtag is not None:
  244. self._source_remove(self._simtag)
  245. self._simtag = None
  246. timeout = self.timeout()
  247. if timeout is not None:
  248. self._simtag = self._timeout_add(
  249. int(timeout * 1000),
  250. self._simulate,
  251. priority=self._glib.PRIORITY_DEFAULT_IDLE,
  252. )
  253. def _simulate(self):
  254. """
  255. Run timers, and then reschedule glib timeout for next scheduled event.
  256. """
  257. self.runUntilCurrent()
  258. self._reschedule()
  259. class PortableGlibReactorBase(selectreactor.SelectReactor):
  260. """
  261. Base class for GObject event loop reactors that works on Windows.
  262. Sockets aren't supported by GObject's input_add on Win32.
  263. """
  264. def __init__(self, glib_module, gtk_module, useGtk=False):
  265. self._simtag = None
  266. self._glib = glib_module
  267. self._gtk = gtk_module
  268. selectreactor.SelectReactor.__init__(self)
  269. self._source_remove = self._glib.source_remove
  270. self._timeout_add = self._glib.timeout_add
  271. def _mainquit():
  272. if self._gtk.main_level():
  273. self._gtk.main_quit()
  274. if useGtk:
  275. self._crash = _mainquit
  276. self._run = self._gtk.main
  277. else:
  278. self.loop = self._glib.MainLoop()
  279. self._crash = lambda: self._glib.idle_add(self.loop.quit)
  280. self._run = self.loop.run
  281. def crash(self):
  282. selectreactor.SelectReactor.crash(self)
  283. self._crash()
  284. def run(self, installSignalHandlers=True):
  285. self.startRunning(installSignalHandlers=installSignalHandlers)
  286. self._timeout_add(0, self.simulate)
  287. if self._started:
  288. self._run()
  289. def simulate(self):
  290. """
  291. Run simulation loops and reschedule callbacks.
  292. """
  293. if self._simtag is not None:
  294. self._source_remove(self._simtag)
  295. self.iterate()
  296. timeout = self.timeout()
  297. if timeout is None or timeout > 0.01:
  298. timeout = 0.01
  299. self._simtag = self._timeout_add(
  300. int(timeout * 1000),
  301. self.simulate,
  302. priority=self._glib.PRIORITY_DEFAULT_IDLE,
  303. )