internet.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. # -*- test-case-name: twisted.application.test.test_internet,twisted.test.test_application,twisted.test.test_cooperator -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Reactor-based Services
  6. Here are services to run clients, servers and periodic services using
  7. the reactor.
  8. If you want to run a server service, L{StreamServerEndpointService} defines a
  9. service that can wrap an arbitrary L{IStreamServerEndpoint
  10. <twisted.internet.interfaces.IStreamServerEndpoint>}
  11. as an L{IService}. See also L{twisted.application.strports.service} for
  12. constructing one of these directly from a descriptive string.
  13. Additionally, this module (dynamically) defines various Service subclasses that
  14. let you represent clients and servers in a Service hierarchy. Endpoints APIs
  15. should be preferred for stream server services, but since those APIs do not yet
  16. exist for clients or datagram services, many of these are still useful.
  17. They are as follows::
  18. TCPServer, TCPClient,
  19. UNIXServer, UNIXClient,
  20. SSLServer, SSLClient,
  21. UDPServer,
  22. UNIXDatagramServer, UNIXDatagramClient,
  23. MulticastServer
  24. These classes take arbitrary arguments in their constructors and pass
  25. them straight on to their respective reactor.listenXXX or
  26. reactor.connectXXX calls.
  27. For example, the following service starts a web server on port 8080:
  28. C{TCPServer(8080, server.Site(r))}. See the documentation for the
  29. reactor.listen/connect* methods for more information.
  30. """
  31. from typing import List
  32. from twisted.application import service
  33. from twisted.internet import task
  34. from twisted.internet.defer import CancelledError
  35. from twisted.python import log
  36. from ._client_service import ClientService, _maybeGlobalReactor, backoffPolicy
  37. class _VolatileDataService(service.Service):
  38. volatile: List[str] = []
  39. def __getstate__(self):
  40. d = service.Service.__getstate__(self)
  41. for attr in self.volatile:
  42. if attr in d:
  43. del d[attr]
  44. return d
  45. class _AbstractServer(_VolatileDataService):
  46. """
  47. @cvar volatile: list of attribute to remove from pickling.
  48. @type volatile: C{list}
  49. @ivar method: the type of method to call on the reactor, one of B{TCP},
  50. B{UDP}, B{SSL} or B{UNIX}.
  51. @type method: C{str}
  52. @ivar reactor: the current running reactor.
  53. @type reactor: a provider of C{IReactorTCP}, C{IReactorUDP},
  54. C{IReactorSSL} or C{IReactorUnix}.
  55. @ivar _port: instance of port set when the service is started.
  56. @type _port: a provider of L{twisted.internet.interfaces.IListeningPort}.
  57. """
  58. volatile = ["_port"]
  59. method: str = ""
  60. reactor = None
  61. _port = None
  62. def __init__(self, *args, **kwargs):
  63. self.args = args
  64. if "reactor" in kwargs:
  65. self.reactor = kwargs.pop("reactor")
  66. self.kwargs = kwargs
  67. def privilegedStartService(self):
  68. service.Service.privilegedStartService(self)
  69. self._port = self._getPort()
  70. def startService(self):
  71. service.Service.startService(self)
  72. if self._port is None:
  73. self._port = self._getPort()
  74. def stopService(self):
  75. service.Service.stopService(self)
  76. # TODO: if startup failed, should shutdown skip stopListening?
  77. # _port won't exist
  78. if self._port is not None:
  79. d = self._port.stopListening()
  80. del self._port
  81. return d
  82. def _getPort(self):
  83. """
  84. Wrapper around the appropriate listen method of the reactor.
  85. @return: the port object returned by the listen method.
  86. @rtype: an object providing
  87. L{twisted.internet.interfaces.IListeningPort}.
  88. """
  89. return getattr(
  90. _maybeGlobalReactor(self.reactor),
  91. "listen{}".format(
  92. self.method,
  93. ),
  94. )(*self.args, **self.kwargs)
  95. class _AbstractClient(_VolatileDataService):
  96. """
  97. @cvar volatile: list of attribute to remove from pickling.
  98. @type volatile: C{list}
  99. @ivar method: the type of method to call on the reactor, one of B{TCP},
  100. B{UDP}, B{SSL} or B{UNIX}.
  101. @type method: C{str}
  102. @ivar reactor: the current running reactor.
  103. @type reactor: a provider of C{IReactorTCP}, C{IReactorUDP},
  104. C{IReactorSSL} or C{IReactorUnix}.
  105. @ivar _connection: instance of connection set when the service is started.
  106. @type _connection: a provider of L{twisted.internet.interfaces.IConnector}.
  107. """
  108. volatile = ["_connection"]
  109. method: str = ""
  110. reactor = None
  111. _connection = None
  112. def __init__(self, *args, **kwargs):
  113. self.args = args
  114. if "reactor" in kwargs:
  115. self.reactor = kwargs.pop("reactor")
  116. self.kwargs = kwargs
  117. def startService(self):
  118. service.Service.startService(self)
  119. self._connection = self._getConnection()
  120. def stopService(self):
  121. service.Service.stopService(self)
  122. if self._connection is not None:
  123. self._connection.disconnect()
  124. del self._connection
  125. def _getConnection(self):
  126. """
  127. Wrapper around the appropriate connect method of the reactor.
  128. @return: the port object returned by the connect method.
  129. @rtype: an object providing L{twisted.internet.interfaces.IConnector}.
  130. """
  131. return getattr(_maybeGlobalReactor(self.reactor), f"connect{self.method}")(
  132. *self.args, **self.kwargs
  133. )
  134. _clientDoc = """Connect to {tran}
  135. Call reactor.connect{tran} when the service starts, with the
  136. arguments given to the constructor.
  137. """
  138. _serverDoc = """Serve {tran} clients
  139. Call reactor.listen{tran} when the service starts, with the
  140. arguments given to the constructor. When the service stops,
  141. stop listening. See twisted.internet.interfaces for documentation
  142. on arguments to the reactor method.
  143. """
  144. class TCPServer(_AbstractServer):
  145. __doc__ = _serverDoc.format(tran="TCP")
  146. method = "TCP"
  147. class TCPClient(_AbstractClient):
  148. __doc__ = _clientDoc.format(tran="TCP")
  149. method = "TCP"
  150. class UNIXServer(_AbstractServer):
  151. __doc__ = _serverDoc.format(tran="UNIX")
  152. method = "UNIX"
  153. class UNIXClient(_AbstractClient):
  154. __doc__ = _clientDoc.format(tran="UNIX")
  155. method = "UNIX"
  156. class SSLServer(_AbstractServer):
  157. __doc__ = _serverDoc.format(tran="SSL")
  158. method = "SSL"
  159. class SSLClient(_AbstractClient):
  160. __doc__ = _clientDoc.format(tran="SSL")
  161. method = "SSL"
  162. class UDPServer(_AbstractServer):
  163. __doc__ = _serverDoc.format(tran="UDP")
  164. method = "UDP"
  165. class UNIXDatagramServer(_AbstractServer):
  166. __doc__ = _serverDoc.format(tran="UNIXDatagram")
  167. method = "UNIXDatagram"
  168. class UNIXDatagramClient(_AbstractClient):
  169. __doc__ = _clientDoc.format(tran="UNIXDatagram")
  170. method = "UNIXDatagram"
  171. class MulticastServer(_AbstractServer):
  172. __doc__ = _serverDoc.format(tran="Multicast")
  173. method = "Multicast"
  174. class TimerService(_VolatileDataService):
  175. """
  176. Service to periodically call a function
  177. Every C{step} seconds call the given function with the given arguments.
  178. The service starts the calls when it starts, and cancels them
  179. when it stops.
  180. @ivar clock: Source of time. This defaults to L{None} which is
  181. causes L{twisted.internet.reactor} to be used.
  182. Feel free to set this to something else, but it probably ought to be
  183. set *before* calling L{startService}.
  184. @type clock: L{IReactorTime<twisted.internet.interfaces.IReactorTime>}
  185. @ivar call: Function and arguments to call periodically.
  186. @type call: L{tuple} of C{(callable, args, kwargs)}
  187. """
  188. volatile = ["_loop", "_loopFinished"]
  189. def __init__(self, step, callable, *args, **kwargs):
  190. """
  191. @param step: The number of seconds between calls.
  192. @type step: L{float}
  193. @param callable: Function to call
  194. @type callable: L{callable}
  195. @param args: Positional arguments to pass to function
  196. @param kwargs: Keyword arguments to pass to function
  197. """
  198. self.step = step
  199. self.call = (callable, args, kwargs)
  200. self.clock = None
  201. def startService(self):
  202. service.Service.startService(self)
  203. callable, args, kwargs = self.call
  204. # we have to make a new LoopingCall each time we're started, because
  205. # an active LoopingCall remains active when serialized. If
  206. # LoopingCall were a _VolatileDataService, we wouldn't need to do
  207. # this.
  208. self._loop = task.LoopingCall(callable, *args, **kwargs)
  209. self._loop.clock = _maybeGlobalReactor(self.clock)
  210. self._loopFinished = self._loop.start(self.step, now=True)
  211. self._loopFinished.addErrback(self._failed)
  212. def _failed(self, why):
  213. # make a note that the LoopingCall is no longer looping, so we don't
  214. # try to shut it down a second time in stopService. I think this
  215. # should be in LoopingCall. -warner
  216. self._loop.running = False
  217. log.err(why)
  218. def stopService(self):
  219. """
  220. Stop the service.
  221. @rtype: L{Deferred<defer.Deferred>}
  222. @return: a L{Deferred<defer.Deferred>} which is fired when the
  223. currently running call (if any) is finished.
  224. """
  225. if self._loop.running:
  226. self._loop.stop()
  227. self._loopFinished.addCallback(lambda _: service.Service.stopService(self))
  228. return self._loopFinished
  229. class CooperatorService(service.Service):
  230. """
  231. Simple L{service.IService} which starts and stops a L{twisted.internet.task.Cooperator}.
  232. """
  233. def __init__(self):
  234. self.coop = task.Cooperator(started=False)
  235. def coiterate(self, iterator):
  236. return self.coop.coiterate(iterator)
  237. def startService(self):
  238. self.coop.start()
  239. def stopService(self):
  240. self.coop.stop()
  241. class StreamServerEndpointService(service.Service):
  242. """
  243. A L{StreamServerEndpointService} is an L{IService} which runs a server on a
  244. listening port described by an L{IStreamServerEndpoint
  245. <twisted.internet.interfaces.IStreamServerEndpoint>}.
  246. @ivar factory: A server factory which will be used to listen on the
  247. endpoint.
  248. @ivar endpoint: An L{IStreamServerEndpoint
  249. <twisted.internet.interfaces.IStreamServerEndpoint>} provider
  250. which will be used to listen when the service starts.
  251. @ivar _waitingForPort: a Deferred, if C{listen} has yet been invoked on the
  252. endpoint, otherwise None.
  253. @ivar _raiseSynchronously: Defines error-handling behavior for the case
  254. where C{listen(...)} raises an exception before C{startService} or
  255. C{privilegedStartService} have completed.
  256. @type _raiseSynchronously: C{bool}
  257. @since: 10.2
  258. """
  259. _raiseSynchronously = False
  260. def __init__(self, endpoint, factory):
  261. self.endpoint = endpoint
  262. self.factory = factory
  263. self._waitingForPort = None
  264. def privilegedStartService(self):
  265. """
  266. Start listening on the endpoint.
  267. """
  268. service.Service.privilegedStartService(self)
  269. self._waitingForPort = self.endpoint.listen(self.factory)
  270. raisedNow = []
  271. def handleIt(err):
  272. if self._raiseSynchronously:
  273. raisedNow.append(err)
  274. elif not err.check(CancelledError):
  275. log.err(err)
  276. self._waitingForPort.addErrback(handleIt)
  277. if raisedNow:
  278. raisedNow[0].raiseException()
  279. self._raiseSynchronously = False
  280. def startService(self):
  281. """
  282. Start listening on the endpoint, unless L{privilegedStartService} got
  283. around to it already.
  284. """
  285. service.Service.startService(self)
  286. if self._waitingForPort is None:
  287. self.privilegedStartService()
  288. def stopService(self):
  289. """
  290. Stop listening on the port if it is already listening, otherwise,
  291. cancel the attempt to listen.
  292. @return: a L{Deferred<twisted.internet.defer.Deferred>} which fires
  293. with L{None} when the port has stopped listening.
  294. """
  295. self._waitingForPort.cancel()
  296. def stopIt(port):
  297. if port is not None:
  298. return port.stopListening()
  299. d = self._waitingForPort.addCallback(stopIt)
  300. def stop(passthrough):
  301. self.running = False
  302. return passthrough
  303. d.addBoth(stop)
  304. return d
  305. __all__ = [
  306. "TimerService",
  307. "CooperatorService",
  308. "MulticastServer",
  309. "StreamServerEndpointService",
  310. "UDPServer",
  311. "ClientService",
  312. "TCPServer",
  313. "TCPClient",
  314. "UNIXServer",
  315. "UNIXClient",
  316. "SSLServer",
  317. "SSLClient",
  318. "UNIXDatagramServer",
  319. "UNIXDatagramClient",
  320. "ClientService",
  321. "backoffPolicy",
  322. ]