6Qc@`s^dZddlmZmZddlZddlmZmZddlZddl Z ddl m Z m Z m Z ddlZddlmZmZmZddlmZmZddlmZmZdd lmZmZmZmZmZmZdd lmZm Z m!Z"dd l#m$Z%m&Z&dd l'm(Z(m)Z)dd lm*Z*eedfdYZ+eede,fdYZ-eedfdYZ.de,fdYZ/eeeede,fdYZ0e&j1ree0eneedfdYZ2dej3fdYZ4de,fdYZ5gZ6dS(s8 Very basic functionality for a Reactor implementation. i(tdivisiontabsolute_importN(t implementertclassImplements(theappushtheappoptheapify(t IReactorCoret IReactorTimetIReactorThreads(tIResolverSimpletIReactorPluggableResolver(t IConnectort IDelayedCall(tfdesctmainterrortabstracttdefertthreads(tlogtfailuret _reflectpy3(tsecondstplatform(tDeferredt DeferredList(t threadablet DelayedCallcB`sqeZeZd ZedZdZdZ dZ dZ dZ dZ dZdZd ZRS( cC`s||||f\|_|_|_|_||_||_||_d|_|_d|_ |j r}t j d |_ ndS(sR @param time: Seconds from the epoch at which to call C{func}. @param func: The callable to call. @param args: The positional arguments to pass to the callable. @param kw: The keyword arguments to pass to the callable. @param cancel: A callable which will be called with this DelayedCall before cancellation. @param reset: A callable which will be called with this DelayedCall after changing this DelayedCall's scheduled execution time. The callable should adjust any necessary scheduling details to ensure this DelayedCall is invoked at the new appropriate time. @param seconds: If provided, a no-argument callable which will be used to determine the current time any time that information is needed. iiN(ttimetfunctargstkwtresettert cancellerRt cancelledtcalledt delayed_timetdebugt tracebackt format_stacktcreator(tselfRRRR tcanceltresetR((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyt__init__)s*     cC`s|j|jS(sReturn the time at which this call will fire @rtype: C{float} @return: The number of seconds after the epoch at which this call is scheduled to be made. (RR%(R*((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pytgetTimeDscC`sq|jrtjnX|jr*tjnC|j|d|_|jr[t||_n|` |` |` dS(sUnschedule this call @raise AlreadyCancelled: Raised if this call has already been unscheduled. @raise AlreadyCalled: Raised if this call has already been made. iN( R#RtAlreadyCancelledR$t AlreadyCalledR"R&tstrt_strRRR (R*((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyR+Ms       cC`s|jrtjnf|jr*tjnQ|j|}||jkrkd|_||_|j|n||j|_dS(ssReschedule this call for a different time @type secondsFromNow: C{float} @param secondsFromNow: The number of seconds from the time of the C{reset} call at which this call will be scheduled. @raise AlreadyCancelled: Raised if this call has been cancelled. @raise AlreadyCalled: Raised if this call has already been made. iN( R#RR/R$R0RRR%R!(R*tsecondsFromNowtnewTime((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyR,`s      cC`sf|jrtjnM|jr*tjn8|j|7_|jdkrb|j|j|ndS(siReschedule this call for a later time @type secondsLater: C{float} @param secondsLater: The number of seconds after the originally scheduled time for which to reschedule this call. @raise AlreadyCancelled: Raised if this call has been cancelled. @raise AlreadyCalled: Raised if this call has already been made. iN(R#RR/R$R0R%tactivate_delayR!(R*t secondsLater((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pytdelayws     cC`s|j|j7_d|_dS(Ni(RR%(R*((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyR5scC`s|jp|j S(sDetermine whether this call is still pending @rtype: C{bool} @return: True if this call has not yet been made or cancelled, False otherwise. (R#R$(R*((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pytactivescC`s|j|jkS(s Implement C{<=} operator between two L{DelayedCall} instances. Comparison is based on the C{time} attribute (unadjusted by the delayed time). (R(R*tother((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyt__le__scC`s|j|jkS(s Implement C{<} operator between two L{DelayedCall} instances. Comparison is based on the C{time} attribute (unadjusted by the delayed time). (R(R*R9((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyt__lt__scC`s |jdk r|jSt|drt|jdrF|jj}qt|jdr|jj}t|jdr|jjjd|}qqtj |j}nd}|j }dt ||j ||j |jfg}|dk r|jd|df|jrb|jd jg|jD]}tj |^q'|jrb|jd qbn|jr|jd jg|jjD]%\}}d |tj |f^qn|jd n|jr|jd d j|jn|jddj|S(NRt __qualname__t__name__tim_classt.s.t(R2tNonethasattrRR<t func_nameR>R=treflectt safe_reprRtidRR$R#textendRtappendtjoinR titemsR&R)(R*RtnowtLtetktv((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyt__str__s8    5  N   N(R=t __module__tFalseR&RER2truntimeSecondsR-R.R+R,R7R5R8R:R;RT(((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyR!s      tThreadedResolvercB`s>eZdZdZdZdZdZd d ZRS( s L{ThreadedResolver} uses a reactor, a threadpool, and L{socket.gethostbyname} to perform name lookups without blocking the reactor thread. It also supports timeouts indepedently from whatever timeout logic L{socket.gethostbyname} might have. @ivar reactor: The reactor the threadpool of which will be used to call L{socket.gethostbyname} and the I/O thread of which the result will be delivered. cC`s||_i|_dS(N(treactort_runningQueries(R*RY((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyR-s cC`s&tjd||f}tj|S(Nsaddress %r not found: %s(RtDNSLookupErrorRtFailure(R*tnameterr((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyt_failscC`s:|j|\}}|j|=|j|j|ddS(Ns timeout error(RZterrbackR_(R*R]tlookupDeferredt userDeferredt cancelCall((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyt_cleanups cC`sy|j|\}}Wntk r*nVX|j|=|jt|tjrs|j|j||jn |j |dS(N( RZtKeyErrorR+t isinstanceRR\R`R_tgetErrorMessagetcallback(R*tresultR]RaRbRc((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyt _checkTimeouts   "iii i-cC`s|rt|}nd}tj}tj|j|jjtj|}|jj ||j ||}||f|j |<|j |j |||S(s1 See L{twisted.internet.interfaces.IResolverSimple.getHostByName}. Note that the elements of C{timeout} are summed and the result is used as a timeout for the lookup. Any intermediate timeout or retry logic is left up to the platform via L{socket.gethostbyname}. i<(tsumRRRtdeferToThreadPoolRYt getThreadPooltsockett gethostbynamet callLaterRdRZtaddBothRj(R*R]ttimeoutt timeoutDelayRbRaRc((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyt getHostByNames  (iii i-(R=RUt__doc__R-R_RdRjRt(((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyRXs      tBlockingResolvercB`seZddZRS(iii i-cC`saytj|}Wn:tjk rOd|f}tj|}tj|SXtj|SdS(Nsaddress %r not found(RnRoRR[Rtfailtsucceed(R*R]RrtaddresstmsgR^((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyRts (iii i-(R=RURt(((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyRvst_ThreePhaseEventcB`sMeZdZdZdZdZdZdZdZdZ RS(s Collection of callables (with arguments) which can be invoked as a group in a particular order. This provides the underlying implementation for the reactor's system event triggers. An instance of this class tracks triggers for all phases of a single type of event. @ivar before: A list of the before-phase triggers containing three-tuples of a callable, a tuple of positional arguments, and a dict of keyword arguments @ivar finishedBefore: A list of the before-phase triggers which have already been executed. This is only populated in the C{'BEFORE'} state. @ivar during: A list of the during-phase triggers containing three-tuples of a callable, a tuple of positional arguments, and a dict of keyword arguments @ivar after: A list of the after-phase triggers containing three-tuples of a callable, a tuple of positional arguments, and a dict of keyword arguments @ivar state: A string indicating what is currently going on with this object. One of C{'BASE'} (for when nothing in particular is happening; this is the initial value), C{'BEFORE'} (when the before-phase triggers are in the process of being executed). cC`s(g|_g|_g|_d|_dS(NtBASE(tbeforetduringtaftertstate(R*((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyR-Cs   cO`sJ|dkrtdnt||j|||f||||fS(s Add a trigger to the indicate phase. @param phase: One of C{'before'}, C{'during'}, or C{'after'}. @param callable: An object to be called when this event is triggered. @param *args: Positional arguments to pass to C{callable}. @param **kwargs: Keyword arguments to pass to C{callable}. @return: An opaque handle which may be passed to L{removeTrigger} to reverse the effects of calling this method. R}R~Rs invalid phase(sbeforesduringsafter(RetgetattrRL(R*tphasetcallableRtkwargs((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyt addTriggerJs cC`st|d|j|S(sS Remove a previously added trigger callable. @param handle: An object previously returned by L{addTrigger}. The trigger added by that call will be removed. @raise ValueError: If the trigger associated with C{handle} has already been removed or if C{handle} is not a valid handle. tremoveTrigger_(RR(R*thandle((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyt removeTrigger]s cC`szy|\}}}}Wn#ttfk r;tdn;X|dkrWtdnt||j|||fdS(sN Just try to remove the trigger. @see: removeTrigger sinvalid trigger handleR}R~Rs invalid phaseN(sbeforesduringsafter(t TypeErrort ValueErrorReRtremove(R*RRRRR((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pytremoveTrigger_BASEjs cC`sp|\}}}}|dkr+|j|S|||f|jkr_tjddtddn |j|dS(s Remove the trigger if it has yet to be executed, otherwise emit a warning that in the future an exception will be raised when removing an already-executed trigger. @see: removeTrigger R}sdRemoving already-fired system event triggers will raise an exception in a future version of Twisted.tcategoryt stackleveliN(RtfinishedBeforetwarningstwarntDeprecationWarning(R*RRRRR((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pytremoveTrigger_BEFOREzs   cC`sd|_g|_g}x|jr|jjd\}}}|jj|||fy|||}WntjqXt|tr|j|qqWt |j |j dS(s8 Call the triggers added to this event. tBEFOREiN( RRR}tpopRLRR^RfRRt addCallbackt_continueFiring(R*t beforeResultsRRRRi((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyt fireEvents   cC`s}d|_g|_xd|j|jfD]P}xG|rt|jd\}}}y|||Wq.tjq.Xq.Wq%WdS(sJ Call the during and after phase triggers for this event. R|iN(RRR~RRRR^(R*tignoredRRRR((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyRs   ( R=RURuR-RRRRRR(((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyR{&s     t ReactorBasecB`seZdZeZeZeZeZe Z dZdZ d2Z dZdZdZdZdZdZd Zd Zd Zd Zd Zd3dZdZdZdZdZdZdZddZ dZ!dZ"dZ#dZ$dZ%d Z&e'e(Z)d!Z*d"Z+d#Z,d$Z-d%Z.d&Z/d'Z0d(Z1e2j3rd2Z4d2Z5d2Z6d)Z7d*Z8d+Z9d,Z:d-Z;d.Z<d/Z=d0Z>n d1Z8RS(4s Default base class for Reactors. @type _stopped: C{bool} @ivar _stopped: A flag which is true between paired calls to C{reactor.run} and C{reactor.stop}. This should be replaced with an explicit state machine. @type _justStopped: C{bool} @ivar _justStopped: A flag which is true between the time C{reactor.stop} is called and the time the shutdown system event is fired. This is used to determine whether that event should be fired after each iteration through the mainloop. This should be replaced with an explicit state machine. @type _started: C{bool} @ivar _started: A flag which is true from the time C{reactor.run} is called until the time C{reactor.run} returns. This is used to prevent calls to C{reactor.run} on a running reactor. This should be replaced with an explicit state machine. @ivar running: See L{IReactorCore.running} @ivar _registerAsIOThread: A flag controlling whether the reactor will register the thread it is running in as the I/O thread when it starts. If C{True}, registration will be done, otherwise it will not be. stwisted.internet.reactorcC`sg|_i|_g|_g|_d|_t|_t|_t|_t|_ t |_ d|_ |jdd|j|jdd|j|jdd|jtjr|jn|jdS(NiR~tstartuptshutdown(tthreadCallQueuet_eventTriggerst_pendingTimedCallst_newTimedCallst_cancellationsRVtrunningt_startedt _justStoppedt_startedBeforetsett_internalReadersREtwakertaddSystemEventTriggert_reallyStartRunningtcrasht disconnectAllRtsupportsThreadst _initThreadst installWaker(R*((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyR-s$             cC`s ttj|jddS(Ns did not implement installWaker(tNotImplementedErrorRHtqualt __class__(R*((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyRscC`s+tj|st|j}||_|S(N(R t providedBytAssertionErrortresolver(R*Rt oldResolver((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pytinstallResolvers  cC`s|jr|jjndS(s) Wake up the event loop. N(RtwakeUp(R*((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyRs cC`s ttj|jddS(sV Do one iteration over the readers and writers which have been added. s did not implement doIterationN(RRHRR(R*R7((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyt doIteration scC`s ttj|jddS(Ns did not implement addReader(RRHRR(R*treader((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyt addReaderscC`s ttj|jddS(Ns did not implement addWriter(RRHRR(R*twriter((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyt addWriterscC`s ttj|jddS(Ns did not implement removeReader(RRHRR(R*R((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyt removeReaderscC`s ttj|jddS(Ns did not implement removeWriter(RRHRR(R*R((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyt removeWriterscC`s ttj|jddS(Ns did not implement removeAll(RRHRR(R*((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyt removeAll"scC`s ttj|jddS(Ns did not implement getReaders(RRHRR(R*((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyt getReaders'scC`s ttj|jddS(Ns did not implement getWriters(RRHRR(R*((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyt getWriters,siii i-cC`sB|stjdStj|r/tj|S|jj||S(s8Return a Deferred that will resolve a hostname. s0.0.0.0(RRxRt isIPAddressRRt(R*R]Rr((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pytresolve1s   cC`s:|jrtjdnt|_t|_t|_dS(sD See twisted.internet.interfaces.IReactorCore.stop. s&Can't stop reactor that isn't running.N(t_stoppedRtReactorNotRunningtTrueRR(R*((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pytstop>s     cC`s,t|_t|_|jdd|jdS(s See twisted.internet.interfaces.IReactorCore.crash. Reset reactor state tracking attributes and re-initialize certain state-transition helpers which were set up in C{__init__} but later destroyed (through use). R~RN(RVRRRR(R*((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyRJs  cG`s!tjd|j|jdS(s#Handle a SIGINT interrupt. sReceived SIGINT, shutting down.N(RRztcallFromThreadR(R*R((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pytsigIntWs cG`s!tjd|j|jdS(s%Handle a SIGBREAK interrupt. s!Received SIGBREAK, shutting down.N(RRzRR(R*R((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pytsigBreak]s cG`s!tjd|j|jdS(s$Handle a SIGTERM interrupt. s Received SIGTERM, shutting down.N(RRzRR(R*R((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pytsigTermcs cC`sC|j}x0|D](}tj||jtjtjqWdS(s;Disconnect every reader, and writer in the system. N(RRtcallWithLoggertconnectionLostRR\RtCONNECTION_LOST(R*t selectablesR((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pyRis    icC`s|j|j|dS(s>See twisted.internet.interfaces.IReactorCore.iterate. N(trunUntilCurrentR(R*R7((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pytiteratess cC`s/|jj|}|dk r+|jndS(sFSee twisted.internet.interfaces.IReactorCore.fireSystemEvent. N(RtgetRER(R*t eventTypetevent((s9/usr/lib/python2.7/dist-packages/twisted/internet/base.pytfireSystemEventzs cO`sat|std|||jkr>t|j|s<    . CA S\