def createWrappingFactory(f): """ Wrap creation of _WrappingFactory since __init__() doesn't take a canceller as of Twisted 12.1 or something. """ if len(inspect.getargspec(_WrappingFactory.__init__)[0]) == 3: def _canceller(deferred): connector.stopConnecting() deferred.errback( error.ConnectingCancelledError( connector.getDestination())) return _WrappingFactory(f, _canceller) else: # Twisted >= 12.1. return _WrappingFactory(f)
def createWrappingFactory(f): """ Wrap creation of _WrappingFactory since __init__() doesn't take a canceller as of Twisted 12.1 or something. """ if len(inspect.getargspec(_WrappingFactory.__init__)[0]) == 3: def _canceller(deferred): connector.stopConnecting() deferred.errback(error.ConnectingCancelledError(connector.getDestination())) return _WrappingFactory(f, _canceller) else: # Twisted >= 12.1. return _WrappingFactory(f)
def test_failedBuildProtocol(self): """ An exception raised in C{buildProtocol} of our wrappedFactory results in our C{onConnection} errback being fired. """ class BogusFactory(ClientFactory): """ A one off factory whose C{buildProtocol} raises an C{Exception}. """ def buildProtocol(self, addr): raise ValueError("My protocol is poorly defined.") wf = endpoints._WrappingFactory(BogusFactory(), None) wf.buildProtocol(None) d = self.assertFailure(wf._onConnection, ValueError) d.addCallback(lambda e: self.assertEquals( e.args, ("My protocol is poorly defined.",))) return d
def test_failedBuildProtocol(self): """ An exception raised in C{buildProtocol} of our wrappedFactory results in our C{onConnection} errback being fired. """ class BogusFactory(ClientFactory): """ A one off factory whose C{buildProtocol} raises an C{Exception}. """ def buildProtocol(self, addr): raise ValueError("My protocol is poorly defined.") wf = endpoints._WrappingFactory(BogusFactory(), None) wf.buildProtocol(None) d = self.assertFailure(wf._onConnection, ValueError) d.addCallback(lambda e: self.assertEqual( e.args, ("My protocol is poorly defined.",))) return d
def test_logPrefixPassthrough(self): """ If the wrapped protocol provides L{ILoggingContext}, whatever is returned from the wrapped C{logPrefix} method is returned from L{_WrappingProtocol.logPrefix}. """ wf = endpoints._WrappingFactory(TestFactory(), None) wp = wf.buildProtocol(None) self.assertEqual(wp.logPrefix(), "A Test Protocol")
def test_doStart(self): """ L{_WrappingFactory.doStart} passes through to the wrapped factory's C{doStart} method, allowing application-specific setup and logging. """ factory = ClientFactory() wf = endpoints._WrappingFactory(factory, None) wf.doStart() self.assertEqual(1, factory.numPorts)
def connect(self, protocolFactory): """ Implement L{IStreamClientEndpoint.connect} to connect via TCP. """ try: wf = _WrappingFactory(protocolFactory) self.agent = Agent(self._reactor) return wf._onConnection except: return defer.fail()
def test_wrappedProtocolConnectionLost(self): """ Our wrappedProtocol's connectionLost method is called when L{_WrappingProtocol.connectionLost} is called. """ tf = TestFactory() wf = endpoints._WrappingFactory(tf, None) p = wf.buildProtocol(None) p.connectionLost("fail") self.assertEquals(p._wrappedProtocol.connectionsLost, ["fail"])
def connect(self, protocolFactory): # noinspection PyBroadException try: wf = endpoints._WrappingFactory(protocolFactory) reader = LircReader(self._program_name, self._lirc_config, self._reactor) reader.protocol = wf.buildProtocol(None) reader.protocol.transport = reader reader.startReading() return wf._onConnection except: return defer.fail()
def test_logPrefixDefault(self): """ If the wrapped protocol does not provide L{ILoggingContext}, the wrapped protocol's class name is returned from L{_WrappingProtocol.logPrefix}. """ class NoProtocol(object): pass factory = TestFactory() factory.protocol = NoProtocol wf = endpoints._WrappingFactory(factory, None) wp = wf.buildProtocol(None) self.assertEqual(wp.logPrefix(), "NoProtocol")
def connect(self, protocolFactory): def _canceller(deferred): connector.stopConnecting() deferred.errback( error.ConnectingCancelledError(connector.getDestination())) try: wf = _WrappingFactory(protocolFactory, _canceller) connector = SRVConnector(self._reactor, self._service, self._domain, wf, protocol=self._protocol) connector.connect() return wf._onConnection except: return defer.fail()
def test_wrappedProtocolDataReceived(self): """ The wrapped C{Protocol}'s C{dataReceived} will get called when our C{_WrappingProtocol}'s C{dataReceived} gets called. """ wf = endpoints._WrappingFactory(TestFactory(), None) p = wf.buildProtocol(None) p.makeConnection(None) p.dataReceived('foo') self.assertEquals(p._wrappedProtocol.data, ['foo']) p.dataReceived('bar') self.assertEquals(p._wrappedProtocol.data, ['foo', 'bar'])
def connect(self, factory): tls_factory = tls.TLSMemoryBIOFactory(self._server_options, isClient=False, wrappedFactory=factory) try: wf = _WrappingFactory(tls_factory) self._reactor.connectTCP(self._host, self._port, wf, timeout=self._timeout, bindAddress=self._bindAddress) return wf._onConnection except: return defer.fail()
def _connectRelay(self, process, protocolFactory): """Set up and connect the protocol we want to relay to the process. This method is automatically called when the process is started, and we are ready to relay through it. """ try: wf = _WrappingFactory(protocolFactory) connector = RelayConnector(process, wf, self.timeout, self.inductor.reactor) connector.connect() except: return defer.fail() # Return a deferred that is called back when the protocol is connected. return wf._onConnection
def test_wrappedProtocolTransport(self): """ Our transport is properly hooked up to the wrappedProtocol when a connection is made. """ wf = endpoints._WrappingFactory(TestFactory(), None) p = wf.buildProtocol(None) dummyTransport = object() p.makeConnection(dummyTransport) self.assertEquals(p.transport, dummyTransport) self.assertEquals(p._wrappedProtocol.transport, dummyTransport)
def connect(self, protocolFactory): """ Return a deferred firing when the SOCKS connection is established. """ try: # Connect with an intermediate SOCKS factory/protocol, # which then hands control to the provided protocolFactory # once a SOCKS connection has been established. f = self.factory() f.postHandshakeEndpoint = self._endpoint f.postHandshakeFactory = protocolFactory f.handshakeDone = defer.Deferred() wf = _WrappingFactory(f) self._reactor.connectTCP(self._host, self._port, wf) return f.handshakeDone except: return defer.fail()
def test_clientConnectionFailed(self): """ Calls to L{_WrappingFactory.clientConnectionLost} should errback the L{_WrappingFactory._onConnection} L{Deferred} """ wf = endpoints._WrappingFactory(TestFactory(), None) expectedFailure = Failure(error.ConnectError(string="fail")) wf.clientConnectionFailed(None, expectedFailure) errors = [] def gotError(f): errors.append(f) wf._onConnection.addErrback(gotError) self.assertEquals(errors, [expectedFailure])
def test_clientConnectionFailed(self): """ Calls to L{_WrappingFactory.clientConnectionLost} should errback the L{_WrappingFactory._onConnection} L{Deferred} """ wf = endpoints._WrappingFactory(TestFactory(), None) expectedFailure = Failure(error.ConnectError(string="fail")) wf.clientConnectionFailed( None, expectedFailure) errors = [] def gotError(f): errors.append(f) wf._onConnection.addErrback(gotError) self.assertEquals(errors, [expectedFailure])
def connect(self, protocolFactory): """ Return a deferred firing when the SOCKS connection is established. """ def _canceller(deferred): connector.stopConnecting() deferred.errback( error.ConnectingCancelledError(connector.getDestination())) try: # Connect with an intermediate SOCKS factory/protocol, # which then hands control to the provided protocolFactory # once a SOCKS connection has been established. f = self.factory() f.protocol.postHandshakeEndpoint = self._endpoint f.protocol.postHandshakeFactory = protocolFactory f.protocol.handshakeDone = defer.Deferred() wf = _WrappingFactory(f, _canceller) self._reactor.connectTCP(self._host, self._port, wf) return f.protocol.handshakeDone except: return defer.fail()
def connect(self, protocolFactory): wf = _WrappingFactory(protocolFactory) connector = StringTransportConnector( self._string_transport, wf, self._reactor) connector.connect() return wf._onConnection
def connect(self, protocolFactory): wf = _WrappingFactory(protocolFactory) connector = StringTransportConnector(self._string_transport, wf, self._reactor) connector.connect() return wf._onConnection