Your IP : 172.28.240.42


Current Path : /usr/lib/python2.7/dist-packages/twisted/protocols/
Upload File :
Current File : //usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyc


[XMc@sZdZddlZddlmZddlmZddlmZmZm	Z	m
Z
ddlmZddl
mZddlmZd	efd
YZdefdYZd
efdYZdZdZedZdZdZdddYZdejfdYZdejfdYZdedZ edZ!dS(sF
Testing support for protocols -- loopback between client and server.
iN(t
implements(tpolicies(t
interfacestprotocoltmaintdefer(t
deferLater(tfailure(tIAddresst_LoopbackQueuecBs>eZdZdZeZdZdZdZ	dZ
RS(s
    Trivial wrapper around a list to give it an interface like a queue, which
    the addition of also sending notifications by way of a Deferred whenever
    the list has something added to it.
    cCs
g|_dS(N(t_queue(tself((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyt__init__scCsF|jj||jdk	rB|jd}|_|jdndS(N(R
tappendt_notificationDeferredtNonetcallback(Rtvtd((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pytput#scCs
t|jS(N(tboolR
(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyt__nonzero__*scCs|jjdS(Ni(R
tpop(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pytget.sN(t__name__t
__module__t__doc__RRtFalset
disconnectRRRR(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyR	s			t_LoopbackAddresscBseZeeRS((RRRR(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyR3st_LoopbackTransportcBsxeZeejejeZd	Z	dZ
dZdZdZ
dZdZdZdZdZRS(
cCs
||_dS(N(tq(RR((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyR>scCs|jj|dS(N(RR(Rtbytes((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pytwriteAscCs|jjdj|dS(Nt(RRtjoin(Rtiovec((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyt
writeSequenceDscCs t|j_|jjddS(N(tTrueRRRR(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pytloseConnectionGscCstS(N(R(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pytgetPeerKscCstS(N(R(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pytgetHostNscCs5|jdkst||_||_|jdS(N(tproducerRtAssertionErrortstreamingProducert
_pollProducer(RR*t	streaming((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pytregisterProducerRs		cCs"|jdk	std|_dS(N(R*RR+(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pytunregisterProducerXscCs-|jdk	r)|jr)|jjndS(N(R*RR,tresumeProducing(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyR-\sN(RRRRt
ITransportt	IConsumerRt
disconnectingRR*RR!R%R'R(R)R/R0R-(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyR7s								cCs:x3|r5|j}|dkr%Pn|j|qWdS(s
    L{identityPumpPolicy} is a policy which delivers each chunk of data written
    to the given queue as-is to the target.

    This isn't a particularly realistic policy.

    @see: L{loopbackAsync}
    N(RRtdataReceived(tqueuettargetR ((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pytidentityPumpPolicybs
		cCs_g}x3|r;|j}|dkr+Pn|j|q	W|r[|jdj|ndS(s
    L{collapsingPumpPolicy} is a policy which collapses all outstanding chunks
    into a single string and delivers it to the target.

    @see: L{loopbackAsync}
    R"N(RRR
R5R#(R6R7R tchunk((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pytcollapsingPumpPolicyss	cCsNt}t}|jt||jt|t|||||S(s
    Establish a connection between C{server} and C{client} then transfer data
    between them until the connection is closed. This is often useful for
    testing a protocol.

    @param server: The protocol instance representing the server-side of this
        connection.

    @param client: The protocol instance representing the client-side of this
        connection.

    @param pumpPolicy: When either C{server} or C{client} writes to its
        transport, the string passed in is added to a queue of data for the
        other protocol.  Eventually, C{pumpPolicy} will be called with one such
        queue and the corresponding protocol object.  The pump policy callable
        is responsible for emptying the queue and passing the strings it
        contains to the given protocol's C{dataReceived} method.  The signature
        of C{pumpPolicy} is C{(queue, protocol)}.  C{queue} is an object with a
        C{get} method which will return the next string written to the
        transport, or C{None} if the transport has been disconnected, and which
        evaluates to C{True} if and only if there are more items to be
        retrieved via C{get}.

    @return: A L{Deferred} which fires when the connection has been closed and
        both sides have received notification of this.
    (R	tmakeConnectionRt_loopbackAsyncBody(tservertclientt
pumpPolicytserverToClienttclientToServer((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyt
loopbackAsyncs		c
s fd}x
t}}}||||}||||}|r|rtj}	|	|_|	|_|	jt|||||	S|jrt}||||n"|jrt}||||n|r|jt	j
tj|jt	j
tjtj
dSqdS(sk
    Transfer bytes from the output queue of each protocol to the input of the other.

    @param server: The protocol instance representing the server-side of this
    connection.

    @param serverToClient: The L{_LoopbackQueue} holding the server's output.

    @param client: The protocol instance representing the client-side of this
    connection.

    @param clientToServer: The L{_LoopbackQueue} holding the client's output.

    @param pumpPolicy: See L{loopbackAsync}.

    @return: A L{Deferred} which fires when the connection has been closed and
        both sides have received notification of this.
    csCt}|r"||t}n|r?|r?|jjn|S(N(RR&t	transportR-(tsourceRR7tsent(R?(s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pytpumps
	
N(RRtDeferredRtaddCallbackt_loopbackAsyncContinueRR&tconnectionLostRtFailureRtCONNECTION_DONEtsucceedR(
R=R@R>RAR?RFRt
clientSentt
serverSentR((R?s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyR<s.				c	CsAd|_d|_ddlm}t|dt|||||S(Ni(treactori(RRttwisted.internetRPRR<(tignoredR=R@R>RAR?RP((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyRIs		t
LoopbackRelaycBseZeejejdZdZdZdZ
ddZdZdZ
dZdZdZdZd	Zd
ZdZRS(
R"icCs||_||_dS(N(R7tlogFile(RR7RT((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyR
s	cCs:|j||_|jr6|jjdt|ndS(Nsloopback writing %s
(tbufferRTR!trepr(Rtdata((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyR!s	cCs|jdj|dS(NR"(R!R#(RR$((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyR%scCs|jdkrdS|jr,|jjn|jr|jr^|jjdt|jn|j}d|_|jj|n|jdkrd|_|jj	t
jtj
ndS(Nisloopback receiving %s
R"i(t
shouldLoseR*R1RURTR!RVR7R5RJRRKRRL(RRU((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pytclearBuffers			 			cCs|jdkrd|_ndS(Nii(RX(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyR'&scCsdS(Ntloopback((R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyR)*scCsdS(NRZ((R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyR(-scCs
||_dS(N(R*(RR*R.((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyR/0scCs
d|_dS(N(RR*(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyR03scCsd|jjjfS(NsLoopback(%r)(R7t	__class__R(R((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyt	logPrefix6sN(RRRRR2R3RURXR4RR*RR!R%RYR'R)R(R/R0R\(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyRSs								tLoopbackClientFactorycBs#eZdZdZdZRS(cCs%d|_tj|_||_dS(Ni(tdisconnectedRRGtdeferredR(RR((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyR=s	cCs|jS(N(R(Rtaddr((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyt
buildProtocolBscCsd|_|jjddS(Ni(R^R_RR(Rt	connectortreason((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pytclientConnectionLostEs	(RRRRaRd(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyR];s		t_FireOnClosecBseZdZdZRS(cCs)tjj|||tj|_dS(N(RtProtocolWrapperRRRGR_(RRtfactory((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyRKscCs'tjj|||jjddS(N(RRfRJR_RR(RRc((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyRJOs(RRRRJ(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyReJs	icsddlm}tjtj}t||||_fd|_|j	||ddt
|}||_|jdjj
||j}|jfd|jfd|S(sBRun session between server and client protocol instances over TCP.i(RPcsS(N((R`(t
serverWrapper(s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyt<lambda>Zst	interfaces	127.0.0.1csjS(N(R_(tx(Rh(s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyRi`scs
jS(N(t
stopListening(Rk(t
serverPort(s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyRias(RQRPRtWrappingFactoryRtFactoryRetnoisyRat	listenTCPR]t
connectTCPR)tportR_RH(R=R>RsRpRPtftclientFR((RmRhs>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pytloopbackTCPTs			cstj}ddlm}tjtj}t||||_	fd|_
|j||t|}||_	|j
|||j}|jfd|jfd|S(sJRun session between server and client protocol instances over UNIX socket.i(RPcsS(N((R`(Rh(s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyRilscsjS(N(R_(Rk(Rh(s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyRirscs
jS(N(Rl(Rk(Rm(s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyRiss(ttempfiletmktempRQRPRRnRRoReRpRat
listenUNIXR]tconnectUNIXR_RH(R=R>RptpathRPRtRuR((RmRhs>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pytloopbackUNIXes			(("RRwtzope.interfaceRttwisted.protocolsRRQRRRRttwisted.internet.taskRttwisted.pythonRttwisted.internet.interfacesRtobjectR	RRR8R:RBR<RIRSt
ClientFactoryR]RfReR&RvR|(((s>/usr/lib/python2.7/dist-packages/twisted/protocols/loopback.pyt<module>s("+		&	C	: