<s> """<STR_LIT>""" <EOL> import random <EOL> from zope . interface import implements <EOL> from twisted . python import log , failure , components <EOL> from twisted . internet import interfaces , error , defer <EOL> class Factory : <EOL> """<STR_LIT>""" <EOL> implements ( interfaces . IProtocolFactory , interfaces . ILoggingContext ) <EOL> protocol = None <EOL> numPorts = <NUM_LIT:0> <EOL> noisy = True <EOL> def logPrefix ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __class__ . __name__ <EOL> def doStart ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . numPorts : <EOL> if self . noisy : <EOL> log . msg ( "<STR_LIT>" % self ) <EOL> self . startFactory ( ) <EOL> self . numPorts = self . numPorts + <NUM_LIT:1> <EOL> def doStop ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . numPorts == <NUM_LIT:0> : <EOL> return <EOL> self . numPorts = self . numPorts - <NUM_LIT:1> <EOL> if not self . numPorts : <EOL> if self . noisy : <EOL> log . msg ( "<STR_LIT>" % self ) <EOL> self . stopFactory ( ) <EOL> def startFactory ( self ) : <EOL> """<STR_LIT>""" <EOL> def stopFactory ( self ) : <EOL> """<STR_LIT>""" <EOL> def buildProtocol ( self , addr ) : <EOL> """<STR_LIT>""" <EOL> p = self . protocol ( ) <EOL> p . factory = self <EOL> return p <EOL> class ClientFactory ( Factory ) : <EOL> """<STR_LIT>""" <EOL> def startedConnecting ( self , connector ) : <EOL> """<STR_LIT>""" <EOL> def clientConnectionFailed ( self , connector , reason ) : <EOL> """<STR_LIT>""" <EOL> def clientConnectionLost ( self , connector , reason ) : <EOL> """<STR_LIT>""" <EOL> class _InstanceFactory ( ClientFactory ) : <EOL> """<STR_LIT>""" <EOL> noisy = False <EOL> pending = None <EOL> def __init__ ( self , reactor , instance , deferred ) : <EOL> self . reactor = reactor <EOL> self . instance = instance <EOL> self . deferred = deferred <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . instance , ) <EOL> def buildProtocol ( self , addr ) : <EOL> """<STR_LIT>""" <EOL> self . pending = self . reactor . callLater ( <EOL> <NUM_LIT:0> , self . fire , self . deferred . callback , self . instance ) <EOL> self . deferred = None <EOL> return self . instance <EOL> def clientConnectionFailed ( self , connector , reason ) : <EOL> """<STR_LIT>""" <EOL> self . pending = self . reactor . callLater ( <EOL> <NUM_LIT:0> , self . fire , self . deferred . errback , reason ) <EOL> self . deferred = None <EOL> def fire ( self , func , value ) : <EOL> """<STR_LIT>""" <EOL> self . pending = None <EOL> func ( value ) <EOL> class ClientCreator : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , reactor , protocolClass , * args , ** kwargs ) : <EOL> self . reactor = reactor <EOL> self . protocolClass = protocolClass <EOL> self . args = args <EOL> self . kwargs = kwargs <EOL> def _connect ( self , method , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> def cancelConnect ( deferred ) : <EOL> connector . disconnect ( ) <EOL> if f . pending is not None : <EOL> f . pending . cancel ( ) <EOL> d = defer . Deferred ( cancelConnect ) <EOL> f = _InstanceFactory ( <EOL> self . reactor , self . protocolClass ( * self . args , ** self . kwargs ) , d ) <EOL> connector = method ( factory = f , * args , ** kwargs ) <EOL> return d <EOL> def connectTCP ( self , host , port , timeout = <NUM_LIT:30> , bindAddress = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _connect ( <EOL> self . reactor . connectTCP , host , port , timeout = timeout , <EOL> bindAddress = bindAddress ) <EOL> def connectUNIX ( self , address , timeout = <NUM_LIT:30> , checkPID = False ) : <EOL> """<STR_LIT>""" <EOL> return self . _connect ( <EOL> self . reactor . connectUNIX , address , timeout = timeout , <EOL> checkPID = checkPID ) <EOL> def connectSSL ( self , host , port , contextFactory , timeout = <NUM_LIT:30> , bindAddress = None ) : <EOL> """<STR_LIT>""" <EOL> return self . _connect ( <EOL> self . reactor . connectSSL , host , port , <EOL> contextFactory = contextFactory , timeout = timeout , <EOL> bindAddress = bindAddress ) <EOL> class ReconnectingClientFactory ( ClientFactory ) : <EOL> """<STR_LIT>""" <EOL> maxDelay = <NUM_LIT> <EOL> initialDelay = <NUM_LIT:1.0> <EOL> factor = <NUM_LIT> <EOL> jitter = <NUM_LIT> <EOL> delay = initialDelay <EOL> retries = <NUM_LIT:0> <EOL> maxRetries = None <EOL> _callID = None <EOL> connector = None <EOL> clock = None <EOL> continueTrying = <NUM_LIT:1> <EOL> def clientConnectionFailed ( self , connector , reason ) : <EOL> if self . continueTrying : <EOL> self . connector = connector <EOL> self . retry ( ) <EOL> def clientConnectionLost ( self , connector , unused_reason ) : <EOL> if self . continueTrying : <EOL> self . connector = connector <EOL> self . retry ( ) <EOL> def retry ( self , connector = None ) : <EOL> """<STR_LIT>""" <EOL> if not self . continueTrying : <EOL> if self . noisy : <EOL> log . msg ( "<STR_LIT>" % ( connector , ) ) <EOL> return <EOL> if connector is None : <EOL> if self . connector is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> else : <EOL> connector = self . connector <EOL> self . retries += <NUM_LIT:1> <EOL> if self . maxRetries is not None and ( self . retries > self . maxRetries ) : <EOL> if self . noisy : <EOL> log . msg ( "<STR_LIT>" % <EOL> ( connector , self . retries ) ) <EOL> return <EOL> self . delay = min ( self . delay * self . factor , self . maxDelay ) <EOL> if self . jitter : <EOL> self . delay = random . normalvariate ( self . delay , <EOL> self . delay * self . jitter ) <EOL> if self . noisy : <EOL> log . msg ( "<STR_LIT>" % ( connector , self . delay , ) ) <EOL> def reconnector ( ) : <EOL> self . _callID = None <EOL> connector . connect ( ) <EOL> if self . clock is None : <EOL> from twisted . internet import reactor <EOL> self . clock = reactor <EOL> self . _callID = self . clock . callLater ( self . delay , reconnector ) <EOL> def stopTrying ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _callID : <EOL> self . _callID . cancel ( ) <EOL> self . _callID = None <EOL> self . continueTrying = <NUM_LIT:0> <EOL> if self . connector : <EOL> try : <EOL> self . connector . stopConnecting ( ) <EOL> except error . NotConnectingError : <EOL> pass <EOL> def resetDelay ( self ) : <EOL> """<STR_LIT>""" <EOL> self . delay = self . initialDelay <EOL> self . retries = <NUM_LIT:0> <EOL> self . _callID = None <EOL> self . continueTrying = <NUM_LIT:1> <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> state = self . __dict__ . copy ( ) <EOL> for key in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if key in state : <EOL> del state [ key ] <EOL> return state <EOL> class ServerFactory ( Factory ) : <EOL> """<STR_LIT>""" <EOL> class BaseProtocol : <EOL> """<STR_LIT>""" <EOL> connected = <NUM_LIT:0> <EOL> transport = None <EOL> def makeConnection ( self , transport ) : <EOL> """<STR_LIT>""" <EOL> self . connected = <NUM_LIT:1> <EOL> self . transport = transport <EOL> self . connectionMade ( ) <EOL> def connectionMade ( self ) : <EOL> """<STR_LIT>""" <EOL> connectionDone = failure . Failure ( error . ConnectionDone ( ) ) <EOL> connectionDone . cleanFailure ( ) <EOL> class Protocol ( BaseProtocol ) : <EOL> """<STR_LIT>""" <EOL> implements ( interfaces . IProtocol , interfaces . ILoggingContext ) <EOL> def logPrefix ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __class__ . __name__ <EOL> def dataReceived ( self , data ) : <EOL> """<STR_LIT>""" <EOL> def connectionLost ( self , reason = connectionDone ) : <EOL> """<STR_LIT>""" <EOL> class ProtocolToConsumerAdapter ( components . Adapter ) : <EOL> implements ( interfaces . IConsumer ) <EOL> def write ( self , data ) : <EOL> self . original . dataReceived ( data ) <EOL> def registerProducer ( self , producer , streaming ) : <EOL> pass <EOL> def unregisterProducer ( self ) : <EOL> pass <EOL> components . registerAdapter ( ProtocolToConsumerAdapter , interfaces . IProtocol , <EOL> interfaces . IConsumer ) <EOL> class ConsumerToProtocolAdapter ( components . Adapter ) : <EOL> implements ( interfaces . IProtocol ) <EOL> def dataReceived ( self , data ) : <EOL> self . original . write ( data ) <EOL> def connectionLost ( self , reason ) : <EOL> pass <EOL> def makeConnection ( self , transport ) : <EOL> pass <EOL> def connectionMade ( self ) : <EOL> pass <EOL> components . registerAdapter ( ConsumerToProtocolAdapter , interfaces . IConsumer , <EOL> interfaces . IProtocol ) <EOL> class ProcessProtocol ( BaseProtocol ) : <EOL> """<STR_LIT>""" <EOL> implements ( interfaces . IProcessProtocol ) <EOL> def childDataReceived ( self , childFD , data ) : <EOL> if childFD == <NUM_LIT:1> : <EOL> self . outReceived ( data ) <EOL> elif childFD == <NUM_LIT:2> : <EOL> self . errReceived ( data ) <EOL> def outReceived ( self , data ) : <EOL> """<STR_LIT>""" <EOL> def errReceived ( self , data ) : <EOL> """<STR_LIT>""" <EOL> def childConnectionLost ( self , childFD ) : <EOL> if childFD == <NUM_LIT:0> : <EOL> self . inConnectionLost ( ) <EOL> elif childFD == <NUM_LIT:1> : <EOL> self . outConnectionLost ( ) <EOL> elif childFD == <NUM_LIT:2> : <EOL> self . errConnectionLost ( ) <EOL> def inConnectionLost ( self ) : <EOL> """<STR_LIT>""" <EOL> def outConnectionLost ( self ) : <EOL> """<STR_LIT>""" <EOL> def errConnectionLost ( self ) : <EOL> """<STR_LIT>""" <EOL> def processExited ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> def processEnded ( self , reason ) : <EOL> """<STR_LIT>""" <EOL> class AbstractDatagramProtocol : <EOL> """<STR_LIT>""" <EOL> transport = None <EOL> numPorts = <NUM_LIT:0> <EOL> noisy = True <EOL> def __getstate__ ( self ) : <EOL> d = self . __dict__ . copy ( ) <EOL> d [ '<STR_LIT>' ] = None <EOL> return d <EOL> def doStart ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . numPorts : <EOL> if self . noisy : <EOL> log . msg ( "<STR_LIT>" % self ) <EOL> self . startProtocol ( ) <EOL> self . numPorts = self . numPorts + <NUM_LIT:1> <EOL> def doStop ( self ) : <EOL> """<STR_LIT>""" <EOL> assert self . numPorts > <NUM_LIT:0> <EOL> self . numPorts = self . numPorts - <NUM_LIT:1> <EOL> self . transport = None <EOL> if not self . numPorts : <EOL> if self . noisy : <EOL> log . msg ( "<STR_LIT>" % self ) <EOL> self . stopProtocol ( ) <EOL> def startProtocol ( self ) : <EOL> """<STR_LIT>""" <EOL> def stopProtocol ( self ) : <EOL> """<STR_LIT>""" <EOL> def makeConnection ( self , transport ) : <EOL> """<STR_LIT>""" <EOL> assert self . transport == None <EOL> self . transport = transport <EOL> self . doStart ( ) <EOL> def datagramReceived ( self , datagram , addr ) : <EOL> """<STR_LIT>""" <EOL> class DatagramProtocol ( AbstractDatagramProtocol ) : <EOL> """<STR_LIT>""" <EOL> implements ( interfaces . ILoggingContext ) <EOL> def logPrefix ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __class__ . __name__ <EOL> def connectionRefused ( self ) : <EOL> """<STR_LIT>""" <EOL> class ConnectedDatagramProtocol ( DatagramProtocol ) : <EOL> """<STR_LIT>""" <EOL> def datagramReceived ( self , datagram ) : <EOL> """<STR_LIT>""" <EOL> def connectionFailed ( self , failure ) : <EOL> """<STR_LIT>""" <EOL> class FileWrapper : <EOL> """<STR_LIT>""" <EOL> implements ( interfaces . ITransport ) <EOL> closed = <NUM_LIT:0> <EOL> disconnecting = <NUM_LIT:0> <EOL> producer = None <EOL> streamingProducer = <NUM_LIT:0> <EOL> def __init__ ( self , file ) : <EOL> self . file = file <EOL> def write ( self , data ) : <EOL> try : <EOL> self . file . write ( data ) <EOL> except : <EOL> self . handleException ( ) <EOL> def _checkProducer ( self ) : <EOL> if self . producer : <EOL> self . producer . resumeProducing ( ) <EOL> def registerProducer ( self , producer , streaming ) : <EOL> """<STR_LIT>""" <EOL> self . producer = producer <EOL> self . streamingProducer = streaming <EOL> if not streaming : <EOL> producer . resumeProducing ( ) <EOL> def unregisterProducer ( self ) : <EOL> self . producer = None <EOL> def stopConsuming ( self ) : <EOL> self . unregisterProducer ( ) <EOL> self . loseConnection ( ) <EOL> def writeSequence ( self , iovec ) : <EOL> self . write ( "<STR_LIT>" . join ( iovec ) ) <EOL> def loseConnection ( self ) : <EOL> self . closed = <NUM_LIT:1> <EOL> try : <EOL> self . file . close ( ) <EOL> except ( IOError , OSError ) : <EOL> self . handleException ( ) <EOL> def getPeer ( self ) : <EOL> return '<STR_LIT:file>' , '<STR_LIT:file>' <EOL> def getHost ( self ) : <EOL> return '<STR_LIT:file>' <EOL> def handleException ( self ) : <EOL> pass <EOL> def resumeProducing ( self ) : <EOL> pass <EOL> def pauseProducing ( self ) : <EOL> pass <EOL> def stopProducing ( self ) : <EOL> self . loseConnection ( ) <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] </s>
