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.

internet.py 36KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206
  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 random import random as _goodEnoughRandom
  32. from typing import List
  33. from automat import MethodicalMachine # type: ignore[import]
  34. from twisted.application import service
  35. from twisted.internet import task
  36. from twisted.internet.defer import (
  37. CancelledError,
  38. Deferred,
  39. fail,
  40. maybeDeferred,
  41. succeed,
  42. )
  43. from twisted.logger import Logger
  44. from twisted.python import log
  45. from twisted.python.failure import Failure
  46. def _maybeGlobalReactor(maybeReactor):
  47. """
  48. @return: the argument, or the global reactor if the argument is L{None}.
  49. """
  50. if maybeReactor is None:
  51. from twisted.internet import reactor
  52. return reactor
  53. else:
  54. return maybeReactor
  55. class _VolatileDataService(service.Service):
  56. volatile: List[str] = []
  57. def __getstate__(self):
  58. d = service.Service.__getstate__(self)
  59. for attr in self.volatile:
  60. if attr in d:
  61. del d[attr]
  62. return d
  63. class _AbstractServer(_VolatileDataService):
  64. """
  65. @cvar volatile: list of attribute to remove from pickling.
  66. @type volatile: C{list}
  67. @ivar method: the type of method to call on the reactor, one of B{TCP},
  68. B{UDP}, B{SSL} or B{UNIX}.
  69. @type method: C{str}
  70. @ivar reactor: the current running reactor.
  71. @type reactor: a provider of C{IReactorTCP}, C{IReactorUDP},
  72. C{IReactorSSL} or C{IReactorUnix}.
  73. @ivar _port: instance of port set when the service is started.
  74. @type _port: a provider of L{twisted.internet.interfaces.IListeningPort}.
  75. """
  76. volatile = ["_port"]
  77. method: str = ""
  78. reactor = None
  79. _port = None
  80. def __init__(self, *args, **kwargs):
  81. self.args = args
  82. if "reactor" in kwargs:
  83. self.reactor = kwargs.pop("reactor")
  84. self.kwargs = kwargs
  85. def privilegedStartService(self):
  86. service.Service.privilegedStartService(self)
  87. self._port = self._getPort()
  88. def startService(self):
  89. service.Service.startService(self)
  90. if self._port is None:
  91. self._port = self._getPort()
  92. def stopService(self):
  93. service.Service.stopService(self)
  94. # TODO: if startup failed, should shutdown skip stopListening?
  95. # _port won't exist
  96. if self._port is not None:
  97. d = self._port.stopListening()
  98. del self._port
  99. return d
  100. def _getPort(self):
  101. """
  102. Wrapper around the appropriate listen method of the reactor.
  103. @return: the port object returned by the listen method.
  104. @rtype: an object providing
  105. L{twisted.internet.interfaces.IListeningPort}.
  106. """
  107. return getattr(
  108. _maybeGlobalReactor(self.reactor),
  109. "listen{}".format(
  110. self.method,
  111. ),
  112. )(*self.args, **self.kwargs)
  113. class _AbstractClient(_VolatileDataService):
  114. """
  115. @cvar volatile: list of attribute to remove from pickling.
  116. @type volatile: C{list}
  117. @ivar method: the type of method to call on the reactor, one of B{TCP},
  118. B{UDP}, B{SSL} or B{UNIX}.
  119. @type method: C{str}
  120. @ivar reactor: the current running reactor.
  121. @type reactor: a provider of C{IReactorTCP}, C{IReactorUDP},
  122. C{IReactorSSL} or C{IReactorUnix}.
  123. @ivar _connection: instance of connection set when the service is started.
  124. @type _connection: a provider of L{twisted.internet.interfaces.IConnector}.
  125. """
  126. volatile = ["_connection"]
  127. method: str = ""
  128. reactor = None
  129. _connection = None
  130. def __init__(self, *args, **kwargs):
  131. self.args = args
  132. if "reactor" in kwargs:
  133. self.reactor = kwargs.pop("reactor")
  134. self.kwargs = kwargs
  135. def startService(self):
  136. service.Service.startService(self)
  137. self._connection = self._getConnection()
  138. def stopService(self):
  139. service.Service.stopService(self)
  140. if self._connection is not None:
  141. self._connection.disconnect()
  142. del self._connection
  143. def _getConnection(self):
  144. """
  145. Wrapper around the appropriate connect method of the reactor.
  146. @return: the port object returned by the connect method.
  147. @rtype: an object providing L{twisted.internet.interfaces.IConnector}.
  148. """
  149. return getattr(_maybeGlobalReactor(self.reactor), f"connect{self.method}")(
  150. *self.args, **self.kwargs
  151. )
  152. _clientDoc = """Connect to {tran}
  153. Call reactor.connect{tran} when the service starts, with the
  154. arguments given to the constructor.
  155. """
  156. _serverDoc = """Serve {tran} clients
  157. Call reactor.listen{tran} when the service starts, with the
  158. arguments given to the constructor. When the service stops,
  159. stop listening. See twisted.internet.interfaces for documentation
  160. on arguments to the reactor method.
  161. """
  162. class TCPServer(_AbstractServer):
  163. __doc__ = _serverDoc.format(tran="TCP")
  164. method = "TCP"
  165. class TCPClient(_AbstractClient):
  166. __doc__ = _clientDoc.format(tran="TCP")
  167. method = "TCP"
  168. class UNIXServer(_AbstractServer):
  169. __doc__ = _serverDoc.format(tran="UNIX")
  170. method = "UNIX"
  171. class UNIXClient(_AbstractClient):
  172. __doc__ = _clientDoc.format(tran="UNIX")
  173. method = "UNIX"
  174. class SSLServer(_AbstractServer):
  175. __doc__ = _serverDoc.format(tran="SSL")
  176. method = "SSL"
  177. class SSLClient(_AbstractClient):
  178. __doc__ = _clientDoc.format(tran="SSL")
  179. method = "SSL"
  180. class UDPServer(_AbstractServer):
  181. __doc__ = _serverDoc.format(tran="UDP")
  182. method = "UDP"
  183. class UNIXDatagramServer(_AbstractServer):
  184. __doc__ = _serverDoc.format(tran="UNIXDatagram")
  185. method = "UNIXDatagram"
  186. class UNIXDatagramClient(_AbstractClient):
  187. __doc__ = _clientDoc.format(tran="UNIXDatagram")
  188. method = "UNIXDatagram"
  189. class MulticastServer(_AbstractServer):
  190. __doc__ = _serverDoc.format(tran="Multicast")
  191. method = "Multicast"
  192. class TimerService(_VolatileDataService):
  193. """
  194. Service to periodically call a function
  195. Every C{step} seconds call the given function with the given arguments.
  196. The service starts the calls when it starts, and cancels them
  197. when it stops.
  198. @ivar clock: Source of time. This defaults to L{None} which is
  199. causes L{twisted.internet.reactor} to be used.
  200. Feel free to set this to something else, but it probably ought to be
  201. set *before* calling L{startService}.
  202. @type clock: L{IReactorTime<twisted.internet.interfaces.IReactorTime>}
  203. @ivar call: Function and arguments to call periodically.
  204. @type call: L{tuple} of C{(callable, args, kwargs)}
  205. """
  206. volatile = ["_loop", "_loopFinished"]
  207. def __init__(self, step, callable, *args, **kwargs):
  208. """
  209. @param step: The number of seconds between calls.
  210. @type step: L{float}
  211. @param callable: Function to call
  212. @type callable: L{callable}
  213. @param args: Positional arguments to pass to function
  214. @param kwargs: Keyword arguments to pass to function
  215. """
  216. self.step = step
  217. self.call = (callable, args, kwargs)
  218. self.clock = None
  219. def startService(self):
  220. service.Service.startService(self)
  221. callable, args, kwargs = self.call
  222. # we have to make a new LoopingCall each time we're started, because
  223. # an active LoopingCall remains active when serialized. If
  224. # LoopingCall were a _VolatileDataService, we wouldn't need to do
  225. # this.
  226. self._loop = task.LoopingCall(callable, *args, **kwargs)
  227. self._loop.clock = _maybeGlobalReactor(self.clock)
  228. self._loopFinished = self._loop.start(self.step, now=True)
  229. self._loopFinished.addErrback(self._failed)
  230. def _failed(self, why):
  231. # make a note that the LoopingCall is no longer looping, so we don't
  232. # try to shut it down a second time in stopService. I think this
  233. # should be in LoopingCall. -warner
  234. self._loop.running = False
  235. log.err(why)
  236. def stopService(self):
  237. """
  238. Stop the service.
  239. @rtype: L{Deferred<defer.Deferred>}
  240. @return: a L{Deferred<defer.Deferred>} which is fired when the
  241. currently running call (if any) is finished.
  242. """
  243. if self._loop.running:
  244. self._loop.stop()
  245. self._loopFinished.addCallback(lambda _: service.Service.stopService(self))
  246. return self._loopFinished
  247. class CooperatorService(service.Service):
  248. """
  249. Simple L{service.IService} which starts and stops a L{twisted.internet.task.Cooperator}.
  250. """
  251. def __init__(self):
  252. self.coop = task.Cooperator(started=False)
  253. def coiterate(self, iterator):
  254. return self.coop.coiterate(iterator)
  255. def startService(self):
  256. self.coop.start()
  257. def stopService(self):
  258. self.coop.stop()
  259. class StreamServerEndpointService(service.Service):
  260. """
  261. A L{StreamServerEndpointService} is an L{IService} which runs a server on a
  262. listening port described by an L{IStreamServerEndpoint
  263. <twisted.internet.interfaces.IStreamServerEndpoint>}.
  264. @ivar factory: A server factory which will be used to listen on the
  265. endpoint.
  266. @ivar endpoint: An L{IStreamServerEndpoint
  267. <twisted.internet.interfaces.IStreamServerEndpoint>} provider
  268. which will be used to listen when the service starts.
  269. @ivar _waitingForPort: a Deferred, if C{listen} has yet been invoked on the
  270. endpoint, otherwise None.
  271. @ivar _raiseSynchronously: Defines error-handling behavior for the case
  272. where C{listen(...)} raises an exception before C{startService} or
  273. C{privilegedStartService} have completed.
  274. @type _raiseSynchronously: C{bool}
  275. @since: 10.2
  276. """
  277. _raiseSynchronously = False
  278. def __init__(self, endpoint, factory):
  279. self.endpoint = endpoint
  280. self.factory = factory
  281. self._waitingForPort = None
  282. def privilegedStartService(self):
  283. """
  284. Start listening on the endpoint.
  285. """
  286. service.Service.privilegedStartService(self)
  287. self._waitingForPort = self.endpoint.listen(self.factory)
  288. raisedNow = []
  289. def handleIt(err):
  290. if self._raiseSynchronously:
  291. raisedNow.append(err)
  292. elif not err.check(CancelledError):
  293. log.err(err)
  294. self._waitingForPort.addErrback(handleIt)
  295. if raisedNow:
  296. raisedNow[0].raiseException()
  297. self._raiseSynchronously = False
  298. def startService(self):
  299. """
  300. Start listening on the endpoint, unless L{privilegedStartService} got
  301. around to it already.
  302. """
  303. service.Service.startService(self)
  304. if self._waitingForPort is None:
  305. self.privilegedStartService()
  306. def stopService(self):
  307. """
  308. Stop listening on the port if it is already listening, otherwise,
  309. cancel the attempt to listen.
  310. @return: a L{Deferred<twisted.internet.defer.Deferred>} which fires
  311. with L{None} when the port has stopped listening.
  312. """
  313. self._waitingForPort.cancel()
  314. def stopIt(port):
  315. if port is not None:
  316. return port.stopListening()
  317. d = self._waitingForPort.addCallback(stopIt)
  318. def stop(passthrough):
  319. self.running = False
  320. return passthrough
  321. d.addBoth(stop)
  322. return d
  323. class _ReconnectingProtocolProxy:
  324. """
  325. A proxy for a Protocol to provide connectionLost notification to a client
  326. connection service, in support of reconnecting when connections are lost.
  327. """
  328. def __init__(self, protocol, lostNotification):
  329. """
  330. Create a L{_ReconnectingProtocolProxy}.
  331. @param protocol: the application-provided L{interfaces.IProtocol}
  332. provider.
  333. @type protocol: provider of L{interfaces.IProtocol} which may
  334. additionally provide L{interfaces.IHalfCloseableProtocol} and
  335. L{interfaces.IFileDescriptorReceiver}.
  336. @param lostNotification: a 1-argument callable to invoke with the
  337. C{reason} when the connection is lost.
  338. """
  339. self._protocol = protocol
  340. self._lostNotification = lostNotification
  341. def connectionLost(self, reason):
  342. """
  343. The connection was lost. Relay this information.
  344. @param reason: The reason the connection was lost.
  345. @return: the underlying protocol's result
  346. """
  347. try:
  348. return self._protocol.connectionLost(reason)
  349. finally:
  350. self._lostNotification(reason)
  351. def __getattr__(self, item):
  352. return getattr(self._protocol, item)
  353. def __repr__(self) -> str:
  354. return f"<{self.__class__.__name__} wrapping {self._protocol!r}>"
  355. class _DisconnectFactory:
  356. """
  357. A L{_DisconnectFactory} is a proxy for L{IProtocolFactory} that catches
  358. C{connectionLost} notifications and relays them.
  359. """
  360. def __init__(self, protocolFactory, protocolDisconnected):
  361. self._protocolFactory = protocolFactory
  362. self._protocolDisconnected = protocolDisconnected
  363. def buildProtocol(self, addr):
  364. """
  365. Create a L{_ReconnectingProtocolProxy} with the disconnect-notification
  366. callback we were called with.
  367. @param addr: The address the connection is coming from.
  368. @return: a L{_ReconnectingProtocolProxy} for a protocol produced by
  369. C{self._protocolFactory}
  370. """
  371. return _ReconnectingProtocolProxy(
  372. self._protocolFactory.buildProtocol(addr), self._protocolDisconnected
  373. )
  374. def __getattr__(self, item):
  375. return getattr(self._protocolFactory, item)
  376. def __repr__(self) -> str:
  377. return "<{} wrapping {!r}>".format(
  378. self.__class__.__name__, self._protocolFactory
  379. )
  380. def backoffPolicy(
  381. initialDelay=1.0, maxDelay=60.0, factor=1.5, jitter=_goodEnoughRandom
  382. ):
  383. """
  384. A timeout policy for L{ClientService} which computes an exponential backoff
  385. interval with configurable parameters.
  386. @since: 16.1.0
  387. @param initialDelay: Delay for the first reconnection attempt (default
  388. 1.0s).
  389. @type initialDelay: L{float}
  390. @param maxDelay: Maximum number of seconds between connection attempts
  391. (default 60 seconds, or one minute). Note that this value is before
  392. jitter is applied, so the actual maximum possible delay is this value
  393. plus the maximum possible result of C{jitter()}.
  394. @type maxDelay: L{float}
  395. @param factor: A multiplicative factor by which the delay grows on each
  396. failed reattempt. Default: 1.5.
  397. @type factor: L{float}
  398. @param jitter: A 0-argument callable that introduces noise into the delay.
  399. By default, C{random.random}, i.e. a pseudorandom floating-point value
  400. between zero and one.
  401. @type jitter: 0-argument callable returning L{float}
  402. @return: a 1-argument callable that, given an attempt count, returns a
  403. floating point number; the number of seconds to delay.
  404. @rtype: see L{ClientService.__init__}'s C{retryPolicy} argument.
  405. """
  406. def policy(attempt):
  407. try:
  408. delay = min(initialDelay * (factor ** min(100, attempt)), maxDelay)
  409. except OverflowError:
  410. delay = maxDelay
  411. return delay + jitter()
  412. return policy
  413. _defaultPolicy = backoffPolicy()
  414. def _firstResult(gen):
  415. """
  416. Return the first element of a generator and exhaust it.
  417. C{MethodicalMachine.upon}'s C{collector} argument takes a generator of
  418. output results. If the generator is exhausted, the later outputs aren't
  419. actually run.
  420. @param gen: Generator to extract values from
  421. @return: The first element of the generator.
  422. """
  423. return list(gen)[0]
  424. class _ClientMachine:
  425. """
  426. State machine for maintaining a single outgoing connection to an endpoint.
  427. @ivar _awaitingConnected: notifications to make when connection
  428. succeeds, fails, or is cancelled
  429. @type _awaitingConnected: list of (Deferred, count) tuples
  430. @see: L{ClientService}
  431. """
  432. _machine = MethodicalMachine()
  433. def __init__(self, endpoint, factory, retryPolicy, clock, prepareConnection, log):
  434. """
  435. @see: L{ClientService.__init__}
  436. @param log: The logger for the L{ClientService} instance this state
  437. machine is associated to.
  438. @type log: L{Logger}
  439. """
  440. self._endpoint = endpoint
  441. self._failedAttempts = 0
  442. self._stopped = False
  443. self._factory = factory
  444. self._timeoutForAttempt = retryPolicy
  445. self._clock = clock
  446. self._prepareConnection = prepareConnection
  447. self._connectionInProgress = succeed(None)
  448. self._awaitingConnected = []
  449. self._stopWaiters = []
  450. self._log = log
  451. @_machine.state(initial=True)
  452. def _init(self):
  453. """
  454. The service has not been started.
  455. """
  456. @_machine.state()
  457. def _connecting(self):
  458. """
  459. The service has started connecting.
  460. """
  461. @_machine.state()
  462. def _waiting(self):
  463. """
  464. The service is waiting for the reconnection period
  465. before reconnecting.
  466. """
  467. @_machine.state()
  468. def _connected(self):
  469. """
  470. The service is connected.
  471. """
  472. @_machine.state()
  473. def _disconnecting(self):
  474. """
  475. The service is disconnecting after being asked to shutdown.
  476. """
  477. @_machine.state()
  478. def _restarting(self):
  479. """
  480. The service is disconnecting and has been asked to restart.
  481. """
  482. @_machine.state()
  483. def _stopped(self):
  484. """
  485. The service has been stopped and is disconnected.
  486. """
  487. @_machine.input()
  488. def start(self):
  489. """
  490. Start this L{ClientService}, initiating the connection retry loop.
  491. """
  492. @_machine.output()
  493. def _connect(self):
  494. """
  495. Start a connection attempt.
  496. """
  497. factoryProxy = _DisconnectFactory(
  498. self._factory, lambda _: self._clientDisconnected()
  499. )
  500. self._connectionInProgress = (
  501. self._endpoint.connect(factoryProxy)
  502. .addCallback(self._runPrepareConnection)
  503. .addCallback(self._connectionMade)
  504. .addErrback(self._connectionFailed)
  505. )
  506. def _runPrepareConnection(self, protocol):
  507. """
  508. Run any C{prepareConnection} callback with the connected protocol,
  509. ignoring its return value but propagating any failure.
  510. @param protocol: The protocol of the connection.
  511. @type protocol: L{IProtocol}
  512. @return: Either:
  513. - A L{Deferred} that succeeds with the protocol when the
  514. C{prepareConnection} callback has executed successfully.
  515. - A L{Deferred} that fails when the C{prepareConnection} callback
  516. throws or returns a failed L{Deferred}.
  517. - The protocol, when no C{prepareConnection} callback is defined.
  518. """
  519. if self._prepareConnection:
  520. return maybeDeferred(self._prepareConnection, protocol).addCallback(
  521. lambda _: protocol
  522. )
  523. return protocol
  524. @_machine.output()
  525. def _resetFailedAttempts(self):
  526. """
  527. Reset the number of failed attempts.
  528. """
  529. self._failedAttempts = 0
  530. @_machine.input()
  531. def stop(self):
  532. """
  533. Stop trying to connect and disconnect any current connection.
  534. @return: a L{Deferred} that fires when all outstanding connections are
  535. closed and all in-progress connection attempts halted.
  536. """
  537. @_machine.output()
  538. def _waitForStop(self):
  539. """
  540. Return a deferred that will fire when the service has finished
  541. disconnecting.
  542. @return: L{Deferred} that fires when the service has finished
  543. disconnecting.
  544. """
  545. self._stopWaiters.append(Deferred())
  546. return self._stopWaiters[-1]
  547. @_machine.output()
  548. def _stopConnecting(self):
  549. """
  550. Stop pending connection attempt.
  551. """
  552. self._connectionInProgress.cancel()
  553. @_machine.output()
  554. def _stopRetrying(self):
  555. """
  556. Stop pending attempt to reconnect.
  557. """
  558. self._retryCall.cancel()
  559. del self._retryCall
  560. @_machine.output()
  561. def _disconnect(self):
  562. """
  563. Disconnect the current connection.
  564. """
  565. self._currentConnection.transport.loseConnection()
  566. @_machine.input()
  567. def _connectionMade(self, protocol):
  568. """
  569. A connection has been made.
  570. @param protocol: The protocol of the connection.
  571. @type protocol: L{IProtocol}
  572. """
  573. @_machine.output()
  574. def _notifyWaiters(self, protocol):
  575. """
  576. Notify all pending requests for a connection that a connection has been
  577. made.
  578. @param protocol: The protocol of the connection.
  579. @type protocol: L{IProtocol}
  580. """
  581. # This should be in _resetFailedAttempts but the signature doesn't
  582. # match.
  583. self._failedAttempts = 0
  584. self._currentConnection = protocol._protocol
  585. self._unawait(self._currentConnection)
  586. @_machine.input()
  587. def _connectionFailed(self, f):
  588. """
  589. The current connection attempt failed.
  590. """
  591. @_machine.output()
  592. def _wait(self):
  593. """
  594. Schedule a retry attempt.
  595. """
  596. self._doWait()
  597. @_machine.output()
  598. def _ignoreAndWait(self, f):
  599. """
  600. Schedule a retry attempt, and ignore the Failure passed in.
  601. """
  602. return self._doWait()
  603. def _doWait(self):
  604. self._failedAttempts += 1
  605. delay = self._timeoutForAttempt(self._failedAttempts)
  606. self._log.info(
  607. "Scheduling retry {attempt} to connect {endpoint} " "in {delay} seconds.",
  608. attempt=self._failedAttempts,
  609. endpoint=self._endpoint,
  610. delay=delay,
  611. )
  612. self._retryCall = self._clock.callLater(delay, self._reconnect)
  613. @_machine.input()
  614. def _reconnect(self):
  615. """
  616. The wait between connection attempts is done.
  617. """
  618. @_machine.input()
  619. def _clientDisconnected(self):
  620. """
  621. The current connection has been disconnected.
  622. """
  623. @_machine.output()
  624. def _forgetConnection(self):
  625. """
  626. Forget the current connection.
  627. """
  628. del self._currentConnection
  629. @_machine.output()
  630. def _cancelConnectWaiters(self):
  631. """
  632. Notify all pending requests for a connection that no more connections
  633. are expected.
  634. """
  635. self._unawait(Failure(CancelledError()))
  636. @_machine.output()
  637. def _ignoreAndCancelConnectWaiters(self, f):
  638. """
  639. Notify all pending requests for a connection that no more connections
  640. are expected, after ignoring the Failure passed in.
  641. """
  642. self._unawait(Failure(CancelledError()))
  643. @_machine.output()
  644. def _finishStopping(self):
  645. """
  646. Notify all deferreds waiting on the service stopping.
  647. """
  648. self._doFinishStopping()
  649. @_machine.output()
  650. def _ignoreAndFinishStopping(self, f):
  651. """
  652. Notify all deferreds waiting on the service stopping, and ignore the
  653. Failure passed in.
  654. """
  655. self._doFinishStopping()
  656. def _doFinishStopping(self):
  657. self._stopWaiters, waiting = [], self._stopWaiters
  658. for w in waiting:
  659. w.callback(None)
  660. @_machine.input()
  661. def whenConnected(self, failAfterFailures=None):
  662. """
  663. Retrieve the currently-connected L{Protocol}, or the next one to
  664. connect.
  665. @param failAfterFailures: number of connection failures after which
  666. the Deferred will deliver a Failure (None means the Deferred will
  667. only fail if/when the service is stopped). Set this to 1 to make
  668. the very first connection failure signal an error. Use 2 to
  669. allow one failure but signal an error if the subsequent retry
  670. then fails.
  671. @type failAfterFailures: L{int} or None
  672. @return: a Deferred that fires with a protocol produced by the
  673. factory passed to C{__init__}
  674. @rtype: L{Deferred} that may:
  675. - fire with L{IProtocol}
  676. - fail with L{CancelledError} when the service is stopped
  677. - fail with e.g.
  678. L{DNSLookupError<twisted.internet.error.DNSLookupError>} or
  679. L{ConnectionRefusedError<twisted.internet.error.ConnectionRefusedError>}
  680. when the number of consecutive failed connection attempts
  681. equals the value of "failAfterFailures"
  682. """
  683. @_machine.output()
  684. def _currentConnection(self, failAfterFailures=None):
  685. """
  686. Return the currently connected protocol.
  687. @return: L{Deferred} that is fired with currently connected protocol.
  688. """
  689. return succeed(self._currentConnection)
  690. @_machine.output()
  691. def _noConnection(self, failAfterFailures=None):
  692. """
  693. Notify the caller that no connection is expected.
  694. @return: L{Deferred} that is fired with L{CancelledError}.
  695. """
  696. return fail(CancelledError())
  697. @_machine.output()
  698. def _awaitingConnection(self, failAfterFailures=None):
  699. """
  700. Return a deferred that will fire with the next connected protocol.
  701. @return: L{Deferred} that will fire with the next connected protocol.
  702. """
  703. result = Deferred()
  704. self._awaitingConnected.append((result, failAfterFailures))
  705. return result
  706. @_machine.output()
  707. def _deferredSucceededWithNone(self):
  708. """
  709. Return a deferred that has already fired with L{None}.
  710. @return: A L{Deferred} that has already fired with L{None}.
  711. """
  712. return succeed(None)
  713. def _unawait(self, value):
  714. """
  715. Fire all outstanding L{ClientService.whenConnected} L{Deferred}s.
  716. @param value: the value to fire the L{Deferred}s with.
  717. """
  718. self._awaitingConnected, waiting = [], self._awaitingConnected
  719. for (w, remaining) in waiting:
  720. w.callback(value)
  721. @_machine.output()
  722. def _deliverConnectionFailure(self, f):
  723. """
  724. Deliver connection failures to any L{ClientService.whenConnected}
  725. L{Deferred}s that have met their failAfterFailures threshold.
  726. @param f: the Failure to fire the L{Deferred}s with.
  727. """
  728. ready = []
  729. notReady = []
  730. for (w, remaining) in self._awaitingConnected:
  731. if remaining is None:
  732. notReady.append((w, remaining))
  733. elif remaining <= 1:
  734. ready.append(w)
  735. else:
  736. notReady.append((w, remaining - 1))
  737. self._awaitingConnected = notReady
  738. for w in ready:
  739. w.callback(f)
  740. # State Transitions
  741. _init.upon(start, enter=_connecting, outputs=[_connect])
  742. _init.upon(
  743. stop,
  744. enter=_stopped,
  745. outputs=[_deferredSucceededWithNone],
  746. collector=_firstResult,
  747. )
  748. _connecting.upon(start, enter=_connecting, outputs=[])
  749. # Note that this synchonously triggers _connectionFailed in the
  750. # _disconnecting state.
  751. _connecting.upon(
  752. stop,
  753. enter=_disconnecting,
  754. outputs=[_waitForStop, _stopConnecting],
  755. collector=_firstResult,
  756. )
  757. _connecting.upon(_connectionMade, enter=_connected, outputs=[_notifyWaiters])
  758. _connecting.upon(
  759. _connectionFailed,
  760. enter=_waiting,
  761. outputs=[_ignoreAndWait, _deliverConnectionFailure],
  762. )
  763. _waiting.upon(start, enter=_waiting, outputs=[])
  764. _waiting.upon(
  765. stop,
  766. enter=_stopped,
  767. outputs=[_waitForStop, _cancelConnectWaiters, _stopRetrying, _finishStopping],
  768. collector=_firstResult,
  769. )
  770. _waiting.upon(_reconnect, enter=_connecting, outputs=[_connect])
  771. _connected.upon(start, enter=_connected, outputs=[])
  772. _connected.upon(
  773. stop,
  774. enter=_disconnecting,
  775. outputs=[_waitForStop, _disconnect],
  776. collector=_firstResult,
  777. )
  778. _connected.upon(
  779. _clientDisconnected, enter=_waiting, outputs=[_forgetConnection, _wait]
  780. )
  781. _disconnecting.upon(start, enter=_restarting, outputs=[_resetFailedAttempts])
  782. _disconnecting.upon(
  783. stop, enter=_disconnecting, outputs=[_waitForStop], collector=_firstResult
  784. )
  785. _disconnecting.upon(
  786. _clientDisconnected,
  787. enter=_stopped,
  788. outputs=[_cancelConnectWaiters, _finishStopping, _forgetConnection],
  789. )
  790. # Note that this is triggered synchonously with the transition from
  791. # _connecting
  792. _disconnecting.upon(
  793. _connectionFailed,
  794. enter=_stopped,
  795. outputs=[_ignoreAndCancelConnectWaiters, _ignoreAndFinishStopping],
  796. )
  797. _restarting.upon(start, enter=_restarting, outputs=[])
  798. _restarting.upon(
  799. stop, enter=_disconnecting, outputs=[_waitForStop], collector=_firstResult
  800. )
  801. _restarting.upon(
  802. _clientDisconnected, enter=_connecting, outputs=[_finishStopping, _connect]
  803. )
  804. _stopped.upon(start, enter=_connecting, outputs=[_connect])
  805. _stopped.upon(
  806. stop,
  807. enter=_stopped,
  808. outputs=[_deferredSucceededWithNone],
  809. collector=_firstResult,
  810. )
  811. _init.upon(
  812. whenConnected,
  813. enter=_init,
  814. outputs=[_awaitingConnection],
  815. collector=_firstResult,
  816. )
  817. _connecting.upon(
  818. whenConnected,
  819. enter=_connecting,
  820. outputs=[_awaitingConnection],
  821. collector=_firstResult,
  822. )
  823. _waiting.upon(
  824. whenConnected,
  825. enter=_waiting,
  826. outputs=[_awaitingConnection],
  827. collector=_firstResult,
  828. )
  829. _connected.upon(
  830. whenConnected,
  831. enter=_connected,
  832. outputs=[_currentConnection],
  833. collector=_firstResult,
  834. )
  835. _disconnecting.upon(
  836. whenConnected,
  837. enter=_disconnecting,
  838. outputs=[_awaitingConnection],
  839. collector=_firstResult,
  840. )
  841. _restarting.upon(
  842. whenConnected,
  843. enter=_restarting,
  844. outputs=[_awaitingConnection],
  845. collector=_firstResult,
  846. )
  847. _stopped.upon(
  848. whenConnected, enter=_stopped, outputs=[_noConnection], collector=_firstResult
  849. )
  850. class ClientService(service.Service):
  851. """
  852. A L{ClientService} maintains a single outgoing connection to a client
  853. endpoint, reconnecting after a configurable timeout when a connection
  854. fails, either before or after connecting.
  855. @since: 16.1.0
  856. """
  857. _log = Logger()
  858. def __init__(
  859. self, endpoint, factory, retryPolicy=None, clock=None, prepareConnection=None
  860. ):
  861. """
  862. @param endpoint: A L{stream client endpoint
  863. <interfaces.IStreamClientEndpoint>} provider which will be used to
  864. connect when the service starts.
  865. @param factory: A L{protocol factory <interfaces.IProtocolFactory>}
  866. which will be used to create clients for the endpoint.
  867. @param retryPolicy: A policy configuring how long L{ClientService} will
  868. wait between attempts to connect to C{endpoint}.
  869. @type retryPolicy: callable taking (the number of failed connection
  870. attempts made in a row (L{int})) and returning the number of
  871. seconds to wait before making another attempt.
  872. @param clock: The clock used to schedule reconnection. It's mainly
  873. useful to be parametrized in tests. If the factory is serialized,
  874. this attribute will not be serialized, and the default value (the
  875. reactor) will be restored when deserialized.
  876. @type clock: L{IReactorTime}
  877. @param prepareConnection: A single argument L{callable} that may return
  878. a L{Deferred}. It will be called once with the L{protocol
  879. <interfaces.IProtocol>} each time a new connection is made. It may
  880. call methods on the protocol to prepare it for use (e.g.
  881. authenticate) or validate it (check its health).
  882. The C{prepareConnection} callable may raise an exception or return
  883. a L{Deferred} which fails to reject the connection. A rejected
  884. connection is not used to fire an L{Deferred} returned by
  885. L{whenConnected}. Instead, L{ClientService} handles the failure
  886. and continues as if the connection attempt were a failure
  887. (incrementing the counter passed to C{retryPolicy}).
  888. L{Deferred}s returned by L{whenConnected} will not fire until
  889. any L{Deferred} returned by the C{prepareConnection} callable
  890. fire. Otherwise its successful return value is consumed, but
  891. ignored.
  892. Present Since Twisted 18.7.0
  893. @type prepareConnection: L{callable}
  894. """
  895. clock = _maybeGlobalReactor(clock)
  896. retryPolicy = _defaultPolicy if retryPolicy is None else retryPolicy
  897. self._machine = _ClientMachine(
  898. endpoint,
  899. factory,
  900. retryPolicy,
  901. clock,
  902. prepareConnection=prepareConnection,
  903. log=self._log,
  904. )
  905. def whenConnected(self, failAfterFailures=None):
  906. """
  907. Retrieve the currently-connected L{Protocol}, or the next one to
  908. connect.
  909. @param failAfterFailures: number of connection failures after which
  910. the Deferred will deliver a Failure (None means the Deferred will
  911. only fail if/when the service is stopped). Set this to 1 to make
  912. the very first connection failure signal an error. Use 2 to
  913. allow one failure but signal an error if the subsequent retry
  914. then fails.
  915. @type failAfterFailures: L{int} or None
  916. @return: a Deferred that fires with a protocol produced by the
  917. factory passed to C{__init__}
  918. @rtype: L{Deferred} that may:
  919. - fire with L{IProtocol}
  920. - fail with L{CancelledError} when the service is stopped
  921. - fail with e.g.
  922. L{DNSLookupError<twisted.internet.error.DNSLookupError>} or
  923. L{ConnectionRefusedError<twisted.internet.error.ConnectionRefusedError>}
  924. when the number of consecutive failed connection attempts
  925. equals the value of "failAfterFailures"
  926. """
  927. return self._machine.whenConnected(failAfterFailures)
  928. def startService(self):
  929. """
  930. Start this L{ClientService}, initiating the connection retry loop.
  931. """
  932. if self.running:
  933. self._log.warn("Duplicate ClientService.startService {log_source}")
  934. return
  935. super().startService()
  936. self._machine.start()
  937. def stopService(self):
  938. """
  939. Stop attempting to reconnect and close any existing connections.
  940. @return: a L{Deferred} that fires when all outstanding connections are
  941. closed and all in-progress connection attempts halted.
  942. """
  943. super().stopService()
  944. return self._machine.stop()
  945. __all__ = [
  946. "TimerService",
  947. "CooperatorService",
  948. "MulticastServer",
  949. "StreamServerEndpointService",
  950. "UDPServer",
  951. "ClientService",
  952. "TCPServer",
  953. "TCPClient",
  954. "UNIXServer",
  955. "UNIXClient",
  956. "SSLServer",
  957. "SSLClient",
  958. "UNIXDatagramServer",
  959. "UNIXDatagramClient",
  960. ]