def testNotifyFinishConnectionLost(self): d = DummyChannel() d.transport = DummyChannel.TCP() request = server.Request(d, 1) finished = request.notifyFinish() request.connectionLost(error.ConnectionDone("Connection done")) return self.assertFailure(finished, error.ConnectionDone)
def connectionLost(self, reason=failure.Failure(error.ConnectionDone())): if self.request_handler is not None: self.request_handler.stopProducing() self.factory.rate_limiter.unregister_protocol(self) if not reason.check(error.ConnectionDone): log.warning("Closing a connection. Reason: %s", reason.getErrorMessage())
def loseConnection(self, reason=Failure(error.ConnectionDone())): """Request that the connection be dropped.""" if self.disconnecting is None: self.registeredChannels.clear() d = self.disconnecting = Deferred() d.addBoth(callOut, self.stopReading) d.addBoth(callOut, self.cancelChannelRegistrar) d.addBoth(callOut, self.cancelHandleNotify) d.addBoth(callOut, deferToThread, self.stopConnection) d.addBoth(callOut, self.connectionLost, reason) def done(): self.disconnecting = None d.addBoth(callOut, done) if self.connecting is None: # Already/never connected: begin shutdown now. self.disconnecting.callback(None) else: # Still connecting: cancel before disconnect. self.connecting.addErrback(suppress, CancelledError) self.connecting.chainDeferred(self.disconnecting) self.connecting.cancel() return self.disconnecting
def _get(path, dn): path = to_unicode(path) dn = distinguishedname.DistinguishedName(dn) l = list(dn.split()) assert len(l) >= 1 l.reverse() parser = StoreParsedLDIF() entry = os.path.join(path, *[u'%s.dir' % rdn.getText() for rdn in l[:-1]]) entry = os.path.join(entry, u'%s.ldif' % l[-1].getText()) f = open(entry, 'rb') while 1: data = f.read(8192) if not data: break parser.dataReceived(data) parser.connectionLost(failure.Failure(error.ConnectionDone())) assert parser.done entries = parser.seen if len(entries) == 0: raise LDIFTreeEntryContainsNoEntries() elif len(entries) > 1: raise LDIFTreeEntryContainsMultipleEntries(entries) else: return entries[0]
def _doReadOrWrite(self, source, condition, faildict=None): faildict = faildict or { error.ConnectionDone: failure.Failure(error.ConnectionDone()), error.ConnectionLost: failure.Failure(error.ConnectionLost()), } why = None inRead = False if condition & POLL_DISCONNECTED and not (condition & glib.IO_IN): if source in self._reads: why = main.CONNECTION_DONE inRead = True else: why = main.CONNECTION_LOST else: try: if condition & glib.IO_IN: why = source.doRead() inRead = True if not why and condition & glib.IO_OUT: # if doRead caused connectionLost, don't call doWrite # if doRead is doWrite, don't call it again. if not source.disconnected: why = source.doWrite() except: why = sys.exc_info()[1] log.msg('Error In %s' % source) log.deferr() if why: self._disconnectSelectable(source, why, inRead)
def _doReadOrWrite(self, selectable, fd, event, POLLIN, POLLOUT, log, faildict={ error.ConnectionDone: failure.Failure(error.ConnectionDone()), error.ConnectionLost: failure.Failure(error.ConnectionLost()) }): why = None inRead = False if event & POLL_DISCONNECTED and not (event & POLLIN): why = main.CONNECTION_LOST else: try: if event & POLLIN: why = selectable.doRead() inRead = True if not why and event & POLLOUT: why = selectable.doWrite() inRead = False if not selectable.fileno() == fd: why = error.ConnectionFdescWentAway('Filedescriptor went away') inRead = False except AttributeError, ae: if "'NoneType' object has no attribute 'writeHeaders'" not in ae.message: log.deferr() why = sys.exc_info()[1] else: why = None except:
def stopService(self): # note that once you stopService a Tub, I cannot be restarted. (at # least this code is not designed to make that possible.. it might be # doable in the future). assert self.running self.startService = self._tubsAreNotRestartable self.getReference = self._tubHasBeenShutDown self.connectTo = self._tubHasBeenShutDown dl = [] for rc in self.reconnectors: rc.stopConnecting() del self.reconnectors for l in self.listeners: # TODO: rethink this, what I want is for stopService to cause all # Listeners to shut down, but I'm not sure this is the right way # to do it. d = l.removeTub(self) if isinstance(d, defer.Deferred): dl.append(d) dl.append(service.MultiService.stopService(self)) if self._activeConnectors: dl.append(self._allConnectorsAreFinished.whenFired()) for c in self._activeConnectors: c.shutdown() if self._allConnections: dl.append(self._allConnectionsAreDisconnected.whenFired()) why = Failure(error.ConnectionDone("Tub.stopService was called")) for b in self.brokers.values(): b.shutdown(why, fireDisconnectWatchers=False) for b in self.unauthenticatedBrokers: b.shutdown(why, fireDisconnectWatchers=False) return defer.DeferredList(dl)
def _doReadOrWrite(self, selectable, method, dict, faildict={ error.ConnectionDone: failure.Failure(error.ConnectionDone()), error.ConnectionLost: failure.Failure(error.ConnectionLost()) }): """ Handle IOErrors/out of disk space """ try: why = getattr(selectable, method)() handfn = getattr(selectable, 'fileno', None) if not handfn: why = _NO_FILENO elif handfn() == -1: why = _NO_FILEDESC except IOError, ioe: # NOTE: Importing this in the module causes TimeoutMixin to not work. ??? from twisted.protocols.policies import ThrottlingProtocol # Handle OutOfDiskSpace exceptions. Piggybacking this check into the reactor # here uses less CPU than try: excepting in NZBLeecher.dataReceived if selectable.protocol.__class__ is ThrottlingProtocol: from Hellanzb.Util import OutOfDiskSpace from Hellanzb.NZBLeecher.ArticleDecoder import handleIOError try: handleIOError(ioe) except OutOfDiskSpace: # handleIOError would have just paused the downloader for us selectable.protocol.wrappedProtocol.transport.loseConnection() selectable.protocol.wrappedProtocol.isLoggedIn = False selectable.protocol.wrappedProtocol.deactivate() return except: pass why = sys.exc_info()[1] log.err()
def connectionLost( self, reason ): """Connection lost, clean up callbacks""" for key,callable in self.actionIDCallbacks.items(): try: callable( tw_error.ConnectionDone( """FastAGI connection terminated""") ) except Exception, err: log.error( """Failure during connectionLost for callable %s: %s""", callable, err )
def test_encode_task(self): self.protocol.handshakeSuccess('') self.protocol.startEncoding() self.assertTrue(hasattr(self.protocol, 'encoder_task')) self.protocol.connectionLost(error.ConnectionDone()) self.assertFalse(hasattr(self.protocol, 'encoder_task'))
def test_stream(self): self.protocol.handshakeSuccess('') self.protocol.connectionLost(error.ConnectionDone()) self.assertFalse(hasattr(self.protocol, 'decoder')) self.assertFalse(hasattr(self.protocol, 'encoder'))
def test_connectionLost(self): """ The producer is unregistered at the dispatcher on lost connection. """ connectionLostCalls = [] callbackCalls = [] def connectionLost(self, reason): connectionLostCalls.append(reason) def cb(event): raise Exception("Unexpected callback") # Set up publisher as if connected clock = task.Clock() self.publisher.producer = twisted.QueueProducer(callback=cb, clock=clock) self.publisher.producer.resumeProducing() self.publisher.dispatcher = twisted.Dispatcher() self.publisher.dispatcher.register(self.publisher.producer.put) # Patch parent class to test up-call self.patch(AMQClient, 'connectionLost', connectionLost) # Drop connection self.publisher.connectionLost(error.ConnectionDone()) self.assertEqual(1, len(connectionLostCalls)) self.assertEqual(1, len(self.flushLoggedErrors(error.ConnectionDone))) # Test that the producer was unregistered with the dispatcher event = {'message': 'test'} self.publisher.dispatcher.eventReceived(event) clock.advance(0) self.assertEqual(0, len(callbackCalls))
def test_connectionLost_does_not_log_reason_when_lost_cleanly(self): listener = PostgresListenerService() with TwistedLoggerFixture() as logger: listener.connectionLost(Failure(error.ConnectionDone())) self.assertThat(logger.errors, HasLength(0))
def _load(self): assert self.path.endswith(u'.dir') entryPath = u'%s.ldif' % self.path[:-len(u'.dir')] parser = StoreParsedLDIF() try: f = open(entryPath, 'rb') except IOError as e: if e.errno == errno.ENOENT: return else: raise while 1: data = f.read(8192) if not data: break parser.dataReceived(data) parser.connectionLost(failure.Failure(error.ConnectionDone())) assert parser.done entries = parser.seen if len(entries) == 0: raise LDIFTreeEntryContainsNoEntries() elif len(entries) > 1: raise LDIFTreeEntryContainsMultipleEntries(entries) else: for k, v in entries[0].items(): self._attributes[k] = attributeset.LDAPAttributeSet(k, v)
def _disconnectSelectable( self, selectable, why, isRead, faildict={ error.ConnectionDone: failure.Failure(error.ConnectionDone()), error.ConnectionLost: failure.Failure(error.ConnectionLost()) }): """ Utility function for disconnecting a selectable. Supports half-close notification, isRead should be boolean indicating whether error resulted from doRead(). """ self.removeReader(selectable) f = faildict.get(why.__class__) if f: if (isRead and why.__class__ == error.ConnectionDone and IHalfCloseableDescriptor.providedBy(selectable)): selectable.readConnectionLost(f) else: self.removeWriter(selectable) selectable.connectionLost(f) else: self.removeWriter(selectable) selectable.connectionLost(failure.Failure(why))
def stopService(self): # note that once you stopService a Tub, I cannot be restarted. (at # least this code is not designed to make that possible.. it might be # doable in the future). assert self.running self.startService = self._tubsAreNotRestartable self.getReference = self._tubHasBeenShutDown self.connectTo = self._tubHasBeenShutDown # Tell everything to shut down now. We assume that it will stop # twitching by the next tick, so Trial unit tests won't complain # about a dirty reactor. We wait on a few things that might not # behave. dl = [] for rc in list(self.reconnectors): rc.stopConnecting() del self.reconnectors for c in list(self._activeConnectors): c.shutdown() why = Failure(error.ConnectionDone("Tub.stopService was called")) for b in self.brokers.values(): broker_disconnected = defer.Deferred() dl.append(broker_disconnected) b._notifyOnConnectionLost( lambda d=broker_disconnected: d.callback(None)) b.shutdown(why, fireDisconnectWatchers=False) d = defer.DeferredList(dl) d.addCallback(lambda _: service.MultiService.stopService(self)) d.addCallback(eventual.fireEventually) return d
def _doReadOrWrite( self, source, condition, faildict={ error.ConnectionDone: failure.Failure(error.ConnectionDone()), error.ConnectionLost: failure.Failure(error.ConnectionLost()), }): why = None didRead = None if condition & POLL_DISCONNECTED and \ not (condition & gobject.IO_IN): why = main.CONNECTION_LOST else: try: if condition & gobject.IO_IN: why = source.doRead() didRead = source.doRead if not why and condition & gobject.IO_OUT: # if doRead caused connectionLost, don't call doWrite # if doRead is doWrite, don't call it again. if not source.disconnected and source.doWrite != didRead: why = source.doWrite() didRead = source.doWrite # if failed it was in write except: why = sys.exc_info()[1] log.msg('Error In %s' % source) log.deferr() if why: self._disconnectSelectable(source, why, didRead == source.doRead)
def _doReadOrWrite( self, selectable, fd, event, POLLIN, POLLOUT, log, faildict={ error.ConnectionDone: failure.Failure(error.ConnectionDone()), error.ConnectionLost: failure.Failure(error.ConnectionLost()) }): why = None inRead = False if event & POLL_DISCONNECTED and not (event & POLLIN): why = main.CONNECTION_LOST else: try: if event & POLLIN: why = selectable.doRead() inRead = True if not why and event & POLLOUT: why = selectable.doWrite() inRead = False if not selectable.fileno() == fd: why = error.ConnectionFdescWentAway( 'Filedescriptor went away') inRead = False except: log.deferr() why = sys.exc_info()[1] if why: self._disconnectSelectable(selectable, why, inRead)
def test_incompleteUsername(self): """ Test that a login attempt using a username without a domain part results in a customized authentication failure message which points out that a domain part should be included in the username. """ mta = mail.MailTransferAgent(store=self.store) installOn(mta, self.store) factory = mta.getFactory() protocol = factory.buildProtocol(('192.168.1.1', 12345)) transport = StringTransport() transport.getHost = lambda: IPv4Address('TCP', '192.168.1.1', 54321) transport.getPeer = lambda: IPv4Address('TCP', '192.168.1.1', 12345) protocol.makeConnection(transport) protocol.dataReceived('EHLO example.net\r\n') protocol.dataReceived('AUTH LOGIN\r\n') protocol.dataReceived('testuser'.encode('base64') + '\r\n') transport.clear() protocol.dataReceived('password'.encode('base64') + '\r\n') written = transport.value() protocol.connectionLost(failure.Failure(error.ConnectionDone())) self.assertEquals( written, '535 Authentication failure [Username without domain name (ie ' '"yourname" instead of "yourname@yourdomain") not allowed; try ' 'with a domain name.]\r\n')
def test_connection_lost_connectiondone(self): # When the ConnectionDone is transmitted, consider it an expected # disconnection. self.conn._on_stream_terminated = mock.Mock() error = Failure(twisted_error.ConnectionDone()) self.conn.connection_lost(error) self.assertEqual(self.conn._error, error.value) self.conn._on_stream_terminated.assert_called_with(None) self.assertIsNone(self.conn._transport)
def connectionLost( self, reason ): """(Internal) Handle loss of the connection (remote hangup)""" log.info( """Connection terminated""" ) try: for df in self.pendingMessages: df.errback( tw_error.ConnectionDone( """FastAGI connection terminated""") ) finally: if self.lostConnectionDeferred: self.lostConnectionDeferred.errback( reason ) del self.pendingMessages[:]
def test_connectionLost(self): """ L{server.Request.connectionLost} triggers all finish notification Deferreds and cleans up per-request state. """ d = DummyChannel() request = server.Request(d, 1) finished = request.notifyFinish() request.connectionLost(error.ConnectionDone("Connection done")) self.assertIdentical(request.channel, None) return self.assertFailure(finished, error.ConnectionDone)
def testESMTPGreetingExtended(self): """ Test that the string "ESMTP" does appear in the ESMTP server's greeting since L{smtp.ESMTP} does support the SMTP extensions which that advertises to the client. """ s = smtp.ESMTP() t = StringTransport() s.makeConnection(t) s.connectionLost(error.ConnectionDone()) self.assertIn("ESMTP", t.value())
def testSMTPGreetingHost(self, serverClass=smtp.SMTP): """ Test that the specified hostname shows up in the SMTP server's greeting. """ s = serverClass() s.host = "example.com" t = StringTransport() s.makeConnection(t) s.connectionLost(error.ConnectionDone()) self.assertIn("example.com", t.value())
def testSMTPGreetingNotExtended(self): """ Test that the string "ESMTP" does not appear in the SMTP server's greeting since that string strongly suggests the presence of support for various SMTP extensions which are not supported by L{smtp.SMTP}. """ s = smtp.SMTP() t = StringTransport() s.makeConnection(t) s.connectionLost(error.ConnectionDone()) self.assertNotIn("ESMTP", t.value())
def pump(self, debug=False): """Move data back and forth. Returns whether any data was moved. """ if self.debug or debug: print '-- GLUG --' sData = readAndDestroy(self.serverIO) cData = readAndDestroy(self.clientIO) self.client.transport._checkProducer() self.server.transport._checkProducer() if self.debug or debug: print '.' # XXX slightly buggy in the face of incremental output if cData: for line in cData.split('\r\n'): print 'C: '+line if sData: for line in sData.split('\r\n'): print 'S: '+line if cData: self.server.dataReceived(cData) if sData: self.client.dataReceived(sData) if cData or sData: return True if self.server.transport.disconnecting and not self.server.transport.disconnected: if self.debug or debug: print '* C' self.server.transport.disconnected = True self.client.transport.disconnecting = True self.client.connectionLost(error.ConnectionDone("Connection done")) return True if self.client.transport.disconnecting and not self.client.transport.disconnected: if self.debug or debug: print '* S' self.client.transport.disconnected = True self.server.transport.disconnecting = True self.server.connectionLost(error.ConnectionDone("Connection done")) return True return False
def fromLDIFFile(f): """Read LDIF data from a file.""" p = InMemoryLDIFProtocol() while 1: data = f.read() if not data: break p.dataReceived(data) p.connectionLost(Failure(error.ConnectionDone())) return p.completed
def conn_setup(node: NeoNode): # at this point we should have a fully connected node, so lets try to simulate a connection lost by the other side with self.assertLogHandler('network', 10) as log: node.connectionLost(failure.Failure(error.ConnectionDone())) self.assertTrue( "disconnected normally with reason" in log.output[-1]) self.assertNotIn(self.addr, self.leader.DEAD_ADDRS) self.assertIn(self.addr, self.leader.KNOWN_ADDRS) self.assertNotIn(self.addr, self.leader.Peers) self.assertFalse(node.has_tasks_running())
def eventReceived(self, e): opcode = e[0] if opcode == 'c': command, user, message = e[1:] messageLen = len(message) # how many elements in msg handler = getattr(self, 'e_%s' % command, None) or getattr(self, 'elen_%i' % messageLen, None) or self.eUnknown line = handler(command, user, message) if line: if not user: line = line[2:] return line elif opcode == 'disconnect': raise error.ConnectionDone()
def testPASV(self): # Login self._anonymousLogin() # Issue a PASV command, and extract the host and port from the response responseLines = wait(self.client.queueStringCommand('PASV')) host, port = ftp.decodeHostPort(responseLines[-1][4:]) # Make sure the server is listening on the port it claims to be self.assertEqual(port, self.serverProtocol.dtpPort.getHost().port) # Semi-reasonable way to force cleanup self.serverProtocol.connectionLost(error.ConnectionDone())