mò  ‹Dc @sdZdkZdklZdklZlZlZdkl Z l Z l Z dfd„ƒYZ de fd„ƒYZ d e fd „ƒYZd fd „ƒYZd e fd„ƒYZde fd„ƒYZdfd„ƒYZeie iƒƒZeiƒdefd„ƒYZdeifd„ƒYZeiee ie iƒdeifd„ƒYZeiee ie iƒdefd„ƒYZdfd„ƒYZdefd„ƒYZ de fd „ƒYZ!d!fd"„ƒYZ"ddd d#ddd!ddddd g Z#dS($sCStandard implementations of Twisted protocol-related interfaces. Start here if you are looking to write a new protocol implementation for Twisted. The Protocol class contains some introductory material. API Stability: stable, other than ClientCreator. Maintainer: U{Itamar Shtull-Trauring} N(s implements(slogsfailures components(s interfacesserrorsdefertFactorycBsZtZdZeeiƒeZdZe Z d„Z d„Z d„Z d„Zd„ZRS(sThis is a factory which produces protocols. By default, buildProtocol will create a protocol of the class given in self.protocol. icCsK|ip-|iotid|ƒn|iƒn|id|_dS(scMake sure startFactory is called. Users should not call this function themselves! sStarting factory %riN(tselftnumPortstnoisytlogtmsgt startFactory(R((tq/home/radix/Projects/Twisted/branches/releases/twisted-core-2.5.x-2329-2/Twisted.exp/twisted/internet/protocol.pytdoStart(s   cCsc|idjodSn|id|_|ip-|iotid|ƒn|iƒndS(sbMake sure stopFactory is called. Users should not call this function themselves! iNisStopping factory %r(RRRRRt stopFactory(R((RtdoStop3s  cCsdS(spThis will be called before I begin listening on a Port or Connector. It will only be called once, even if the factory is connected to multiple ports. This can be used to perform 'unserialization' tasks that are best put off until things are actually running, such as connecting to a database, opening files, etcetera. N((R((RRBs cCsdS(s}This will be called before I stop listening on all Ports/Connectors. This can be overridden to perform 'shutdown' tasks such as disconnecting database connections, closing files, etc. It will be called, for example, before an application shuts down, if it was connected to a port. User code should not call this function directly. N((R((RR Ms cCs|iƒ}||_|S(svCreate an instance of a subclass of Protocol. The returned instance will handle input on an incoming server connection, and an attribute "factory" pointing to the creating factory. Override this method to alter how Protocol instances get created. @param addr: an object implementing L{twisted.internet.interfaces.IAddress} N(Rtprotocoltptfactory(RtaddrR ((Rt buildProtocolXs   (t__name__t __module__t__doc__t implementst interfacestIProtocolFactorytNoneR RtTrueRRR RR R(((RRs    t ClientFactorycBs)tZdZd„Zd„Zd„ZRS(sxA Protocol factory for clients. This can be used together with the various connectXXX methods in reactors. cCsdS(s³Called when a connection has been started. You can call connector.stopConnecting() to stop the connection attempt. @param connector: a Connector object. N((Rt connector((RtstartedConnectingoscCsdS(s¾Called when a connection has failed to connect. It may be useful to call connector.connect() - this will reconnect. @type reason: L{twisted.python.failure.Failure} N((RRtreason((RtclientConnectionFailedwscCsdS(s½Called when an established connection is lost. It may be useful to call connector.connect() - this will reconnect. @type reason: L{twisted.python.failure.Failure} N((RRR((RtclientConnectionLosts(RRRRRR(((RRhs   t_InstanceFactorycBs8tZdZeZd„Zd„Zd„Zd„ZRS(sFactory used by ClientCreator.cCs||_||_||_dS(N(treactorRtinstancetdeferred(RRR R!((Rt__init__s  cCsd|ifS(Ns(RR (R((Rt__repr__’scCs,|iid|ii|iƒ|`|iS(Ni(RRt callLaterR!tcallbackR (RR((RR•scCs&|iid|ii|ƒ|`dS(Ni(RRR$R!terrbackR(RRR((RRšs( RRRtFalseRR"R#RR(((RRˆs    t ClientCreatorcBsDtZdZd„Zded„Zddd„Zded„ZRS(sClient connections that do not require a factory. The various connect* methods create a protocol instance using the given protocol class and arguments, and connect it, returning a Deferred of the resulting protocol instance. Useful for cases when we don't really need a factory. Mainly this is when there is no shared state between protocol instances, and no need to reconnect. cOs(||_||_||_||_dS(N(RRt protocolClasstargstkwargs(RRR)R*R+((RR"«s   icCsYtiƒ}t|i|i|i|iŽ|ƒ}|ii |||d|d|ƒ|S(sGConnect to remote host, return Deferred of resulting protocol instance.ttimeoutt bindAddressN(tdefertDeferredtdRRRR)R*R+tft connectTCPthosttportR,R-(RR3R4R,R-R0R1((RR2±s  '"icCsVtiƒ}t|i|i|i|iŽ|ƒ}|ii ||d|d|ƒ|S(sGConnect to Unix socket, return Deferred of resulting protocol instance.R,tcheckPIDN(R.R/R0RRRR)R*R+R1t connectUNIXtaddressR,R5(RR7R,R5R0R1((RR6¸s  'c Cs\tiƒ}t|i|i|i|iŽ|ƒ}|ii ||||d|d|ƒ|S(sFConnect to SSL server, return Deferred of resulting protocol instance.R,R-N(R.R/R0RRRR)R*R+R1t connectSSLR3R4tcontextFactoryR,R-(RR3R4R9R,R-R0R1((RR8¿s  '%(RRRR"RR2R6R8(((RR(Ÿs  tReconnectingClientFactorycBsztZdZdZdZdZdZeZdZe Z e Z e Z dZ d„Zd„Ze d „Zd „Zd „ZRS( sÞMy clients auto-reconnect with an exponential back-off. Note that clients should call my resetDelay method after they have connected successfully. @ivar maxDelay: Maximum number of seconds between connection attempts. @ivar initialDelay: Delay for the first reconnection attempt. @ivar factor: a multiplicitive factor by which the delay grows @ivar jitter: percentage of randomness to introduce into the delay length to prevent stampeding. if1.0f2.7182818284590451f 0.11962656492iicCs%|io||_|iƒndS(N(RtcontinueTryingRtretry(RRR((RRås  cCs%|io||_|iƒndS(N(RR;RR<(RRt unused_reason((RRês  csˆip*ˆiotidˆfƒndSnˆdjo-ˆidjotdƒ‚qnˆi‰nˆid7_ˆi dj oCˆiˆi jo0ˆiotidˆˆifƒndSnt ˆi ˆi ˆi ƒˆ_ ˆio&tiˆi ˆi ˆiƒˆ_ nˆiotidˆˆi fƒndkl}‡‡d†}|iˆi |ƒˆ_dS( sCHave this connector connect again, after a suitable delay. s!Abandoning %s on explicit requestNsno connector to retryisAbandoning %s after %d retries.s%s will retry in %d seconds(sreactorcsdˆ_ˆiƒdS(N(RRt_callIDRtconnect((RR(Rt reconnector s (RR;RRRRRt ValueErrortretriest maxRetriestmintdelaytfactortmaxDelaytjittertrandomt normalvariatettwisted.internetRR@R$R>(RRR@R((RRRR<ïs.    #     cCsi|io|iiƒd|_n|io.y|iiƒWq\tij oq\Xnd|_dS(s>I put a stop to any attempt to reconnect in progress. iN( RR>tcancelRRtstopConnectingterrortNotConnectingErrorR;(R((Rt stopTryings     cCs+|i|_d|_d|_d|_dS(sjCall me after a successful connection to reset. I reset the delay and the retry counter. iiN(Rt initialDelayRERBRR>R;(R((Rt resetDelay"s    (RRRRGRQRFRHRERBRRCR>RR;RRR<RPRR(((RR:Çs    # t ServerFactorycBstZdZRS(sUSubclass this to indicate that your protocol.Factory is only usable for servers. (RRR(((RRS-s t BaseProtocolcBs,tZdZdZeZd„Zd„ZRS(s­This is the abstract superclass of all protocols. If you are going to write a new protocol for Twisted, start here. The docstrings of this class explain how you can get started. Any protocol implementation, either client or server, should be a subclass of me. My API is quite simple. Implement dataReceived(data) to handle both event-based and synchronous input; output can be sent through the 'transport' attribute, which is to be an instance that implements L{twisted.internet.interfaces.ITransport}. Some subclasses exist already to help you write common types of protocols: see the L{twisted.protocols.basic} module for a few of them. icCs d|_||_|iƒdS(s§Make a connection to a transport and a server. This sets the 'transport' attribute of this Protocol, and calls the connectionMade() callback. iN(Rt connectedt transporttconnectionMade(RRV((RtmakeConnectionEs  cCsdS(sÃCalled when a connection is made. This may be considered the initializer of the protocol, because it is called when the connection is completed. For clients, this is called once the connection to the server has been established; for servers, this is called after an accept() call stops blocking and a socket has been received. If you need to send any greeting or initial message, do it here. N((R((RRWOs (RRRRURRVRXRW(((RRT2s  tProtocolcBs*tZeeiƒd„Zed„ZRS(NcCsdS(s=Called whenever data is received. Use this method to translate to a higher-level message. Usually, some callback will be made upon the receipt of each complete protocol message. @param data: a string of indeterminate length. Please keep in mind that you will probably need to buffer some data, as partial (or multiple) protocol messages may be received! I recommend that unit tests for protocols call through to this method with differing chunk sizes, down to one byte at a time. N((Rtdata((Rt dataReceivedbs cCsdS(sîCalled when the connection is shut down. Clear any circular references here, and any external references to this Protocol. The connection has been closed. @type reason: L{twisted.python.failure.Failure} N((RR((RtconnectionLostps(RRRRt IProtocolR[tconnectionDoneR\(((RRY^s  tProtocolToConsumerAdaptercBs6tZdZeeiƒd„Zd„Zd„ZRS(s! This class is unstable. cCs|ii|ƒdS(N(RtoriginalR[RZ(RRZ((Rtwrite€scCsdS(N((Rtproducert streaming((RtregisterProducerƒscCsdS(N((R((RtunregisterProducer†s( RRRRRt IConsumerRaRdRe(((RR_zs    tConsumerToProtocolAdaptercBs?tZdZeeiƒd„Zd„Zd„Zd„Z RS(s! This class is unstable. cCs|ii|ƒdS(N(RR`RaRZ(RRZ((RR[’scCsdS(N((RR((RR\•scCsdS(N((RRV((RRX˜scCsdS(N((R((RRW›s( RRRRRR]R[R\RXRW(((RRgŒs     tProcessProtocolcBsVtZdZd„Zd„Zd„Zd„Zd„Zd„Zd„Z d„Z RS( sCProcesses have some additional methods besides receiving data. cCs@|djo|i|ƒn|djo|i|ƒndS(Nii(tchildFDRt outReceivedRZt errReceived(RRiRZ((RtchildDataReceived¥s  cCsdS(s#Some data was received from stdout.N((RRZ((RRj«scCsdS(s#Some data was received from stderr.N((RRZ((RRk­scCsU|djo|iƒn7|djo|iƒn|djo|iƒndS(Niii(RiRtinConnectionLosttoutConnectionLostterrConnectionLost(RRi((RtchildConnectionLost°s    cCsdS(s)This will be called when stdin is closed.N((R((RRm¸scCsdS(s*This will be called when stdout is closed.N((R((RRnºscCsdS(s*This will be called when stderr is closed.N((R((RRo¼scCsdS(svThis will be called when the subprocess is finished. @type reason: L{twisted.python.failure.Failure} N((RR((Rt processEnded¿s( RRRRlRjRkRpRmRnRoRq(((RRh¡s        tAbstractDatagramProtocolcBs_tZdZeZdZeZd„Zd„Z d„Z d„Z d„Z d„Z d„ZRS( sLAbstract protocol for datagram-oriented transports, e.g. IP, ICMP, ARP, UDP.icCs|iiƒ}d|d<|S(NRV(Rt__dict__tcopyR0R(RR0((Rt __getstate__Ís cCsK|ip-|iotid|ƒn|iƒn|id|_dS(swMake sure startProtocol is called. This will be called by makeConnection(), users should not call it. sStarting protocol %siN(RRRRRt startProtocol(R((RRÒs   cCsk|idjpt‚|id|_d|_|ip-|iotid|ƒn|iƒndS(snMake sure stopProtocol is called. This will be called by the port, users should not call it. iisStopping protocol %sN( RRtAssertionErrorRRVRRRt stopProtocol(R((RR Ýs   cCsdS(sˆCalled when a transport is connected to this protocol. Will only be called once, even if multiple ports are connected. N((R((RRvêscCsdS(sxCalled when the transport is disconnected. Will only be called once, after all ports are disconnected. N((R((RRxðscCs.|idjpt‚||_|iƒdS(s¨Make a connection to a transport and a server. This sets the 'transport' attribute of this DatagramProtocol, and calls the doStart() callback. N(RRVRRwR(RRV((RRXös cCsdS(s Called when a datagram is received. @param datagram: the string received from the transport. @param addr: tuple of source of datagram. N((RtdatagramR((RtdatagramReceiveds(RRRRRVRRRRuRR RvRxRXRz(((RRrÆs     tDatagramProtocolcBstZdZd„ZRS(s3Protocol for datagram-oriented transport, e.g. UDP.cCsdS(sCalled due to error from write in connected mode. Note this is a result of ICMP message generated by *previous* write. N((R((RtconnectionRefused s(RRRR|(((RR{s tConnectedDatagramProtocolcBs tZdZd„Zd„ZRS(sZProtocol for connected datagram-oriented transport. No longer necessary for UDP. cCsdS(snCalled when a datagram is received. @param datagram: the string received from the transport. N((RRy((RRzscCsdS(s`Called if connecting failed. Usually this will be due to a DNS lookup failure. N((Rtfailure((RtconnectionFaileds(RRRRzR(((RR}s  t FileWrappercBs±tZdZeeiƒdZdZeZ dZ d„Z d„Z d„Z d„Zd„Zd„Zd„Zd „Zd „Zd „Zd „Zd „Zd„Zd„ZRS(sÓA wrapper around a file-like object to make it behave as a Transport. This doesn't actually stream the file to the attached protocol, and is thus useful mainly as a utility for debugging protocols. icCs ||_dS(N(tfileR(RR((RR"5scCs,y|ii|ƒWn|iƒnXdS(N(RRRaRZthandleException(RRZ((RRa8scCs|io|iiƒndS(N(RRbtresumeProducing(R((Rt_checkProducer?s cCs+||_||_|p|iƒndS(s%From abstract.FileDescriptor N(RbRRctstreamingProducerRƒ(RRbRc((RRdEs   cCs d|_dS(N(RRRb(R((RReMscCs|iƒ|iƒdS(N(RRetloseConnection(R((Rt stopConsumingPs cCs|idi|ƒƒdS(Nt(RRatjointiovec(RRŠ((Rt writeSequenceTscCsDd|_y|iiƒWn#ttfj o|iƒnXdS(Ni(RtclosedRtclosetIOErrortOSErrorR‚(R((RR†Ws  cCsdS(NR(sfilesfile((R((RtgetPeer^scCsdS(NR((R((RtgetHostbscCsdS(N((R((RR‚fscCsdS(N((R((RRƒiscCsdS(N((R((RtpauseProducingmscCs|iƒdS(N(RR†(R((Rt stopProducingqs(RRRRRt ITransportRŒt disconnectingRRbR…R"RaR„RdReR‡R‹R†RR‘R‚RƒR’R“(((RR€'s(               R^($RRItzope.interfaceRttwisted.pythonRR~t componentsRKRRNR.RRRR(R:RSRTtFailuretConnectionDoneR^t cleanFailureRYtAdapterR_tregisterAdapterR]RfRgRhRrR{R}R€t__all__(R.RIRRrRhRRžR_RR€R~R}RYRTRRR{R^R(R:RSRRgR˜RN((Rt?s4  O (f(   %B N