我们从Python开源项目中,提取了以下18个代码示例,用于说明如何使用twisted.internet.protocol.ReconnectingClientFactory()。
def testStopTrying(self): f = Factory() f.protocol = In f.connections = 0 f.allMessages = [] f.goal = 2 f.d = defer.Deferred() c = ReconnectingClientFactory() c.initialDelay = c.delay = 0.2 c.protocol = Out c.howManyTimes = 2 port = self.port = reactor.listenTCP(0, f) PORT = port.getHost().port reactor.connectTCP('127.0.0.1', PORT, c) f.d.addCallback(self._testStopTrying_1, f, c) return f.d
def test_stopTryingWhenConnected(self): """ If a L{ReconnectingClientFactory} has C{stopTrying} called while it is connected, it does not subsequently attempt to reconnect if the connection is later lost. """ class NoConnectConnector(object): def stopConnecting(self): raise RuntimeError("Shouldn't be called, we're connected.") def connect(self): raise RuntimeError("Shouldn't be reconnecting.") c = ReconnectingClientFactory() c.protocol = Protocol # Let's pretend we've connected: c.buildProtocol(None) # Now we stop trying, then disconnect: c.stopTrying() c.clientConnectionLost(NoConnectConnector(), None) self.assertFalse(c.continueTrying)
def clientConnectionLost(self, connector, reason): print(colorama.Back.RED + colorama.Style.BRIGHT + " ERROR " + colorama.Back.RESET + ' Lost connection. Reason:' + str(reason)) protocol.ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
def clientConnectionFailed(self, connector, reason): print(colorama.Back.RED + colorama.Style.BRIGHT + " ERROR " + colorama.Back.RESET + ' Connection failed. Reason:' + str(reason)) protocol.ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
def clientConnectionLost(self, connector, reason): log.msg('Lost connection') del self._onkyo self._onkyo = None protocol.ReconnectingClientFactory.clientConnectionLost( self, connector, reason)
def clientConnectionFailed(self, connector, reason): log.msg('Connection failed {}'.format(reason)) protocol.ReconnectingClientFactory.clientConnectionFailed(self, connector, reason) # noinspection PyUnusedLocal
def clientConnectionLost(self, connector, reason): log.debug('IRC connection lost: ' + str(reason)) if not self.wrapper.give_up: if reactor.running: log.info('Attempting to reconnect...') protocol.ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
def clientConnectionFailed(self, connector, reason): log.info('IRC connection failed') if not self.wrapper.give_up: if reactor.running: log.info('Attempting to reconnect...') protocol.ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
def buildProtocol(self, addr): """ Create a protocol instance. Overrides L{XmlStreamFactoryMixin.buildProtocol} to work with a L{ReconnectingClientFactory}. As this is called upon having an connection established, we are resetting the delay for reconnection attempts when the connection is lost again. """ self.resetDelay() return XmlStreamFactoryMixin.buildProtocol(self, addr)
def test_stopTryingDoesNotReconnect(self): """ Calling stopTrying on a L{ReconnectingClientFactory} doesn't attempt a retry on any active connector. """ class FactoryAwareFakeConnector(FakeConnector): attemptedRetry = False def stopConnecting(self): """ Behave as though an ongoing connection attempt has now failed, and notify the factory of this. """ f.clientConnectionFailed(self, None) def connect(self): """ Record an attempt to reconnect, since this is what we are trying to avoid. """ self.attemptedRetry = True f = ReconnectingClientFactory() f.clock = Clock() # simulate an active connection - stopConnecting on this connector should # be triggered when we call stopTrying f.connector = FactoryAwareFakeConnector() f.stopTrying() # make sure we never attempted to retry self.assertFalse(f.connector.attemptedRetry) self.assertFalse(f.clock.getDelayedCalls())
def test_serializeUnused(self): """ A L{ReconnectingClientFactory} which hasn't been used for anything can be pickled and unpickled and end up with the same state. """ original = ReconnectingClientFactory() reconstituted = pickle.loads(pickle.dumps(original)) self.assertEqual(original.__dict__, reconstituted.__dict__)
def test_serializeWithClock(self): """ The clock attribute of L{ReconnectingClientFactory} is not serialized, and the restored value sets it to the default value, the reactor. """ clock = Clock() original = ReconnectingClientFactory() original.clock = clock reconstituted = pickle.loads(pickle.dumps(original)) self.assertIsNone(reconstituted.clock)
def test_parametrizedClock(self): """ The clock used by L{ReconnectingClientFactory} can be parametrized, so that one can cleanly test reconnections. """ clock = Clock() factory = ReconnectingClientFactory() factory.clock = clock factory.clientConnectionLost(FakeConnector(), None) self.assertEqual(len(clock.calls), 1)
def clientConnectionLost(self, connector, reason): log.msg('Lost connection. Reason:' + str(reason)) self.proto = None self._do_failover(connector) log.msg('Reconnecting to Riemann on %s:%s' % (connector.host, connector.port)) protocol.ReconnectingClientFactory.clientConnectionLost( self, connector, reason)
def clientConnectionFailed(self, connector, reason): log.msg('Connection failed. Reason:' + str(reason)) self.proto = None self._do_failover(connector) log.msg('Reconnecting to Riemann on %s:%s' % (connector.host, connector.port)) protocol.ReconnectingClientFactory.clientConnectionFailed( self, connector, reason)
def clientConnectionFailed(self, connector, reason): # debug("[SimpleIMAP4ClientFactory] clientConnectionFailed: %s" %reason.getErrorMessage()) self.e2session.onConnectionFailed(reason) protocol.ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
def clientConnectionLost(self, connector, reason): # debug("[SimpleIMAP4ClientFactory] clientConnectionLost: %s" %reason.getErrorMessage()) self.e2session.onConnectionLost(reason) protocol.ReconnectingClientFactory.clientConnectionLost(self, connector, reason)